test_pagure_flask_form.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 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 datetime
  9. import unittest
  10. import sys
  11. import time
  12. import os
  13. import flask
  14. import flask_wtf
  15. from mock import patch, MagicMock
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.forms
  19. import tests
  20. class PagureFlaskFormTests(tests.SimplePagureTest):
  21. """ Tests for forms of the flask application """
  22. @patch.dict('pagure.config.config', {'SERVER_NAME': 'localhost.localdomain'})
  23. def setUp(self):
  24. super(PagureFlaskFormTests, self).setUp()
  25. def test_csrf_form_no_input(self):
  26. """ Test the CSRF validation if not CSRF is specified. """
  27. with self.app.application.test_request_context(method='POST'):
  28. flask.g.session = MagicMock()
  29. form = pagure.forms.ConfirmationForm()
  30. self.assertFalse(form.validate_on_submit())
  31. def test_csrf_form_w_invalid_input(self):
  32. """ Test the CSRF validation with an invalid CSRF specified. """
  33. with self.app.application.test_request_context(method='POST'):
  34. flask.g.session = MagicMock()
  35. form = pagure.forms.ConfirmationForm()
  36. form.csrf_token.data = 'foobar'
  37. self.assertFalse(form.validate_on_submit())
  38. def test_csrf_form_w_input(self):
  39. """ Test the CSRF validation with a valid CSRF specified. """
  40. with self.app.application.test_request_context(method='POST'):
  41. flask.g.session = MagicMock()
  42. form = pagure.forms.ConfirmationForm()
  43. form.csrf_token.data = form.csrf_token.current_token
  44. self.assertTrue(form.validate_on_submit())
  45. def test_csrf_form_w_expired_input(self):
  46. """ Test the CSRF validation with an expired CSRF specified. """
  47. with self.app.application.test_request_context(method='POST'):
  48. flask.g.session = MagicMock()
  49. form = pagure.forms.ConfirmationForm()
  50. data = form.csrf_token.current_token
  51. # CSRF token expired
  52. if hasattr(flask_wtf, '__version__') and \
  53. tuple(
  54. [int(v) for v in flask_wtf.__version__.split('.')]
  55. ) < (0, 10, 0):
  56. expires = time.time() - 1
  57. else:
  58. expires = (
  59. datetime.datetime.now() - datetime.timedelta(minutes=1)
  60. ).strftime('%Y%m%d%H%M%S')
  61. # Change the CSRF format
  62. if hasattr(flask_wtf, '__version__') and \
  63. tuple([int(e) for e in flask_wtf.__version__.split('.')]
  64. ) >= (0,14,0):
  65. import itsdangerous
  66. try: # ItsDangerous-1.0
  67. timestamp = itsdangerous.base64_encode(
  68. itsdangerous.encoding.int_to_bytes(int(expires)))
  69. except AttributeError: # ItsDangerous-0.24
  70. timestamp = itsdangerous.base64_encode(
  71. itsdangerous.int_to_bytes(int(expires)))
  72. timestamp = timestamp.decode("ascii")
  73. part1, _, part2 = data.split('.', 2)
  74. form.csrf_token.data = '.'.join([part1, timestamp, part2])
  75. else:
  76. _, hmac_csrf = data.split('##', 1)
  77. form.csrf_token.data = '%s##%s' % (expires, hmac_csrf)
  78. self.assertFalse(form.validate_on_submit())
  79. def test_csrf_form_w_unexpiring_input(self):
  80. """ Test the CSRF validation with a CSRF not expiring. """
  81. pagure.config.config['WTF_CSRF_TIME_LIMIT'] = None
  82. with self.app.application.test_request_context(method='POST'):
  83. flask.g.session = MagicMock()
  84. form = pagure.forms.ConfirmationForm()
  85. data = form.csrf_token.current_token
  86. if hasattr(flask_wtf, '__version__') and \
  87. tuple([int(e) for e in flask_wtf.__version__.split('.')]
  88. ) >= (0,14,0):
  89. form.csrf_token.data = data
  90. else:
  91. _, hmac_csrf = data.split('##', 1)
  92. # CSRF can no longer expire, they have no expiration info
  93. form.csrf_token.data = '##%s' % hmac_csrf
  94. self.assertTrue(form.validate_on_submit())
  95. def test_add_user_form(self):
  96. """ Test the AddUserForm of pagure.forms """
  97. with self.app.application.test_request_context(method='POST'):
  98. flask.g.session = MagicMock()
  99. form = pagure.forms.AddUserForm()
  100. form.csrf_token.data = form.csrf_token.current_token
  101. # No user or access given
  102. self.assertFalse(form.validate_on_submit())
  103. # No access given
  104. form.user.data = 'foo'
  105. self.assertFalse(form.validate_on_submit())
  106. form.access.data = 'admin'
  107. self.assertTrue(form.validate_on_submit())
  108. def test_add_user_to_group_form(self):
  109. """ Test the AddUserToGroup form of pagure.forms """
  110. with self.app.application.test_request_context(method='POST'):
  111. flask.g.session = MagicMock()
  112. form = pagure.forms.AddUserToGroupForm()
  113. form.csrf_token.data = form.csrf_token.current_token
  114. # No user given
  115. self.assertFalse(form.validate_on_submit())
  116. form.user.data = 'foo'
  117. # Everything given
  118. self.assertTrue(form.validate_on_submit())
  119. def test_add_group_form(self):
  120. """ Test the AddGroupForm form of pagure.forms """
  121. with self.app.application.test_request_context(method='POST'):
  122. flask.g.session = MagicMock()
  123. form = pagure.forms.AddGroupForm()
  124. form.csrf_token.data = form.csrf_token.current_token
  125. # No group given
  126. self.assertFalse(form.validate_on_submit())
  127. # No access given
  128. form.group.data = 'gname'
  129. self.assertFalse(form.validate_on_submit())
  130. form.access.data = 'admin'
  131. self.assertTrue(form.validate_on_submit())
  132. if __name__ == '__main__':
  133. unittest.main(verbosity=2)