test_pagure_flask_ui_plugins.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. import wtforms
  16. from mock import patch
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.lib
  20. import pagure.hooks
  21. import pagure.ui.plugins
  22. import tests
  23. class FakeForm(wtforms.Form):
  24. ''' Form to configure the mail hook. '''
  25. field1 = wtforms.TextField(
  26. 'Title', [pagure.hooks.RequiredIf('active')]
  27. )
  28. field2 = wtforms.BooleanField(
  29. 'Title2', [wtforms.validators.Optional()]
  30. )
  31. class PagureFlaskPluginstests(tests.Modeltests):
  32. """ Tests for flask plugins controller of pagure """
  33. def setUp(self):
  34. """ Set up the environnment, ran before every tests. """
  35. super(PagureFlaskPluginstests, self).setUp()
  36. pagure.APP.config['TESTING'] = True
  37. pagure.SESSION = self.session
  38. pagure.ui.SESSION = self.session
  39. pagure.ui.app.SESSION = self.session
  40. pagure.ui.plugins.SESSION = self.session
  41. pagure.ui.repo.SESSION = self.session
  42. pagure.ui.filters.SESSION = self.session
  43. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  44. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  45. tests.HERE, 'forks')
  46. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  47. tests.HERE, 'tickets')
  48. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  49. tests.HERE, 'docs')
  50. self.app = pagure.APP.test_client()
  51. def test_get_plugin_names(self):
  52. """ Test the get_plugin_names function. """
  53. names = pagure.ui.plugins.get_plugin_names()
  54. self.assertEqual(
  55. sorted(names),
  56. [
  57. 'Block Un-Signed commits', 'Block non fast-forward pushes',
  58. 'Fedmsg', 'IRC', 'Mail', 'Pagure', 'Pagure CI',
  59. 'Pagure requests', 'Pagure tickets', 'Read the Doc',
  60. ]
  61. )
  62. def test_get_plugin(self):
  63. """ Test the get_plugin function. """
  64. name = pagure.ui.plugins.get_plugin('Mail')
  65. self.assertEqual(str(name), "<class 'pagure.hooks.mail.Mail'>")
  66. def test_view_plugin_page(self):
  67. """ Test the view_plugin_page endpoint. """
  68. output = self.app.get('/foo/settings/Mail')
  69. self.assertEqual(output.status_code, 302)
  70. user = tests.FakeUser()
  71. with tests.user_set(pagure.APP, user):
  72. output = self.app.get('/foo/settings/Mail')
  73. self.assertEqual(output.status_code, 404)
  74. tests.create_projects(self.session)
  75. tests.create_projects_git(tests.HERE)
  76. output = self.app.get('/test/settings/Mail')
  77. self.assertEqual(output.status_code, 403)
  78. user.username = 'pingou'
  79. with tests.user_set(pagure.APP, user):
  80. output = self.app.get('/test/settings/Mail')
  81. self.assertEqual(output.status_code, 200)
  82. self.assertIn(
  83. '<form action="/test/settings/Mail" method="post">',
  84. output.data)
  85. self.assertIn(
  86. '<td><label for="mail_to">Mail to</label></td>',
  87. output.data)
  88. csrf_token = output.data.split(
  89. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  90. data = {
  91. 'active': True,
  92. 'mail_to': 'pingou@fp.org',
  93. 'csrf_token': csrf_token,
  94. }
  95. output = self.app.post(
  96. '/test/settings/Mail', data=data, follow_redirects=True)
  97. self.assertEqual(output.status_code, 200)
  98. self.assertIn(
  99. '<section class="settings">\n <h3>Settings for test</h3>',
  100. output.data)
  101. self.assertIn(
  102. '</button>\n Hook Mail activated', output.data)
  103. data = {
  104. 'mail_to': '',
  105. 'csrf_token': csrf_token,
  106. }
  107. output = self.app.post(
  108. '/test/settings/Mail', data=data, follow_redirects=True)
  109. self.assertEqual(output.status_code, 200)
  110. self.assertIn(
  111. '<section class="settings">\n <h3>Settings for test</h3>',
  112. output.data)
  113. self.assertIn(
  114. '</button>\n Hook Mail inactived', output.data)
  115. def test_RequiredIf(self):
  116. """ Test the behavior of the RequiredIf validator. """
  117. form = FakeForm()
  118. try:
  119. form.validate()
  120. except Exception as err:
  121. self.assertEqual(
  122. str(err), 'no field named "active" in form')
  123. if __name__ == '__main__':
  124. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskPluginstests)
  125. unittest.TextTestRunner(verbosity=2).run(SUITE)