test_pagure_flask_ui_plugins_rtd_hook.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016-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 PagureFlaskPluginRtdHooktests(tests.SimplePagureTest):
  17. """ Tests for rtd_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/Read the Doc")
  25. self.assertEqual(output.status_code, 200)
  26. output_text = output.get_data(as_text=True)
  27. self.assertIn(
  28. "<title>Settings Read the Doc - 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/Read the Doc", data=data)
  41. self.assertEqual(output.status_code, 200)
  42. output_text = output.get_data(as_text=True)
  43. self.assertIn(
  44. "<title>Settings Read the Doc - 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 requests repo
  54. tests.create_projects_git(os.path.join(self.path, "requests"))
  55. output = self.app.post(
  56. "/test/settings/Read the Doc", data=data, follow_redirects=True
  57. )
  58. self.assertEqual(output.status_code, 200)
  59. output_text = output.get_data(as_text=True)
  60. self.assertIn(
  61. '<h5 class="pl-2 font-weight-bold text-muted">'
  62. "Project Settings</h5>\n",
  63. output_text,
  64. )
  65. self.assertIn("Hook Read the Doc deactivated", output_text)
  66. output = self.app.get("/test/settings/Read the Doc")
  67. self.assertEqual(output.status_code, 200)
  68. output_text = output.get_data(as_text=True)
  69. self.assertIn(
  70. "<title>Settings Read the Doc - test - Pagure</title>",
  71. output_text,
  72. )
  73. self.assertIn(
  74. '<input class="form-check-input mt-2" id="active" name="active" '
  75. 'type="checkbox" value="y">',
  76. output_text,
  77. )
  78. self.assertFalse(
  79. os.path.exists(
  80. os.path.join(
  81. self.path,
  82. "requests",
  83. "test.git",
  84. "hooks",
  85. "post-receive.pagure",
  86. )
  87. )
  88. )
  89. # Activate hook
  90. data = {
  91. "csrf_token": csrf_token,
  92. "active": "y",
  93. "project_name": "foo",
  94. }
  95. output = self.app.post(
  96. "/test/settings/Read the Doc", data=data, follow_redirects=True
  97. )
  98. self.assertEqual(output.status_code, 200)
  99. output_text = output.get_data(as_text=True)
  100. self.assertIn(
  101. '<h5 class="pl-2 font-weight-bold text-muted">'
  102. "Project Settings</h5>\n",
  103. output_text,
  104. )
  105. self.assertIn("Hook Read the Doc activated", output_text)
  106. output = self.app.get("/test/settings/Read the Doc")
  107. self.assertEqual(output.status_code, 200)
  108. output_text = output.get_data(as_text=True)
  109. self.assertIn(
  110. "<title>Settings Read the Doc - test - Pagure</title>",
  111. output_text,
  112. )
  113. self.assertIn(
  114. '<input checked class="form-check-input mt-2" id="active" name="active" '
  115. 'type="checkbox" value="y">',
  116. output_text,
  117. )
  118. # De-Activate hook
  119. data = {"csrf_token": csrf_token}
  120. output = self.app.post(
  121. "/test/settings/Read the Doc", data=data, follow_redirects=True
  122. )
  123. self.assertEqual(output.status_code, 200)
  124. output_text = output.get_data(as_text=True)
  125. self.assertIn(
  126. '<h5 class="pl-2 font-weight-bold text-muted">'
  127. "Project Settings</h5>\n",
  128. output_text,
  129. )
  130. self.assertIn("Hook Read the Doc deactivated", output_text)
  131. output = self.app.get("/test/settings/Read the Doc")
  132. self.assertEqual(output.status_code, 200)
  133. output_text = output.get_data(as_text=True)
  134. self.assertIn(
  135. "<title>Settings Read the Doc - test - Pagure</title>",
  136. output_text,
  137. )
  138. self.assertIn(
  139. '<input class="form-check-input mt-2" id="active" name="active" '
  140. 'type="checkbox" value="y">',
  141. output_text,
  142. )
  143. self.assertFalse(
  144. os.path.exists(
  145. os.path.join(
  146. self.path,
  147. "repos",
  148. "test.git",
  149. "hooks",
  150. "post-receive.rtd",
  151. )
  152. )
  153. )
  154. # Try re-activate hook w/o the git repo
  155. data = {
  156. "csrf_token": csrf_token,
  157. "active": "y",
  158. "project_name": "foo",
  159. }
  160. shutil.rmtree(os.path.join(self.path, "repos", "test.git"))
  161. output = self.app.post("/test/settings/Read the Doc", data=data)
  162. self.assertEqual(output.status_code, 404)
  163. if __name__ == "__main__":
  164. unittest.main(verbosity=2)