def test_middleware_success(self): m = OAuth2TokenMiddleware() auth_headers = { 'HTTP_AUTHORIZATION': 'Bearer ' + 'tokstr', } request = self.factory.get("/a-resource", **auth_headers) m.process_request(request) self.assertEqual(request.user, self.user)
def test_middleware_success(self): m = OAuth2TokenMiddleware(self.dummy_get_response) auth_headers = { "HTTP_AUTHORIZATION": "Bearer " + "tokstr", } request = self.factory.get("/a-resource", **auth_headers) m.process_request(request) self.assertEqual(request.user, self.user)
def test_middleware_response(self): m = OAuth2TokenMiddleware(self.dummy_get_response) auth_headers = { "HTTP_AUTHORIZATION": "Bearer " + "tokstr", } request = self.factory.get("/a-resource", **auth_headers) response = m(request) self.assertIsInstance(response, HttpResponse)
def test_middleware_response(self): m = OAuth2TokenMiddleware() auth_headers = { 'HTTP_AUTHORIZATION': 'Bearer ' + 'tokstr', } request = self.factory.get("/a-resource", **auth_headers) response = HttpResponse() processed = m.process_response(request, response) self.assertIs(response, processed)
def test_middleware_response_header(self): m = OAuth2TokenMiddleware(self.dummy_get_response) auth_headers = { "HTTP_AUTHORIZATION": "Bearer " + "tokstr", } request = self.factory.get("/a-resource", **auth_headers) response = m(request) self.assertIn("Vary", response) self.assertIn("Authorization", response["Vary"])
def test_middleware_wrong_headers(self): m = OAuth2TokenMiddleware() request = self.factory.get("/a-resource") self.assertIsNone(m.process_request(request)) auth_headers = { "HTTP_AUTHORIZATION": "Beerer " + "badstring", # a Beer token for you! } request = self.factory.get("/a-resource", **auth_headers) self.assertIsNone(m.process_request(request))
def test_middleware_response_header(self): m = OAuth2TokenMiddleware() auth_headers = { 'HTTP_AUTHORIZATION': 'Bearer ' + 'tokstr', } request = self.factory.get("/a-resource", **auth_headers) response = HttpResponse() m.process_response(request, response) self.assertIn('Vary', response) self.assertIn('Authorization', response['Vary'])
def test_middleware_user_is_set(self): m = OAuth2TokenMiddleware() auth_headers = { 'HTTP_AUTHORIZATION': 'Bearer ' + 'tokstr', } request = self.factory.get("/a-resource", **auth_headers) request.user = self.user self.assertIsNone(m.process_request(request)) request.user = self.anon_user self.assertIsNone(m.process_request(request))
def test_middleware_user_is_set(self): m = OAuth2TokenMiddleware(self.dummy_get_response) auth_headers = { "HTTP_AUTHORIZATION": "Bearer " + "tokstr", } request = self.factory.get("/a-resource", **auth_headers) request.user = self.user self.assertIsNone(m.process_request(request)) request.user = self.anon_user self.assertIsNone(m.process_request(request))
def test_middleware_wrong_headers(self): m = OAuth2TokenMiddleware(self.dummy_get_response) request = self.factory.get("/a-resource") m(request) self.assertFalse(hasattr(request, "user")) auth_headers = { "HTTP_AUTHORIZATION": "Beerer " + "badstring", # a Beer token for you! } request = self.factory.get("/a-resource", **auth_headers) m(request) self.assertFalse(hasattr(request, "user"))