test_pagure_flask_ui_plugins_mail.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskPluginMailtests(tests.Modeltests):
  21. """ Tests for flask plugins controller of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskPluginMailtests, self).setUp()
  25. pagure.APP.config['TESTING'] = True
  26. pagure.SESSION = self.session
  27. pagure.ui.SESSION = self.session
  28. pagure.ui.app.SESSION = self.session
  29. pagure.ui.plugins.SESSION = self.session
  30. pagure.ui.repo.SESSION = self.session
  31. pagure.ui.filters.SESSION = self.session
  32. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. tests.HERE, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. tests.HERE, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. tests.HERE, 'docs')
  39. self.app = pagure.APP.test_client()
  40. def test_plugin_mail(self):
  41. """ Test the mail plugin on/off endpoint. """
  42. tests.create_projects(self.session)
  43. user = tests.FakeUser(username='pingou')
  44. with tests.user_set(pagure.APP, user):
  45. output = self.app.get('/test/settings/Mail')
  46. self.assertEqual(output.status_code, 200)
  47. self.assertIn(
  48. '<div class="projectinfo m-t-1 m-b-1">\n'
  49. 'test project #1 </div>', output.data)
  50. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  51. self.assertTrue(
  52. '<td><label for="mail_to">Mail to</label></td>'
  53. in output.data)
  54. self.assertTrue(
  55. '<input id="active" name="active" type="checkbox" value="y">'
  56. in output.data)
  57. csrf_token = output.data.split(
  58. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  59. data = {}
  60. output = self.app.post('/test/settings/Mail', data=data)
  61. self.assertEqual(output.status_code, 200)
  62. self.assertIn(
  63. '<div class="projectinfo m-t-1 m-b-1">\n'
  64. 'test project #1 </div>', output.data)
  65. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  66. self.assertTrue(
  67. '<td><label for="mail_to">Mail to</label></td>'
  68. in output.data)
  69. self.assertTrue(
  70. '<input id="active" name="active" type="checkbox" value="y">'
  71. in output.data)
  72. data['csrf_token'] = csrf_token
  73. # No git found
  74. output = self.app.post('/test/settings/Mail', data=data)
  75. self.assertEqual(output.status_code, 404)
  76. tests.create_projects_git(tests.HERE)
  77. # With the git repo
  78. output = self.app.post(
  79. '/test/settings/Mail', data=data, follow_redirects=True)
  80. self.assertEqual(output.status_code, 200)
  81. self.assertIn(
  82. '<section class="settings">\n <h3>Settings for test</h3>',
  83. output.data)
  84. self.assertTrue(
  85. '</button>\n Hook Mail inactived' in output.data)
  86. output = self.app.get('/test/settings/Mail')
  87. self.assertEqual(output.status_code, 200)
  88. self.assertIn(
  89. '<div class="projectinfo m-t-1 m-b-1">\n'
  90. 'test project #1 </div>', output.data)
  91. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  92. self.assertTrue(
  93. '<td><label for="mail_to">Mail to</label></td>'
  94. in output.data)
  95. self.assertTrue(
  96. '<input id="active" name="active" type="checkbox" value="y">'
  97. in output.data)
  98. self.assertFalse(os.path.exists(os.path.join(
  99. tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))
  100. # Missing the required mail_to
  101. data = {'csrf_token': csrf_token, 'active': 'y'}
  102. output = self.app.post(
  103. '/test/settings/Mail', data=data, follow_redirects=True)
  104. self.assertEqual(output.status_code, 200)
  105. self.assertIn(
  106. '<div class="projectinfo m-t-1 m-b-1">\n'
  107. 'test project #1 </div>', output.data)
  108. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  109. self.assertFalse(
  110. '</button>\n Hook activated' in output.data)
  111. self.assertTrue(
  112. '<input id="mail_to" name="mail_to" type="text" value=""></td>'
  113. '\n<td class="errors">This field is required.</td>'
  114. in output.data)
  115. self.assertTrue(
  116. '<input checked id="active" name="active" type="checkbox" '
  117. 'value="y">' in output.data)
  118. self.assertFalse(os.path.exists(os.path.join(
  119. tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))
  120. # Activate hook
  121. data = {
  122. 'csrf_token': csrf_token,
  123. 'active': 'y',
  124. 'mail_to': 'foo@bar'
  125. }
  126. output = self.app.post(
  127. '/test/settings/Mail', data=data, follow_redirects=True)
  128. self.assertEqual(output.status_code, 200)
  129. self.assertIn(
  130. '<section class="settings">\n <h3>Settings for test</h3>',
  131. output.data)
  132. self.assertTrue(
  133. '</button>\n Hook Mail activated' in output.data)
  134. output = self.app.get('/test/settings/Mail')
  135. self.assertIn(
  136. '<div class="projectinfo m-t-1 m-b-1">\n'
  137. 'test project #1 </div>', output.data)
  138. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  139. self.assertTrue(
  140. '<td><label for="mail_to">Mail to</label></td>'
  141. in output.data)
  142. self.assertTrue(
  143. '<input checked id="active" name="active" type="checkbox" '
  144. 'value="y">' in output.data)
  145. self.assertTrue(os.path.exists(os.path.join(
  146. tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))
  147. # De-Activate hook
  148. data = {'csrf_token': csrf_token}
  149. output = self.app.post(
  150. '/test/settings/Mail', data=data, follow_redirects=True)
  151. self.assertEqual(output.status_code, 200)
  152. self.assertIn(
  153. '<section class="settings">\n <h3>Settings for test</h3>',
  154. output.data)
  155. self.assertTrue(
  156. '</button>\n Hook Mail inactived' in output.data)
  157. output = self.app.get('/test/settings/Mail')
  158. self.assertIn(
  159. '<div class="projectinfo m-t-1 m-b-1">\n'
  160. 'test project #1 </div>', output.data)
  161. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  162. self.assertTrue(
  163. '<td><label for="mail_to">Mail to</label></td>'
  164. in output.data)
  165. self.assertTrue(
  166. '<input id="active" name="active" type="checkbox" '
  167. 'value="y">' in output.data)
  168. self.assertFalse(os.path.exists(os.path.join(
  169. tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))
  170. if __name__ == '__main__':
  171. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  172. PagureFlaskPluginMailtests)
  173. unittest.TextTestRunner(verbosity=2).run(SUITE)