Skip to content

Commit

Permalink
Add type checks for tests
Browse files Browse the repository at this point in the history
Fixes: #70
  • Loading branch information
robin-nitrokey committed Nov 17, 2023
1 parent 0eba835 commit 57cd237
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 199 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ check-style:

check-typing:
@echo "Note: run semi-clean target in case this fails without any proper reason"
$(PYTHON3_VENV) -m mypy $(PACKAGE_NAME)/
$(PYTHON3_VENV) -m mypy $(PACKAGE_NAME)/ tests/

check: check-format check-import-sorting check-style check-typing test

Expand Down
60 changes: 30 additions & 30 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
from dataclasses import dataclass
from os import environ
from typing import Literal


@dataclass
class UserData:
user_id: str
real_name: str
role: Literal["Administrator", "Operator", "Metrics", "Backup"]


class Constants:
Expand Down Expand Up @@ -60,12 +69,12 @@ class Constants:
PORT = 514
NETMASK = "255.255.255.0"
GATEWAY = "0.0.0.0"
UNATTENDED_BOOT_OFF = "off"
UNATTENDED_BOOT_ON = "on"
LOG_LEVEL = "info"
UNATTENDED_BOOT_OFF: Literal["off"] = "off"
UNATTENDED_BOOT_ON: Literal["on"] = "on"
LOG_LEVEL: Literal["info"] = "info"

# test_nethsm_keys
TYPE = "RSA"
TYPE: Literal["RSA"] = "RSA"
MECHANISM = [
"RSA_Signature_PKCS1",
"RSA_Decryption_PKCS1",
Expand All @@ -77,40 +86,31 @@ class Constants:
KEY_ID_GENERATED = "KeyIdGenerated"
KEY_ID_AES = "KeyIdAES"
DATA = "Test data 123456"
MODE = "PKCS1"
MODE: Literal["PKCS1"] = "PKCS1"
# 'PKCS1', 'PSS_MD5', 'PSS_SHA1', 'PSS_SHA224', 'PSS_SHA256', 'PSS_SHA384', 'PSS_SHA512', 'EdDSA', 'ECDSA'
# test_nethsm_users, test_nethsm_keys
TAG1 = "Frankfurt"
TAG2 = "Berlin"
TAG3 = "Teltow"
TAGS = [TAG1, TAG2, TAG3]

class AdminUser:
USER_ID = "admin"
REAL_NAME = "admin"
ROLE = "Administrator"

class AdministratorUser:
USER_ID = "UIAdministrator"
REAL_NAME = "RNAdministrator"
ROLE = "Administrator"

class OperatorUser:
USER_ID = "UIOperator"
REAL_NAME = "RNOperator"
ROLE = "Operator"

class MetricsUser:
USER_ID = "UIMetrics"
REAL_NAME = "RNMetrics"
ROLE = "Metrics"

class BackupUser:
USER_ID = "UIBackup"
REAL_NAME = "RNBackup"
ROLE = "Backup"
ADMIN_USER = UserData(user_id="admin", real_name="admin", role="Administrator")
ADMINISTRATOR_USER = UserData(
user_id="UIAdministrator", real_name="RNAdministrator", role="Administrator"
)
OPERATOR_USER = UserData(
user_id="UIOperator", real_name="RNOperator", role="Operator"
)
METRICS_USER = UserData(user_id="UIMetrics", real_name="RNMetrics", role="Metrics")
BACKUP_USER = UserData(user_id="UIBackup", real_name="RNBackup", role="Backup")

DETAILS = ""
USERS_LIST = [AdministratorUser, BackupUser, MetricsUser, OperatorUser, AdminUser]
USERS_LIST = [
ADMINISTRATOR_USER,
BACKUP_USER,
METRICS_USER,
OPERATOR_USER,
ADMIN_USER,
]

# nitropy nethsm --host nethsmdemo.nitrokey.com --no-verify-tls info
42 changes: 21 additions & 21 deletions tests/test_nethsm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from utilities import lock, nethsm, self_sign_csr, unlock # noqa: F401

import nethsm as nethsm_module
from nethsm import NetHSM

"""########## Preparation for the Tests ##########
Expand All @@ -16,21 +17,21 @@
"""


def get_config_logging(nethsm):
def get_config_logging(nethsm: NetHSM) -> None:
data = nethsm.get_config_logging()
assert data.ip_address == C.IP_ADDRESS_LOGGING
assert data.port == C.PORT
assert data.log_level.value == C.LOG_LEVEL


def get_config_network(nethsm):
def get_config_network(nethsm: NetHSM) -> None:
data = nethsm.get_config_network()
assert data.ip_address == C.IP_ADDRESS_NETWORK
assert data.netmask == C.NETMASK
assert data.gateway == C.GATEWAY


def get_config_time(nethsm):
def get_config_time(nethsm: NetHSM) -> None:
dt_nethsm = datetime.datetime.strptime(
nethsm.get_config_time(), "%Y-%m-%dT%H:%M:%SZ"
).replace(tzinfo=datetime.timezone.utc)
Expand All @@ -48,7 +49,7 @@ def get_config_time(nethsm):
"""##########Start of Tests##########"""


def test_csr(nethsm):
def test_csr(nethsm: NetHSM) -> None:
csr = nethsm.csr(
C.COUNTRY,
C.STATE_OR_PROVINCE,
Expand All @@ -61,7 +62,7 @@ def test_csr(nethsm):
print(csr)


def test_set_certificate(nethsm: nethsm_module.NetHSM) -> None:
def test_set_certificate(nethsm: NetHSM) -> None:

csr = nethsm.csr(
C.COUNTRY,
Expand All @@ -73,18 +74,17 @@ def test_set_certificate(nethsm: nethsm_module.NetHSM) -> None:
C.EMAIL_ADDRESS,
)
cert = self_sign_csr(csr)
nethsm.set_certificate(BytesIO(cert))
nethsm.set_certificate(BytesIO(cert)) # type: ignore

remote_cert = nethsm.get_certificate()
assert cert.decode("utf-8") == remote_cert


def generate_tls_key(nethsm):
resp = nethsm.generate_tls_key("RSA", 2048)
print(resp)
def generate_tls_key(nethsm: NetHSM) -> None:
nethsm.generate_tls_key("RSA", 2048)


def test_get_config_logging(nethsm):
def test_get_config_logging(nethsm: NetHSM) -> None:
"""Query the configuration of a NetHSM.
For logging
Expand All @@ -94,7 +94,7 @@ def test_get_config_logging(nethsm):
get_config_logging(nethsm)


def test_get_config_network(nethsm):
def test_get_config_network(nethsm: NetHSM) -> None:
"""Query the configuration of a NetHSM.
For network
Expand All @@ -104,7 +104,7 @@ def test_get_config_network(nethsm):
get_config_logging(nethsm)


def test_get_config_time(nethsm):
def test_get_config_time(nethsm: NetHSM) -> None:
"""Query the configuration of a NetHSM.
For time
Expand All @@ -115,7 +115,7 @@ def test_get_config_time(nethsm):
get_config_time(nethsm)


def test_get_config_unattended_boot(nethsm):
def test_get_config_unattended_boot(nethsm: NetHSM) -> None:
"""Query the configuration of a NetHSM.
For unattended boot
Expand All @@ -129,7 +129,7 @@ def test_get_config_unattended_boot(nethsm):
)


def test_get_config_get_public_key(nethsm):
def test_get_config_get_public_key(nethsm: NetHSM) -> None:
"""Query the configuration of a NetHSM.
For get public key
Expand All @@ -144,7 +144,7 @@ def test_get_config_get_public_key(nethsm):
assert str_end == "-----END PUBLIC KEY-----"


def test_get_config_get_certificate(nethsm):
def test_get_config_get_certificate(nethsm: NetHSM) -> None:
"""Query the configuration of a NetHSM.
For get certificate
Expand All @@ -159,7 +159,7 @@ def test_get_config_get_certificate(nethsm):
assert str_end == "-----END CERTIFICATE-----"


def test_set_backup_passphrase(nethsm):
def test_set_backup_passphrase(nethsm: NetHSM) -> None:
"""Set the backup passphrase of a NetHSM.
This command requires authentication as a user with the Administrator
Expand All @@ -175,7 +175,7 @@ def test_set_backup_passphrase(nethsm):


# @pytest.mark.skip(reason="not finished yet")
def test_set_get_logging_config(nethsm):
def test_set_get_logging_config(nethsm: NetHSM) -> None:
"""Set the logging configuration of a NetHSM.
This command requires authentication as a user with the Administrator
Expand All @@ -188,7 +188,7 @@ def test_set_get_logging_config(nethsm):


# @pytest.mark.skip(reason="not finished yet")
def test_set_get_network_config(nethsm):
def test_set_get_network_config(nethsm: NetHSM) -> None:
"""Set the network configuration of a NetHSM.
This command requires authentication as a user with the Administrator
Expand All @@ -200,7 +200,7 @@ def test_set_get_network_config(nethsm):
get_config_network(nethsm)


def test_set_get_time(nethsm):
def test_set_get_time(nethsm: NetHSM) -> None:
"""Set the system time of a NetHSM.
If the time is not given as an argument, the system time of this system
Expand All @@ -213,7 +213,7 @@ def test_set_get_time(nethsm):
get_config_time(nethsm)


def test_set_get_unattended_boot(nethsm):
def test_set_get_unattended_boot(nethsm: NetHSM) -> None:
"""Set the unattended boot configuration of a NetHSM.
This command requires authentication as a user with the Administrator
Expand All @@ -234,7 +234,7 @@ def test_set_get_unattended_boot(nethsm):
assert str(nethsm.get_config_unattended_boot()) == C.UNATTENDED_BOOT_ON


def test_set_unlock_passphrase_lock_unlock(nethsm):
def test_set_unlock_passphrase_lock_unlock(nethsm: NetHSM) -> None:
"""Set the unlock passphrase of a NetHSM.
This command requires authentication as a user with the Administrator
Expand Down
Loading

0 comments on commit 57cd237

Please sign in to comment.