test_pagure_flask_ui_plugins_mail.py 6.6 KB

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