Skip to content

Commit

Permalink
Added ApiBasketMiddleWareTest class with test_correct_cookie_key test
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn Jacobs committed Nov 28, 2019
1 parent 63aa171 commit 658090e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions oscarapi/tests/unit/testmiddleware.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from mock import MagicMock

from django.contrib.auth.models import AnonymousUser
from django.contrib.sessions.middleware import SessionMiddleware
from django.core.exceptions import PermissionDenied
from django.urls import reverse
from django.test import RequestFactory, TestCase

from oscarapi.middleware import (
ApiBasketMiddleWare,
ApiGatewayMiddleWare,
HeaderSessionMiddleware,
parse_session_id,
Expand Down Expand Up @@ -110,3 +115,29 @@ def test_process_request(self):
response.content,
b'{"reason": "Can not accept cookie with realm example.com on realm testserver"}',
)


class ApiBasketMiddleWareTest(TestCase):
rf = RequestFactory()

def test_correct_cookie_key(self):
basket_url = reverse("api-basket")
mock_response = MagicMock()
mock_response.set_cookie = MagicMock()

request = self.rf.get(basket_url)
request.user = AnonymousUser()

# this is the easiest way to have a "real" session object in the request
SessionMiddleware().process_request(request)
request.cookies_to_delete = ["just-a-cookie", "another-cookies"]

# see https:/django-oscar/django-oscar-api/issues/205
middleware = ApiBasketMiddleWare(get_response=mock_response)
expected_cookie_key = middleware.get_cookie_key(request)

response = middleware(request)

self.assertEqual(
response.set_cookie.call_args_list[0][0][0], expected_cookie_key
)

0 comments on commit 658090e

Please sign in to comment.