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