From 73998831c85b5e578d83b78445ac6364d25ecb26 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Sat, 11 May 2024 16:22:03 +0200 Subject: [PATCH 1/4] Add unit tests for api enums and environments This commit introduces new unit tests for the api enums and environments modules. The tests cover the basic functionality and expected outputs of the methods in these modules, improving our confidence in their reliability and correctness. --- tests/agrirouter/api/__init__.py | 0 tests/agrirouter/api/test_enums.py | 25 ++++++++++ tests/agrirouter/api/test_environments.py | 57 +++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tests/agrirouter/api/__init__.py create mode 100644 tests/agrirouter/api/test_enums.py create mode 100644 tests/agrirouter/api/test_environments.py diff --git a/tests/agrirouter/api/__init__.py b/tests/agrirouter/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/agrirouter/api/test_enums.py b/tests/agrirouter/api/test_enums.py new file mode 100644 index 00000000..a19057ac --- /dev/null +++ b/tests/agrirouter/api/test_enums.py @@ -0,0 +1,25 @@ +import unittest + +import pytest + +from src.agrirouter.api.enums import BaseEnum + + +class TestEnum(BaseEnum): + VALUE1 = 'value1' + VALUE2 = 'value2' + + +class TestBaseEnum(unittest.TestCase): + + @pytest.fixture(autouse=True) + def setup_class(self): + self.enum = TestEnum + + def test_choices(self): + expected_choices = [('value1', 'VALUE1'), ('value2', 'VALUE2')] + assert self.enum.choices() == expected_choices + + def test_values_list(self): + expected_values_list = ['value1', 'value2'] + assert self.enum.values_list() == expected_values_list diff --git a/tests/agrirouter/api/test_environments.py b/tests/agrirouter/api/test_environments.py new file mode 100644 index 00000000..d4d019d5 --- /dev/null +++ b/tests/agrirouter/api/test_environments.py @@ -0,0 +1,57 @@ +import unittest + +from src.agrirouter.api import environments + + +class TestEnvironments(unittest.TestCase): + def test_get_base_url(self): + env = environments.BaseEnvironment() + assert env.get_base_url() == "" + + def test_get_api_prefix(self): + env = environments.BaseEnvironment() + assert env.get_api_prefix() == "" + + def test_get_registration_service_url(self): + env = environments.BaseEnvironment() + assert env.get_registration_service_url() == "" + + def test_get_onboard_url(self): + env = environments.BaseEnvironment() + expected_url = "/registration/onboard" + assert env.get_onboard_url() == expected_url + + def test_get_secured_onboard_url(self): + env = environments.BaseEnvironment() + expected_url = "/registration/onboard/request" + assert env.get_secured_onboard_url() == expected_url + + def test_get_verify_onboard_request_url(self): + env = environments.BaseEnvironment() + expected_url = "/registration/onboard/verify" + assert env.get_verify_onboard_request_url() == expected_url + + def test_get_revoke_url(self): + env = environments.BaseEnvironment() + expected_url = "/registration/onboard/revoke" + assert env.get_revoke_url() == expected_url + + def test_get_agrirouter_login_url(self): + env = environments.BaseEnvironment() + expected_url = "/app" + assert env.get_agrirouter_login_url() == expected_url + + def test_get_secured_onboarding_authorization_url(self): + env = environments.BaseEnvironment() + expected_url = "/application/test_app/authorize?response_type=test_response_type&state=test_state&redirect_uri=test_redirect_uri" + assert env.get_secured_onboarding_authorization_url("test_app", "test_response_type", "test_state", + "test_redirect_uri") == expected_url + + def test_get_mqtt_server_url(self): + env = environments.BaseEnvironment() + expected_url = "ssl://test_host:test_port" + assert env.get_mqtt_server_url("test_host", "test_port") == expected_url + + def test_get_env_public_key(self): + env = environments.BaseEnvironment() + assert env.get_env_public_key() is None From b6c81de57209c981f09ab9f0b4641a5eb51f76c3 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Sat, 11 May 2024 16:29:58 +0200 Subject: [PATCH 2/4] Add auth module tests The commit introduces a suite of tests for the authorization module. It covers testing for initialization, generation and extraction of authentication requests, response handling for both successful and error situations as well as query parameters extraction. --- tests/test_auth.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/test_auth.py diff --git a/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 00000000..783958f4 --- /dev/null +++ b/tests/test_auth.py @@ -0,0 +1,54 @@ +import unittest + +from agrirouter.api.enums import ResponseTypes +from agrirouter.api.environments import BaseEnvironment +from agrirouter.auth.auth import Authorization +from agrirouter.auth.parameters import AuthUrlParameter + + +class MockEnvironment(BaseEnvironment): + def get_secured_onboarding_authorization_url(self, **kwargs): + return 'https://secured.url' + + def get_env_public_key(self): + return 'public_key_env' + + +class TestAuthorization(unittest.TestCase): + + def test_authorization_initialization(self): + authorization = Authorization(MockEnvironment(), 'public_key', 'private_key') + assert authorization._public_key == 'public_key' + assert authorization._private_key == 'private_key' + + def test_get_auth_request_url(self): + authorization = Authorization(MockEnvironment(), 'public_key', 'private_key') + parameters = AuthUrlParameter(application_id='app_id', response_type=ResponseTypes.ONBOARD.value, + state='state', + redirect_uri='redirect_uri') + auth_url = authorization.get_auth_request_url(parameters=parameters) + assert auth_url == 'https://secured.url' + + def test_extract_auth_response(self): + authorization = Authorization(MockEnvironment(), 'public_key', 'private_key') + auth_response = authorization.extract_auth_response( + 'https://example.com/?state=state&token=token&signature=signature') + assert auth_response.state == 'state' + assert auth_response.token == 'token' + assert auth_response.signature == 'signature' + assert auth_response.error == None + + def test_extract_auth_response_and_error(self): + authorization = Authorization(MockEnvironment(), 'public_key', 'private_key') + auth_response = authorization.extract_auth_response( + 'https://example.com/?state=state&token=token&signature=signature&error=error') + assert auth_response.state == 'state' + assert auth_response.token == 'token' + assert auth_response.signature == 'signature' + assert auth_response.error == 'error' + + def test_extract_query_params(self): + query_str = "param1=value1¶m2=value2¶m3=value3" + extracted_params = Authorization._extract_query_params(query_str) + expected_params = {'param1': 'value1', 'param2': 'value2', 'param3': 'value3'} + assert extracted_params == expected_params From 3c3b4efe6f652cf3b52481616a15ecbae156bf24 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Sat, 11 May 2024 16:30:45 +0200 Subject: [PATCH 3/4] Refactor auth tests location The commit renames and moves the 'test_auth.py' file to a more specific directory within the tests folder. This new location 'agrirouter/auth/' better represents its purpose in the project structure. --- tests/agrirouter/auth/__init__.py | 0 tests/{ => agrirouter/auth}/test_auth.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/agrirouter/auth/__init__.py rename tests/{ => agrirouter/auth}/test_auth.py (100%) diff --git a/tests/agrirouter/auth/__init__.py b/tests/agrirouter/auth/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_auth.py b/tests/agrirouter/auth/test_auth.py similarity index 100% rename from tests/test_auth.py rename to tests/agrirouter/auth/test_auth.py From a44910f8671b9a4b7994dd053563b5419668b926 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Sat, 11 May 2024 16:40:35 +0200 Subject: [PATCH 4/4] Refactor test assertions in auth and environments modules Minor changes were applied to the assertion tests in the 'test_auth.py' and 'test_environments.py' modules. In 'test_auth.py', the test for 'None' value is now using the 'is' keyword instead of '==', following Python's convention for comparing against None. In 'test_environments.py', the line exceeding the length limit has been handled by appending '# noqa' to bypass the flake8 warning. --- tests/agrirouter/api/test_environments.py | 4 ++-- tests/agrirouter/auth/test_auth.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/agrirouter/api/test_environments.py b/tests/agrirouter/api/test_environments.py index d4d019d5..dca9142b 100644 --- a/tests/agrirouter/api/test_environments.py +++ b/tests/agrirouter/api/test_environments.py @@ -43,8 +43,8 @@ def test_get_agrirouter_login_url(self): def test_get_secured_onboarding_authorization_url(self): env = environments.BaseEnvironment() - expected_url = "/application/test_app/authorize?response_type=test_response_type&state=test_state&redirect_uri=test_redirect_uri" - assert env.get_secured_onboarding_authorization_url("test_app", "test_response_type", "test_state", + expected_url = "/application/test_app/authorize?response_type=test_response_type&state=test_state&redirect_uri=test_redirect_uri" # noqa + assert env.get_secured_onboarding_authorization_url("test_app", "test_response_type", "test_state", # noqa "test_redirect_uri") == expected_url def test_get_mqtt_server_url(self): diff --git a/tests/agrirouter/auth/test_auth.py b/tests/agrirouter/auth/test_auth.py index 783958f4..fffc48af 100644 --- a/tests/agrirouter/auth/test_auth.py +++ b/tests/agrirouter/auth/test_auth.py @@ -36,7 +36,7 @@ def test_extract_auth_response(self): assert auth_response.state == 'state' assert auth_response.token == 'token' assert auth_response.signature == 'signature' - assert auth_response.error == None + assert auth_response.error is None def test_extract_auth_response_and_error(self): authorization = Authorization(MockEnvironment(), 'public_key', 'private_key')