test_pagure_flask_ui_plugins_mail.py 6.8 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 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 PagureFlaskPluginMailtests(tests.SimplePagureTest):
  16. """ Tests for flask plugins controller of pagure """
  17. def test_plugin_mail(self):
  18. """ Test the mail 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/Mail")
  24. self.assertEqual(output.status_code, 200)
  25. output_text = output.get_data(as_text=True)
  26. self.assertIn(
  27. "<title>Settings Mail - test - Pagure</title>", output_text
  28. )
  29. self.assertIn('<label for="mail_to">Mail to</label>', output_text)
  30. self.assertIn(
  31. '<input class="form-check-input mt-2" id="active" name="active" '
  32. 'type="checkbox" value="y">',
  33. 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("/test/settings/Mail", data=data)
  40. self.assertEqual(output.status_code, 200)
  41. output_text = output.get_data(as_text=True)
  42. self.assertIn(
  43. "<title>Settings Mail - test - Pagure</title>", output_text
  44. )
  45. self.assertIn('<label for="mail_to">Mail to</label>', output_text)
  46. self.assertIn(
  47. '<input class="form-check-input mt-2" id="active" name="active" '
  48. 'type="checkbox" value="y">',
  49. output_text,
  50. )
  51. data["csrf_token"] = csrf_token
  52. # With the git repo
  53. output = self.app.post(
  54. "/test/settings/Mail", data=data, follow_redirects=True
  55. )
  56. self.assertEqual(output.status_code, 200)
  57. output_text = output.get_data(as_text=True)
  58. self.assertIn(
  59. '<h5 class="pl-2 font-weight-bold text-muted">'
  60. "Project Settings</h5>\n",
  61. output_text,
  62. )
  63. self.assertIn("Hook Mail deactivated", output_text)
  64. output = self.app.get("/test/settings/Mail")
  65. self.assertEqual(output.status_code, 200)
  66. output_text = output.get_data(as_text=True)
  67. self.assertIn(
  68. "<title>Settings Mail - test - Pagure</title>", output_text
  69. )
  70. self.assertIn('<label for="mail_to">Mail to</label>', output_text)
  71. self.assertIn(
  72. '<input class="form-check-input mt-2" id="active" name="active" '
  73. 'type="checkbox" value="y">',
  74. output_text,
  75. )
  76. # Missing the required mail_to
  77. data = {"csrf_token": csrf_token, "active": "y"}
  78. output = self.app.post(
  79. "/test/settings/Mail", data=data, follow_redirects=True
  80. )
  81. self.assertEqual(output.status_code, 200)
  82. output_text = output.get_data(as_text=True)
  83. self.assertIn(
  84. "<title>Settings Mail - test - Pagure</title>", output_text
  85. )
  86. self.assertNotIn("Hook activated", output_text)
  87. if self.get_wtforms_version() >= (2, 2):
  88. self.assertIn(
  89. '<div class="col-sm-10">\n '
  90. '<input class="form-control pl-0" id="mail_to" name="mail_to" '
  91. 'required type="text" value="">\n </div>\n </div>\n '
  92. '<div class="alert alert-danger">This field is required.</div>',
  93. output_text,
  94. )
  95. else:
  96. self.assertIn(
  97. '<div class="col-sm-10">\n '
  98. '<input class="form-control pl-0" id="mail_to" name="mail_to" '
  99. 'type="text" value="">\n </div>\n </div>\n '
  100. '<div class="alert alert-danger">This field is required.</div>',
  101. output_text,
  102. )
  103. self.assertIn(
  104. '<input checked class="form-check-input mt-2" id="active" name="active" '
  105. 'type="checkbox" value="y">',
  106. output_text,
  107. )
  108. # Activate hook
  109. data = {
  110. "csrf_token": csrf_token,
  111. "active": "y",
  112. "mail_to": "foo@bar",
  113. }
  114. output = self.app.post(
  115. "/test/settings/Mail", data=data, follow_redirects=True
  116. )
  117. self.assertEqual(output.status_code, 200)
  118. output_text = output.get_data(as_text=True)
  119. self.assertIn(
  120. '<h5 class="pl-2 font-weight-bold text-muted">'
  121. "Project Settings</h5>\n",
  122. output_text,
  123. )
  124. self.assertIn("Hook Mail activated", output_text)
  125. output = self.app.get("/test/settings/Mail")
  126. output_text = output.get_data(as_text=True)
  127. self.assertIn(
  128. "<title>Settings Mail - test - Pagure</title>", output_text
  129. )
  130. self.assertIn('<label for="mail_to">Mail to</label>', output_text)
  131. self.assertIn(
  132. '<input checked class="form-check-input mt-2" id="active" name="active" '
  133. 'type="checkbox" value="y">',
  134. output_text,
  135. )
  136. # De-Activate hook
  137. data = {"csrf_token": csrf_token}
  138. output = self.app.post(
  139. "/test/settings/Mail", data=data, follow_redirects=True
  140. )
  141. self.assertEqual(output.status_code, 200)
  142. output_text = output.get_data(as_text=True)
  143. self.assertIn(
  144. '<h5 class="pl-2 font-weight-bold text-muted">'
  145. "Project Settings</h5>\n",
  146. output_text,
  147. )
  148. self.assertIn("Hook Mail deactivated", output_text)
  149. output = self.app.get("/test/settings/Mail")
  150. output_text = output.get_data(as_text=True)
  151. self.assertIn(
  152. "<title>Settings Mail - test - Pagure</title>", output_text
  153. )
  154. self.assertIn('<label for="mail_to">Mail to</label>', output_text)
  155. self.assertIn(
  156. '<input class="form-check-input mt-2" id="active" name="active" '
  157. 'type="checkbox" value="y">',
  158. output_text,
  159. )
  160. if __name__ == "__main__":
  161. unittest.main(verbosity=2)