Skip to content
Snippets Groups Projects
Commit d8472146 authored by rha104's avatar rha104
Browse files

Updated tests

parent 7cc31562
No related branches found
No related tags found
1 merge request!4Added lots of tests. And fixed some issues with the client, discovered while testing
Pipeline #51073 passed
......@@ -21,7 +21,7 @@ def load_json_file(name):
@pytest.fixture
def baseurl():
return 'http://localhost'
return 'https://localhost'
@pytest.fixture
......
import pytest
import requests
import requests_mock
from requests import HTTPError
from setra_client.client import SetraEndpoints
from setra_client.client import SetraClient
from json.decoder import JSONDecodeError
@pytest.fixture
......@@ -45,58 +44,119 @@ def test_init_modify_defaults(client_cls, baseurl, header_name):
assert client.default_headers[header_name] != headers[header_name]
# def test_get_call(client, requests_mock):
# """Ensure getting update schema works"""
# requests_mock.get('https://localhost/_webservices/?ws=contacts/upsert/1.0',
# json={'foo': 'bar'})
# response = client.call(method_name='GET', url=)
# assert response == {'foo': 'bar'}
def test_get_batch_call(client, requests_mock, baseurl):
def test_get_successful_batch_with_json_content(client, requests_mock, baseurl):
"""A working GET call should return HTTP 200, with json content"""
requests_mock.get('http://localhost/api/batch/3',
json={'foo': 'bar'})
response = client.call(method_name='GET', url=SetraEndpoints(baseurl).batch(batch_id='3'))
url = SetraEndpoints(baseurl).batch(batch_id='3') # https://localhost/api/batch/3
requests_mock.get(url, json={'foo': 'bar'}, status_code=200)
response = client.call(method_name='GET', url=url)
assert response.status_code == 200
assert response.json() == {'foo': 'bar'}
def test_get_batch_call4(client, baseurl):
def test_get_successful_batch_with_text_content(client, requests_mock, baseurl):
"""A working GET call should return HTTP 200, with json content"""
session = requests.Session()
adapter = requests_mock.Adapter()
session.mount('http://', adapter)
adapter.register_uri('GET', 'http://test.com/1', json={'a': 'b'}, status_code=200)
resp = session.get('http://test.com/1')
assert resp.status_code == 200
assert resp.json() == {'a': 'b'}
url = SetraEndpoints(baseurl).batch(batch_id='3') # https://localhost/api/batch/3
requests_mock.get(url, text="some content", status_code=200)
response = client.call(method_name='GET', url=url)
assert response.status_code == 200
assert response.text == "some content"
def test_get_failing_batch_with_json_content(client, requests_mock, baseurl):
"""A failing GET call with 404 should pass through the same response from the client, and return HTTP 404,
and the request with json content"""
def test_get_batch_call5(client, baseurl):
"""A failing GET call with 404 should return HTTP 200, and the request with json content"""
session = requests.Session()
adapter = requests_mock.Adapter()
session.mount('http://', adapter)
adapter.register_uri('GET', 'http://localhost/api/batch', json={'a': 'b'}, status_code=404)
resp = client.call(method_name='GET', url='http://localhost/api/batch')
assert resp.status_code == 200
assert resp.text == "test"
# assert resp.json() == {'a': 'b'}
def test_get_batch_call6(client, baseurl):
"""A failing GET call with 401 should return custom HTTP status"""
session = requests.Session()
adapter = requests_mock.Adapter()
session.mount('http://', adapter)
adapter.register_uri('GET', 'http://test.com/1', text="", status_code=401)
resp = client.call(method_name='GET', url='http://test.com/1')
assert resp.status_code == 200
print("TExt: ------------")
print(resp.text)
print("JSON: ------------")
print(resp.json())
url = SetraEndpoints(baseurl).batch() # https://localhost/api/batch
requests_mock.get(url, json={'a': 'b'}, status_code=404)
resp = client.call(method_name='GET', url=url)
assert resp.status_code == 404
assert resp.json() == {'a': 'b'}
def test_get_failing_batch_with_text_content(client, requests_mock, baseurl):
"""A failing GET call with 404 should pass through the same response from the client, and return HTTP 404,
and the request with json content"""
url = SetraEndpoints(baseurl).batch() # https://localhost/api/batch
requests_mock.get(url, text="some content", status_code=404)
resp = client.call(method_name='GET', url=url)
assert resp.status_code == 404
assert resp.text == "some content"
def test_get_failing_batch_without_return_response_text(client, requests_mock, baseurl):
"""A failing GET call with 404 should raise HTTPError from the client,
and return HTTP 404, and the request with text content"""
url = SetraEndpoints(baseurl).batch() # https://localhost/api/batch
requests_mock.get(url, text="some content", status_code=404)
with pytest.raises(HTTPError) as err:
client.call(method_name='GET', url=url, return_response=False)
assert err.type == requests.exceptions.HTTPError
assert err.value.response.status_code == 404
assert err.value.response.text == "some content"
def test_get_failing_batch_without_return_response_json(client, requests_mock, baseurl):
"""A failing GET call with 404 should raise HTTPError from the client,
and return HTTP 404, and the request with json content"""
url = SetraEndpoints(baseurl).batch() # https://localhost/api/batch
requests_mock.get(url, json={'a': 'b'}, status_code=404)
with pytest.raises(HTTPError) as err:
client.call(method_name='GET', url=url, return_response=False)
assert err.type == requests.exceptions.HTTPError
assert err.value.response.status_code == 404
assert err.value.response.json() == {'a': 'b'}
def test_get_failing_batch_without_return_response2(client, requests_mock, baseurl):
"""A failing GET call with 404 returning text content, with return_response=False
should raise for HTTPError from the client
(because we expect json from setra) """
url = SetraEndpoints(baseurl).batch() # https://localhost/api/batch
requests_mock.get(url, text="some content", status_code=200)
with pytest.raises(JSONDecodeError) as err:
client.call(method_name='GET', url=url, return_response=False)
assert err.type == JSONDecodeError
def test_get_failing_batch_with_http500(client, requests_mock, baseurl):
"""A failing POST call with return_response=False and http 500,
should raise HTTPError from the client,
and return HTTP 500, and the request with json content"""
url = SetraEndpoints(baseurl).batch() # https://localhost/api/batch
requests_mock.post(url, json={'a': 'b'}, status_code=500)
with pytest.raises(HTTPError) as err:
tt = client.post(url=url, return_response=False)
assert err.type == requests.exceptions.HTTPError
assert err.value.response.status_code == 500
assert err.value.response.json() == {'a': 'b'}
def test_get_failing_batch_with_http5002(client, requests_mock, baseurl):
"""A failing POST call with 500 with return_response=True,
should just return the response, with 500"""
url = SetraEndpoints(baseurl).batch() # https://localhost/api/batch
requests_mock.post(url, json={'a': 'b'}, status_code=500)
response = client.post(url=url)
assert response.status_code == 500
assert response.json() == {'a': 'b'}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment