def test_session_modification(self): with init_cookie_session(http.Header(""), self.response_headers) as session: session["user"] = 1 modified_session = self.response_headers["Set-Cookie"].replace( "a", "e") cookie = http.Header(modified_session) with init_cookie_session(cookie, self.response_headers) as session: self.assertEqual(session.data, {})
def test_set_session(self): with init_cookie_session(http.Header(""), self.response_headers) as session: session["user"] = 1 session["is_active"] = True cookie = http.Header(self.response_headers["Set-Cookie"]) with init_cookie_session(cookie, self.response_headers) as session: self.assertEqual(session["user"], 1) self.assertEqual(session.get("is_active"), True) cookie = http.Header(self.response_headers["Set-Cookie"]) with init_cookie_session(cookie, self.response_headers) as session: self.assertEqual(session.data, {"user": 1, "is_active": True})
def resolve(self, parameter: Parameter, headers: http.Headers) -> http.Header: name = parameter.name.replace('_', '-') assert name in headers or parameter.default != inspect._empty, \ f"Header: {name} not found!" return http.Header(headers.get(name, parameter.default))
def test_returns_django_auth_instance_if_django_authenticates_user(self, mocked_authenticate): mocked_authenticate.return_value = User() auth_header = http.Header('Basic dXNlcm5hbWU6cGFzc3dvcmQ=') auth_user = self.authenticator.authenticate(auth_header) assert isinstance(auth_user, authentication.DjangoAuth) mocked_authenticate.assert_called_once_with(username='******', password='******')
def test_returns_user_if_resolve_user(self): user = User(username='******') user.set_password('password') user.save() auth_header = http.Header('Basic dXNlcm5hbWU6cGFzc3dvcmQ=') auth_user = self.authenticator.resolve(auth_header) self.assertEqual(user, auth_user)
def test_token_logout_returns_200_when_deleting_token(self): self.user.auth_token = authentication.models.Token() self.user.auth_token.save() self.assertEqual(1, authentication.models.Token.objects.count()) response = authentication.views.token_logout(authorization=http.Header('Bearer {}'.format(self.user.auth_token.key))) self.assertEqual(0, authentication.models.Token.objects.count()) self.assertEqual(200, response.status) self.assertEqual({"message": "Logged out."}, response.content)
def test_unset_session(self): with init_cookie_session(http.Header(""), self.response_headers) as session: session["user"] = 1 session["is_active"] = True cookie = http.Header(self.response_headers["Set-Cookie"]) with init_cookie_session(cookie, self.response_headers) as session: del session["user"] self.assertTrue(session.get("is_active")) cookie = http.Header(self.response_headers["Set-Cookie"]) with init_cookie_session(cookie, self.response_headers) as session: self.assertTrue("user" not in session) self.assertTrue(session.get("is_active")) del session["is_active"] cookie = http.Header(self.response_headers["Set-Cookie"]) with init_cookie_session(cookie, self.response_headers) as session: self.assertEqual(session.data, {})
def test_returns_django_auth_instance_if_django_authenticates_user(self): user = User(username='******') user.save() user.auth_token = authentication.models.Token() user.auth_token.save() auth_header = http.Header('Bearer {}'.format(user.auth_token.key)) auth_user = self.authenticator.authenticate(auth_header) assert isinstance(auth_user, authentication.DjangoAuth) assert user == auth_user.user assert user.auth_token.key == auth_user.token
def test_returns_user_if_resolve_user(self): user = User(username='******') user.set_password('password') user.save() token = models.Token(user=user) token.save() auth_header = http.Header(f'Bearer {token.key}') auth_user = self.authenticator.resolve(auth_header) self.assertEqual(user, auth_user)
def test_new_session_is_empty(self): with init_cookie_session(http.Header(""), self.response_headers) as session: self.assertEqual(session.data, {})
def test_returns_none_if_django_does_not_authenticate_user(self): auth_header = http.Header('Bearer dXNlcm5hbWU6cGFzc3dvcmQ=') self.assertIsNone(self.authenticator.authenticate(auth_header))
def test_returns_none_if_scheme_is_not_bearer(self): auth_header = http.Header('not_bearer token') self.assertIsNone(self.authenticator.authenticate(auth_header))
def test_token_logout_returns_204_for_no_token_related(self): response = authentication.views.token_logout(authorization=http.Header('Bearer invalid_token')) self.assertIsNone(response)
def test_token_logout_returns_204_for_no_bearer_auth(self): response = authentication.views.token_logout(authorization=http.Header('not_bearer token')) self.assertIsNone(response)
def test_returns_none_if_scheme_is_not_basic(self): auth_header = http.Header('not_basic token') self.assertIsNone(self.authenticator.resolve(auth_header))
def resolve(self, parameter: Parameter, headers: http.Headers) -> http.Header: name = parameter.name.replace('_', '-') if name not in headers: return None return http.Header(headers[name])
def test_returns_none_if_django_does_not_authenticate_user(self, mocked_authenticate): mocked_authenticate.return_value = None auth_header = http.Header('Basic dXNlcm5hbWU6cGFzc3dvcmQ=') self.assertIsNone(self.authenticator.authenticate(auth_header)) mocked_authenticate.assert_called_once_with(username='******', password='******')
def resolve(self, parameter: Parameter, headers: http.Headers) -> typing.Optional[http.Header]: name = parameter.name.replace("_", "-") if name not in headers: return None return http.Header(headers[name])