test_pagure_flask_ui_plugins.py 4.5 KB

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