def test_to_representation_2(self): data = {'url': '/api'} request = Request(**data) self.assertEqual( request.to_representation(), {'method': None, 'url': '/api'} )
def test_to_representation_4(self): data = {'url': '/api', 'method': 'GET'} request = Request(**data) self.assertEqual( request.to_representation(), {'method': 'GET', 'url': '/api', } )
def test_append_request(self): data = {'url': '/api', 'method': 'GET'} request = Request(**data) response = Response() response.append_request(request) self.assertEqual( response.content['request'], request.to_representation() )
def test_attribute_error_for_added_property(): request = Request() add_property(request, 'token', None) assert '_token' in request.__dict__.keys() with pytest.raises(AttributeError): request.token = "test_token" assert request.token is None
def test_attribute_error_for_added_property(): request = Request() add_property(request, "token", None) assert "_token" in request.__dict__.keys() with pytest.raises(AttributeError): request.token = "test_token" assert request.token is None
def test_authenticate_for_view_without_auth_required_pass(self): class TestView(MethodBasedView): pass request = Request() view = TestView() self.assertIsNone(self.middleware.authenticate(request, view))
def test_dispatch_failed_2(self): data = {'method': 'POST'} request = Request(**data) self.view.methods = [ 'GET', ] self.assertRaises(NotSpecifiedHandler, self.view.dispatch, request)
def test_raised_token_not_provided_exception_in_authenticate(self): class ViewWithTokenCheck(MethodBasedView): auth_required = True request = Request() view = ViewWithTokenCheck() self.assertRaises(TokenNotProvidedException, self.middleware.authenticate, request, view)
def test_dispatch_failed(self): data = { 'method': [ 'POST', ] } request = Request(**data) self.assertRaises(IncorrectMethodNameType, self.view.dispatch, request)
def test_search_handler_with_dynamic_endpoint_2(self): self.router.register('/api/{version}/', FakeView, 'GET') data = {'args': {'format': 'json'}} request = Request(**data) url = '/api/v2/' handler, args, kwargs = self.router.search_handler(request, url) self.assertIsInstance(handler, FakeView) self.assertEqual(args, ('v2', )) self.assertEqual(kwargs, {'format': 'json'})
def test_search_handler_with_dynamic_endpoint(self): self.router.register('/api/{version}/', FakeView, 'GET') data = {} request = Request(**data) url = '/api/v1/' handler, args, kwargs = self.router.search_handler(request, url) self.assertIsInstance(handler, FakeView) self.assertEqual(args, ('v1', )) self.assertEqual(kwargs, {})
def test_process_request(self, log_info): self.router.register('/api/get/', FakeGetView, 'GET') decoded_json = {'method': 'GET', 'url': '/api/get/'} request = Request(**decoded_json) response = self.router.process_request(request).decode('utf-8') json_response = json.loads(response) self.assertIn('data', json_response.keys()) self.assertEqual(json_response['data'], 'fake') self.assertIn('event_name', json_response) self.assertIsNone(json_response['event_name'])
def test_process_request_with_failed_middleware(self, log_info, log_exc): self.router._middlewares = [ FakeTokenMiddlewareWithExc(), ] self.router.register('/api/get/', FakeGetView, 'GET') decoded_json = {'method': 'GET', 'url': '/api/get/'} request = Request(**decoded_json) response = self.router.process_request(request).decode('utf-8') json_response = json.loads(response) self.assertIn('detail', json_response.keys()) self.assertNotIn('data', json_response.keys())
def test_process_request_without_url(self, log_info, log_exc): self.router.register('/api/get/', FakeGetView, 'GET') decoded_json = {'method': 'GET'} request = Request(**decoded_json) response = self.router.process_request(request).decode('utf-8') json_response = json.loads(response) self.assertIn('detail', json_response.keys()) self.assertEqual(json_response['detail'], "In query not specified `url` argument.") self.assertIn('event_name', json_response) self.assertIsNone(json_response['event_name'])
def test_process_request_by_invalid_url(self, log_info, log_exc): self.router.register('/api/get/', FakeGetView, 'GET') decoded_json = {'method': 'GET', 'url': '/api/invalid/'} request = Request(**decoded_json) response = self.router.process_request(request).decode('utf-8') json_response = json.loads(response) self.assertIn('detail', json_response.keys()) self.assertEqual(json_response['detail'], "For URL, typed in request, handler not specified.") self.assertIn('event_name', json_response) self.assertIsNone(json_response['event_name'])
def test_authenticate(self): class TestView(MethodBasedView): auth_required = True user = User(is_user=True) token_data = {'key': 'value'} raw_token = self.middleware.manager.generate(token_data) request = Request() add_property(request, 'user', user) add_property(request, 'token', raw_token) view = TestView() self.assertIsNone(self.middleware.authenticate(request, view))
def _decode_message(self, payload, isBinary=False): """ Decoding input message to Request object. :param payload: input message. :param isBinary: boolean value, means that received data had a binary format. """ # Message was taken in base64 if isBinary: payload = b64decode(payload) input_data = json.loads(payload.decode('utf-8')) return Request(**input_data)
def test_authenticate_with_permissions(self): class ViewWithIsAuthenticatedPermission(MethodBasedView): auth_required = True permission_classes = (IsAuthenticated, ) user = User(is_user=True) token_data = {'key': 'value'} raw_token = self.middleware.manager.generate(token_data) request = Request() add_property(request, 'user', user) add_property(request, 'token', raw_token) view = ViewWithIsAuthenticatedPermission() self.assertIsNone(self.middleware.authenticate(request, view))
def test_process_request_wrapped_function(self, log_info): @endpoint('/api', 'GET') def fake_handler(request, *args, **kwargs): return "fake" self.router.register_endpoint(fake_handler) decoded_json = {'url': '/api', 'method': 'GET'} request = Request(**decoded_json) response = self.router.process_request(request).decode('utf-8') json_response = json.loads(response) self.assertIn('data', json_response.keys()) self.assertEqual(json_response['data'], 'fake') self.assertIn('event_name', json_response) self.assertIsNone(json_response['event_name'])
def test_init_credentials(self): user = self._create_simple_user(self.middleware.user_model) token_data = {'key': 'value'} raw_token = self.middleware.manager.generate(token_data) init_data = {'name': 'api', 'token': raw_token, 'user_id': user.id} self.middleware.storage_backend.save(init_data['name'], init_data['token'], user_id=init_data['user_id']) request = Request() add_property(request, 'token', raw_token) self.middleware.init_credentials(request) self.assertIsInstance(request.user, User) self.assertTrue(request.user.is_user) self.assertIsNotNone(request.token_payload)
def test_process_request_with_defined_args_and_event_name(self, log_info): self.router.register('/api/get/', FakeGetView, 'GET') decoded_json = { 'method': 'GET', 'url': '/api/get/', 'args': { 'format': 'xml' }, 'event_name': 'test' } request = Request(**decoded_json) response = self.router.process_request(request).decode('utf-8') json_response = json.loads(response) self.assertIn('data', json_response.keys()) self.assertEqual(json_response['data'], 'fake') self.assertIn('event_name', json_response) self.assertEqual(json_response['event_name'], decoded_json['event_name'])
def test_authenticate_with_permissions_and_raise_exception(self): class CustomPermission(AbstractPermission): @staticmethod def check(request, handler): raise PermissionDeniedException class ViewWithPermissions(MethodBasedView): auth_required = True permission_classes = (CustomPermission, ) user = User(is_user=True) token_data = {'key': 'value'} raw_token = self.middleware.manager.generate(token_data) request = Request() add_property(request, 'user', user) add_property(request, 'token', raw_token) view = ViewWithPermissions() self.assertRaises(PermissionDeniedException, self.middleware.authenticate, request, view)
def test_to_representation_with_specified_method_argument(self): options = {'method': 'GET'} request = Request(**options) self.assertEqual(request.to_representation(), {'event_name': None})
def test_to_representation_with_specified_url_argument(self): options = {'url': '/api'} request = Request(**options) self.assertEqual(request.to_representation(), {'event_name': None})
def test_to_representation_with_specified_url_and_method_argument(self): options = {'url': '/api', 'method': 'GET', 'event_name': 'test-event'} request = Request(**options) self.assertEqual( request.to_representation(), {'event_name': options['event_name']} )
def test_dispatch(self): data = {} request = Request(**data) self.assertRaises(NotSpecifiedMethodName, self.view.dispatch, request)
def test_dispatch_2(self): data = {'method': 'GET'} request = Request(**data) self.assertEqual(self.view.dispatch(request), 'fake')
def test_get_argument(self): data = {'args': {'param': 'test'}} request = Request(**data) self.assertEqual(request.get_argument('param'), 'test')
def test_get_argument_with_unfilled_dict(self): data = {'args': {}} request = Request(**data) self.assertIsNone(request.get_argument('param'), None)
def test_to_representation_with_empty_dictionary(self): options = {} request = Request(**options) self.assertEqual(request.to_representation(), {'event_name': None})
def test_add_property(): request = Request() add_property(request, 'token', None) assert '_token' in request.__dict__.keys() assert request.token is None
def test_get_argument_with_unfilled_dict_2(self): options = {} request = Request(**options) self.assertIsNone(request.get_argument('param'))
def test_get_argument(self): options = {'args': {'param': 'test'}} request = Request(**options) self.assertEqual(request.get_argument('param'), 'test')
def test_init_credentials_without_token(self): request = Request() self.middleware.init_credentials(request) self.assertIsInstance(request.user, User) self.assertTrue(request.user.is_anonymous) self.assertIsNone(request.token_payload)
def test_init_with_patching(self): options = {'token': 'base64token'} request = Request(**options) self.assertEqual(request.token, 'base64token')
def test_process_request(self): request = Request() handler = FakeGetView() self.assertIsNone(self.middleware.process_request(request, handler))
def test_authenticate(self): request = Request() handler = FakeGetView() self.assertIsNone(self.middleware.authenticate(request, handler))
def test_init_credentials(self): request = Request() self.assertIsNone(self.middleware.init_credentials(request))