Skip to content

Commit

Permalink
Merge pull request #140 from whyscream/permission-denied
Browse files Browse the repository at this point in the history
Changed exception for invalid session ids, add test for the correct response.
  • Loading branch information
Martijn Jacobs committed Nov 19, 2018
2 parents 3553d5a + cf590bb commit cfeb0c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion oscarapi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def process_request(self, request):
if parsed_session_uri is not None:
domain = get_domain(request)
if parsed_session_uri['realm'] != domain:
raise exceptions.NotAcceptable(
raise exceptions.PermissionDenied(
_('Can not accept cookie with realm %s on realm %s') % (
parsed_session_uri['realm'],
domain
Expand Down
18 changes: 17 additions & 1 deletion oscarapi/tests/testmiddleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.urls import reverse
from django.test import RequestFactory, TestCase

from oscarapi.middleware import ApiGatewayMiddleWare, parse_session_id
from oscarapi.middleware import ApiGatewayMiddleWare, HeaderSessionMiddleware, parse_session_id
from oscarapi.models import ApiKey


Expand Down Expand Up @@ -82,3 +82,19 @@ def test_parse_session_id(self):

dummy_request.META['HTTP_SESSION_ID'] = 'SID:ANON:987171879'
self.assertIsNone(parse_session_id(dummy_request))


class HeaderSessionMiddlewareTest(TestCase):
rf = RequestFactory()

def test_process_request(self):
basket_url = reverse('api-basket')

# invalid cookie realm
request = self.rf.get(basket_url, HTTP_SESSION_ID='SID:ANON:example.com:987171879')
response = HeaderSessionMiddleware().process_request(request)
self.assertEqual(response.status_code, 403)
self.assertEqual(
response.content,
b'{"reason": "Can not accept cookie with realm example.com on realm testserver"}'
)

0 comments on commit cfeb0c3

Please sign in to comment.