test_pagure_flask_ui_plugins_pagure_ticket_hook.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import shutil
  10. import sys
  11. import os
  12. sys.path.insert(
  13. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  14. )
  15. import tests
  16. class PagureFlaskPluginPagureTicketHooktests(tests.SimplePagureTest):
  17. """Tests for pagure_hook plugin of pagure"""
  18. def test_plugin_pagure_ticket(self):
  19. """Test the pagure_ticket plugin on/off endpoint."""
  20. tests.create_projects(self.session)
  21. tests.create_projects_git(os.path.join(self.path, "repos"))
  22. user = tests.FakeUser(username="pingou")
  23. with tests.user_set(self.app.application, user):
  24. output = self.app.get("/test/settings/Pagure tickets")
  25. self.assertEqual(output.status_code, 200)
  26. output_text = output.get_data(as_text=True)
  27. self.assertIn(
  28. "<title>Settings Pagure tickets - test - Pagure</title>",
  29. output_text,
  30. )
  31. self.assertIn(
  32. '<input class="form-check-input mt-2" id="active" name="active" '
  33. 'type="checkbox" value="y">',
  34. output_text,
  35. )
  36. csrf_token = output_text.split(
  37. 'name="csrf_token" type="hidden" value="'
  38. )[1].split('">')[0]
  39. data = {}
  40. output = self.app.post("/test/settings/Pagure tickets", data=data)
  41. self.assertEqual(output.status_code, 200)
  42. output_text = output.get_data(as_text=True)
  43. self.assertIn(
  44. "<title>Settings Pagure tickets - test - Pagure</title>",
  45. output_text,
  46. )
  47. self.assertIn(
  48. '<input class="form-check-input mt-2" id="active" name="active" '
  49. 'type="checkbox" value="y">',
  50. output_text,
  51. )
  52. data["csrf_token"] = csrf_token
  53. # Create the tickets repo
  54. tests.create_projects_git(
  55. os.path.join(self.path, "repos", "tickets")
  56. )
  57. output = self.app.post(
  58. "/test/settings/Pagure tickets",
  59. data=data,
  60. follow_redirects=True,
  61. )
  62. self.assertEqual(output.status_code, 200)
  63. output_text = output.get_data(as_text=True)
  64. self.assertIn(
  65. '<h5 class="pl-2 font-weight-bold text-muted">'
  66. "Project Settings</h5>\n",
  67. output_text,
  68. )
  69. self.assertIn("Hook Pagure tickets deactivated", output_text)
  70. output = self.app.get("/test/settings/Pagure tickets")
  71. self.assertEqual(output.status_code, 200)
  72. output_text = output.get_data(as_text=True)
  73. self.assertIn(
  74. "<title>Settings Pagure tickets - test - Pagure</title>",
  75. output_text,
  76. )
  77. self.assertIn(
  78. '<input class="form-check-input mt-2" id="active" name="active" '
  79. 'type="checkbox" value="y">',
  80. output_text,
  81. )
  82. self.assertFalse(
  83. os.path.exists(
  84. os.path.join(
  85. self.path,
  86. "tickets",
  87. "test.git",
  88. "hooks",
  89. "post-receive.pagure",
  90. )
  91. )
  92. )
  93. # Activate hook
  94. data = {"csrf_token": csrf_token, "active": "y"}
  95. print(" ==== ACTIVATE ")
  96. output = self.app.post(
  97. "/test/settings/Pagure tickets",
  98. data=data,
  99. follow_redirects=True,
  100. )
  101. self.assertEqual(output.status_code, 200)
  102. output_text = output.get_data(as_text=True)
  103. self.assertIn(
  104. '<h5 class="pl-2 font-weight-bold text-muted">'
  105. "Project Settings</h5>\n",
  106. output_text,
  107. )
  108. self.assertIn("Hook Pagure tickets activated", output_text)
  109. print(" ==== CHECK ")
  110. output = self.app.get("/test/settings/Pagure tickets")
  111. self.assertEqual(output.status_code, 200)
  112. output_text = output.get_data(as_text=True)
  113. self.assertIn(
  114. "<title>Settings Pagure tickets - test - Pagure</title>",
  115. output_text,
  116. )
  117. self.assertIn(
  118. '<input checked class="form-check-input mt-2" id="active" name="active" '
  119. 'type="checkbox" value="y">',
  120. output_text,
  121. )
  122. # De-Activate hook
  123. print(" ==== DEACTIVATE ")
  124. data = {"csrf_token": csrf_token}
  125. output = self.app.post(
  126. "/test/settings/Pagure tickets",
  127. data=data,
  128. follow_redirects=True,
  129. )
  130. self.assertEqual(output.status_code, 200)
  131. output_text = output.get_data(as_text=True)
  132. self.assertIn(
  133. '<h5 class="pl-2 font-weight-bold text-muted">'
  134. "Project Settings</h5>\n",
  135. output_text,
  136. )
  137. self.assertIn("Hook Pagure tickets deactivated", output_text)
  138. print(" ==== CHECK ")
  139. output = self.app.get("/test/settings/Pagure tickets")
  140. self.assertEqual(output.status_code, 200)
  141. output_text = output.get_data(as_text=True)
  142. self.assertIn(
  143. "<title>Settings Pagure tickets - test - Pagure</title>",
  144. output_text,
  145. )
  146. self.assertIn(
  147. '<input class="form-check-input mt-2" id="active" name="active" '
  148. 'type="checkbox" value="y">',
  149. output_text,
  150. )
  151. self.assertFalse(
  152. os.path.exists(
  153. os.path.join(
  154. self.path,
  155. "tickets",
  156. "test.git",
  157. "hooks",
  158. "post-receive.pagure-ticket",
  159. )
  160. )
  161. )
  162. # Try re-activate hook w/o the git repo
  163. data = {"csrf_token": csrf_token, "active": "y"}
  164. shutil.rmtree(
  165. os.path.join(self.path, "repos", "tickets", "test.git")
  166. )
  167. print(" ==== NO GIT REPO ")
  168. output = self.app.post("/test/settings/Pagure tickets", data=data)
  169. self.assertEqual(output.status_code, 404)
  170. print("*** DONE ***")
  171. if __name__ == "__main__":
  172. unittest.main(verbosity=2)