Skip to content

Commit 7575d59

Browse files
committed
Fix CORS auth, additional tests
1 parent f7d188f commit 7575d59

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

gmn/src/d1_gmn/app/middleware/request_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __call__(self, request):
5454
hasattr(request, 'allowed_method_list')
5555
):
5656
d1_gmn.app.views.headers.add_cors_headers_to_response(
57-
response, request.allowed_method_list
57+
response, request
5858
)
5959

6060
return response

gmn/src/d1_gmn/app/middleware/view_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,6 @@ def pem_in_http_header_to_pem_in_string(self, header_str):
143143
def create_cors_options_response(self, request):
144144
response = django.http.HttpResponse(b'Header response to OPTIONS request')
145145
d1_gmn.app.views.headers.add_cors_headers_to_response(
146-
response, request.allowed_method_list
146+
response, request
147147
)
148148
return response

gmn/src/d1_gmn/app/views/headers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ def add_http_date_header_to_response(response, date_time=None):
7878
)
7979

8080

81-
def add_cors_headers_to_response(response, method_list):
81+
def add_cors_headers_to_response(response, request):
8282
"""Add Cross-Origin Resource Sharing (CORS) headers to response
8383
- {method_list} is a list of HTTP methods that are allowed for the endpoint
8484
that was called. It should not include "OPTIONS", which is included
8585
automatically since it's allowed for all endpoints.
8686
"""
87-
opt_method_list = ','.join(method_list + ['OPTIONS'])
87+
opt_method_list = ','.join(request.allowed_method_list + ['OPTIONS'])
8888
response['Allow'] = opt_method_list
8989
response['Access-Control-Allow-Methods'] = opt_method_list
90-
response['Access-Control-Allow-Origin'] = '*'
90+
response['Access-Control-Allow-Origin'] = request.META.get('Origin', '*')
9191
response['Access-Control-Allow-Headers'] = 'Authorization'
9292
response['Access-Control-Allow-Credentials'] = 'true'

gmn/src/d1_gmn/tests/test_cors.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
1919
# limitations under the License.
2020
"""Test Cross-Origin Resource Sharing (CORS) Headers
2121
"""
22+
import d1_common
2223
import freezegun
2324
import responses
2425

2526
import d1_gmn.tests.gmn_mock
2627
import d1_gmn.tests.gmn_test_case
2728

2829
import d1_test.d1_test_case
30+
import d1_common.const
2931

3032

3133
@d1_test.d1_test_case.reproducible_random_decorator('TestCors')
32-
@freezegun.freeze_time('1961-01-02')
34+
@freezegun.freeze_time('1981-01-02')
3335
class TestCors(d1_gmn.tests.gmn_test_case.GMNTestCase):
3436
@responses.activate
3537
def test_1000(self, gmn_client_v1_v2):
@@ -81,6 +83,16 @@ def test_1050(self, gmn_client_v1_v2):
8183
response = gmn_client_v1_v2.OPTIONS(['object', pid])
8284
self.sample.assert_equals(response, 'get_options', gmn_client_v1_v2)
8385

86+
87+
@responses.activate
88+
def test_1051(self, gmn_client_v2):
89+
"""getPackage(): OPTIONS request returns expected headers"""
90+
pid_list = self.create_multiple_objects(gmn_client_v2, object_count=2)
91+
ore_pid = self.create_resource_map(gmn_client_v2, pid_list)
92+
response = gmn_client_v2.OPTIONS(['packages', d1_common.const.DEFAULT_DATA_PACKAGE_FORMAT_ID, ore_pid])
93+
self.sample.assert_equals(response, 'get_package_options', gmn_client_v2)
94+
95+
8496
@responses.activate
8597
def test_1060(self, gmn_client_v1_v2):
8698
"""Invalid method against endpoint raises 405 Method Not Allowed and returns
@@ -89,3 +101,26 @@ def test_1060(self, gmn_client_v1_v2):
89101
with d1_gmn.tests.gmn_mock.disable_auth():
90102
response = gmn_client_v1_v2.PUT(['object'])
91103
self.sample.assert_equals(response, 'put_object_list', gmn_client_v1_v2)
104+
105+
106+
@responses.activate
107+
def test_1061(self, gmn_client_v1_v2):
108+
"""get(): WITHOUT Origin header sets Access-Control-Allow-Origin to wildcard
109+
"""
110+
pid, sid, sciobj_bytes, sysmeta_pyxb = self.create_obj(gmn_client_v1_v2)
111+
with d1_gmn.tests.gmn_mock.disable_auth():
112+
response = gmn_client_v1_v2.get(pid)
113+
self.sample.assert_equals(response.headers, 'get_without_origin', gmn_client_v1_v2)
114+
assert response.headers['Access-Control-Allow-Origin'] == '*'
115+
116+
117+
@responses.activate
118+
def test_1062(self, gmn_client_v1_v2):
119+
"""get(): WITH Origin header sets Access-Control-Allow-Origin to the Origin
120+
"""
121+
pid, sid, sciobj_bytes, sysmeta_pyxb = self.create_obj(gmn_client_v1_v2)
122+
origin_url = 'https://somewhere.com'
123+
with d1_gmn.tests.gmn_mock.disable_auth():
124+
response = gmn_client_v1_v2.get(pid, vendorSpecific={'Origin': origin_url})
125+
self.sample.assert_equals(response.headers, 'get_with_origin', gmn_client_v1_v2)
126+
assert response.headers['Access-Control-Allow-Origin'] == origin_url

0 commit comments

Comments
 (0)