test_pagure_send_notification.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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])
  29. repo = pygit2.Repository(self.projects[0])
  30. sha = repo.references["refs/heads/master"].peel().hex
  31. project = pagure.lib.query.get_authorized_project(self.session, "test")
  32. return project, sha
  33. @mock.patch("pagure.hooks.default.send_fedmsg_notifications")
  34. def test_send_action_notification(self, fedmsg):
  35. project, sha = self.init_test_repo()
  36. pagure.hooks.default.send_action_notification(
  37. self.session,
  38. "tag",
  39. "bar",
  40. project,
  41. self.folder,
  42. "pingou",
  43. "master",
  44. sha,
  45. )
  46. (_, args, kwargs) = fedmsg.mock_calls[0]
  47. self.assertEqual(args[1], "git.tag.bar")
  48. self.assertEqual(args[2]["repo"]["name"], "test")
  49. self.assertEqual(args[2]["rev"], sha)
  50. @mock.patch("pagure.hooks.default.send_fedmsg_notifications")
  51. def test_send_notifications(self, fedmsg):
  52. oldrev = "9e5f51c951c6cab20fe81419320ed740533e2f2f"
  53. project, sha = self.init_test_repo()
  54. pagure.hooks.default.send_notifications(
  55. self.session,
  56. project,
  57. self.folder,
  58. "pingou",
  59. "master",
  60. [sha],
  61. False,
  62. oldrev,
  63. )
  64. (_, args, kwargs) = fedmsg.mock_calls[0]
  65. self.assertEqual(args[1], "git.receive")
  66. self.assertEqual(args[2]["repo"]["name"], "test")
  67. self.assertEqual(args[2]["start_commit"], sha)
  68. self.assertEqual(args[2]["forced"], False)
  69. self.assertEqual(args[2]["old_commit"], oldrev)
  70. if __name__ == "__main__":
  71. unittest.main(verbosity=2)