|
| 1 | +importbase64 |
1 | 2 | importjson |
2 | 3 |
|
3 | 4 | importpytest |
| 5 | +fromdjango.contrib.authimportget_user_model |
4 | 6 | fromdjango.testimportRequestFactory, TestCase |
| 7 | +fromdjango.utils.timezoneimportnow, timedelta |
5 | 8 |
|
6 | 9 | fromoauth2_provider.backendsimportget_oauthlib_core |
7 | | -fromoauth2_provider.modelsimportredirect_to_uri_allowed |
| 10 | +fromoauth2_provider.modelsimportget_access_token_model, get_application_model, redirect_to_uri_allowed |
8 | 11 | fromoauth2_provider.oauth2_backendsimportJSONOAuthLibCore, 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 | +classTestOAuthLibCoreBackendErrorHandling(TestCase): |
| 63 | +defsetUp(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 | +deftearDown(self): |
| 77 | +self.user.delete() |
| 78 | +self.app.delete() |
| 79 | + |
| 80 | +deftest_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 | +deftest_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 | +deftest_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 | +deftest_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 | classTestCustomOAuthLibCoreBackend(TestCase): |
54 | 147 | """ |
55 | 148 | Tests that the public API behaves as expected when we override |
|
0 commit comments