From d97431ccf41c106a3c5be257e8a843c9d38ef0ca Mon Sep 17 00:00:00 2001
From: Jo Sama <jo.sama@usit.uio.no>
Date: Mon, 16 Aug 2021 09:21:27 +0200
Subject: [PATCH] Redefine the interface for post_new_batch()

This interface avoids leaking transport-specific information, while
returning an indication of how the request fared so the user can decide
on how to proceed with processing.
---
 setra_client/client.py | 12 ++++++++----
 tests/test_client.py   | 37 ++++++++++++++++++++++++++++---------
 2 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/setra_client/client.py b/setra_client/client.py
index 088300c..ad39b64 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 f33135b..45d4355 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(
-- 
GitLab