|
| 1 | +import datetime |
| 2 | +import hashlib |
| 3 | + |
| 4 | +from django.contrib.auth import get_user_model |
| 5 | +from django.test import RequestFactory, TestCase |
| 6 | + |
| 7 | +from oauth2_provider.middleware import OAuth2ExtraTokenMiddleware |
| 8 | +from oauth2_provider.models import get_access_token_model, get_application_model |
| 9 | + |
| 10 | + |
| 11 | +Application = get_application_model() |
| 12 | +AccessToken = get_access_token_model() |
| 13 | +User = get_user_model() |
| 14 | + |
| 15 | + |
| 16 | +class TestOAuth2ExtraTokenMiddleware(TestCase): |
| 17 | + def setUp(self): |
| 18 | + self.factory = RequestFactory() |
| 19 | + self.middleware = OAuth2ExtraTokenMiddleware(lambda r: None) |
| 20 | + |
| 21 | + # Create test user and application for valid token tests |
| 22 | + self.user = User.objects.create_user("test_user", "test@example.com", "123456") |
| 23 | + self.application = Application.objects.create( |
| 24 | + name="Test Application", |
| 25 | + user=self.user, |
| 26 | + client_type=Application.CLIENT_CONFIDENTIAL, |
| 27 | + authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE, |
| 28 | + ) |
| 29 | + |
| 30 | + def test_malformed_bearer_header_no_token(self): |
| 31 | + """Test that 'Authorization: Bearer' without token doesn't crash""" |
| 32 | + request = self.factory.get("/", HTTP_AUTHORIZATION="Bearer") |
| 33 | + |
| 34 | + # This should not raise an IndexError |
| 35 | + _ = self.middleware(request) |
| 36 | + |
| 37 | + # Should not have access_token attribute |
| 38 | + self.assertFalse(hasattr(request, "access_token")) |
| 39 | + |
| 40 | + def test_malformed_bearer_header_empty_token(self): |
| 41 | + """Test that 'Authorization: Bearer ' with empty token doesn't crash""" |
| 42 | + request = self.factory.get("/", HTTP_AUTHORIZATION="Bearer ") |
| 43 | + |
| 44 | + # This should not raise an IndexError |
| 45 | + _ = self.middleware(request) |
| 46 | + |
| 47 | + # Should not have access_token attribute |
| 48 | + self.assertFalse(hasattr(request, "access_token")) |
| 49 | + |
| 50 | + def test_valid_bearer_token(self): |
| 51 | + """Test that valid bearer token works correctly""" |
| 52 | + # Create a valid access token |
| 53 | + token_string = "test-token-12345" |
| 54 | + token_checksum = hashlib.sha256(token_string.encode("utf-8")).hexdigest() |
| 55 | + access_token = AccessToken.objects.create( |
| 56 | + user=self.user, |
| 57 | + scope="read", |
| 58 | + expires=datetime.datetime.now() + datetime.timedelta(days=1), |
| 59 | + token=token_string, |
| 60 | + token_checksum=token_checksum, |
| 61 | + application=self.application, |
| 62 | + ) |
| 63 | + |
| 64 | + request = self.factory.get("/", HTTP_AUTHORIZATION=f"Bearer {token_string}") |
| 65 | + |
| 66 | + _ = self.middleware(request) |
| 67 | + |
| 68 | + # Should have access_token attribute set |
| 69 | + self.assertTrue(hasattr(request, "access_token")) |
| 70 | + self.assertEqual(request.access_token, access_token) |
| 71 | + |
| 72 | + def test_invalid_bearer_token(self): |
| 73 | + """Test that invalid bearer token doesn't crash but doesn't set access_token""" |
| 74 | + request = self.factory.get("/", HTTP_AUTHORIZATION="Bearer invalid-token-xyz") |
| 75 | + |
| 76 | + # This should not raise an exception |
| 77 | + _ = self.middleware(request) |
| 78 | + |
| 79 | + # Should not have access_token attribute |
| 80 | + self.assertFalse(hasattr(request, "access_token")) |
| 81 | + |
| 82 | + def test_no_authorization_header(self): |
| 83 | + """Test that request without Authorization header works normally""" |
| 84 | + request = self.factory.get("/") |
| 85 | + |
| 86 | + _ = self.middleware(request) |
| 87 | + |
| 88 | + # Should not have access_token attribute |
| 89 | + self.assertFalse(hasattr(request, "access_token")) |
| 90 | + |
| 91 | + def test_non_bearer_authorization_header(self): |
| 92 | + """Test that non-Bearer authorization headers are ignored""" |
| 93 | + request = self.factory.get("/", HTTP_AUTHORIZATION="Basic dXNlcjpwYXNz") |
| 94 | + |
| 95 | + _ = self.middleware(request) |
| 96 | + |
| 97 | + # Should not have access_token attribute |
| 98 | + self.assertFalse(hasattr(request, "access_token")) |
0 commit comments