|
6 | 6 | @override_settings( |
7 | 7 | CORS_ALLOWED_ORIGINS=["http://localhost:3000"], |
8 | 8 | CORS_ALLOWED_ORIGIN_REGEXES=[], |
9 | | - EXTERNAL_CORS_ALLOW_ALL_ORIGINS=True, |
| 9 | + CORS_ALLOW_ALL_ORIGINS=True, |
| 10 | + CORS_ALLOW_CREDENTIALS=True, |
| 11 | + CORS_EXPOSE_HEADERS=["Test-Expose"], |
10 | 12 | ) |
11 | 13 | class MiddlewareTest(TestCase): |
| 14 | + options_cors_headers = ( |
| 15 | + "Access-Control-Allow-Headers", |
| 16 | + "Access-Control-Allow-Methods", |
| 17 | + "Access-Control-Max-Age", |
| 18 | + ) |
| 19 | + |
12 | 20 | def setUp(self): |
13 | 21 | self.client = Client() |
14 | 22 |
|
15 | | - def test_whitelisted_origin(self): |
| 23 | + def test_get_whitelisted_origin(self): |
16 | 24 | res = self.client.get("/health", headers={"Origin": "http://localhost:3000"}) |
17 | 25 |
|
18 | 26 | assert res.headers["Access-Control-Allow-Origin"] == "http://localhost:3000" |
19 | 27 | assert res.headers["Access-Control-Allow-Credentials"] == "true" |
| 28 | + assert res.headers["Access-Control-Expose-Headers"] == "Test-Expose" |
| 29 | + |
| 30 | + for header in self.options_cors_headers: |
| 31 | + assert header not in res.headers |
| 32 | + |
| 33 | + def test_options_whitelisted_origin(self): |
| 34 | + res = self.client.options( |
| 35 | + "/health", headers={"Origin": "http://localhost:3000"} |
| 36 | + ) |
| 37 | + |
| 38 | + assert res.headers["Access-Control-Allow-Origin"] == "http://localhost:3000" |
| 39 | + assert res.headers["Access-Control-Allow-Credentials"] == "true" |
| 40 | + assert res.headers["Access-Control-Expose-Headers"] == "Test-Expose" |
20 | 41 |
|
21 | | - def test_non_whitelisted_origin(self): |
| 42 | + for header in self.options_cors_headers: |
| 43 | + assert header in res.headers |
| 44 | + |
| 45 | + def test_get_non_whitelisted_origin(self): |
22 | 46 | res = self.client.get("/health", headers={"Origin": "http://example.com"}) |
23 | 47 |
|
24 | 48 | assert res.headers["Access-Control-Allow-Origin"] == "*" |
25 | 49 | assert "Access-Control-Allow-Credentials" not in res.headers |
| 50 | + assert res.headers["Access-Control-Expose-Headers"] == "Test-Expose" |
| 51 | + |
| 52 | + for header in self.options_cors_headers: |
| 53 | + assert header not in res.headers |
| 54 | + |
| 55 | + def test_options_non_whitelisted_origin(self): |
| 56 | + res = self.client.options("/health", headers={"Origin": "http://example.com"}) |
| 57 | + |
| 58 | + assert res.headers["Access-Control-Allow-Origin"] == "*" |
| 59 | + assert "Access-Control-Allow-Credentials" not in res.headers |
| 60 | + assert res.headers["Access-Control-Expose-Headers"] == "Test-Expose" |
26 | 61 |
|
27 | | - @override_settings(EXTERNAL_CORS_ALLOW_ALL_ORIGINS=False) |
28 | | - def test_external_cors_allow_all_origins_false(self): |
| 62 | + for header in self.options_cors_headers: |
| 63 | + assert header in res.headers |
| 64 | + |
| 65 | + @override_settings(CORS_ALLOW_ALL_ORIGINS=False) |
| 66 | + def test_options_cors_allow_all_origins_false(self): |
| 67 | + res = self.client.options("/health", headers={"Origin": "http://example.com"}) |
| 68 | + |
| 69 | + assert "Access-Control-Allow-Origin" not in res.headers |
| 70 | + assert "Access-Control-Allow-Credentials" not in res.headers |
| 71 | + assert "Access-Control-Expose-Headers" not in res.headers |
| 72 | + |
| 73 | + for header in self.options_cors_headers: |
| 74 | + assert header not in res.headers |
| 75 | + |
| 76 | + @override_settings(CORS_ALLOW_ALL_ORIGINS=False, CORS_ALLOW_CREDENTIALS=False) |
| 77 | + def test_get_cors_allow_all_origins_credentials_false(self): |
29 | 78 | res = self.client.get("/health", headers={"Origin": "http://example.com"}) |
30 | 79 |
|
31 | 80 | assert "Access-Control-Allow-Origin" not in res.headers |
32 | 81 | assert "Access-Control-Allow-Credentials" not in res.headers |
| 82 | + assert "Access-Control-Expose-Headers" not in res.headers |
| 83 | + |
| 84 | + for header in self.options_cors_headers: |
| 85 | + assert header not in res.headers |
| 86 | + |
| 87 | + @override_settings(CORS_ALLOW_CREDENTIALS=False) |
| 88 | + def test_options_cors_allow_credentials_false(self): |
| 89 | + res = self.client.options( |
| 90 | + "/health", headers={"Origin": "http://localhost:3000"} |
| 91 | + ) |
| 92 | + |
| 93 | + assert res.headers["Access-Control-Allow-Origin"] == "*" |
| 94 | + assert "Access-Control-Allow-Credentials" not in res.headers |
| 95 | + assert res.headers["Access-Control-Expose-Headers"] == "Test-Expose" |
| 96 | + |
| 97 | + for header in self.options_cors_headers: |
| 98 | + assert header in res.headers |
0 commit comments