From a39e0f92a066e851820529600965440e56e33d9e Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Sat, 20 Jul 2024 17:35:15 +0100 Subject: [PATCH] added open api handler to client --- apiclient/client.py | 10 ++- tests/oauth_service/__init__.py | 0 tests/{ => oauth_service}/conftest.py | 0 .../test_datadelivery_acms_redaction_get.json | 0 ..._datadelivery_client_applications_get.json | 0 .../test_datadelivery_endpoint_get.json | 0 .../test_datadelivery_redaction_type_get.json | 0 .../test_datadelivery_role_get.json | 0 tests/{ => oauth_service}/helpers.py | 0 .../{ => oauth_service}/test_datadelivery.py | 6 +- tests/open_service/__init__.py | 0 tests/open_service/conftest.py | 29 ++++++++ .../expected_responses/test_get_creature.json | 67 +++++++++++++++++++ .../test_get_creatures.json | 62 +++++++++++++++++ tests/open_service/helpers.py | 15 +++++ tests/open_service/test_gammaworld.py | 19 ++++++ 16 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 tests/oauth_service/__init__.py rename tests/{ => oauth_service}/conftest.py (100%) rename tests/{ => oauth_service}/expected_responses/test_datadelivery_acms_redaction_get.json (100%) rename tests/{ => oauth_service}/expected_responses/test_datadelivery_client_applications_get.json (100%) rename tests/{ => oauth_service}/expected_responses/test_datadelivery_endpoint_get.json (100%) rename tests/{ => oauth_service}/expected_responses/test_datadelivery_redaction_type_get.json (100%) rename tests/{ => oauth_service}/expected_responses/test_datadelivery_role_get.json (100%) rename tests/{ => oauth_service}/helpers.py (100%) rename tests/{ => oauth_service}/test_datadelivery.py (85%) create mode 100644 tests/open_service/__init__.py create mode 100644 tests/open_service/conftest.py create mode 100644 tests/open_service/expected_responses/test_get_creature.json create mode 100644 tests/open_service/expected_responses/test_get_creatures.json create mode 100644 tests/open_service/helpers.py create mode 100644 tests/open_service/test_gammaworld.py diff --git a/apiclient/client.py b/apiclient/client.py index 4f37e2a..0234604 100644 --- a/apiclient/client.py +++ b/apiclient/client.py @@ -1,15 +1,21 @@ import sys + +import requests from requests import RequestException from requests_oauthlib import OAuth2Session -def api_client(call_dict, verify_cert=False): +def api_client(call_dict, verify_cert=False, oauth=False): url = call_dict["url"] headers = call_dict["headers"] body = call_dict["body"] - client = OAuth2Session(token=call_dict["token"]) + if oauth: + client = OAuth2Session(token=call_dict["token"]) + else: + client = requests.Session() + method = call_dict["method"] try: diff --git a/tests/oauth_service/__init__.py b/tests/oauth_service/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/oauth_service/conftest.py similarity index 100% rename from tests/conftest.py rename to tests/oauth_service/conftest.py diff --git a/tests/expected_responses/test_datadelivery_acms_redaction_get.json b/tests/oauth_service/expected_responses/test_datadelivery_acms_redaction_get.json similarity index 100% rename from tests/expected_responses/test_datadelivery_acms_redaction_get.json rename to tests/oauth_service/expected_responses/test_datadelivery_acms_redaction_get.json diff --git a/tests/expected_responses/test_datadelivery_client_applications_get.json b/tests/oauth_service/expected_responses/test_datadelivery_client_applications_get.json similarity index 100% rename from tests/expected_responses/test_datadelivery_client_applications_get.json rename to tests/oauth_service/expected_responses/test_datadelivery_client_applications_get.json diff --git a/tests/expected_responses/test_datadelivery_endpoint_get.json b/tests/oauth_service/expected_responses/test_datadelivery_endpoint_get.json similarity index 100% rename from tests/expected_responses/test_datadelivery_endpoint_get.json rename to tests/oauth_service/expected_responses/test_datadelivery_endpoint_get.json diff --git a/tests/expected_responses/test_datadelivery_redaction_type_get.json b/tests/oauth_service/expected_responses/test_datadelivery_redaction_type_get.json similarity index 100% rename from tests/expected_responses/test_datadelivery_redaction_type_get.json rename to tests/oauth_service/expected_responses/test_datadelivery_redaction_type_get.json diff --git a/tests/expected_responses/test_datadelivery_role_get.json b/tests/oauth_service/expected_responses/test_datadelivery_role_get.json similarity index 100% rename from tests/expected_responses/test_datadelivery_role_get.json rename to tests/oauth_service/expected_responses/test_datadelivery_role_get.json diff --git a/tests/helpers.py b/tests/oauth_service/helpers.py similarity index 100% rename from tests/helpers.py rename to tests/oauth_service/helpers.py diff --git a/tests/test_datadelivery.py b/tests/oauth_service/test_datadelivery.py similarity index 85% rename from tests/test_datadelivery.py rename to tests/oauth_service/test_datadelivery.py index 7a3e199..ddade1c 100644 --- a/tests/test_datadelivery.py +++ b/tests/oauth_service/test_datadelivery.py @@ -1,13 +1,13 @@ import json import pytest from apiclient.client import api_client -from tests.helpers import get_expected_response +from oauth_service.helpers import get_expected_response #### # NOTE: -# * api call fixtures can be found in tests/conftest.py -# * expected response fixtures can be found in tests/expected_responses +# * api call fixtures can be found in tests/oauth_service/conftest.py +# * expected response fixtures can be found in tests/oauth_service/expected_responses #### @pytest.mark.get diff --git a/tests/open_service/__init__.py b/tests/open_service/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/open_service/conftest.py b/tests/open_service/conftest.py new file mode 100644 index 0000000..0c893ea --- /dev/null +++ b/tests/open_service/conftest.py @@ -0,0 +1,29 @@ +import pytest +from apiclient.config import get_cfg + +ENV = 'gw' # This would be set in an actual OS env var on the execution platform +CFG = get_cfg(ENV) # needed for the token, and the full api url + +application_json = {'Content-Type': 'application/json'} + + +@pytest.fixture +def get_creatures_call(): + api_call = { + "method": "GET", + "url": CFG["api_url"] + '/rules/creature', + "headers": application_json, + "body": {} + } + return api_call + + +@pytest.fixture +def get_creature_call(): + api_call = { + "method": "GET", + "url": CFG["api_url"] + '/rules/creature', + "headers": application_json, + "body": {"creature": "badder"} + } + return api_call diff --git a/tests/open_service/expected_responses/test_get_creature.json b/tests/open_service/expected_responses/test_get_creature.json new file mode 100644 index 0000000..0230281 --- /dev/null +++ b/tests/open_service/expected_responses/test_get_creature.json @@ -0,0 +1,67 @@ +{ + "number": [ + 3, + 6, + 0 + ], + "morale": [ + 2, + 4, + 0 + ], + "hit dice": [ + 6, + 6, + 0 + ], + "armour": 5, + "environ": [ + "land" + ], + "land speed": [ + 12, + 900, + 18 + ], + "ms": [ + 1, + 10, + 8 + ], + "in": [ + 3, + 6, + 0 + ], + "dx": [ + 1, + 10, + 11 + ], + "ch": [ + 1, + 10, + 2 + ], + "cn": [ + 1, + 6, + 8 + ], + "ps": [ + 1, + 10, + 5 + ], + "attacks": { + "bite": [ + 1, + 6, + 0 + ] + }, + "mutations": [ + "Empathy" + ], + "description": "1.5 meter tall bipedal mutated badgers. They inhabit temperate areas. Organized into Tech Level II societies run by their 'nobility'. 10% chance of each badder in a party having 1 Tech Level III weapon. 10d10 males of fighting age live in tunnels under their villages." +} \ No newline at end of file diff --git a/tests/open_service/expected_responses/test_get_creatures.json b/tests/open_service/expected_responses/test_get_creatures.json new file mode 100644 index 0000000..9248a28 --- /dev/null +++ b/tests/open_service/expected_responses/test_get_creatures.json @@ -0,0 +1,62 @@ +[ + "android", + "ark", + "arn", + "badder", + "barl nep", + "ber lep", + "bigoon", + "blaash", + "blackun", + "blight", + "blood bird", + "brutorz", + "buggem", + "cal then", + "carrin", + "centisteed", + "cren tosh", + "crep plant", + "dabber", + "ert", + "ert telden", + "fen", + "fleshin", + "gator", + "gren", + "hawkoid", + "herkel", + "herp", + "hisser", + "hoop", + "hopper", + "horl choo", + "jaget", + "kai lin", + "kamodo", + "keeshin", + "kep plant", + "lil", + "manta", + "menarl", + "narl ep", + "obb", + "orlen", + "parn", + "perth", + "pineto", + "podog", + "rakox", + "sep", + "serf", + "seroon lou", + "sleeth", + "soul besh", + "squeeker", + "terl", + "wardent", + "win seen", + "yexil", + "zarn", + "zeethh" +] \ No newline at end of file diff --git a/tests/open_service/helpers.py b/tests/open_service/helpers.py new file mode 100644 index 0000000..ed089ba --- /dev/null +++ b/tests/open_service/helpers.py @@ -0,0 +1,15 @@ +import os +import json + + +CWD = os.path.dirname(os.path.realpath(__file__)) + + +#### +# HELPERS +#### +def get_expected_response(test_name): + expected_response_file = test_name + ".json" + with open(os.path.join(CWD, 'expected_responses', expected_response_file), 'r') as file: + expected_response = json.load(file) + return expected_response diff --git a/tests/open_service/test_gammaworld.py b/tests/open_service/test_gammaworld.py new file mode 100644 index 0000000..beab308 --- /dev/null +++ b/tests/open_service/test_gammaworld.py @@ -0,0 +1,19 @@ +import json +import pytest + +from apiclient.client import api_client +from open_service.helpers import get_expected_response + + +@pytest.mark.rules +def test_get_creatures(request, get_creatures_call): + expected_response = get_expected_response(request.node.name) + actual_response = api_client(get_creatures_call) + assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) + + +@pytest.mark.rules +def test_get_creature(request, get_creature_call): + expected_response = get_expected_response(request.node.name) + actual_response = api_client(get_creature_call) + assert json.dumps(actual_response, indent=4) == json.dumps(expected_response, indent=4) \ No newline at end of file