test_pagure_flask_ui_plugins_irc.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 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 PagureFlaskPluginIRCtests(tests.SimplePagureTest):
  16. """ Tests for pagure_hook plugin of pagure """
  17. def test_plugin_mail(self):
  18. """ Test the irc 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/IRC")
  24. self.assertEqual(output.status_code, 200)
  25. output_text = output.get_data(as_text=True)
  26. self.assertIn(
  27. "<title>Settings IRC - test - Pagure</title>", output_text
  28. )
  29. self.assertIn(
  30. '<input class="form-check-input mt-2" id="active" name="active" '
  31. 'type="checkbox" value="y">',
  32. output_text,
  33. )
  34. csrf_token = output_text.split(
  35. 'name="csrf_token" type="hidden" value="'
  36. )[1].split('">')[0]
  37. data = {}
  38. output = self.app.post("/test/settings/IRC", data=data)
  39. self.assertEqual(output.status_code, 200)
  40. output_text = output.get_data(as_text=True)
  41. self.assertIn(
  42. "<title>Settings IRC - test - Pagure</title>", output_text
  43. )
  44. self.assertIn(
  45. '<input class="form-check-input mt-2" id="active" name="active" '
  46. 'type="checkbox" value="y">',
  47. output_text,
  48. )
  49. self.assertFalse(
  50. os.path.exists(
  51. os.path.join(
  52. self.path,
  53. "repos",
  54. "test.git",
  55. "hooks",
  56. "post-receive.irc",
  57. )
  58. )
  59. )
  60. data["csrf_token"] = csrf_token
  61. # With the git repo
  62. output = self.app.post(
  63. "/test/settings/IRC", data=data, follow_redirects=True
  64. )
  65. self.assertEqual(output.status_code, 200)
  66. output_text = output.get_data(as_text=True)
  67. self.assertIn(
  68. '<h5 class="pl-2 font-weight-bold text-muted">'
  69. "Project Settings</h5>\n",
  70. output_text,
  71. )
  72. self.assertIn("Hook IRC deactivated", output_text)
  73. output = self.app.get("/test/settings/IRC")
  74. self.assertEqual(output.status_code, 200)
  75. output_text = output.get_data(as_text=True)
  76. self.assertIn(
  77. "<title>Settings IRC - test - Pagure</title>", 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. "repos",
  89. "test.git",
  90. "hooks",
  91. "post-receive.irc",
  92. )
  93. )
  94. )
  95. # Activate hook
  96. data = {
  97. "csrf_token": csrf_token,
  98. "active": "y",
  99. "server": "irc.freenode.net",
  100. "port": 6667,
  101. "room": "#fedora-apps",
  102. }
  103. output = self.app.post(
  104. "/test/settings/IRC", data=data, follow_redirects=True
  105. )
  106. self.assertEqual(output.status_code, 200)
  107. output_text = output.get_data(as_text=True)
  108. self.assertIn(
  109. '<h5 class="pl-2 font-weight-bold text-muted">'
  110. "Project Settings</h5>\n",
  111. output_text,
  112. )
  113. self.assertIn("Hook IRC activated", output_text)
  114. output = self.app.get("/test/settings/IRC")
  115. self.assertEqual(output.status_code, 200)
  116. output_text = output.get_data(as_text=True)
  117. self.assertIn(
  118. "<title>Settings IRC - test - Pagure</title>", output_text
  119. )
  120. self.assertIn(
  121. '<input checked class="form-check-input mt-2" id="active" name="active" '
  122. 'type="checkbox" value="y">',
  123. output_text,
  124. )
  125. # TODO: Fix this
  126. # self.assertTrue(os.path.exists(os.path.join(
  127. # self.path, 'repos', 'test.git', 'hooks', 'post-receive.irc')))
  128. # De-Activate hook
  129. data = {"csrf_token": csrf_token}
  130. output = self.app.post(
  131. "/test/settings/IRC", data=data, follow_redirects=True
  132. )
  133. self.assertEqual(output.status_code, 200)
  134. output_text = output.get_data(as_text=True)
  135. self.assertIn(
  136. '<h5 class="pl-2 font-weight-bold text-muted">'
  137. "Project Settings</h5>\n",
  138. output_text,
  139. )
  140. self.assertIn("Hook IRC deactivated", output_text)
  141. output = self.app.get("/test/settings/IRC")
  142. self.assertEqual(output.status_code, 200)
  143. output_text = output.get_data(as_text=True)
  144. self.assertIn(
  145. "<title>Settings IRC - test - Pagure</title>", output_text
  146. )
  147. self.assertIn(
  148. '<input class="form-check-input mt-2" id="active" name="active" '
  149. 'type="checkbox" value="y">',
  150. output_text,
  151. )
  152. self.assertFalse(
  153. os.path.exists(
  154. os.path.join(
  155. self.path,
  156. "repos",
  157. "test.git",
  158. "hooks",
  159. "post-receive.irc",
  160. )
  161. )
  162. )
  163. if __name__ == "__main__":
  164. unittest.main(verbosity=2)