test_pagure_send_notification.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2019 - Copyright Red Hat Inc
  4. Authors:
  5. Fabien Boucher <fboucher@redhat.com>
  6. """
  7. import unittest
  8. import sys
  9. import os
  10. sys.path.insert(
  11. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  12. )
  13. import mock
  14. import pygit2
  15. import pagure.hooks.default
  16. import tests
  17. class PagureHooksDefault(tests.SimplePagureTest):
  18. """Tests for pagure.hooks.default"""
  19. def setUp(self):
  20. """Set up the environnment, ran before every tests."""
  21. super(PagureHooksDefault, self).setUp()
  22. tests.create_projects(self.session)
  23. self.projects = tests.create_projects_git(
  24. os.path.join(self.path, "repos"), bare=True
  25. )
  26. self.folder = os.path.join(self.path, "repos", "test.git")
  27. def init_test_repo(self):
  28. tests.add_content_git_repo(self.projects[0], extra_commit=True)
  29. repo = pygit2.Repository(self.projects[0])
  30. commit = repo.references["refs/heads/master"].peel()
  31. sha = commit.hex
  32. history = [c.hex for c in repo.walk(sha)]
  33. project = pagure.lib.query.get_authorized_project(self.session, "test")
  34. return project, sha, history
  35. @mock.patch("pagure.hooks.default.send_fedmsg_notifications")
  36. def test_send_action_notification(self, fedmsg):
  37. project, sha, _ = self.init_test_repo()
  38. pagure.hooks.default.send_action_notification(
  39. self.session,
  40. "tag",
  41. "bar",
  42. project,
  43. self.folder,
  44. "pingou",
  45. "master",
  46. sha,
  47. )
  48. (_, args, kwargs) = fedmsg.mock_calls[0]
  49. self.assertEqual(args[1], "git.tag.bar")
  50. self.assertEqual(args[2]["repo"]["name"], "test")
  51. self.assertEqual(args[2]["rev"], sha)
  52. @mock.patch("pagure.hooks.default.send_fedmsg_notifications")
  53. def test_send_notifications(self, fedmsg):
  54. project, _, history = self.init_test_repo()
  55. pagure.hooks.default.send_notifications(
  56. self.session,
  57. project,
  58. self.folder,
  59. "pingou",
  60. "master",
  61. history[:-1],
  62. False,
  63. history[-1],
  64. None,
  65. )
  66. (_, args, kwargs) = fedmsg.mock_calls[0]
  67. self.assertEqual(args[1], "git.receive")
  68. self.assertEqual(args[2]["repo"]["name"], "test")
  69. self.assertEqual(args[2]["start_commit"], history[1])
  70. self.assertEqual(args[2]["forced"], False)
  71. self.assertEqual(args[2]["old_commit"], history[-1])
  72. self.assertEqual(
  73. args[2]["changed_files"],
  74. {
  75. "folder1/folder2/file": "A",
  76. "folder1/folder2/fileŠ": "A",
  77. "test": "A",
  78. },
  79. )
  80. self.assertIsNone(args[2]["pull_request_id"])
  81. if __name__ == "__main__":
  82. unittest.main(verbosity=2)