test_pagure_flask_ui_plugins_mail.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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(0, os.path.join(os.path.dirname(
  12. os.path.abspath(__file__)), '..'))
  13. import tests
  14. class PagureFlaskPluginMailtests(tests.SimplePagureTest):
  15. """ Tests for flask plugins controller of pagure """
  16. def test_plugin_mail(self):
  17. """ Test the mail plugin on/off endpoint. """
  18. tests.create_projects(self.session)
  19. tests.create_projects_git(os.path.join(self.path, 'repos'))
  20. user = tests.FakeUser(username='pingou')
  21. with tests.user_set(self.app.application, user):
  22. output = self.app.get('/test/settings/Mail')
  23. self.assertEqual(output.status_code, 200)
  24. output_text = output.get_data(as_text=True)
  25. self.assertIn(
  26. '<title>Settings Mail - test - Pagure</title>', output_text)
  27. self.assertIn(
  28. '<label for="mail_to">Mail to</label>',
  29. output_text)
  30. self.assertIn(
  31. '<input class="form-check-input mt-2" id="active" name="active" '
  32. 'type="checkbox" value="y">', output_text)
  33. csrf_token = output_text.split(
  34. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  35. data = {}
  36. output = self.app.post('/test/settings/Mail', data=data)
  37. self.assertEqual(output.status_code, 200)
  38. output_text = output.get_data(as_text=True)
  39. self.assertIn(
  40. '<title>Settings Mail - test - Pagure</title>', output_text)
  41. self.assertIn(
  42. '<label for="mail_to">Mail to</label>', output_text)
  43. self.assertIn(
  44. '<input class="form-check-input mt-2" id="active" name="active" '
  45. 'type="checkbox" value="y">', output_text)
  46. data['csrf_token'] = csrf_token
  47. # With the git repo
  48. output = self.app.post(
  49. '/test/settings/Mail', data=data, follow_redirects=True)
  50. self.assertEqual(output.status_code, 200)
  51. output_text = output.get_data(as_text=True)
  52. self.assertIn(
  53. '<h5 class="pl-2 font-weight-bold text-muted">'
  54. 'Project Settings</h5>\n', output_text)
  55. self.assertIn(
  56. 'Hook Mail deactivated', output_text)
  57. output = self.app.get('/test/settings/Mail')
  58. self.assertEqual(output.status_code, 200)
  59. output_text = output.get_data(as_text=True)
  60. self.assertIn(
  61. '<title>Settings Mail - test - Pagure</title>', output_text)
  62. self.assertIn(
  63. '<label for="mail_to">Mail to</label>', output_text)
  64. self.assertIn(
  65. '<input class="form-check-input mt-2" id="active" name="active" '
  66. 'type="checkbox" value="y">', output_text)
  67. # Missing the required mail_to
  68. data = {'csrf_token': csrf_token, 'active': 'y'}
  69. output = self.app.post(
  70. '/test/settings/Mail', data=data, follow_redirects=True)
  71. self.assertEqual(output.status_code, 200)
  72. output_text = output.get_data(as_text=True)
  73. self.assertIn(
  74. '<title>Settings Mail - test - Pagure</title>', output_text)
  75. self.assertNotIn(
  76. 'Hook activated', output_text)
  77. if self.get_wtforms_version() >= (2, 2):
  78. self.assertIn(
  79. '<div class="col-sm-10">\n '
  80. '<input class="form-control pl-0" id="mail_to" name="mail_to" '
  81. 'required type="text" value="">\n </div>\n </div>\n '
  82. '<div class="alert alert-danger">This field is required.</div>',
  83. output_text)
  84. else:
  85. self.assertIn(
  86. '<div class="col-sm-10">\n '
  87. '<input class="form-control pl-0" id="mail_to" name="mail_to" '
  88. 'type="text" value="">\n </div>\n </div>\n '
  89. '<div class="alert alert-danger">This field is required.</div>',
  90. output_text)
  91. self.assertIn(
  92. '<input checked class="form-check-input mt-2" id="active" name="active" '
  93. 'type="checkbox" value="y">', output_text)
  94. # Activate hook
  95. data = {
  96. 'csrf_token': csrf_token,
  97. 'active': 'y',
  98. 'mail_to': 'foo@bar'
  99. }
  100. output = self.app.post(
  101. '/test/settings/Mail', data=data, follow_redirects=True)
  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', output_text)
  107. self.assertIn(
  108. 'Hook Mail activated', output_text)
  109. output = self.app.get('/test/settings/Mail')
  110. output_text = output.get_data(as_text=True)
  111. self.assertIn(
  112. '<title>Settings Mail - test - Pagure</title>', output_text)
  113. self.assertIn(
  114. '<label for="mail_to">Mail to</label>', output_text)
  115. self.assertIn(
  116. '<input checked class="form-check-input mt-2" id="active" name="active" '
  117. 'type="checkbox" value="y">', output_text)
  118. # De-Activate hook
  119. data = {'csrf_token': csrf_token}
  120. output = self.app.post(
  121. '/test/settings/Mail', data=data, follow_redirects=True)
  122. self.assertEqual(output.status_code, 200)
  123. output_text = output.get_data(as_text=True)
  124. self.assertIn(
  125. '<h5 class="pl-2 font-weight-bold text-muted">'
  126. 'Project Settings</h5>\n', output_text)
  127. self.assertIn(
  128. 'Hook Mail deactivated', output_text)
  129. output = self.app.get('/test/settings/Mail')
  130. output_text = output.get_data(as_text=True)
  131. self.assertIn(
  132. '<title>Settings Mail - test - Pagure</title>', output_text)
  133. self.assertIn(
  134. '<label for="mail_to">Mail to</label>', output_text)
  135. self.assertIn(
  136. '<input class="form-check-input mt-2" id="active" name="active" '
  137. 'type="checkbox" value="y">', output_text)
  138. if __name__ == '__main__':
  139. unittest.main(verbosity=2)