diff --git a/setra_client/client.py b/setra_client/client.py index 088300cceb92c7538b03c73483a2d17cabf4715f..ad39b64c9dce0e9a957daf7a4d3acfdc369c4ec8 100644 --- a/setra_client/client.py +++ b/setra_client/client.py @@ -288,13 +288,17 @@ class SetraClient(object): data=batchdata.json(), headers=headers, return_response=True) + try: + content = response.json() + except requests.exceptions.JSONDecodeError: + content = response.content + if response.status_code == 202: - return response.content, 'Accepted' + return 'Accepted', {} elif response.status_code == 409: - return response.content, "Conflict" + return 'Conflict', content else: - response.raise_for_status() - return response + return 'Unknown', {'code': response.status_code, 'content': content} def put_update_batch(self, batchdata: InputBatch): """ diff --git a/tests/test_client.py b/tests/test_client.py index f33135b710ff219765230145b5e752bbcd1bca99..45d4355cb891ebf6463d14f0da41d9d38ec2bdc1 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -289,25 +289,44 @@ def test_failing_to_get_all_transactions(client, requests_mock, baseurl): # Test post_new_batch method def test_successfully_post_batch_with_voucher(client, batch_with_voucher_fixture, requests_mock, baseurl): - """A working GET call should return HTTP 200, with json content""" + """A working GET call should return HTTP 202, with json content""" url = SetraEndpoints(baseurl).post_new_batch() batch = InputBatch.from_dict(batch_with_voucher_fixture) - requests_mock.post(url, json={'somestatus': 'ok'}, status_code=200, request_headers={"Content-Type": "application/json"}) + requests_mock.post(url, json={}, status_code=202, request_headers={"Content-Type": "application/json"}) - response = client.post_new_batch(batch) # we get a response object back - assert response.json() == {'somestatus': 'ok'} + state, data = client.post_new_batch(batch) # we get a response object back + assert state == 'Accepted' + assert data == {} def test_successfully_post_batch_with_voucher_and_response(client, batch_with_voucher_fixture, requests_mock, baseurl): """A working POST new batch call with return_response=True, - should return the response with HTTP 200, with json content""" + should return the response with HTTP 202, with json content""" url = SetraEndpoints(baseurl).post_new_batch() - requests_mock.post(url, json={'somestatus': 'ok'}, status_code=200, request_headers={"Content-Type": "application/json"}) #expect json content + requests_mock.post(url, json={}, status_code=202, request_headers={"Content-Type": "application/json"}) #expect json content batch = InputBatch.from_dict(batch_with_voucher_fixture) - response = client.post_new_batch(batch) # we get a response object back - assert response.json() == {'somestatus': 'ok'} - assert response.status_code == 200 + state, data = client.post_new_batch(batch) # we get a response object back + assert state == 'Accepted' + assert data == {} + + +def test_conflicting_post_new_batch(client, batch_with_voucher_fixture, requests_mock, baseurl): + url = SetraEndpoints(baseurl).post_new_batch() + requests_mock.post(url, json={'error': 'batch is being processed'}, status_code=409, request_headers={"Content-Type": "application/json"}) #expect json content + batch = InputBatch.from_dict(batch_with_voucher_fixture) + state, data = client.post_new_batch(batch) + assert state == 'Conflict' + assert data == {'error': 'batch is being processed'} + + +def test_unknown_post_new_batch_state(client, batch_with_voucher_fixture, requests_mock, baseurl): + url = SetraEndpoints(baseurl).post_new_batch() + requests_mock.post(url, json={'error': 'Batch is malformed, no id'}, status_code=500, request_headers={"Content-Type": "application/json"}) #expect json content + batch = InputBatch.from_dict(batch_with_voucher_fixture) + state, data = client.post_new_batch(batch) + assert state == 'Unknown' + assert data == {'code': 500, 'content': {'error': 'Batch is malformed, no id'}} def test_successfully_getting_batch_complete(