test_pagure_flask_api_auth.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.SimplePagureTest):
  21. """ Tests for the authentication in the flask API of pagure """
  22. def test_auth_no_data(self):
  23. """ Test the authentication when there is nothing in the database.
  24. """
  25. output = self.app.post('/api/0/foo/new_issue')
  26. self.assertEqual(output.status_code, 401)
  27. data = json.loads(output.data)
  28. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  29. data['error_code'])
  30. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  31. headers = {'Authorization': 'token aabbbccc'}
  32. output = self.app.post('/api/0/foo/new_issue', headers=headers)
  33. self.assertEqual(output.status_code, 401)
  34. data = json.loads(output.data)
  35. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  36. data['error_code'])
  37. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  38. def test_auth_noacl(self):
  39. """ Test the authentication when the token does not have any ACL.
  40. """
  41. tests.create_projects(self.session)
  42. tests.create_tokens(self.session)
  43. output = self.app.post('/api/0/test/new_issue')
  44. self.assertEqual(output.status_code, 401)
  45. data = json.loads(output.data)
  46. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  47. data['error_code'])
  48. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  49. headers = {'Authorization': 'token aaabbbcccddd'}
  50. output = self.app.post('/api/0/test/new_issue', headers=headers)
  51. self.assertEqual(output.status_code, 401)
  52. data = json.loads(output.data)
  53. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  54. data['error_code'])
  55. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  56. def test_auth_expired(self):
  57. """ Test the authentication when the token has expired.
  58. """
  59. tests.create_projects(self.session)
  60. tests.create_tokens(self.session)
  61. output = self.app.post('/api/0/test/new_issue')
  62. self.assertEqual(output.status_code, 401)
  63. data = json.loads(output.data)
  64. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  65. data['error_code'])
  66. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  67. headers = {'Authorization': 'token expired_token'}
  68. output = self.app.post('/api/0/test/new_issue', headers=headers)
  69. self.assertEqual(output.status_code, 401)
  70. data = json.loads(output.data)
  71. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  72. data['error_code'])
  73. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  74. def test_auth(self):
  75. """ Test the token based authentication.
  76. """
  77. tests.create_projects(self.session)
  78. tests.create_tokens(self.session)
  79. tests.create_tokens_acl(self.session)
  80. output = self.app.post('/api/0/test/new_issue')
  81. self.assertEqual(output.status_code, 401)
  82. data = json.loads(output.data)
  83. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  84. data['error_code'])
  85. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  86. headers = {'Authorization': 'token aaabbbcccddd'}
  87. output = self.app.post('/api/0/test/new_issue', headers=headers)
  88. self.assertEqual(output.status_code, 400)
  89. data = json.loads(output.data)
  90. self.assertDictEqual(
  91. data,
  92. {
  93. "error": "Invalid or incomplete input submitted",
  94. "error_code": "EINVALIDREQ",
  95. "errors": {
  96. "issue_content": ["This field is required."],
  97. "title": ["This field is required."]
  98. }
  99. }
  100. )
  101. if __name__ == '__main__':
  102. unittest.main(verbosity=2)