def test_argument_check(self, mock_rest_response): view = StufRestView() view._validate_request_args = MagicMock( return_value={'error': 'any error'}) self.assertEqual(view.get(), mock_rest_response.bad_request.return_value) mock_rest_response.bad_request.assert_called_with(error='any error')
def test_make_request(self, mock_post): stufreq = MagicMock() stufreq.soap_action = 'THE SOAP action' stufreq.to_string = lambda: 'string repr' view = StufRestView() self.assertEqual(mock_post.return_value, view._make_request(stufreq)) mock_post.assert_called_with('scheme://netloc/route/path', data='string repr', headers={ 'Soapaction': 'THE SOAP action', 'Content-Type': 'text/xml', })
def test_error_response(self, mock_rest_response, mock_logging): mock_request = MagicMock() with patch("gobstuf.rest.brp.base_view.request", mock_request): mock_request.url = 'REQUEST_URL' view = StufRestView() response_arg = MagicMock() response_arg.get_http_response.return_value = MagicMock() # Normal case self.assertEqual(response_arg.get_http_response.return_value, view._error_response(response_arg)) # UnknownErrorCode response_arg.get_http_response.side_effect = UnknownErrorCode self.assertEqual(mock_rest_response.bad_request(), view._error_response(response_arg)) self.assertEqual(2, mock_logging.error.call_count)
def test_get_functional_query_parameters(self): mock_request = MagicMock() with patch("gobstuf.rest.brp.base_view.request", mock_request): mock_request.args = { 'a': 1, 'b': '', 'c': None, 'd': False, } view = StufRestView() view._transform_query_parameter_value = lambda x: x view.functional_query_parameters = { 'a': 15, 'b': 16, 'c': 17, 'd': 18, 'e': 19 } self.assertEqual({ **mock_request.args, 'e': 19, }, view._get_functional_query_parameters())
def test_validate(self): mock_request = MagicMock() with patch("gobstuf.rest.brp.base_view.request", mock_request): view = StufRestView() view._validate_request_args = MagicMock(return_value=None) view.expand_options = ['a', 'b'] self.assertIsNone(getattr(view, '_validate_called', None)) valid_options = ['a', 'b', 'a,b'] invalid_options = ['1', 'c'] for expand in valid_options: mock_request.args = {'expand': expand} self.assertEqual({}, view._validate()) error = { 'invalid-params': 'expand', 'title': 'De waarde van expand wordt niet geaccepteerd', 'detail': f'De mogelijke waarden zijn: a,b' } for expand in invalid_options: mock_request.args = {'expand': expand} self.assertEqual(error, view._validate())
def test_get_internal_server_error(self, mock_rest_response): view = StufRestView() view._get = MagicMock() view._validate = MagicMock(return_value={}) view._get_functional_query_parameters = MagicMock(return_value={}) view._validate_called = True view._validate_request_args = MagicMock(return_value=None) # Regular response result = view.get(any='thing') self.assertEqual(result, view._get.return_value) # Request failed for an unknown reason view._get.side_effect = Exception result = view.get(any='thing') self.assertEqual(result, mock_rest_response.internal_server_error.return_value) view._validate.side_effect = StufRestFilterView.InvalidQueryParametersException( {'any': 'error'}) view.get(any='thing') mock_rest_response.bad_request.assert_called_with(any='error')
def test_validate_call_request_args(self): view = StufRestView() view._validate_request_args = MagicMock(return_value={'the': 'errors'}) self.assertEqual({'the': 'errors'}, view._validate(kw='arg')) view._validate_request_args.assert_called_with(kw='arg')