Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.
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
47 changes: 47 additions & 0 deletions tests/auth_test/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Tests agrirouter/auth/auth.py"""

from agrirouter import AuthUrlParameter
from agrirouter.auth.auth import Authorization
from tests.constants import (
public_key,
private_key,
auth_result_url,
ENV,
application_id,
)
from re import search


class TestAuthorization:
def test_extract_auth_response(self):
auth_client = Authorization(ENV, public_key=public_key, private_key=private_key)
assert search(
"<agrirouter.auth.response.AuthResponse",
str(auth_client.extract_auth_response(auth_result_url)),
)

auth_client = Authorization(
"Production", public_key=public_key, private_key=private_key
)
assert search(
"<agrirouter.auth.response.AuthResponse",
str(auth_client.extract_auth_response(auth_result_url)),
)

def test_get_auth_request_url(self):
auth_params = AuthUrlParameter(
application_id=application_id, response_type="onboard"
)
auth_client = Authorization(
"QA", public_key=public_key, private_key=private_key
)
check_url = "https://agrirouter-qa.cfapps.eu10.hana.ondemand.com/application/"
assert search(check_url, auth_client.get_auth_request_url(auth_params))

def test_get_auth_result(self):
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)
assert auth_response.get_auth_result()["credentials"]
137 changes: 137 additions & 0 deletions tests/auth_test/test_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"""Tests agrirouter/auth/dto.py"""
from agrirouter.onboarding.dto import (
AuthorizationToken,
ConnectionCriteria,
Authentication,
)
from agrirouter.messaging.exceptions import WrongFieldError
import pytest


class TestAuthorizationToken:
def test_json_deserialize(self):
account = "account"
regcode = "regcode"
expires = "01-01-2021"
test_object = AuthorizationToken(
account=account,
)
test_object.json_deserialize({"regcode": regcode, "expires": expires})
assert test_object
assert test_object.account == account
assert test_object.regcode == regcode
assert test_object.expires == expires

test_object_1 = AuthorizationToken(
regcode=regcode,
)
test_object_1.json_deserialize({"account": account})
assert test_object_1
assert test_object_1.account == account
assert test_object_1.regcode == regcode
assert test_object_1.expires is None

test_object_2 = AuthorizationToken(
account=account,
)
test_object_2.json_deserialize({"expires": expires})
assert test_object_2
assert test_object_2.account == account
assert test_object_2.regcode is None
assert test_object_2.expires == expires

test_object_3 = AuthorizationToken(
expires=expires,
)
test_object_3.json_deserialize({"regcode": regcode})
assert test_object_3
assert test_object_3.account is None
assert test_object_3.regcode == regcode
assert test_object_3.expires == expires
with pytest.raises(WrongFieldError):
assert test_object_3.json_deserialize({"wrong_key": regcode})


class TestConnectionCriteria:
def test_json_deserialize(self):
client_id = "1"
commands = "commands"
gateway_id = "3"
host = "localhost"
measures = "test_measures"
port = "80"
test_object = ConnectionCriteria(
gateway_id=gateway_id,
client_id=client_id,
host=host,
measures=measures,
port=port,
)
test_object.json_deserialize({"commands": commands})
assert test_object
assert test_object.gateway_id == gateway_id
assert test_object.client_id == client_id
assert test_object.commands == commands
assert test_object.host == host
assert test_object.measures == measures
assert test_object.port == port

test_object_1 = ConnectionCriteria(
gateway_id=gateway_id,
client_id=client_id,
commands=commands,
measures=measures,
port=port,
)

test_object_1.json_deserialize({"host": host})
assert test_object_1
assert test_object_1.gateway_id == gateway_id
assert test_object_1.client_id == client_id
assert test_object_1.commands == commands
assert test_object_1.host == host
assert test_object_1.measures == measures
assert test_object_1.port == port
with pytest.raises(WrongFieldError):
assert test_object_1.json_deserialize({"wrong_key": measures})


class TestAuthentication:
def test_json_deserialize(self):
type = "type"
secret = "secret"
certificate = "certificate"
test_object = Authentication(
type=type,
secret=secret,
)
test_object.json_deserialize({"certificate": certificate})
assert test_object
assert test_object.type == type
assert test_object.secret == secret
assert test_object.certificate == certificate

test_object_1 = Authentication(type=type, certificate=certificate)
test_object_1.json_deserialize({"secret": secret})
assert test_object_1
assert test_object_1.type == type
assert test_object_1.secret == secret
assert test_object_1.certificate == certificate

test_object_2 = Authentication(secret=secret, certificate=certificate)
test_object_2.json_deserialize({"type": type})
assert test_object_2
assert test_object_2.type == type
assert test_object_2.secret == secret
assert test_object_2.certificate == certificate

test_object_2 = Authentication(
secret=secret,
)
test_object_2.json_deserialize({"type": type})
assert test_object_2
assert test_object_2.type == type
assert test_object_2.secret == secret
assert test_object_2.certificate is None
with pytest.raises(WrongFieldError):
assert test_object_2.json_deserialize({"wrong_key": certificate})
3 changes: 0 additions & 3 deletions tests/auth_test/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ def test_decode_token():
"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"]
52 changes: 52 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,55 @@
"HbEeY0F96nioXArdQWXcjUQsTch+p0p9eqh23Ak4ef5oGcZhNd4yp\nY8M6ppvIMiXkgWSPJevCJjhxRJRmndY+ajYGx7CLePx7wN"
"vxXWtkng3yh+7WiZ/Y\nqwIDAQAB\n-----END PUBLIC KEY-----"
)

wrong_auth_result_url = (
"http://fuf.me/?state=46c81f94-d117-4658-9a38-a85692448219&token=eyJhY2NvdW50IjoiMGJhMjRlZWUtYzMwY"
"i00N2U1LWJkYzktNzcwM2NmYjEzNmEwIiwicmVnY29kZSI6IjhlYWNiMTk4ZmMiLCJleHBpcmVzIjoiMjAyMS0wOS0yM1QxNj"
"owODo0My44ODhaIn0%3D&signature=SUL9SQMWAfG4%2FEyT0rejkRfAyioxJIOs4sxI5wxeB8TkIiv0MR6YFKw1tPIkM4ll"
"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BIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvFlVFgYRIkGL6Ay/av2e\n"
"S2yIag7XHRWFgVFewPegFyWjUQPSe5t2unrNOUu6Ucp7/ck/Ivm4c/6g39fDDzmq\n"
"i4JU8OfMpVbUxpiJSGa/OSiXnDuWkJyjdac/C8ip0EpOCFjAWdE+pnGhDny1XAwp\n"
"i4t0/WtO8U+IOYtjxpyyOp3daX97C7ihM1I6eOecVN6Caz9B38EnPg12UGA5NkZO\n"
"pnz4BHMwYUZqgxaeOPlh4MquAnF5fdjOV3TkmFWkbP1un3BJkU6owcadbjN5DQCG\n"
"jguFzX8VVfJEgn2VtIFbbhqsRivvNDmWst1XNZ0GRpviFFQRymz1WroV0lB9P9vK\n"
"mwIDAQAB\n"
"-----END PUBLIC KEY-----"
)
2 changes: 2 additions & 0 deletions tests/enviroments_test/test_environmental_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@


def test_arclient_set_env():
assert EnvironmentalService(ENV)._set_env(ENV) is None
assert EnvironmentalService("Production")._set_env("Production") is None
with pytest.raises(InvalidEnvironmentSetup):
assert EnvironmentalService("WRONG")._set_env("WRONG")
2 changes: 1 addition & 1 deletion tests/messaging_test/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def test_json_serialize():
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"
assert message_request["measures"]
1 change: 0 additions & 1 deletion tests/onboarding_test/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class TestSoftwareOnboardingHeader:
)

def test_get_header(self):
assert isinstance(self.test_object.get_header(), dict)
assert (
self.test_object.get_header()["Authorization"] == "Bearer " + self.reg_code
)
Expand Down
61 changes: 61 additions & 0 deletions tests/onboarding_test/test_onboarding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Test agrirouter/onboarding/onboarding.py"""

from agrirouter.onboarding.exceptions import WrongCertificationType, WrongGateWay
from agrirouter.onboarding.onboarding import SoftwareOnboarding, CUOnboarding
from agrirouter.onboarding.parameters import SoftwareOnboardingParameter
from agrirouter.onboarding.enums import GateWays, CertificateTypes
from tests.constants import public_key, private_key, ENV, application_id
import pytest


class TestSoftwareOnboarding:
def test__create_request(self):
params = SoftwareOnboardingParameter(
id_=1,
application_id=application_id,
content_type="json",
certification_version_id="13",
gateway_id=GateWays.MQTT.value,
certificate_type=CertificateTypes.PEM.value,
utc_timestamp="+03:00",
time_zone="01-01-2021",
reg_code="8eloz190fd",
)
onboarding = SoftwareOnboarding(
public_key=public_key, private_key=private_key, env=ENV
)
assert onboarding._create_request(params, "localhost")

params = SoftwareOnboardingParameter(
id_=2,
application_id=application_id,
content_type="json",
certification_version_id="13",
gateway_id=GateWays.MQTT.value,
certificate_type="wrong_certificate",
utc_timestamp="+03:00",
time_zone="01-01-2021",
reg_code="8eloz190fd",
)
onboarding = SoftwareOnboarding(
public_key=public_key, private_key=private_key, env=ENV
)
with pytest.raises(WrongCertificationType):
assert onboarding._create_request(params, "localhost")

params = SoftwareOnboardingParameter(
id_=3,
application_id=application_id,
content_type="content_type",
certification_version_id="13",
gateway_id="wrong_gateway_id",
certificate_type=CertificateTypes.PEM.value,
utc_timestamp="+03:00",
time_zone="01-01-2021",
reg_code="8eloz190fd",
)
onboarding = SoftwareOnboarding(
public_key=public_key, private_key=private_key, env=ENV
)
with pytest.raises(WrongGateWay):
assert onboarding._create_request(params, "localhost")
51 changes: 51 additions & 0 deletions tests/onboarding_test/test_request_onboarding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Test agrirouter/onboarding/request.py"""

from agrirouter import SoftwareOnboardingParameter, SoftwareOnboarding
from agrirouter.onboarding.enums import GateWays, CertificateTypes
from tests.constants import application_id, public_key, private_key, ENV


class TestBaseOnboardingRequest:
reg_code = "8eloz190fd"
content_type = "json"
certification_version_id = "13"
utc_timestamp = "+03:00"
time_zone = "01-01-2021"
url = "localhost"
params = SoftwareOnboardingParameter(
id_=1,
application_id=application_id,
content_type=content_type,
certification_version_id=certification_version_id,
gateway_id=GateWays.MQTT.value,
certificate_type=CertificateTypes.PEM.value,
utc_timestamp=utc_timestamp,
time_zone=time_zone,
reg_code=reg_code,
)
onboarding = SoftwareOnboarding(
public_key=public_key, private_key=private_key, env=ENV
)
test_object = onboarding._create_request(params, url)

def test_get_url(self):
assert self.test_object.get_url() == self.url

def test_get_data(self):
assert self.test_object.get_data()["applicationId"] == application_id
assert (
self.test_object.get_data()["certificateType"] == CertificateTypes.PEM.value
)
assert (
self.test_object.get_data()["certificateType"] == CertificateTypes.PEM.value
)

def test_get_header(self):
assert (
self.test_object.get_header()["Authorization"] == "Bearer " + self.reg_code
)
assert self.test_object.get_header()["Content-Type"] == self.content_type
assert (
self.test_object.get_header()["X-Agrirouter-ApplicationId"]
== application_id
)
Loading