diff --git a/agrirouter/utils/type_url.py b/agrirouter/utils/type_url.py index 2457b9da..caa764b9 100644 --- a/agrirouter/utils/type_url.py +++ b/agrirouter/utils/type_url.py @@ -14,32 +14,27 @@ class TypeUrl: prefix = "types.agrirouter.com/" + commands = ( + Messages, + ListEndpointsResponse, + HeaderQueryResponse, + MessageQueryResponse, + MessageDelete, + MessageConfirm, + OnboardingResponse, + OnboardingRequest, + CapabilitySpecification, + Subscription, + MessageQuery, + ListEndpointsQuery, + ) @classmethod def get_type_url(cls, class_): - if class_ == Messages: - return cls.prefix + Messages.DESCRIPTOR.full_name - elif class_ == ListEndpointsResponse: - return cls.prefix + ListEndpointsResponse.DESCRIPTOR.full_name - elif class_ == HeaderQueryResponse: - return cls.prefix + HeaderQueryResponse.DESCRIPTOR.full_name - elif class_ == MessageQueryResponse: - return cls.prefix + MessageQueryResponse.DESCRIPTOR.full_name - elif class_ == MessageDelete: - return cls.prefix + MessageDelete.DESCRIPTOR.full_name - elif class_ == MessageConfirm: - return cls.prefix + MessageConfirm.DESCRIPTOR.full_name - elif class_ == OnboardingResponse: - return cls.prefix + OnboardingResponse.DESCRIPTOR.full_name - elif class_ == OnboardingRequest: - return cls.prefix + OnboardingRequest.DESCRIPTOR.full_name - elif class_ == CapabilitySpecification: - return cls.prefix + CapabilitySpecification.DESCRIPTOR.full_name - elif class_ == Subscription: - return cls.prefix + Subscription.DESCRIPTOR.full_name - elif class_ == MessageQuery: - return cls.prefix + MessageQuery.DESCRIPTOR.full_name - elif class_ == ListEndpointsQuery: - return cls.prefix + ListEndpointsQuery.DESCRIPTOR.full_name - else: + return TypeUrl.get_command(class_) + + @classmethod + def get_command(cls, class_) -> str: + if class_ not in cls.commands: raise TypeUrlNotFoundError(f"The {class_} type url not found") + return cls.prefix + class_.DESCRIPTOR.full_name diff --git a/setup.py b/setup.py index 1d3753e5..c0741fa2 100644 --- a/setup.py +++ b/setup.py @@ -12,27 +12,27 @@ description="""This project contains the API for the communication with the agrirouter. Everything you need for the onboarding process, secure communication and much more.""", classifiers=[ - 'Intended Audience :: Developers', - 'Natural Language :: English', - 'License :: OSI Approved :: Apache Software License', - 'Programming Language :: Python', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', - 'Programming Language :: Python :: 3.10', - ], + 'Intended Audience :: Developers', + 'Natural Language :: English', + 'License :: OSI Approved :: Apache Software License', + 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + ], install_requires=[ - 'certifi~=2021.5.30', - 'cffi~=1.14.6', - 'charset-normalizer~=2.0.6', - 'cryptography~=3.4.8', - 'idna~=3.2', - 'pycparser~=2.20', - 'requests~=2.26.0', - 'urllib3~=1.26.7' - ], + 'certifi~=2021.5.30', + 'cffi~=1.14.6', + 'charset-normalizer~=2.0.6', + 'cryptography~=3.4.8', + 'idna~=3.2', + 'pycparser~=2.20', + 'requests~=2.26.0', + 'urllib3~=1.26.7' + ], project_urls={ 'Documentation': 'https://github.com/DKE-Data/agrirouter-sdk-python', 'Source': 'https://github.com/DKE-Data/agrirouter-sdk-python', diff --git a/tests/auth_test/test_auth.py b/tests/auth_test/test_auth.py index 350e951e..20a23d77 100644 --- a/tests/auth_test/test_auth.py +++ b/tests/auth_test/test_auth.py @@ -6,10 +6,8 @@ public_key, private_key, auth_result_url, - ENV, - application_id, + ENV ) -import re class TestAuthorization: diff --git a/tests/constants.py b/tests/constants.py index 5651f23d..3c785dd7 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -121,7 +121,7 @@ # taken from auth_result_url valid_response_token = ( - "eyJhY2NvdW50IjoiMGJhMjRlZWUtYzMwYi00N2U1LWJkYzktNzcwM" - "2NmYjEzNmEwIiwicmVnY29kZSI6IjhlYWNiMTk4ZmMiLCJleHBpcm" - "VzIjoiMjAyMS0wOS0yM1QxNjowODo0My44ODhaIn0=" - ) + "eyJhY2NvdW50IjoiMGJhMjRlZWUtYzMwYi00N2U1LWJkYzktNzcwM" + "2NmYjEzNmEwIiwicmVnY29kZSI6IjhlYWNiMTk4ZmMiLCJleHBpcm" + "VzIjoiMjAyMS0wOS0yM1QxNjowODo0My44ODhaIn0=" +) diff --git a/tests/enviroments_test/test_environments.py b/tests/enviroments_test/test_environments.py index c866f1ad..36bbde81 100644 --- a/tests/enviroments_test/test_environments.py +++ b/tests/enviroments_test/test_environments.py @@ -1,157 +1,105 @@ """Test agrirouter/environments/environments.py""" -import pytest -from agrirouter.environments.environments import ProductionEnvironment, QAEnvironment -from tests.constants import application_id, auth_result_url +from agrirouter.environments.environments import ProductionEnvironment as PE +from agrirouter.environments.environments import QAEnvironment as QAE +from tests.constants import application_id -class TestProductionEnvironment: +class TestPE: def test_get_base_url(self): - assert ( - ProductionEnvironment().get_base_url() - == ProductionEnvironment._ENV_BASE_URL - ) + assert PE().get_base_url() == PE._ENV_BASE_URL def test_get_api_prefix(self): - assert ( - ProductionEnvironment().get_api_prefix() - == ProductionEnvironment._API_PREFIX - ) + assert PE().get_api_prefix() == PE._API_PREFIX def test_get_registration_service_url(self): - assert ( - ProductionEnvironment().get_registration_service_url() - == ProductionEnvironment._REGISTRATION_SERVICE_URL - ) + assert PE().get_registration_service_url() == PE._REGISTRATION_SERVICE_URL def test_get_onboard_url(self): - assert ( - ProductionEnvironment().get_onboard_url() - == ProductionEnvironment._REGISTRATION_SERVICE_URL - + ProductionEnvironment._API_PREFIX - + "/registration/onboard" - ) + onb_url = PE._REGISTRATION_SERVICE_URL + PE._API_PREFIX + "/registration/onboard" + assert PE().get_onboard_url() == onb_url def test_get_secured_onboard_url(self): - assert ( - ProductionEnvironment().get_secured_onboard_url() - == ProductionEnvironment._REGISTRATION_SERVICE_URL - + ProductionEnvironment._API_PREFIX - + "/registration/onboard/request" - ) + onb_url = PE._REGISTRATION_SERVICE_URL + PE._API_PREFIX + "/registration/onboard/request" + assert PE().get_secured_onboard_url() == onb_url def test_get_verify_onboard_request_url(self): - assert ( - ProductionEnvironment().get_verify_onboard_request_url() - == ProductionEnvironment._REGISTRATION_SERVICE_URL - + ProductionEnvironment._API_PREFIX - + "/registration/onboard/verify" - ) + req_url = PE._REGISTRATION_SERVICE_URL + PE._API_PREFIX + "/registration/onboard/verify" + assert PE().get_verify_onboard_request_url() == req_url def test_get_revoke_url(self): - assert ( - ProductionEnvironment().get_revoke_url() - == ProductionEnvironment._REGISTRATION_SERVICE_URL - + ProductionEnvironment._API_PREFIX - + "/registration/onboard/revoke" - ) + rev_url = PE._REGISTRATION_SERVICE_URL + PE._API_PREFIX + "/registration/onboard/revoke" + assert PE().get_revoke_url() == rev_url def test_get_agrirouter_login_url(self): - assert ( - ProductionEnvironment().get_agrirouter_login_url() - == ProductionEnvironment._ENV_BASE_URL - + ProductionEnvironment._AGRIROUTER_LOGIN_URL - ) + login_url = PE._ENV_BASE_URL + PE._AGRIROUTER_LOGIN_URL + assert PE().get_agrirouter_login_url() == login_url def test_get_secured_onboarding_authorization_url(self): redirect_uri = "www.my_redirect.com" response_type = "response_type" - assert ProductionEnvironment().get_secured_onboarding_authorization_url( + assert PE().get_secured_onboarding_authorization_url( application_id, response_type, "state", redirect_uri - ) == "https://goto.my-agrirouter.com/application/{application_id}/authorize?response_type={response_type}&state={state}".format( + ) == "https://goto.my-agrirouter.com/application/{application_id}/authorize?response_type={response_type}&state={state}".format( # noqa application_id=application_id, response_type=response_type, state="state") + f"&redirect_uri={redirect_uri}" def test_get_mqtt_server_url(self): - assert ProductionEnvironment().get_mqtt_server_url( + assert PE().get_mqtt_server_url( "localhost", "5000" - ) == ProductionEnvironment._MQTT_URL_TEMPLATE.format( + ) == PE._MQTT_URL_TEMPLATE.format( host="localhost", port="5000" ) def test_get_env_public_key(self): - assert ( - ProductionEnvironment().get_env_public_key() - == ProductionEnvironment.AR_PUBLIC_KEY - ) + assert PE().get_env_public_key() == PE.AR_PUBLIC_KEY -class TestQAEnvironment: +class TestQAE: def test_get_base_url(self): - assert QAEnvironment().get_base_url() == QAEnvironment._ENV_BASE_URL + assert QAE().get_base_url() == QAE._ENV_BASE_URL def test_get_api_prefix(self): - assert QAEnvironment().get_api_prefix() == QAEnvironment._API_PREFIX + assert QAE().get_api_prefix() == QAE._API_PREFIX def test_get_registration_service_url(self): - assert ( - QAEnvironment().get_registration_service_url() - == QAEnvironment._REGISTRATION_SERVICE_URL - ) + assert QAE().get_registration_service_url() == QAE._REGISTRATION_SERVICE_URL def test_get_onboard_url(self): - assert ( - QAEnvironment().get_onboard_url() - == QAEnvironment._REGISTRATION_SERVICE_URL - + QAEnvironment._API_PREFIX - + "/registration/onboard" - ) + onb_url = QAE._REGISTRATION_SERVICE_URL + QAE._API_PREFIX + "/registration/onboard" + assert QAE().get_onboard_url() == onb_url def test_get_secured_onboard_url(self): - assert ( - QAEnvironment().get_secured_onboard_url() - == QAEnvironment._REGISTRATION_SERVICE_URL - + QAEnvironment._API_PREFIX - + "/registration/onboard/request" - ) + onb_url = QAE._REGISTRATION_SERVICE_URL + QAE._API_PREFIX + "/registration/onboard/request" + assert QAE().get_secured_onboard_url() == onb_url def test_get_verify_onboard_request_url(self): - assert ( - QAEnvironment().get_verify_onboard_request_url() - == QAEnvironment._REGISTRATION_SERVICE_URL - + QAEnvironment._API_PREFIX - + "/registration/onboard/verify" - ) + req_url = QAE._REGISTRATION_SERVICE_URL + QAE._API_PREFIX + "/registration/onboard/verify" + assert QAE().get_verify_onboard_request_url() == req_url def test_get_revoke_url(self): - assert ( - QAEnvironment().get_revoke_url() - == QAEnvironment._REGISTRATION_SERVICE_URL - + QAEnvironment._API_PREFIX - + "/registration/onboard/revoke" - ) + rev_url = QAE._REGISTRATION_SERVICE_URL + QAE._API_PREFIX + "/registration/onboard/revoke" + assert QAE().get_revoke_url() == rev_url def test_get_agrirouter_login_url(self): - assert ( - QAEnvironment().get_agrirouter_login_url() - == QAEnvironment._ENV_BASE_URL + QAEnvironment._AGRIROUTER_LOGIN_URL - ) + login_url = QAE._ENV_BASE_URL + QAE._AGRIROUTER_LOGIN_URL + assert QAE().get_agrirouter_login_url() == login_url def test_get_secured_onboarding_authorization_url(self): redirect_uri = "www.my_redirect.com" response_type = "response_type" - assert QAEnvironment().get_secured_onboarding_authorization_url( + assert QAE().get_secured_onboarding_authorization_url( application_id, response_type, "state", redirect_uri - ) == QAEnvironment._ENV_BASE_URL + QAEnvironment._SECURED_ONBOARDING_AUTHORIZATION_LINK_TEMPLATE.format( + ) == QAE._ENV_BASE_URL + QAE._SECURED_ONBOARDING_AUTHORIZATION_LINK_TEMPLATE.format( application_id=application_id, response_type=response_type, state="state") + f"&redirect_uri={redirect_uri}" def test_get_mqtt_server_url(self): - assert QAEnvironment().get_mqtt_server_url( + assert QAE().get_mqtt_server_url( "localhost", "5000" - ) == QAEnvironment._MQTT_URL_TEMPLATE.format(host="localhost", port="5000") + ) == QAE._MQTT_URL_TEMPLATE.format(host="localhost", port="5000") def test_get_env_public_key(self): - assert QAEnvironment().get_env_public_key() == QAEnvironment.AR_PUBLIC_KEY + assert QAE().get_env_public_key() == QAE.AR_PUBLIC_KEY diff --git a/tests/messaging_test/test_decode.py b/tests/messaging_test/test_decode.py index bd2aa5da..5385e285 100644 --- a/tests/messaging_test/test_decode.py +++ b/tests/messaging_test/test_decode.py @@ -1,12 +1,11 @@ import json -import pytest from agrirouter.generated.messaging.response.response_pb2 import ResponseEnvelope from agrirouter.messaging.decode import decode_response from agrirouter.messaging.decode import decode_details -MESSAGING_RESULT = b'[{"sensorAlternateId":"185cd97b-ed0b-4e75-a6e2-6be1cdd38a06","capabilityAlternateId":"bbe9f361-b551-48d9-9fca-1b4dc768287c","command":{"message":"XwjIARAKGiQ5NWUzNWE0Zi1jNWM4LTQ1NDEtODE4OS03NmJlMzM0OTc0NDUiJDUzNzYyM2ZjLWY2NmYtNDc5Yi1hMmJhL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UHl0aG9uU0RLX2RldiAtIDIwMjEtMTAtMjEsIDIxOjQxOjI0GgthcHBsaWNhdGlvbiIGYWN0aXZlMhh1cm46bXlhcHA6c25yMDAwMDMyMzRzZGY="}}]' # noqa def test_decode_response(): @@ -17,8 +16,8 @@ def test_decode_response(): assert message.response_payload.details - assert message.response_payload.details.type_url == "types.agrirouter.com/agrirouter.response.payload.account.ListEndpointsResponse" - assert message.response_payload.details.value == b'\nx\n$d704a443-99f7-47b4-be55-e2fa09689ebe\x12$PythonSDK_dev - 2021-10-25, 10:51:18\x1a\x0bapplication"\x06active2\x15urn:myapp:snr00003234\n{\n$185cd97b-ed0b-4e75-a6e2-6be1cdd38a06\x12$PythonSDK_dev - 2021-10-21, 21:41:24\x1a\x0bapplication"\x06active2\x18urn:myapp:snr00003234sdf' + assert message.response_payload.details.type_url == "types.agrirouter.com/agrirouter.response.payload.account.ListEndpointsResponse" # noqa + assert message.response_payload.details.value == b'\nx\n$d704a443-99f7-47b4-be55-e2fa09689ebe\x12$PythonSDK_dev - 2021-10-25, 10:51:18\x1a\x0bapplication"\x06active2\x15urn:myapp:snr00003234\n{\n$185cd97b-ed0b-4e75-a6e2-6be1cdd38a06\x12$PythonSDK_dev - 2021-10-21, 21:41:24\x1a\x0bapplication"\x06active2\x18urn:myapp:snr00003234sdf' # noqa assert message.response_envelope.response_code == 200 assert message.response_envelope.type == ResponseEnvelope.ResponseBodyType.Value("ENDPOINTS_LISTING") diff --git a/tests/onboarding_test/test_headers.py b/tests/onboarding_test/test_headers.py index 210eb7e9..2c161f88 100644 --- a/tests/onboarding_test/test_headers.py +++ b/tests/onboarding_test/test_headers.py @@ -15,8 +15,7 @@ def test_get_header(self): self.test_object.get_header()["Authorization"] == "Bearer " + self.reg_code ) assert ( - self.test_object.get_header()["Content-Type"] - == ContentTypes.APPLICATION_JSON.value + self.test_object.get_header()["Content-Type"] == ContentTypes.APPLICATION_JSON.value ) assert self.test_object_1.get_header()["Content-Type"] == "json" diff --git a/tests/onboarding_test/test_request_onboarding.py b/tests/onboarding_test/test_request_onboarding.py index 17ab1eea..df39ecd8 100644 --- a/tests/onboarding_test/test_request_onboarding.py +++ b/tests/onboarding_test/test_request_onboarding.py @@ -46,6 +46,5 @@ def test_get_header(self): ) assert self.test_object.get_header()["Content-Type"] == self.content_type assert ( - self.test_object.get_header()["X-Agrirouter-ApplicationId"] - == application_id + self.test_object.get_header()["X-Agrirouter-ApplicationId"] == application_id ) diff --git a/tests/onboarding_test/test_signature.py b/tests/onboarding_test/test_signature.py index e275d5d9..a6761baa 100644 --- a/tests/onboarding_test/test_signature.py +++ b/tests/onboarding_test/test_signature.py @@ -1,12 +1,11 @@ """Test agrirouter/onboarding/signature.py""" import pytest -import re from cryptography.exceptions import InvalidSignature from agrirouter.onboarding.signature import create_signature, verify_signature -from tests.constants import private_key, wrong_private_key, public_key, valid_response_signature +from tests.constants import private_key, public_key def test_create_signature_ok(): diff --git a/tox.ini b/tox.ini index 2aae4364..7bb6e324 100644 --- a/tox.ini +++ b/tox.ini @@ -4,11 +4,13 @@ exclude = .git, __pycache__, .pytest_cache + ./agrirouter/__init__.py, agrirouter_sdk_python.egg-info, assets, old, build, dist, + example_script.py, venv, conftest.py, *_pb2.py