-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_cors_configuration.py
156 lines (124 loc) · 5.15 KB
/
test_cors_configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Documentation on CORS spec, see MDN
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
from unittest.mock import patch
from django.test import override_settings
from django.urls import path
from rest_framework import views
from rest_framework.response import Response
from rest_framework.test import APITestCase
from openklant.accounts.tests.factories import SuperUserFactory
class View(views.APIView):
def get(self, request, *args, **kwargs):
return Response({"ok": True})
post = get
urlpatterns = [path("cors", View.as_view())]
class CorsMixin:
def setUp(self):
super().setUp()
mocker = patch(
"openklant.utils.middleware.get_version_mapping",
return_value={"/": "1.0.0"},
)
mocker.start()
self.addCleanup(mocker.stop)
@override_settings(ROOT_URLCONF="openklant.tests.test_cors_configuration")
class DefaultCORSConfigurationTests(CorsMixin, APITestCase):
"""
Test the default CORS settings.
"""
def test_preflight_request(self):
"""
Test the most basic preflight request.
"""
response = self.client.options(
"/cors",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="POST",
HTTP_ACCESS_CONTROL_REQUEST_HEADERS="origin, x-requested-with",
HTTP_ORIGIN="https://evil.com",
)
self.assertNotIn("Access-Control-Allow-Origin", response)
self.assertNotIn("Access-Control-Allow-Credentials", response)
def test_credentialed_request(self):
user = SuperUserFactory.create(password="secret")
self.client.force_login(user=user)
response = self.client.get(
"/cors",
HTTP_ORIGIN="https://evil.com",
)
self.assertNotIn("Access-Control-Allow-Origin", response)
self.assertNotIn("Access-Control-Allow-Credentials", response)
@override_settings(
ROOT_URLCONF="openklant.tests.test_cors_configuration",
CORS_ALLOW_ALL_ORIGINS=True,
CORS_ALLOW_CREDENTIALS=False,
)
class CORSEnabledWithoutCredentialsTests(CorsMixin, APITestCase):
"""
Test the default CORS settings.
"""
def test_preflight_request(self):
"""
Test the most basic preflight request.
"""
response = self.client.options(
"/cors",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="POST",
HTTP_ACCESS_CONTROL_REQUEST_HEADERS="origin, x-requested-with",
HTTP_ORIGIN="https://evil.com",
)
# wildcard "*" prevents browsers from sending credentials - this is good
self.assertNotEqual(response["Access-Control-Allow-Origin"], "https://evil.com")
self.assertEqual(response["Access-Control-Allow-Origin"], "*")
self.assertNotIn("Access-Control-Allow-Credentials", response)
def test_simple_request(self):
response = self.client.get(
"/cors",
HTTP_ORIGIN="https://evil.com",
)
# wildcard "*" prevents browsers from sending credentials - this is good
self.assertNotEqual(response["Access-Control-Allow-Origin"], "https://evil.com")
self.assertEqual(response["Access-Control-Allow-Origin"], "*")
self.assertNotIn("Access-Control-Allow-Credentials", response)
def test_credentialed_request(self):
user = SuperUserFactory.create(password="secret")
self.client.force_login(user=user)
response = self.client.get(
"/cors",
HTTP_ORIGIN="https://evil.com",
)
# wildcard "*" prevents browsers from sending credentials - this is good
self.assertNotEqual(response["Access-Control-Allow-Origin"], "https://evil.com")
self.assertEqual(response["Access-Control-Allow-Origin"], "*")
self.assertNotIn("Access-Control-Allow-Credentials", response)
@override_settings(
ROOT_URLCONF="openklant.tests.test_cors_configuration",
CORS_ALLOW_ALL_ORIGINS=True,
CORS_ALLOW_CREDENTIALS=False,
)
class CORSEnabledWithAuthHeaderTests(CorsMixin, APITestCase):
def test_preflight_request(self):
"""
Test a pre-flight request for requests including the HTTP Authorization header.
The inclusion of htis header makes it a not-simple request, see
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests
"""
response = self.client.options(
"/cors",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET",
HTTP_ACCESS_CONTROL_REQUEST_HEADERS="origin, x-requested-with, authorization",
HTTP_ORIGIN="https://evil.com",
)
self.assertEqual(response["Access-Control-Allow-Origin"], "*")
self.assertIn(
"authorization",
response["Access-Control-Allow-Headers"].lower(),
)
self.assertNotIn("Access-Control-Allow-Credentials", response)
def test_credentialed_request(self):
response = self.client.get(
"/cors",
HTTP_ORIGIN="http://localhost:3000",
HTTP_AUTHORIZATION="Token foobarbaz",
)
self.assertEqual(response["Access-Control-Allow-Origin"], "*")
self.assertNotIn("Access-Control-Allow-Credentials", response)