test_pagure_flask_ui_plugins_pagure_request_hook.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 PagureFlaskPluginPagureRequestHooktests(tests.SimplePagureTest):
  17. """ Tests for pagure_hook plugin of pagure """
  18. def test_plugin_pagure_request(self):
  19. """ Test the pagure_request 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 requests")
  25. self.assertEqual(output.status_code, 200)
  26. output_text = output.get_data(as_text=True)
  27. self.assertIn(
  28. "<title>Settings Pagure requests - 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 = (
  37. output.get_data(as_text=True)
  38. .split('name="csrf_token" type="hidden" value="')[1]
  39. .split('">')[0]
  40. )
  41. data = {}
  42. output = self.app.post("/test/settings/Pagure requests", data=data)
  43. self.assertEqual(output.status_code, 200)
  44. output_text = output.get_data(as_text=True)
  45. self.assertIn(
  46. "<title>Settings Pagure requests - test - Pagure</title>",
  47. output_text,
  48. )
  49. self.assertIn(
  50. '<input class="form-check-input mt-2" id="active" name="active" '
  51. 'type="checkbox" value="y">',
  52. output_text,
  53. )
  54. data["csrf_token"] = csrf_token
  55. # Create the requests repo
  56. tests.create_projects_git(
  57. os.path.join(self.path, "repos", "requests")
  58. )
  59. output = self.app.post(
  60. "/test/settings/Pagure requests",
  61. data=data,
  62. follow_redirects=True,
  63. )
  64. self.assertEqual(output.status_code, 200)
  65. output_text = output.get_data(as_text=True)
  66. self.assertIn(
  67. '<h5 class="pl-2 font-weight-bold text-muted">'
  68. "Project Settings</h5>\n",
  69. output_text,
  70. )
  71. self.assertIn("Hook Pagure requests deactivated", output_text)
  72. output = self.app.get("/test/settings/Pagure requests")
  73. self.assertEqual(output.status_code, 200)
  74. output_text = output.get_data(as_text=True)
  75. self.assertIn(
  76. "<title>Settings Pagure requests - test - Pagure</title>",
  77. output_text,
  78. )
  79. self.assertIn(
  80. '<input class="form-check-input mt-2" id="active" name="active" '
  81. 'type="checkbox" value="y">',
  82. output_text,
  83. )
  84. self.assertFalse(
  85. os.path.exists(
  86. os.path.join(
  87. self.path,
  88. "requests",
  89. "test.git",
  90. "hooks",
  91. "post-receive.pagure",
  92. )
  93. )
  94. )
  95. # Activate hook
  96. data = {"csrf_token": csrf_token, "active": "y"}
  97. output = self.app.post(
  98. "/test/settings/Pagure requests",
  99. data=data,
  100. follow_redirects=True,
  101. )
  102. self.assertEqual(output.status_code, 200)
  103. output_text = output.get_data(as_text=True)
  104. self.assertIn(
  105. '<h5 class="pl-2 font-weight-bold text-muted">'
  106. "Project Settings</h5>\n",
  107. output_text,
  108. )
  109. self.assertIn("Hook Pagure requests activated", output_text)
  110. output = self.app.get("/test/settings/Pagure requests")
  111. self.assertEqual(output.status_code, 200)
  112. output_text = output.get_data(as_text=True)
  113. self.assertIn(
  114. "<title>Settings Pagure requests - 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. data = {"csrf_token": csrf_token}
  124. output = self.app.post(
  125. "/test/settings/Pagure requests",
  126. data=data,
  127. follow_redirects=True,
  128. )
  129. self.assertEqual(output.status_code, 200)
  130. output_text = output.get_data(as_text=True)
  131. self.assertIn(
  132. '<h5 class="pl-2 font-weight-bold text-muted">'
  133. "Project Settings</h5>\n",
  134. output_text,
  135. )
  136. self.assertIn("Hook Pagure requests deactivated", output_text)
  137. output = self.app.get("/test/settings/Pagure requests")
  138. self.assertEqual(output.status_code, 200)
  139. output_text = output.get_data(as_text=True)
  140. self.assertIn(
  141. "<title>Settings Pagure requests - test - Pagure</title>",
  142. output_text,
  143. )
  144. self.assertIn(
  145. '<input class="form-check-input mt-2" id="active" name="active" '
  146. 'type="checkbox" value="y">',
  147. output_text,
  148. )
  149. self.assertFalse(
  150. os.path.exists(
  151. os.path.join(
  152. self.path,
  153. "requests",
  154. "test.git",
  155. "hooks",
  156. "post-receive.pagure-requests",
  157. )
  158. )
  159. )
  160. if __name__ == "__main__":
  161. unittest.main(verbosity=2)