test_pagure_flask_api_auth.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - 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 unittest
  10. import shutil
  11. import sys
  12. import os
  13. import json
  14. from mock import patch
  15. sys.path.insert(0, os.path.join(os.path.dirname(
  16. os.path.abspath(__file__)), '..'))
  17. import pagure.api
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskApiAuthtests(tests.Modeltests):
  21. """ Tests for the authentication in the flask API of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskApiAuthtests, self).setUp()
  25. pagure.APP.config['TESTING'] = True
  26. pagure.SESSION = self.session
  27. pagure.api.SESSION = self.session
  28. pagure.api.issue.SESSION = self.session
  29. pagure.lib.SESSION = self.session
  30. self.app = pagure.APP.test_client()
  31. def test_auth_no_data(self):
  32. """ Test the authentication when there is nothing in the database.
  33. """
  34. output = self.app.post('/api/0/foo/new_issue')
  35. self.assertEqual(output.status_code, 401)
  36. data = json.loads(output.data)
  37. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  38. data['error_code'])
  39. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  40. headers = {'Authorization': 'token aabbbccc'}
  41. output = self.app.post('/api/0/foo/new_issue', headers=headers)
  42. self.assertEqual(output.status_code, 401)
  43. data = json.loads(output.data)
  44. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  45. data['error_code'])
  46. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  47. def test_auth_noacl(self):
  48. """ Test the authentication when the token does not have any ACL.
  49. """
  50. tests.create_projects(self.session)
  51. tests.create_tokens(self.session)
  52. output = self.app.post('/api/0/test/new_issue')
  53. self.assertEqual(output.status_code, 401)
  54. data = json.loads(output.data)
  55. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  56. data['error_code'])
  57. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  58. headers = {'Authorization': 'token aaabbbcccddd'}
  59. output = self.app.post('/api/0/test/new_issue', headers=headers)
  60. self.assertEqual(output.status_code, 401)
  61. data = json.loads(output.data)
  62. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  63. data['error_code'])
  64. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  65. def test_auth_expired(self):
  66. """ Test the authentication when the token has expired.
  67. """
  68. tests.create_projects(self.session)
  69. tests.create_tokens(self.session)
  70. output = self.app.post('/api/0/test/new_issue')
  71. self.assertEqual(output.status_code, 401)
  72. data = json.loads(output.data)
  73. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  74. data['error_code'])
  75. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  76. headers = {'Authorization': 'token expired_token'}
  77. output = self.app.post('/api/0/test/new_issue', headers=headers)
  78. self.assertEqual(output.status_code, 401)
  79. data = json.loads(output.data)
  80. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  81. data['error_code'])
  82. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  83. def test_auth(self):
  84. """ Test the token based authentication.
  85. """
  86. tests.create_projects(self.session)
  87. tests.create_tokens(self.session)
  88. tests.create_tokens_acl(self.session)
  89. output = self.app.post('/api/0/test/new_issue')
  90. self.assertEqual(output.status_code, 401)
  91. data = json.loads(output.data)
  92. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  93. data['error_code'])
  94. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  95. headers = {'Authorization': 'token aaabbbcccddd'}
  96. output = self.app.post('/api/0/test/new_issue', headers=headers)
  97. self.assertEqual(output.status_code, 400)
  98. data = json.loads(output.data)
  99. self.assertDictEqual(
  100. data,
  101. {
  102. "error": "Invalid or incomplete input submited",
  103. "error_code": "EINVALIDREQ",
  104. "errors": {
  105. "issue_content": ["This field is required."],
  106. "title": ["This field is required."]
  107. }
  108. }
  109. )
  110. if __name__ == '__main__':
  111. unittest.main(verbosity=2)