test_pagure_flask_ui_plugins.py 4.1 KB

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