Skip to content

Commit

Permalink
tests: Move fixture into conftest.py
Browse files Browse the repository at this point in the history
This ensures that it is available in all test modules without importing
the unused function, see:
        https://docs.pytest.org/en/7.1.x/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files
  • Loading branch information
robin-nitrokey committed Apr 29, 2024
1 parent 263cbb9 commit 6f23f13
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 27 deletions.
24 changes: 22 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from dataclasses import dataclass
from os import environ
from typing import Literal
from typing import Iterator, Literal

import pytest

from nethsm import (
DecryptMode,
KeyMechanism,
KeyType,
LogLevel,
NetHSM,
Role,
UnattendedBootStatus,
)
Expand Down Expand Up @@ -122,4 +125,21 @@ class Constants:
ADMIN_USER,
]

# nitropy nethsm --host nethsmdemo.nitrokey.com --no-verify-tls info

@pytest.fixture(scope="module")
def nethsm() -> Iterator[NetHSM]:
"""Start Docker container with Nethsm image and connect to Nethsm
This Pytest Fixture will run before the tests to provide the tests with
a nethsm instance via Docker container, also the first provision of the
NetHSM will be done in here"""

from utilities import connect, provision, start_nethsm

container = start_nethsm()

with connect(Constants.ADMIN_USER) as nethsm:
provision(nethsm)
yield nethsm

container.kill()
2 changes: 1 addition & 1 deletion tests/test_nethsm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
from conftest import Constants as C
from utilities import lock, nethsm, self_sign_csr, unlock # noqa: F401
from utilities import lock, self_sign_csr, unlock

import nethsm as nethsm_module
from nethsm import NetHSM, TlsKeyType
Expand Down
1 change: 0 additions & 1 deletion tests/test_nethsm_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from utilities import nethsm # noqa: F401
from utilities import (
add_user,
connect,
Expand Down
1 change: 0 additions & 1 deletion tests/test_nethsm_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import docker # type: ignore
import pytest
from conftest import Constants as C
from utilities import nethsm # noqa: F401
from utilities import add_user, connect, lock, provision, start_nethsm, unlock

import nethsm as nethsm_sdk
Expand Down
1 change: 0 additions & 1 deletion tests/test_nethsm_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest
from conftest import Constants as C
from test_nethsm_keys import generate_key
from utilities import nethsm # noqa: F401
from utilities import (
add_user,
connect,
Expand Down
1 change: 0 additions & 1 deletion tests/test_nethsm_users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
from conftest import Constants as C
from conftest import UserData
from utilities import nethsm # noqa: F401
from utilities import add_user, connect

import nethsm as nethsm_module
Expand Down
20 changes: 0 additions & 20 deletions tests/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,6 @@
from nethsm import Authentication, Base64, NetHSM, RsaPrivateKey


@pytest.fixture(scope="module")
def nethsm() -> Iterator[NetHSM]:
"""Start Docker container with Nethsm image and connect to Nethsm
This Pytest Fixture will run before the tests to provide the tests with
a nethsm instance via Docker container, also the first provision of the
NetHSM will be done in here"""

container = start_nethsm()

with connect(C.ADMIN_USER) as nethsm:
provision(nethsm)
yield nethsm

try:
container.kill()
except docker.errors.APIError:
pass


class KeyfenderManager:
def __init__(self) -> None:
pass
Expand Down

0 comments on commit 6f23f13

Please sign in to comment.