|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +from django.test import TestCase, Client, override_settings |
| 4 | +from django.utils import timezone |
| 5 | +from django.conf.urls import patterns, url |
| 6 | +from django.http import HttpResponse |
| 7 | +from django.views.generic import View |
| 8 | + |
| 9 | +from ..models import AccessToken, get_application_model |
| 10 | +from django.contrib.auth import get_user_model |
| 11 | + |
| 12 | + |
| 13 | +Application = get_application_model() |
| 14 | +UserModel = get_user_model() |
| 15 | + |
| 16 | + |
| 17 | +class MockView(View): |
| 18 | + def post(self, request): |
| 19 | + return HttpResponse() |
| 20 | + |
| 21 | +urlpatterns = patterns( |
| 22 | + '', |
| 23 | + url(r'^cors-test/$', MockView.as_view()), |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +@override_settings( |
| 28 | + ROOT_URLCONF='oauth2_provider.tests.test_cors_middleware', |
| 29 | + AUTHENTICATION_BACKENDS=('oauth2_provider.backends.OAuth2Backend',), |
| 30 | + MIDDLEWARE_CLASSES=( |
| 31 | + 'oauth2_provider.middleware.OAuth2TokenMiddleware', |
| 32 | + 'oauth2_provider.middleware.CorsMiddleware', |
| 33 | + )) |
| 34 | +class TestCORSMiddleware(TestCase): |
| 35 | + def setUp(self): |
| 36 | + self. user = UserModel. objects. create_user( 'test_user', '[email protected]') |
| 37 | + self.application = Application.objects.create( |
| 38 | + name='Test Application', |
| 39 | + redirect_uris='https://foo.bar', |
| 40 | + user=self.user, |
| 41 | + client_type=Application.CLIENT_CONFIDENTIAL, |
| 42 | + authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE, |
| 43 | + ) |
| 44 | + |
| 45 | + self.access_token = AccessToken.objects.create( |
| 46 | + user=self.user, |
| 47 | + scope='read write', |
| 48 | + expires=timezone.now() + timedelta(seconds=300), |
| 49 | + token='secret-access-token-key', |
| 50 | + application=self.application |
| 51 | + ) |
| 52 | + |
| 53 | + auth_header = "Bearer {0}".format(self.access_token.token) |
| 54 | + self.client = Client(HTTP_AUTHORIZATION=auth_header) |
| 55 | + |
| 56 | + def test_cors_successful(self): |
| 57 | + '''Ensure that we get cors-headers according to our oauth-app''' |
| 58 | + resp = self.client.post('/cors-test/', HTTP_ORIGIN='https://foo.bar') |
| 59 | + self.assertEqual(resp.status_code, 200) |
| 60 | + self.assertEqual(resp['Access-Control-Allow-Origin'], 'https://foo.bar') |
| 61 | + self.assertEqual(resp['Access-Control-Allow-Credentials'], 'true') |
| 62 | + |
| 63 | + def test_cors_no_auth(self): |
| 64 | + '''Ensure that CORS-headers are sent non-authenticated requests''' |
| 65 | + client = Client() |
| 66 | + resp = client.post('/cors-test/', HTTP_ORIGIN='https://foo.bar') |
| 67 | + self.assertEqual(resp.status_code, 200) |
| 68 | + self.assertEqual(resp['Access-Control-Allow-Origin'], 'https://foo.bar') |
| 69 | + self.assertEqual(resp['Access-Control-Allow-Credentials'], 'true') |
| 70 | + |
| 71 | + def test_cors_wrong_origin(self): |
| 72 | + '''Ensure that CORS-headers aren't sent to requests from wrong origin''' |
| 73 | + resp = self.client.post('/cors-test/', HTTP_ORIGIN='https://bar.foo') |
| 74 | + self.assertEqual(resp.status_code, 200) |
| 75 | + self.assertFalse(resp.has_header('Access-Control-Allow-Origin')) |
| 76 | + |
| 77 | + def test_cors_200_preflight(self): |
| 78 | + '''Ensure that preflight always get 200 responses''' |
| 79 | + resp = self.client.options('/cors-test/', |
| 80 | + HTTP_ACCESS_CONTROL_REQUEST_METHOD='GET', |
| 81 | + HTTP_ORIGIN='https://foo.bar') |
| 82 | + self.assertEqual(resp.status_code, 200) |
| 83 | + self.assertEqual(resp['Access-Control-Allow-Origin'], 'https://foo.bar') |
| 84 | + self.assertTrue(resp.has_header('Access-Control-Allow-Headers')) |
| 85 | + self.assertTrue(resp.has_header('Access-Control-Allow-Methods')) |
0 commit comments