def test_post_202(self): mock_session = patch(self.TARGET).start() response_mock = MagicMock(return_value={'message': 'message'}) request_mock = mock_session.return_value.request request_mock.return_value = MagicMock(json=response_mock, status_code=202) data = {'key': 'value'} base = Base(Mock(api_url='http://localhost', token='token123'), 'driver_test', retries=0) base.make_request('POST', 'path', data) headers = { 'Content-Type': 'application/json', 'x-driver-name': 'driver_test', 'Authorization': 'Token token=token123' } request_mock.assert_called_once_with('POST', 'http://localhost/v2/path', data=json.dumps(data), headers=headers, verify=True)
def test_get_exception(self): mock_requests = patch(TARGET).start() mock_requests.return_value.request.side_effect = Exception() base = Base(Mock(), 'driver_test') with self.assertRaises(exceptions.ApiError): base.make_request('GET', 'path', None)
def test_post_exception(self): mock_requests = patch(TARGET).start() mock_requests.return_value.request.side_effect = Exception() data = {'key': 'value'} base = Base(Mock(), 'driver_test') with self.assertRaises(exceptions.ApiError): base.make_request('POST', 'path', data)
def test_post_400(self): mock_requests = patch(self.TARGET).start() response_mock = MagicMock(return_value={'message': 'Error'}) mock_requests.return_value.request.return_value = MagicMock( json=response_mock, status_code=400) with self.assertRaises(exceptions.ValidationError): base = Base(Mock(), 'driver_test', retries=0) data = {'key': 'value'} base.make_request('POST', 'path', data)
def test_get_404(self): mock_requests = patch(self.TARGET).start() response_mock = MagicMock(return_value={'message': 'Error'}) mock_requests.return_value.request.return_value = MagicMock( json=response_mock, status_code=404) base = Base(Mock(), 'driver_test', retries=0) with self.assertRaises(exceptions.NotFound): base.make_request('GET', 'path', None)
def test_get_401(self): mock_requests = patch(self.TARGET).start() mock_time = patch('globomap_loader_api_client.base.time').start() response_mock = MagicMock(return_value={'message': 'Error'}) mock_requests.return_value.request.return_value = MagicMock( json=response_mock, status_code=401) auth = Mock() auth.generate_token.return_value = Mock() base = Base(auth, 'driver_test', 3) mock_time.sleep.return_value = 1 with self.assertRaises(exceptions.Unauthorized): base.make_request('GET', 'path', None) mock_time.sleep.assert_any_call(5 + 0) mock_time.sleep.assert_any_call(5 + 5) mock_time.sleep.assert_any_call(5 + 10)
'x-driver-name': 'driver_test', 'Authorization': 'Token token=token123' } request_mock.assert_called_once_with( 'POST', 'http://localhost/v2/path', data=json.dumps(data), headers=headers ) def test_post_400(self): mock_requests = patch(TARGET).start() response_mock = MagicMock(return_value={'message': 'Error'}) mock_requests.return_value.request.return_value = MagicMock( json=response_mock, status_code=400) with self.assertRaises(exceptions.ValidationError): base = Base(Mock(), 'driver_test') data = {'key': 'value'} base.make_request('POST', 'path', data) def test_post_401(self): mock_requests = patch(TARGET).start() mock_time = patch( 'globomap_loader_api_client.base.time').start() response_mock = MagicMock(return_value={'message': 'Error'}) mock_requests.return_value.request.return_value = MagicMock( json=response_mock, status_code=401) data = {'key': 'value'} auth = Mock() auth.generate_token.return_value = Mock()