test_pagure_flask_ui_plugins_unsigned.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 sys
  10. import os
  11. sys.path.insert(
  12. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  13. )
  14. import tests
  15. class PagureFlaskPluginUnsignedtests(tests.SimplePagureTest):
  16. """ Tests for Block pushes with unsigned commit plugin of pagure """
  17. def test_plugin_unsigned(self):
  18. """ Test the noff plugin on/off endpoint. """
  19. tests.create_projects(self.session)
  20. tests.create_projects_git(os.path.join(self.path, "repos"))
  21. user = tests.FakeUser(username="pingou")
  22. with tests.user_set(self.app.application, user):
  23. output = self.app.get("/test/settings/Block Un-Signed commits")
  24. self.assertEqual(output.status_code, 200)
  25. output_text = output.get_data(as_text=True)
  26. self.assertIn(
  27. "<title>Settings Block Un-Signed commits - test - "
  28. "Pagure</title>",
  29. output_text,
  30. )
  31. self.assertTrue(
  32. '<input class="form-check-input mt-2" id="active" name="active" '
  33. 'type="checkbox" value="y">' in output_text
  34. )
  35. csrf_token = output_text.split(
  36. 'name="csrf_token" type="hidden" value="'
  37. )[1].split('">')[0]
  38. data = {}
  39. output = self.app.post(
  40. "/test/settings/Block Un-Signed commits", data=data
  41. )
  42. self.assertEqual(output.status_code, 200)
  43. output_text = output.get_data(as_text=True)
  44. self.assertIn(
  45. "<title>Settings Block Un-Signed commits - test - "
  46. "Pagure</title>",
  47. output_text,
  48. )
  49. self.assertTrue(
  50. '<input class="form-check-input mt-2" id="active" name="active" '
  51. 'type="checkbox" value="y">' in output_text
  52. )
  53. data["csrf_token"] = csrf_token
  54. # With the git repo
  55. output = self.app.post(
  56. "/test/settings/Block Un-Signed commits",
  57. data=data,
  58. follow_redirects=True,
  59. )
  60. self.assertEqual(output.status_code, 200)
  61. output_text = output.get_data(as_text=True)
  62. self.assertIn(
  63. '<h5 class="pl-2 font-weight-bold text-muted">'
  64. "Project Settings</h5>\n",
  65. output_text,
  66. )
  67. self.assertTrue(
  68. "Hook Block Un-Signed " "commits deactivated" in output_text
  69. )
  70. output = self.app.get("/test/settings/Block Un-Signed commits")
  71. self.assertEqual(output.status_code, 200)
  72. output_text = output.get_data(as_text=True)
  73. self.assertIn(
  74. "<title>Settings Block Un-Signed commits - test - "
  75. "Pagure</title>",
  76. output_text,
  77. )
  78. self.assertTrue(
  79. '<input class="form-check-input mt-2" id="active" name="active" '
  80. 'type="checkbox" value="y">' in output_text
  81. )
  82. self.assertFalse(
  83. os.path.exists(
  84. os.path.join(
  85. self.path,
  86. "repos",
  87. "test.git",
  88. "hooks",
  89. "pre-receive.pagureunsignedcommit",
  90. )
  91. )
  92. )
  93. # Activate the hook
  94. data = {"csrf_token": csrf_token, "active": "y"}
  95. output = self.app.post(
  96. "/test/settings/Block Un-Signed commits",
  97. data=data,
  98. follow_redirects=True,
  99. )
  100. self.assertEqual(output.status_code, 200)
  101. output_text = output.get_data(as_text=True)
  102. self.assertIn(
  103. '<h5 class="pl-2 font-weight-bold text-muted">'
  104. "Project Settings</h5>\n",
  105. output_text,
  106. )
  107. self.assertNotIn("Hook activated", output_text)
  108. # De-Activate hook
  109. data = {"csrf_token": csrf_token}
  110. output = self.app.post(
  111. "/test/settings/Block Un-Signed commits",
  112. data=data,
  113. follow_redirects=True,
  114. )
  115. self.assertEqual(output.status_code, 200)
  116. output_text = output.get_data(as_text=True)
  117. self.assertIn(
  118. '<h5 class="pl-2 font-weight-bold text-muted">'
  119. "Project Settings</h5>\n",
  120. output_text,
  121. )
  122. self.assertTrue(
  123. "Hook Block Un-Signed " "commits deactivated" in output_text
  124. )
  125. output = self.app.get("/test/settings/Block Un-Signed commits")
  126. output_text = output.get_data(as_text=True)
  127. self.assertIn(
  128. "<title>Settings Block Un-Signed commits - test - "
  129. "Pagure</title>",
  130. output_text,
  131. )
  132. self.assertIn(
  133. '<input class="form-check-input mt-2" id="active" name="active" '
  134. 'type="checkbox" value="y">',
  135. output_text,
  136. )
  137. self.assertFalse(
  138. os.path.exists(
  139. os.path.join(
  140. self.path,
  141. "repos",
  142. "test.git",
  143. "hooks",
  144. "pre-receive.pagureunsignedcommit",
  145. )
  146. )
  147. )
  148. if __name__ == "__main__":
  149. unittest.main(verbosity=2)