test_pagure_flask_ui_plugins.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - 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. import wtforms
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.lib.plugins
  16. import pagure.hooks
  17. import tests
  18. class FakeForm(wtforms.Form):
  19. ''' Form to configure the mail hook. '''
  20. field1 = wtforms.StringField(
  21. 'Title', [pagure.hooks.RequiredIf('active')]
  22. )
  23. field2 = wtforms.BooleanField(
  24. 'Title2', [wtforms.validators.Optional()]
  25. )
  26. class PagureFlaskPluginstests(tests.SimplePagureTest):
  27. """ Tests for flask plugins controller of pagure """
  28. def test_get_plugin_names(self):
  29. """ Test the get_plugin_names function. """
  30. names = pagure.lib.plugins.get_plugin_names()
  31. self.assertEqual(
  32. sorted(names),
  33. [
  34. 'Block Un-Signed commits',
  35. 'Block non fast-forward pushes',
  36. 'Fedmsg',
  37. 'IRC',
  38. 'Mail',
  39. 'Mirroring',
  40. 'Pagure',
  41. 'Pagure CI',
  42. 'Pagure requests',
  43. 'Pagure tickets',
  44. 'Prevent creating new branches by git push',
  45. 'Read the Doc',
  46. ]
  47. )
  48. def test_get_plugin(self):
  49. """ Test the get_plugin function. """
  50. name = pagure.lib.plugins.get_plugin('Mail')
  51. self.assertEqual(str(name), "<class 'pagure.hooks.mail.Mail'>")
  52. def test_view_plugin_page(self):
  53. """ Test the view_plugin_page endpoint. """
  54. # No Git repo
  55. output = self.app.get('/foo/settings/Mail')
  56. self.assertEqual(output.status_code, 404)
  57. user = tests.FakeUser()
  58. with tests.user_set(self.app.application, user):
  59. output = self.app.get('/foo/settings/Mail')
  60. self.assertEqual(output.status_code, 404)
  61. tests.create_projects(self.session)
  62. tests.create_projects_git(os.path.join(self.path, 'repos'))
  63. output = self.app.get('/test/settings/Mail')
  64. self.assertEqual(output.status_code, 403)
  65. # User not logged in
  66. output = self.app.get('/test/settings/Mail')
  67. self.assertEqual(output.status_code, 302)
  68. user.username = 'pingou'
  69. with tests.user_set(self.app.application, user):
  70. output = self.app.get('/test/settings/Mail')
  71. self.assertEqual(output.status_code, 200)
  72. output_text = output.get_data(as_text=True)
  73. self.assertIn(
  74. '<form action="/test/settings/Mail" method="post">',
  75. output_text)
  76. self.assertIn(
  77. '<label for="mail_to">Mail to</label>',
  78. output_text)
  79. csrf_token = output_text.split(
  80. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  81. data = {
  82. 'active': True,
  83. 'mail_to': 'pingou@fp.org',
  84. 'csrf_token': csrf_token,
  85. }
  86. output = self.app.post(
  87. '/test/settings/Mail', data=data, follow_redirects=True)
  88. self.assertEqual(output.status_code, 200)
  89. output_text = output.get_data(as_text=True)
  90. self.assertIn(
  91. '<h5 class="pl-2 font-weight-bold text-muted">'
  92. 'Project Settings</h5>\n', output_text)
  93. self.assertIn(
  94. 'Hook Mail activated',
  95. output_text)
  96. data = {
  97. 'mail_to': '',
  98. 'csrf_token': csrf_token,
  99. }
  100. output = self.app.post(
  101. '/test/settings/Mail', data=data, follow_redirects=True)
  102. self.assertEqual(output.status_code, 200)
  103. output_text = output.get_data(as_text=True)
  104. self.assertIn(
  105. '<h5 class="pl-2 font-weight-bold text-muted">'
  106. 'Project Settings</h5>\n', output_text)
  107. self.assertIn(
  108. 'Hook Mail deactivated',
  109. output_text)
  110. def test_RequiredIf(self):
  111. """ Test the behavior of the RequiredIf validator. """
  112. form = FakeForm()
  113. try:
  114. form.validate()
  115. except Exception as err:
  116. self.assertEqual(
  117. str(err), 'no field named "active" in form')
  118. if __name__ == '__main__':
  119. unittest.main(verbosity=2)