|
| 1 | +import base64 |
1 | 2 | import json
|
2 | 3 |
|
3 | 4 | import pytest
|
| 5 | +from django.contrib.auth import get_user_model |
4 | 6 | from django.test import RequestFactory, TestCase
|
| 7 | +from django.utils.timezone import now, timedelta |
5 | 8 |
|
6 | 9 | from oauth2_provider.backends import get_oauthlib_core
|
7 |
| -from oauth2_provider.models import redirect_to_uri_allowed |
| 10 | +from oauth2_provider.models import get_access_token_model, get_application_model, redirect_to_uri_allowed |
8 | 11 | from oauth2_provider.oauth2_backends import JSONOAuthLibCore, OAuthLibCore
|
9 | 12 |
|
10 | 13 |
|
@@ -50,6 +53,96 @@ def test_application_json_extract_params(self):
|
50 | 53 | self.assertNotIn("password=123456", body)
|
51 | 54 |
|
52 | 55 |
|
| 56 | +UserModel = get_user_model() |
| 57 | +ApplicationModel = get_application_model() |
| 58 | +AccessTokenModel = get_access_token_model() |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.usefixtures("oauth2_settings") |
| 62 | +class TestOAuthLibCoreBackendErrorHandling(TestCase): |
| 63 | + def setUp(self): |
| 64 | + self.factory = RequestFactory() |
| 65 | + self.oauthlib_core = OAuthLibCore() |
| 66 | + self. user = UserModel. objects. create_user( "john", "[email protected]", "123456") |
| 67 | + self.app = ApplicationModel.objects.create( |
| 68 | + name="app", |
| 69 | + client_id="app_id", |
| 70 | + client_secret="app_secret", |
| 71 | + client_type=ApplicationModel.CLIENT_CONFIDENTIAL, |
| 72 | + authorization_grant_type=ApplicationModel.GRANT_PASSWORD, |
| 73 | + user=self.user, |
| 74 | + ) |
| 75 | + |
| 76 | + def tearDown(self): |
| 77 | + self.user.delete() |
| 78 | + self.app.delete() |
| 79 | + |
| 80 | + def test_create_token_response_valid(self): |
| 81 | + payload = ( |
| 82 | + "grant_type=password&username=john&password=123456&client_id=app_id&client_secret=app_secret" |
| 83 | + ) |
| 84 | + request = self.factory.post( |
| 85 | + "/o/token/", |
| 86 | + payload, |
| 87 | + content_type="application/x-www-form-urlencoded", |
| 88 | + HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(b"john:123456").decode(), |
| 89 | + ) |
| 90 | + |
| 91 | + uri, headers, body, status = self.oauthlib_core.create_token_response(request) |
| 92 | + self.assertEqual(status, 200) |
| 93 | + |
| 94 | + def test_create_token_response_query_params(self): |
| 95 | + payload = ( |
| 96 | + "grant_type=password&username=john&password=123456&client_id=app_id&client_secret=app_secret" |
| 97 | + ) |
| 98 | + request = self.factory.post( |
| 99 | + "/o/token/?test=foo", |
| 100 | + payload, |
| 101 | + content_type="application/x-www-form-urlencoded", |
| 102 | + HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(b"john:123456").decode(), |
| 103 | + ) |
| 104 | + uri, headers, body, status = self.oauthlib_core.create_token_response(request) |
| 105 | + |
| 106 | + self.assertEqual(status, 400) |
| 107 | + self.assertDictEqual( |
| 108 | + json.loads(body), |
| 109 | + {"error": "invalid_request", "error_description": "URL query parameters are not allowed"}, |
| 110 | + ) |
| 111 | + |
| 112 | + def test_create_revocation_response_valid(self): |
| 113 | + AccessTokenModel.objects.create( |
| 114 | + user=self.user, token="tokstr", application=self.app, expires=now() + timedelta(days=365) |
| 115 | + ) |
| 116 | + payload = "client_id=app_id&client_secret=app_secret&token=tokstr" |
| 117 | + request = self.factory.post( |
| 118 | + "/o/revoke_token/", |
| 119 | + payload, |
| 120 | + content_type="application/x-www-form-urlencoded", |
| 121 | + HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(b"john:123456").decode(), |
| 122 | + ) |
| 123 | + uri, headers, body, status = self.oauthlib_core.create_revocation_response(request) |
| 124 | + self.assertEqual(status, 200) |
| 125 | + |
| 126 | + def test_create_revocation_response_query_params(self): |
| 127 | + token = AccessTokenModel.objects.create( |
| 128 | + user=self.user, token="tokstr", application=self.app, expires=now() + timedelta(days=365) |
| 129 | + ) |
| 130 | + payload = "client_id=app_id&client_secret=app_secret&token=tokstr" |
| 131 | + request = self.factory.post( |
| 132 | + "/o/revoke_token/?test=foo", |
| 133 | + payload, |
| 134 | + content_type="application/x-www-form-urlencoded", |
| 135 | + HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(b"john:123456").decode(), |
| 136 | + ) |
| 137 | + uri, headers, body, status = self.oauthlib_core.create_revocation_response(request) |
| 138 | + self.assertEqual(status, 400) |
| 139 | + self.assertDictEqual( |
| 140 | + json.loads(body), |
| 141 | + {"error": "invalid_request", "error_description": "URL query parameters are not allowed"}, |
| 142 | + ) |
| 143 | + token.delete() |
| 144 | + |
| 145 | + |
53 | 146 | class TestCustomOAuthLibCoreBackend(TestCase):
|
54 | 147 | """
|
55 | 148 | Tests that the public API behaves as expected when we override
|
|
0 commit comments