1
1
from unittest import mock
2
2
3
- from corsheaders .middleware import CorsMiddleware
3
+ from corsheaders .middleware import (
4
+ ACCESS_CONTROL_ALLOW_CREDENTIALS ,
5
+ ACCESS_CONTROL_ALLOW_ORIGIN ,
6
+ CorsMiddleware ,
7
+ )
4
8
from django .conf import settings
5
9
from django .http import HttpResponse
6
10
from django .test import TestCase , override_settings
@@ -73,7 +77,8 @@ def test_allow_linked_domain_from_public_version(self):
73
77
HTTP_ORIGIN = 'http://my.valid.domain' ,
74
78
)
75
79
resp = self .middleware .process_response (request , {})
76
- self .assertIn ('Access-Control-Allow-Origin' , resp )
80
+ self .assertIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
81
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
77
82
78
83
def test_dont_allow_linked_domain_from_private_version (self ):
79
84
self .version .privacy_level = PRIVATE
@@ -84,7 +89,8 @@ def test_dont_allow_linked_domain_from_private_version(self):
84
89
HTTP_ORIGIN = 'http://my.valid.domain' ,
85
90
)
86
91
resp = self .middleware .process_response (request , {})
87
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
92
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
93
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
88
94
89
95
def test_allowed_api_public_version_from_another_domain (self ):
90
96
request = self .factory .get (
@@ -93,15 +99,17 @@ def test_allowed_api_public_version_from_another_domain(self):
93
99
HTTP_ORIGIN = 'http://docs.another.domain' ,
94
100
)
95
101
resp = self .middleware .process_response (request , {})
96
- self .assertIn ('Access-Control-Allow-Origin' , resp )
102
+ self .assertIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
103
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
97
104
98
105
request = self .factory .get (
99
106
self .url ,
100
107
{'project' : self .project .slug , 'version' : self .version .slug },
101
108
HTTP_ORIGIN = 'http://another.valid.domain' ,
102
109
)
103
110
resp = self .middleware .process_response (request , {})
104
- self .assertIn ('Access-Control-Allow-Origin' , resp )
111
+ self .assertIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
112
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
105
113
106
114
def test_not_allowed_api_private_version_from_another_domain (self ):
107
115
self .version .privacy_level = PRIVATE
@@ -112,15 +120,17 @@ def test_not_allowed_api_private_version_from_another_domain(self):
112
120
HTTP_ORIGIN = 'http://docs.another.domain' ,
113
121
)
114
122
resp = self .middleware .process_response (request , {})
115
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
123
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
124
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
116
125
117
126
request = self .factory .get (
118
127
self .url ,
119
128
{'project' : self .project .slug , 'version' : self .version .slug },
120
129
HTTP_ORIGIN = 'http://another.valid.domain' ,
121
130
)
122
131
resp = self .middleware .process_response (request , {})
123
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
132
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
133
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
124
134
125
135
def test_valid_subproject (self ):
126
136
self .assertTrue (
@@ -135,7 +145,8 @@ def test_valid_subproject(self):
135
145
HTTP_ORIGIN = 'http://my.valid.domain' ,
136
146
)
137
147
resp = self .middleware .process_response (request , {})
138
- self .assertIn ('Access-Control-Allow-Origin' , resp )
148
+ self .assertIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
149
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
139
150
140
151
def test_embed_api_private_version_linked_domain (self ):
141
152
self .version .privacy_level = PRIVATE
@@ -146,7 +157,8 @@ def test_embed_api_private_version_linked_domain(self):
146
157
HTTP_ORIGIN = 'http://my.valid.domain' ,
147
158
)
148
159
resp = self .middleware .process_response (request , {})
149
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
160
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
161
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
150
162
151
163
def test_embed_api_external_url (self ):
152
164
request = self .factory .get (
@@ -174,15 +186,17 @@ def test_sustainability_endpoint_allways_allowed(self, has_donate_app):
174
186
HTTP_ORIGIN = 'http://invalid.domain' ,
175
187
)
176
188
resp = self .middleware .process_response (request , {})
177
- self .assertIn ('Access-Control-Allow-Origin' , resp )
189
+ self .assertIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
190
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
178
191
179
192
request = self .factory .get (
180
193
'/api/v2/sustainability/' ,
181
194
{'project' : self .project .slug , 'active' : True , 'version' : self .version .slug },
182
195
HTTP_ORIGIN = 'http://my.valid.domain' ,
183
196
)
184
197
resp = self .middleware .process_response (request , {})
185
- self .assertIn ('Access-Control-Allow-Origin' , resp )
198
+ self .assertIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
199
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
186
200
187
201
@mock .patch ('readthedocs.core.signals._has_donate_app' )
188
202
def test_sustainability_endpoint_no_ext (self , has_donate_app ):
@@ -193,15 +207,17 @@ def test_sustainability_endpoint_no_ext(self, has_donate_app):
193
207
HTTP_ORIGIN = 'http://invalid.domain' ,
194
208
)
195
209
resp = self .middleware .process_response (request , {})
196
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
210
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
211
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
197
212
198
213
request = self .factory .get (
199
214
'/api/v2/sustainability/' ,
200
215
{'project' : self .project .slug , 'active' : True , 'version' : self .version .slug },
201
216
HTTP_ORIGIN = 'http://my.valid.domain' ,
202
217
)
203
218
resp = self .middleware .process_response (request , {})
204
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
219
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
220
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
205
221
206
222
def test_apiv2_endpoint_not_allowed (self ):
207
223
request = self .factory .get (
@@ -210,7 +226,8 @@ def test_apiv2_endpoint_not_allowed(self):
210
226
HTTP_ORIGIN = 'http://invalid.domain' ,
211
227
)
212
228
resp = self .middleware .process_response (request , {})
213
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
229
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
230
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
214
231
215
232
# This also doesn't work on registered domains.
216
233
request = self .factory .get (
@@ -219,7 +236,8 @@ def test_apiv2_endpoint_not_allowed(self):
219
236
HTTP_ORIGIN = 'http://my.valid.domain' ,
220
237
)
221
238
resp = self .middleware .process_response (request , {})
222
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
239
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
240
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
223
241
224
242
# Or from our public domain.
225
243
request = self .factory .get (
@@ -228,7 +246,8 @@ def test_apiv2_endpoint_not_allowed(self):
228
246
HTTP_ORIGIN = 'http://docs.readthedocs.io/' ,
229
247
)
230
248
resp = self .middleware .process_response (request , {})
231
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
249
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
250
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
232
251
233
252
# POST is not allowed
234
253
request = self .factory .post (
@@ -237,7 +256,8 @@ def test_apiv2_endpoint_not_allowed(self):
237
256
HTTP_ORIGIN = 'http://my.valid.domain' ,
238
257
)
239
258
resp = self .middleware .process_response (request , {})
240
- self .assertNotIn ('Access-Control-Allow-Origin' , resp )
259
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_ORIGIN , resp )
260
+ self .assertNotIn (ACCESS_CONTROL_ALLOW_CREDENTIALS , resp )
241
261
242
262
243
263
class TestSessionMiddleware (TestCase ):
0 commit comments