Example #1
0
def test_process_request_assert_settings(middleware_patch, settings, rf,
                                         setting_name):
    delattr(settings, setting_name)

    request = rf.get('/')
    middleware = TokenCookieMiddleware()
    with middleware_patch('get_cookie_user',
                          side_effect=Exception("This should not be called")):
        with pytest.raises(AssertionError, match=setting_name):
            middleware.process_request(request)
Example #2
0
def test_process_request_get_user_called(middleware_patch, rf):
    request = rf.get('/')
    middleware = TokenCookieMiddleware()
    user = AnonymousUser()
    with middleware_patch('get_cookie_user', return_value=user) as mock_fn:
        middleware.process_request(request)
        assert mock_fn.call_count == 0, (
            "request.cookie_user is lazy, and it should not be called.")
        request.cookie_user.is_active
        assert mock_fn.mock_calls == [mock.call(request)]
        assert request.cookie_user == user
Example #3
0
def test_middleware_doesnt_update_cookie_for_read_only_methods(
        method, middleware_patch, user, settings, rf):
    middleware = TokenCookieMiddleware()
    rf.cookies = SimpleCookie({
        settings.JWT_TOKEN_COOKIE_NAME:
        GreenbudgetSlidingToken.for_user(user),
    })
    request = getattr(rf, method)('/')
    response = HttpResponse()
    with mock.patch.object(response, 'set_cookie') as set_cookie:
        with middleware_patch('get_cookie_user', return_value=user):
            middleware.process_request(request)
            response = middleware.process_response(request, response)
            assert set_cookie.called is False
Example #4
0
def test_middleware_updates_cookie_at_refresh_endpoint(middleware_patch, user,
                                                       settings, rf):
    middleware = TokenCookieMiddleware()
    rf.cookies = SimpleCookie({
        settings.JWT_TOKEN_COOKIE_NAME:
        GreenbudgetSlidingToken.for_user(user),
    })
    request = rf.get(reverse('jwt:refresh'))
    response = HttpResponse()
    with mock.patch.object(response, 'set_cookie') as set_cookie:
        with middleware_patch('get_cookie_user', return_value=user):
            middleware.process_request(request)
            response = middleware.process_response(request, response)

            assert set_cookie.called is True
Example #5
0
def test_middleware_invalid_token_deletes_cookies(settings, rf):
    middleware = TokenCookieMiddleware()

    rf.cookies = SimpleCookie({
        settings.JWT_TOKEN_COOKIE_NAME: 'invalid_token',
    })
    request = rf.get('/')

    middleware.process_request(request)

    response = HttpResponse()
    response.set_cookie(settings.JWT_TOKEN_COOKIE_NAME, 'invalid_token')

    new_response = middleware.process_response(request, response)
    assert new_response.cookies[settings.JWT_TOKEN_COOKIE_NAME].value == ''
Example #6
0
def test_middleware_expired_token_deletes_cookies(settings, rf):
    token = GreenbudgetSlidingToken()
    token.set_exp(claim='refresh_exp', from_time=datetime(2010, 1, 1))

    middleware = TokenCookieMiddleware()

    rf.cookies = SimpleCookie({
        settings.JWT_TOKEN_COOKIE_NAME: str(token),
    })
    request = rf.get('/')

    middleware.process_request(request)

    response = HttpResponse()
    response.set_cookie(settings.JWT_TOKEN_COOKIE_NAME, str(token))

    new_response = middleware.process_response(request, response)
    assert new_response.cookies[settings.JWT_TOKEN_COOKIE_NAME].value == ''
Example #7
0
def test_process_response_sets_cookies(settings, rf, user):
    token = GreenbudgetSlidingToken.for_user(user)

    expire_date = datetime.now() + \
        settings.SIMPLE_JWT['SLIDING_TOKEN_REFRESH_LIFETIME']
    http_expire_date = http_date(expire_date.timestamp())

    request = rf.get('/')
    request.cookie_user = user
    response = HttpResponse()
    middleware = TokenCookieMiddleware()

    with mock.patch.object(GreenbudgetSlidingToken,
                           'for_user',
                           return_value=token) as mock_for_user:
        response = middleware.process_response(request, response)
        jwt_cookie = response.cookies[settings.JWT_TOKEN_COOKIE_NAME]

        assert jwt_cookie.value == str(token)
        assert jwt_cookie['expires'] == http_expire_date

        assert mock_for_user.mock_calls == [mock.call(user)]
Example #8
0
def test_middleware_doesnt_set_cookie_for_get(settings, rf):
    middleware = TokenCookieMiddleware()
    request = rf.get("/")
    middleware.process_request(request)
    response = middleware.process_response(request, HttpResponse())
    assert response.cookies.get(settings.JWT_TOKEN_COOKIE_NAME) is None