1
0

test_pagure_flask_ui_plugins.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.lib.plugins
  21. import pagure.hooks
  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. self.app = pagure.APP.test_client()
  44. def test_get_plugin_names(self):
  45. """ Test the get_plugin_names function. """
  46. names = pagure.lib.plugins.get_plugin_names()
  47. self.assertEqual(
  48. sorted(names),
  49. [
  50. 'Block Un-Signed commits', 'Block non fast-forward pushes',
  51. 'Fedmsg', 'IRC', 'Mail', 'Pagure', 'Pagure CI',
  52. 'Pagure requests', 'Pagure tickets', 'Read the Doc',
  53. ]
  54. )
  55. def test_get_plugin(self):
  56. """ Test the get_plugin function. """
  57. name = pagure.lib.plugins.get_plugin('Mail')
  58. self.assertEqual(str(name), "<class 'pagure.hooks.mail.Mail'>")
  59. def test_view_plugin_page(self):
  60. """ Test the view_plugin_page endpoint. """
  61. # No Git repo
  62. output = self.app.get('/foo/settings/Mail')
  63. self.assertEqual(output.status_code, 404)
  64. user = tests.FakeUser()
  65. with tests.user_set(pagure.APP, user):
  66. output = self.app.get('/foo/settings/Mail')
  67. self.assertEqual(output.status_code, 404)
  68. tests.create_projects(self.session)
  69. tests.create_projects_git(os.path.join(self.path, 'repos'))
  70. output = self.app.get('/test/settings/Mail')
  71. self.assertEqual(output.status_code, 403)
  72. # User not logged in
  73. output = self.app.get('/test/settings/Mail')
  74. self.assertEqual(output.status_code, 302)
  75. user.username = 'pingou'
  76. with tests.user_set(pagure.APP, user):
  77. output = self.app.get('/test/settings/Mail')
  78. self.assertEqual(output.status_code, 200)
  79. self.assertIn(
  80. '<form action="/test/settings/Mail" method="post">',
  81. output.data)
  82. self.assertIn(
  83. '<td><label for="mail_to">Mail to</label></td>',
  84. output.data)
  85. csrf_token = output.data.split(
  86. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  87. data = {
  88. 'active': True,
  89. 'mail_to': 'pingou@fp.org',
  90. 'csrf_token': csrf_token,
  91. }
  92. output = self.app.post(
  93. '/test/settings/Mail', data=data, follow_redirects=True)
  94. self.assertEqual(output.status_code, 200)
  95. self.assertIn(
  96. '<section class="settings">\n <h3>Settings for test</h3>',
  97. output.data)
  98. self.assertIn(
  99. '</button>\n Hook Mail activated', output.data)
  100. data = {
  101. 'mail_to': '',
  102. 'csrf_token': csrf_token,
  103. }
  104. output = self.app.post(
  105. '/test/settings/Mail', data=data, follow_redirects=True)
  106. self.assertEqual(output.status_code, 200)
  107. self.assertIn(
  108. '<section class="settings">\n <h3>Settings for test</h3>',
  109. output.data)
  110. self.assertIn(
  111. '</button>\n Hook Mail deactivated', output.data)
  112. def test_RequiredIf(self):
  113. """ Test the behavior of the RequiredIf validator. """
  114. form = FakeForm()
  115. try:
  116. form.validate()
  117. except Exception as err:
  118. self.assertEqual(
  119. str(err), 'no field named "active" in form')
  120. if __name__ == '__main__':
  121. unittest.main(verbosity=2)