test_pagure_flask_ui_plugins.py 4.4 KB

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