1
0

test_pagure_flask_api_auth.py 4.5 KB

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