test_pagure_flask_ui_plugins_mail.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 unittest
  9. import sys
  10. import os
  11. sys.path.insert(0, os.path.join(os.path.dirname(
  12. os.path.abspath(__file__)), '..'))
  13. import pagure.lib
  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. self.assertIn(
  26. '<div class="projectinfo m-t-1 m-b-1">\n'
  27. 'test project #1 </div>', output.data)
  28. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  29. self.assertTrue(
  30. '<label for="mail_to">Mail to</label>'
  31. in output.data)
  32. self.assertTrue(
  33. '<input class="form-control" id="active" name="active" '
  34. 'type="checkbox" value="y">' in output.data)
  35. csrf_token = output.data.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. self.assertIn(
  41. '<div class="projectinfo m-t-1 m-b-1">\n'
  42. 'test project #1 </div>', output.data)
  43. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  44. self.assertTrue(
  45. '<label for="mail_to">Mail to</label>'
  46. in output.data)
  47. self.assertTrue(
  48. '<input class="form-control" id="active" name="active" '
  49. 'type="checkbox" value="y">' in output.data)
  50. data['csrf_token'] = csrf_token
  51. # With the git repo
  52. output = self.app.post(
  53. '/test/settings/Mail', data=data, follow_redirects=True)
  54. self.assertEqual(output.status_code, 200)
  55. self.assertIn(
  56. '<section class="settings">\n <h3>Settings for test</h3>',
  57. output.data)
  58. self.assertTrue(
  59. '</button>\n Hook Mail deactivated' in output.data)
  60. output = self.app.get('/test/settings/Mail')
  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. '<label for="mail_to">Mail to</label>'
  68. in output.data)
  69. self.assertTrue(
  70. '<input class="form-control" id="active" name="active" '
  71. 'type="checkbox" value="y">' in output.data)
  72. self.assertFalse(os.path.exists(os.path.join(
  73. self.path, 'repos', 'test.git', 'hooks', 'post-receive.mail')))
  74. # Missing the required mail_to
  75. data = {'csrf_token': csrf_token, 'active': 'y'}
  76. output = self.app.post(
  77. '/test/settings/Mail', data=data, follow_redirects=True)
  78. self.assertEqual(output.status_code, 200)
  79. self.assertIn(
  80. '<div class="projectinfo m-t-1 m-b-1">\n'
  81. 'test project #1 </div>', output.data)
  82. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  83. self.assertFalse(
  84. '</button>\n Hook activated' in output.data)
  85. self.assertTrue(
  86. '<input class="form-control" id="mail_to" name="mail_to" '
  87. 'type="text" value=""></td>\n<td class="errors">'
  88. 'This field is required.</td>' in output.data)
  89. self.assertTrue(
  90. '<input checked class="form-control" id="active" name="active" '
  91. 'type="checkbox" value="y">' in output.data)
  92. self.assertFalse(os.path.exists(os.path.join(
  93. self.path, 'repos', 'test.git', 'hooks', 'post-receive.mail')))
  94. # Activate hook
  95. data = {
  96. 'csrf_token': csrf_token,
  97. 'active': 'y',
  98. 'mail_to': 'foo@bar'
  99. }
  100. output = self.app.post(
  101. '/test/settings/Mail', data=data, follow_redirects=True)
  102. self.assertEqual(output.status_code, 200)
  103. self.assertIn(
  104. '<section class="settings">\n <h3>Settings for test</h3>',
  105. output.data)
  106. self.assertTrue(
  107. '</button>\n Hook Mail activated' in output.data)
  108. output = self.app.get('/test/settings/Mail')
  109. self.assertIn(
  110. '<div class="projectinfo m-t-1 m-b-1">\n'
  111. 'test project #1 </div>', output.data)
  112. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  113. self.assertTrue(
  114. '<label for="mail_to">Mail to</label>'
  115. in output.data)
  116. self.assertTrue(
  117. '<input checked class="form-control" id="active" name="active" '
  118. 'type="checkbox" value="y">' in output.data)
  119. self.assertTrue(os.path.exists(os.path.join(
  120. self.path, 'repos', 'test.git', 'hooks', 'post-receive.mail')))
  121. # De-Activate hook
  122. data = {'csrf_token': csrf_token}
  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.data)
  129. self.assertTrue(
  130. '</button>\n Hook Mail deactivated' in output.data)
  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.data)
  135. self.assertTrue('<h3>Mail settings</h3>' in output.data)
  136. self.assertTrue(
  137. '<label for="mail_to">Mail to</label>'
  138. in output.data)
  139. self.assertTrue(
  140. '<input class="form-control" id="active" name="active" '
  141. 'type="checkbox" value="y">' in output.data)
  142. self.assertFalse(os.path.exists(os.path.join(
  143. self.path, 'repos', 'test.git', 'hooks', 'post-receive.mail')))
  144. if __name__ == '__main__':
  145. unittest.main(verbosity=2)