12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # -*- coding: utf-8 -*-
- """
- (c) 2019 - Copyright Red Hat Inc
- Authors:
- Fabien Boucher <fboucher@redhat.com>
- """
- import unittest
- import sys
- import os
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import mock
- import pygit2
- import pagure.hooks.default
- import tests
- class PagureHooksDefault(tests.SimplePagureTest):
- """Tests for pagure.hooks.default"""
- def setUp(self):
- """Set up the environnment, ran before every tests."""
- super(PagureHooksDefault, self).setUp()
- tests.create_projects(self.session)
- self.projects = tests.create_projects_git(
- os.path.join(self.path, "repos"), bare=True
- )
- self.folder = os.path.join(self.path, "repos", "test.git")
- def init_test_repo(self):
- tests.add_content_git_repo(self.projects[0], extra_commit=True)
- repo = pygit2.Repository(self.projects[0])
- commit = repo.references["refs/heads/master"].peel()
- sha = commit.hex
- history = [c.hex for c in repo.walk(sha)]
- project = pagure.lib.query.get_authorized_project(self.session, "test")
- return project, sha, history
- @mock.patch("pagure.hooks.default.send_fedmsg_notifications")
- def test_send_action_notification(self, fedmsg):
- project, sha, _ = self.init_test_repo()
- pagure.hooks.default.send_action_notification(
- self.session,
- "tag",
- "bar",
- project,
- self.folder,
- "pingou",
- "master",
- sha,
- )
- (_, args, kwargs) = fedmsg.mock_calls[0]
- self.assertEqual(args[1], "git.tag.bar")
- self.assertEqual(args[2]["repo"]["name"], "test")
- self.assertEqual(args[2]["rev"], sha)
- @mock.patch("pagure.hooks.default.send_fedmsg_notifications")
- def test_send_notifications(self, fedmsg):
- project, _, history = self.init_test_repo()
- pagure.hooks.default.send_notifications(
- self.session,
- project,
- self.folder,
- "pingou",
- "master",
- history[:-1],
- False,
- history[-1],
- None,
- )
- (_, args, kwargs) = fedmsg.mock_calls[0]
- self.assertEqual(args[1], "git.receive")
- self.assertEqual(args[2]["repo"]["name"], "test")
- self.assertEqual(args[2]["start_commit"], history[1])
- self.assertEqual(args[2]["forced"], False)
- self.assertEqual(args[2]["old_commit"], history[-1])
- self.assertEqual(
- args[2]["changed_files"],
- {
- "folder1/folder2/file": "A",
- "folder1/folder2/fileŠ": "A",
- "test": "A",
- },
- )
- self.assertIsNone(args[2]["pull_request_id"])
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|