test_pagure_flask_ui_plugins_mail.py 7.3 KB

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