test_pagure_flask_ui_plugins_mail.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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'] = self.path
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. self.path, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. self.path, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. self.path, '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. tests.create_projects_git(self.path)
  44. user = tests.FakeUser(username='pingou')
  45. with tests.user_set(pagure.APP, user):
  46. output = self.app.get('/test/settings/Mail')
  47. self.assertEqual(output.status_code, 200)
  48. self.assertIn(
  49. '<div class="projectinfo m-t-1 m-b-1">\n'
  50. 'test project #1 </div>', output.get_data(as_text=True))
  51. self.assertTrue('<h3>Mail settings</h3>' in output.get_data(as_text=True))
  52. self.assertTrue(
  53. '<td><label for="mail_to">Mail to</label></td>'
  54. in output.get_data(as_text=True))
  55. self.assertTrue(
  56. '<input id="active" name="active" type="checkbox" value="y">'
  57. in output.get_data(as_text=True))
  58. csrf_token = output.get_data(as_text=True).split(
  59. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  60. data = {}
  61. output = self.app.post('/test/settings/Mail', data=data)
  62. self.assertEqual(output.status_code, 200)
  63. self.assertIn(
  64. '<div class="projectinfo m-t-1 m-b-1">\n'
  65. 'test project #1 </div>', output.get_data(as_text=True))
  66. self.assertTrue('<h3>Mail settings</h3>' in output.get_data(as_text=True))
  67. self.assertTrue(
  68. '<td><label for="mail_to">Mail to</label></td>'
  69. in output.get_data(as_text=True))
  70. self.assertTrue(
  71. '<input id="active" name="active" type="checkbox" value="y">'
  72. in output.get_data(as_text=True))
  73. data['csrf_token'] = csrf_token
  74. # With the git repo
  75. output = self.app.post(
  76. '/test/settings/Mail', data=data, follow_redirects=True)
  77. self.assertEqual(output.status_code, 200)
  78. self.assertIn(
  79. '<section class="settings">\n <h3>Settings for test</h3>',
  80. output.get_data(as_text=True))
  81. self.assertTrue(
  82. '</button>\n Hook Mail inactived' in output.get_data(as_text=True))
  83. output = self.app.get('/test/settings/Mail')
  84. self.assertEqual(output.status_code, 200)
  85. self.assertIn(
  86. '<div class="projectinfo m-t-1 m-b-1">\n'
  87. 'test project #1 </div>', output.get_data(as_text=True))
  88. self.assertTrue('<h3>Mail settings</h3>' in output.get_data(as_text=True))
  89. self.assertTrue(
  90. '<td><label for="mail_to">Mail to</label></td>'
  91. in output.get_data(as_text=True))
  92. self.assertTrue(
  93. '<input id="active" name="active" type="checkbox" value="y">'
  94. in output.get_data(as_text=True))
  95. self.assertFalse(os.path.exists(os.path.join(
  96. self.path, 'test.git', 'hooks', 'post-receive.mail')))
  97. # Missing the required mail_to
  98. data = {'csrf_token': csrf_token, 'active': 'y'}
  99. output = self.app.post(
  100. '/test/settings/Mail', data=data, follow_redirects=True)
  101. self.assertEqual(output.status_code, 200)
  102. self.assertIn(
  103. '<div class="projectinfo m-t-1 m-b-1">\n'
  104. 'test project #1 </div>', output.get_data(as_text=True))
  105. self.assertTrue('<h3>Mail settings</h3>' in output.get_data(as_text=True))
  106. self.assertFalse(
  107. '</button>\n Hook activated' in output.get_data(as_text=True))
  108. self.assertTrue(
  109. '<input id="mail_to" name="mail_to" type="text" value=""></td>'
  110. '\n<td class="errors">This field is required.</td>'
  111. in output.get_data(as_text=True))
  112. self.assertTrue(
  113. '<input checked id="active" name="active" type="checkbox" '
  114. 'value="y">' in output.get_data(as_text=True))
  115. self.assertFalse(os.path.exists(os.path.join(
  116. self.path, 'test.git', 'hooks', 'post-receive.mail')))
  117. # Activate hook
  118. data = {
  119. 'csrf_token': csrf_token,
  120. 'active': 'y',
  121. 'mail_to': 'foo@bar'
  122. }
  123. output = self.app.post(
  124. '/test/settings/Mail', data=data, follow_redirects=True)
  125. self.assertEqual(output.status_code, 200)
  126. self.assertIn(
  127. '<section class="settings">\n <h3>Settings for test</h3>',
  128. output.get_data(as_text=True))
  129. self.assertTrue(
  130. '</button>\n Hook Mail activated' in output.get_data(as_text=True))
  131. output = self.app.get('/test/settings/Mail')
  132. self.assertIn(
  133. '<div class="projectinfo m-t-1 m-b-1">\n'
  134. 'test project #1 </div>', output.get_data(as_text=True))
  135. self.assertTrue('<h3>Mail settings</h3>' in output.get_data(as_text=True))
  136. self.assertTrue(
  137. '<td><label for="mail_to">Mail to</label></td>'
  138. in output.get_data(as_text=True))
  139. self.assertTrue(
  140. '<input checked id="active" name="active" type="checkbox" '
  141. 'value="y">' in output.get_data(as_text=True))
  142. self.assertTrue(os.path.exists(os.path.join(
  143. self.path, 'test.git', 'hooks', 'post-receive.mail')))
  144. # De-Activate hook
  145. data = {'csrf_token': csrf_token}
  146. output = self.app.post(
  147. '/test/settings/Mail', data=data, follow_redirects=True)
  148. self.assertEqual(output.status_code, 200)
  149. self.assertIn(
  150. '<section class="settings">\n <h3>Settings for test</h3>',
  151. output.get_data(as_text=True))
  152. self.assertTrue(
  153. '</button>\n Hook Mail inactived' in output.get_data(as_text=True))
  154. output = self.app.get('/test/settings/Mail')
  155. self.assertIn(
  156. '<div class="projectinfo m-t-1 m-b-1">\n'
  157. 'test project #1 </div>', output.get_data(as_text=True))
  158. self.assertTrue('<h3>Mail settings</h3>' in output.get_data(as_text=True))
  159. self.assertTrue(
  160. '<td><label for="mail_to">Mail to</label></td>'
  161. in output.get_data(as_text=True))
  162. self.assertTrue(
  163. '<input id="active" name="active" type="checkbox" '
  164. 'value="y">' in output.get_data(as_text=True))
  165. self.assertFalse(os.path.exists(os.path.join(
  166. self.path, 'test.git', 'hooks', 'post-receive.mail')))
  167. if __name__ == '__main__':
  168. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  169. PagureFlaskPluginMailtests)
  170. unittest.TextTestRunner(verbosity=2).run(SUITE)