test_pagure_flask_ui_plugins_mail.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import unittest
  10. import sys
  11. import os
  12. sys.path.insert(0, os.path.join(os.path.dirname(
  13. os.path.abspath(__file__)), '..'))
  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. self.assertIn(
  29. '<label for="mail_to">Mail to</label>',
  30. output_text)
  31. self.assertIn(
  32. '<input class="form-check-input mt-2" id="active" name="active" '
  33. 'type="checkbox" value="y">', output_text)
  34. csrf_token = output_text.split(
  35. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  36. data = {}
  37. output = self.app.post('/test/settings/Mail', data=data)
  38. self.assertEqual(output.status_code, 200)
  39. output_text = output.get_data(as_text=True)
  40. self.assertIn(
  41. '<title>Settings Mail - test - Pagure</title>', output_text)
  42. self.assertIn(
  43. '<label for="mail_to">Mail to</label>', output_text)
  44. self.assertIn(
  45. '<input class="form-check-input mt-2" id="active" name="active" '
  46. 'type="checkbox" value="y">', output_text)
  47. data['csrf_token'] = csrf_token
  48. # With the git repo
  49. output = self.app.post(
  50. '/test/settings/Mail', data=data, follow_redirects=True)
  51. self.assertEqual(output.status_code, 200)
  52. output_text = output.get_data(as_text=True)
  53. self.assertIn(
  54. '<h5 class="pl-2 font-weight-bold text-muted">'
  55. 'Project Settings</h5>\n', output_text)
  56. self.assertIn(
  57. 'Hook Mail deactivated', output_text)
  58. output = self.app.get('/test/settings/Mail')
  59. self.assertEqual(output.status_code, 200)
  60. output_text = output.get_data(as_text=True)
  61. self.assertIn(
  62. '<title>Settings Mail - test - Pagure</title>', output_text)
  63. self.assertIn(
  64. '<label for="mail_to">Mail to</label>', output_text)
  65. self.assertIn(
  66. '<input class="form-check-input mt-2" id="active" name="active" '
  67. 'type="checkbox" value="y">', output_text)
  68. # Missing the required mail_to
  69. data = {'csrf_token': csrf_token, 'active': 'y'}
  70. output = self.app.post(
  71. '/test/settings/Mail', data=data, follow_redirects=True)
  72. self.assertEqual(output.status_code, 200)
  73. output_text = output.get_data(as_text=True)
  74. self.assertIn(
  75. '<title>Settings Mail - test - Pagure</title>', output_text)
  76. self.assertNotIn(
  77. 'Hook activated', output_text)
  78. if self.get_wtforms_version() >= (2, 2):
  79. self.assertIn(
  80. '<div class="col-sm-10">\n '
  81. '<input class="form-control pl-0" id="mail_to" name="mail_to" '
  82. 'required type="text" value="">\n </div>\n </div>\n '
  83. '<div class="alert alert-danger">This field is required.</div>',
  84. output_text)
  85. else:
  86. self.assertIn(
  87. '<div class="col-sm-10">\n '
  88. '<input class="form-control pl-0" id="mail_to" name="mail_to" '
  89. 'type="text" value="">\n </div>\n </div>\n '
  90. '<div class="alert alert-danger">This field is required.</div>',
  91. output_text)
  92. self.assertIn(
  93. '<input checked class="form-check-input mt-2" id="active" name="active" '
  94. 'type="checkbox" value="y">', output_text)
  95. # Activate hook
  96. data = {
  97. 'csrf_token': csrf_token,
  98. 'active': 'y',
  99. 'mail_to': 'foo@bar'
  100. }
  101. output = self.app.post(
  102. '/test/settings/Mail', data=data, follow_redirects=True)
  103. self.assertEqual(output.status_code, 200)
  104. output_text = output.get_data(as_text=True)
  105. self.assertIn(
  106. '<h5 class="pl-2 font-weight-bold text-muted">'
  107. 'Project Settings</h5>\n', output_text)
  108. self.assertIn(
  109. 'Hook Mail activated', output_text)
  110. output = self.app.get('/test/settings/Mail')
  111. output_text = output.get_data(as_text=True)
  112. self.assertIn(
  113. '<title>Settings Mail - test - Pagure</title>', output_text)
  114. self.assertIn(
  115. '<label for="mail_to">Mail to</label>', output_text)
  116. self.assertIn(
  117. '<input checked class="form-check-input mt-2" id="active" name="active" '
  118. 'type="checkbox" value="y">', output_text)
  119. # De-Activate hook
  120. data = {'csrf_token': csrf_token}
  121. output = self.app.post(
  122. '/test/settings/Mail', data=data, follow_redirects=True)
  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', output_text)
  128. self.assertIn(
  129. 'Hook Mail deactivated', output_text)
  130. output = self.app.get('/test/settings/Mail')
  131. output_text = output.get_data(as_text=True)
  132. self.assertIn(
  133. '<title>Settings Mail - test - Pagure</title>', output_text)
  134. self.assertIn(
  135. '<label for="mail_to">Mail to</label>', output_text)
  136. self.assertIn(
  137. '<input class="form-check-input mt-2" id="active" name="active" '
  138. 'type="checkbox" value="y">', output_text)
  139. if __name__ == '__main__':
  140. unittest.main(verbosity=2)