Skip to content

Commit

Permalink
added py tests and updated version of hvac dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
obervinov committed May 15, 2023
1 parent 8c64995 commit 9298f1c
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 165 deletions.
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.6.2"
hvac = "^1.0.2"
logger = { git = "https://github.com/obervinov/logger-package.git", tag = "v1.0.1" }
hvac = "^1.1.0"
logger = { git = "https://github.com/obervinov/logger-package.git", tag = "v1.0.1" }

[tool.pytest.ini_options]
pythonpath = ["."]
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
hvac==1.0.2
hvac==1.1.0
git+https://github.com/obervinov/logger-package.git@v1.0.1#egg=logger
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
],
keywords=['vault', 'client'],
install_requires=[
'hvac==1.0.2'
'hvac==1.1.0'
],
dependency_links=[
'https://github.com/obervinov/logger-package/tarball/master#egg=logger-1.0.1'
Expand Down
81 changes: 81 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
This module stores fixtures for performing tests.
"""
import pytest
from vault.vault import VaultClient


@pytest.fixture(name="name")
def fixture_name():
"""Returns the project name."""
return "testapp-1"


@pytest.fixture(name="policy_path")
def fixture_policy_path():
"""Returns the policy path."""
return "tests/vault/policy.hcl"


@pytest.fixture(name="test_data")
def fixture_test_data():
"""Returns test data for the module."""
return {
'username': 'user1',
'password': 'qwerty',
'url': 'https://very-important-site.example.com'
}


@pytest.fixture(name="test_path")
def fixture_test_path():
"""Returns test secret path."""
return "configuration/mysecret"


@pytest.fixture(name="configurator_client")
def fixture_configurator_client(name, policy_path):
"""Returns client of the configurator"""
return VaultClient(
url='http://0.0.0.0:8200',
name=name,
policy=policy_path,
new=True
)


@pytest.fixture(name="secrets_client")
def fixture_secrets_client(approle, name):
"""Returns the client of the secrets."""
return VaultClient(
url='http://0.0.0.0:8200',
name=name,
approle=approle
)


@pytest.fixture(name="namespace")
def fixture_namespace(configurator_client, name):
"""Returns the namespace."""
return configurator_client.create_namespace(
name=name
)


@pytest.fixture(name="policy")
def fixture_policy(configurator_client, policy_path, name):
"""Returns the policy path."""
return configurator_client.create_policy(
name=name,
path=policy_path
)


@pytest.fixture(name="approle")
def fixture_approle(configurator_client, name, policy):
"""Returns the approle data."""
return configurator_client.create_approle(
name=name,
path=name,
policy=policy
)
33 changes: 33 additions & 0 deletions tests/test_configurator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
This test is necessary to check how the module works with the configuration of the vault instance.
"""
import pytest


@pytest.mark.order(1)
def test_create_namespace(namespace, name):
"""
Testing the creation of a new namespace.
"""
assert namespace == name
assert isinstance(namespace, str)


@pytest.mark.order(2)
def test_create_policy(policy):
"""
Testing the creation of a new policy.
"""
assert policy is not None
assert isinstance(policy, str)


@pytest.mark.order(3)
def test_create_approle(approle):
"""
Testing the creation of a new approle.
"""
assert isinstance(approle, dict)
assert approle['id'] is not None
assert approle['secret-id'] is not None
assert approle['mount-point'] is not None
53 changes: 53 additions & 0 deletions tests/test_secrets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
This test is necessary to check how the module works with the secrets of the vault instance.
"""
import pytest


@pytest.mark.order(4)
def test_client_secret(secrets_client):
"""
Testing the client of the vault
"""
assert secrets_client is not None
assert secrets_client.client is not None


@pytest.mark.order(5)
def test_write_secret(secrets_client, test_data, test_path):
"""
Testing writing a secret to the vault
"""
for key, value in test_data.items():
response = secrets_client.write_secret(
path=test_path,
key=key,
value=value
)
assert response['request_id']


@pytest.mark.order(6)
def test_read_secret(secrets_client, test_data, test_path):
"""
Testing reading a secret to the vault
"""
for key, value in test_data.items():
response = secrets_client.read_secret(
path=test_path,
key=key
)
assert response == value
assert isinstance(response, (dict, str))


@pytest.mark.order(5)
def test_list_secrets(secrets_client, test_path):
"""
Testing checks the reading of the list of secrets from the vault
"""
response = secrets_client.list_secrets(
path=f"{test_path.split('/')[0]}/"
)
assert f"{test_path.split('/')[1]}" in response
assert isinstance(response, list)
18 changes: 18 additions & 0 deletions tests/vault/policy.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
path "auth/token/lookup" {
capabilities = ["read"]
}
path "auth/token/renew" {
capabilities = ["update"]
}
path "auth/token/revoke" {
capabilities = ["update"]
}
path "testapp-1/config" {
capabilities = ["read", "list", "update"]
}
path "testapp-1/metadata/configuration/*" {
capabilities = ["read", "list"]
}
path "testapp-1/data/configuration/*" {
capabilities = ["create", "read", "update", "list"]
}
Loading

0 comments on commit 9298f1c

Please sign in to comment.