Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ venv
*.log
*.log.*
.env
.pytest_cache
3 changes: 3 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ For more install options type:

Python 3.6 or above is required.

== Running unit tests

`$ pytest`

== External resources

Expand Down
Empty file removed agrirouter/clients/__init__.py
Empty file.
29 changes: 0 additions & 29 deletions agrirouter/clients/clients.py

This file was deleted.

13 changes: 13 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
from tests.constants import public_key, private_key, auth_result_url


@pytest.fixture(scope="session")
def authorization():
from agrirouter.auth.auth import Authorization

auth_client = Authorization("QA", public_key=public_key, private_key=private_key)
auth_response = auth_client.extract_auth_response(auth_result_url)
auth_client.verify_auth_response(auth_response)
auth_data = auth_response.get_auth_result()
return auth_data
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ protobuf==3.18.0
pycparser==2.20
requests==2.26.0
urllib3==1.26.7
pytest==6.2.5
black==21.9b0
flake8==3.9.2
pyflakes==2.3.1
Empty file removed tests/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions tests/auth_test/test_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Tests agrirouter/auth/response.py"""

import re
from agrirouter.auth.response import AuthResponse


def test_decode_token():
token = (
"eyJhY2NvdW50IjoiMGJhMjRlZWUtYzMwYi00N2U1LWJkYzktNzcwM"
"2NmYjEzNmEwIiwicmVnY29kZSI6IjhlYWNiMTk4ZmMiLCJleHBpcm"
"VzIjoiMjAyMS0wOS0yM1QxNjowODo0My44ODhaIn0="
)
decoded_token = AuthResponse.decode_token(token)
assert isinstance(decoded_token["account"], str)
assert isinstance(decoded_token["expires"], str)
assert re.search(r"[\w]", decoded_token["regcode"])
assert re.search(r"[\w]", decoded_token["account"])


def test_get_auth_result(authorization):
assert isinstance(AuthResponse(authorization).get_auth_result(), dict)
assert AuthResponse(authorization).get_auth_result()["credentials"]
60 changes: 60 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
application_id = "8c947a45-c57d-42d2-affc-206e21d63a50"
ENV = "QA"
auth_result_url = (
"http://fuf.me/?state=46c81f94-d117-4658-9a38-a85692448219&token=eyJhY2NvdW50IjoiMGJhMjRlZWUtYzMwY"
"i00N2U1LWJkYzktNzcwM2NmYjEzNmEwIiwicmVnY29kZSI6IjhlYWNiMTk4ZmMiLCJleHBpcmVzIjoiMjAyMS0wOS0yM1QxNj"
"owODo0My44ODhaIn0%3D&signature=SUL9SQMWAfG4%2FEyT0rejkRfAyioxJIOs4sxI5wxeB8TkIiv0MR6YFKw1tPIkM4ll"
"uZKHEIgr5WvM3b3SvII9TtEbzZf995R8GIlNP6yyP51TF%2F4vZMbkMjq%2B2g1o0qw%2FyuDQcGz1RpOJWCuBOjMXu9quzGO"
"8xvDW7LjrN%2BMA9rzJZYb1toNf51O0eO4BDWL5L1oLvrKrqvaErKcIoRJtTVJ51awOWMARDkGZahcRdWrZbdGUbQwIyKJQu4"
"vH8%2B4ytlyXPSWEYwKE2VFoAjhzWsKODdRRxDbNNLWsW8sxKamdXjSOC8inHUFsFNoxLbwZEnKROm2s3OfKGYuibXOpXw%3D%3D"
)
private_key = (
"-----BEGIN PRIVATE KEY-----\n"
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC8WVUWBhEiQYvo\n"
"DL9q/Z5LbIhqDtcdFYWBUV7A96AXJaNRA9J7m3a6es05S7pRynv9yT8i+bhz/qDf\n"
"18MPOaqLglTw58ylVtTGmIlIZr85KJecO5aQnKN1pz8LyKnQSk4IWMB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F9661acn+iS+QS+9Y\n3HvTfUV"
"cismzbuvxHgHA7YeoOUFxyj3lkaTnXm7hzQe4wDEDgwpJSGAzxIIYSUXe\n8EsWLorg5O0tRexx5SP3+kj1i83DATBJCXP7k+bAF4"
"u2FVJphC1m2BfLxelGLjzx\nVAS/v6+EwvYaT1AI9FFqW/a2o92IsVPOh9oM9eds3lBOAbH/8XrmVIeHofw+XbTH\n1/7MLD6IE2+"
"HbEeY0F96nioXArdQWXcjUQsTch+p0p9eqh23Ak4ef5oGcZhNd4yp\nY8M6ppvIMiXkgWSPJevCJjhxRJRmndY+ajYGx7CLePx7wN"
"vxXWtkng3yh+7WiZ/Y\nqwIDAQAB\n-----END PUBLIC KEY-----"
)
11 changes: 11 additions & 0 deletions tests/enviroments_test/test_environmental_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Test agrirouter/environments/environmental_services.py"""

import pytest
from agrirouter.environments.exceptions import InvalidEnvironmentSetup
from agrirouter.environments.environmental_services import EnvironmentalService
from tests.constants import ENV


def test_arclient_set_env():
with pytest.raises(InvalidEnvironmentSetup):
assert EnvironmentalService("WRONG")._set_env("WRONG")
213 changes: 213 additions & 0 deletions tests/enviroments_test/test_environments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
"""Test agrirouter/environments/environments.py"""

import pytest
from agrirouter.environments.environments import ProductionEnvironment, QAEnvironment
from tests.constants import application_id, auth_result_url


class TestProductionEnvironment:
def test_get_base_url(self):
assert (
ProductionEnvironment().get_base_url()
== ProductionEnvironment._ENV_BASE_URL
)

def test_get_api_prefix(self):
assert (
ProductionEnvironment().get_api_prefix()
== ProductionEnvironment._API_PREFIX
)

def test_get_registration_service_url(self):
assert (
ProductionEnvironment().get_registration_service_url()
== ProductionEnvironment._REGISTRATION_SERVICE_URL
)

def test_get_onboard_url(self):
assert (
ProductionEnvironment().get_onboard_url()
== ProductionEnvironment._REGISTRATION_SERVICE_URL
+ ProductionEnvironment._API_PREFIX
+ "/registration/onboard"
)

def test_get_secured_onboard_url(self):
assert (
ProductionEnvironment().get_secured_onboard_url()
== ProductionEnvironment._REGISTRATION_SERVICE_URL
+ ProductionEnvironment._API_PREFIX
+ "/registration/onboard/request"
)

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"
)

def test_get_revoke_url(self):
assert (
ProductionEnvironment().get_revoke_url()
== ProductionEnvironment._REGISTRATION_SERVICE_URL
+ ProductionEnvironment._API_PREFIX
+ "/registration/onboard/revoke"
)

def test_get_agrirouter_login_url(self):
assert (
ProductionEnvironment().get_agrirouter_login_url()
== ProductionEnvironment._ENV_BASE_URL
+ ProductionEnvironment._AGRIROUTER_LOGIN_URL
)

def test_get_secured_onboarding_authorization_url(self):
assert ProductionEnvironment().get_secured_onboarding_authorization_url(
application_id, str, "state", auth_result_url
) == ProductionEnvironment._ENV_BASE_URL + ProductionEnvironment._SECURED_ONBOARDING_AUTHORIZATION_LINK_TEMPLATE.format(
application_id=application_id,
response_type=str,
state="state",
redirect_uri=auth_result_url,
)
with pytest.raises(AssertionError):
assert ProductionEnvironment().get_secured_onboarding_authorization_url(
application_id, str, "state", auth_result_url
) == ProductionEnvironment._ENV_BASE_URL + ProductionEnvironment._SECURED_ONBOARDING_AUTHORIZATION_LINK_TEMPLATE.format(
application_id=application_id,
response_type=str,
state="123",
redirect_uri=auth_result_url,
)
with pytest.raises(AssertionError):
assert ProductionEnvironment().get_secured_onboarding_authorization_url(
application_id, dict, "state", auth_result_url
) == ProductionEnvironment._ENV_BASE_URL + ProductionEnvironment._SECURED_ONBOARDING_AUTHORIZATION_LINK_TEMPLATE.format(
application_id=application_id,
response_type=str,
state="state",
redirect_uri=auth_result_url,
)

def test_get_mqtt_server_url(self):
assert ProductionEnvironment().get_mqtt_server_url(
"localhost", "5000"
) == ProductionEnvironment._MQTT_URL_TEMPLATE.format(
host="localhost", port="5000"
)
with pytest.raises(AssertionError):
assert ProductionEnvironment().get_mqtt_server_url(
"localhost", "5000"
) == ProductionEnvironment._MQTT_URL_TEMPLATE.format(
host="127.0.0.1", port="5000"
)
with pytest.raises(AssertionError):
assert ProductionEnvironment().get_mqtt_server_url(
"localhost", "5000"
) == ProductionEnvironment._MQTT_URL_TEMPLATE.format(
host="localhost", port="80"
)

def test_get_env_public_key(self):
assert (
ProductionEnvironment().get_env_public_key()
== ProductionEnvironment.AR_PUBLIC_KEY
)


class TestQAEnvironment:
def test_get_base_url(self):
assert QAEnvironment().get_base_url() == QAEnvironment._ENV_BASE_URL

def test_get_api_prefix(self):
assert QAEnvironment().get_api_prefix() == QAEnvironment._API_PREFIX

def test_get_registration_service_url(self):
assert (
QAEnvironment().get_registration_service_url()
== QAEnvironment._REGISTRATION_SERVICE_URL
)

def test_get_onboard_url(self):
assert (
QAEnvironment().get_onboard_url()
== QAEnvironment._REGISTRATION_SERVICE_URL
+ QAEnvironment._API_PREFIX
+ "/registration/onboard"
)

def test_get_secured_onboard_url(self):
assert (
QAEnvironment().get_secured_onboard_url()
== QAEnvironment._REGISTRATION_SERVICE_URL
+ QAEnvironment._API_PREFIX
+ "/registration/onboard/request"
)

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"
)

def test_get_revoke_url(self):
assert (
QAEnvironment().get_revoke_url()
== QAEnvironment._REGISTRATION_SERVICE_URL
+ QAEnvironment._API_PREFIX
+ "/registration/onboard/revoke"
)

def test_get_agrirouter_login_url(self):
assert (
QAEnvironment().get_agrirouter_login_url()
== QAEnvironment._ENV_BASE_URL + QAEnvironment._AGRIROUTER_LOGIN_URL
)

def test_get_secured_onboarding_authorization_url(self):
assert QAEnvironment().get_secured_onboarding_authorization_url(
application_id, str, "state", auth_result_url
) == QAEnvironment._ENV_BASE_URL + QAEnvironment._SECURED_ONBOARDING_AUTHORIZATION_LINK_TEMPLATE.format(
application_id=application_id,
response_type=str,
state="state",
redirect_uri=auth_result_url,
)
with pytest.raises(AssertionError):
assert QAEnvironment().get_secured_onboarding_authorization_url(
application_id, str, "state", auth_result_url
) == QAEnvironment._ENV_BASE_URL + QAEnvironment._SECURED_ONBOARDING_AUTHORIZATION_LINK_TEMPLATE.format(
application_id=application_id,
response_type=str,
state="123",
redirect_uri=auth_result_url,
)
with pytest.raises(AssertionError):
assert QAEnvironment().get_secured_onboarding_authorization_url(
application_id, dict, "state", auth_result_url
) == QAEnvironment._ENV_BASE_URL + QAEnvironment._SECURED_ONBOARDING_AUTHORIZATION_LINK_TEMPLATE.format(
application_id=application_id,
response_type=str,
state="state",
redirect_uri=auth_result_url,
)

def test_get_mqtt_server_url(self):
assert QAEnvironment().get_mqtt_server_url(
"localhost", "5000"
) == QAEnvironment._MQTT_URL_TEMPLATE.format(host="localhost", port="5000")
with pytest.raises(AssertionError):
assert QAEnvironment().get_mqtt_server_url(
"localhost", "5000"
) == QAEnvironment._MQTT_URL_TEMPLATE.format(host="127.0.0.1", port="5000")
with pytest.raises(AssertionError):
assert QAEnvironment().get_mqtt_server_url(
"localhost", "5000"
) == QAEnvironment._MQTT_URL_TEMPLATE.format(host="localhost", port="80")

def test_get_env_public_key(self):
assert QAEnvironment().get_env_public_key() == QAEnvironment.AR_PUBLIC_KEY
15 changes: 15 additions & 0 deletions tests/messaging_test/test_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Test agrirouter/messaging/request.py"""

from agrirouter.messaging.messages import Message
from agrirouter.messaging.request import MessageRequest


def test_json_serialize():
message_request = MessageRequest(
sensor_alternate_id="1",
capability_alternate_id="1",
messages=[Message(content="content")],
).json_serialize()
assert isinstance(message_request, dict)
assert message_request["capabilityAlternateId"] == "1"
assert message_request["sensorAlternateId"] == "1"
Loading