1
0

test_pagure_flask_ui_plugins.py 4.4 KB

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