Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix list of allowed verbs for failed (unauthorized) preflight #4

Merged
merged 3 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions more/cors/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import morepath
from morepath.publish import resolve_model, get_view_name
from webob.exc import HTTPUnauthorized
import dectate
import reg
from . import action
Expand Down Expand Up @@ -27,6 +28,8 @@ def get_cors_allowed_origin(self, model, request, requested_origin):
lambda self, model, request,
requested_method: request.view_name))
def get_cors_allowed_methods(self, model, request, requested_method):
if model is None:
return self.settings.cors.allowed_verbs
res = []
for m in self.settings.cors.allowed_verbs:
f = self.get_view.by_predicates(
Expand Down Expand Up @@ -92,6 +95,8 @@ def cors_handler(request):

try:
context = resolve_model(request) or app
except HTTPUnauthorized:
context = None
except Exception:
context = app

Expand Down
20 changes: 20 additions & 0 deletions more/cors/tests/test_cors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from webtest import TestApp as Client
from webob.exc import HTTPUnauthorized
import morepath
from more.cors import CORSApp

Expand All @@ -15,6 +16,10 @@ class FailedObject(object):
pass


class UnauthorizedObject(object):
pass


class Root(object):
pass

Expand All @@ -34,6 +39,11 @@ def get_failed_object(request):
raise Exception()


@App.path(path='unauthorized', model=UnauthorizedObject)
def get_unauthorized_object(request):
raise HTTPUnauthorized


@App.json(model=Root)
def view(context, request):
return {'view': 'index'}
Expand Down Expand Up @@ -127,6 +137,16 @@ def test_cors_non_preflight():
assert r.headers.get('Access-Control-Max-Age') is None


def test_cors_unauthorized_preflight():
c = get_client(App)

r = c.options('/unauthorized')

assert r.headers.get(
'Access-Control-Allow-Methods').split(',') \
== ['OPTIONS'] + c.app.settings.cors.allowed_verbs


def test_cors_no_allowed_verbs():
@App.setting(section='cors', name='allowed_verbs')
def get_allowed_verbs():
Expand Down