Skip to content

Commit

Permalink
✅ Chore: add tests of network, secret, staticroute
Browse files Browse the repository at this point in the history
  • Loading branch information
guptadev21 committed Nov 26, 2024
1 parent 5cf904e commit 98c11c3
Show file tree
Hide file tree
Showing 5 changed files with 613 additions and 2 deletions.
174 changes: 174 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import httpx
import pytest
from munch import Munch
from pytest_mock import MockerFixture

from tests.utils.test_util import client, network_body # noqa: F401


def test_list_networks_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.get method
mock_get = mocker.patch("httpx.Client.get")

# Set up the mock responses for pagination
mock_get.return_value = httpx.Response(
status_code=200,
json={
"metadata": {"continue": 1},
"items": [{"name": "test-network", "guid": "mock_network_guid"}],
},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
k: v
for k, v in {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}.items()
if v is not None
}

# Call the list_networks method
response = client.list_networks()

# Validate the response
assert isinstance(response, Munch)
assert response["items"] == [{"name": "test-network", "guid": "mock_network_guid"}]


def test_list_networks_not_found(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.get method
mock_get = mocker.patch("httpx.Client.get")

# Set up the mock response
mock_get.return_value = httpx.Response(
status_code=404,
json={"error": "not found"},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}

with pytest.raises(Exception) as exc:
client.list_networks()

assert str(exc.value) == "not found"


def test_create_network_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.post method
mock_post = mocker.patch("httpx.Client.post")

# Set up the mock response
mock_post.return_value = httpx.Response(
status_code=201,
json={
"metadata": {"guid": "mock_network_guid", "name": "test-network"},
},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
k: v
for k, v in {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}.items()
if v is not None
}

# Call the create_network method
response = client.create_network(body=network_body)

# Validate the response
assert isinstance(response, Munch)
assert response["metadata"]["name"] == "test-network"


def test_create_network_failure(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.post method
mock_post = mocker.patch("httpx.Client.post")

# Set up the mock response
mock_post.return_value = httpx.Response(
status_code=409,
json={"error": "already exists"},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}

with pytest.raises(Exception) as exc:
client.create_network(body=network_body)

assert str(exc.value) == "already exists"


def test_get_network_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.get method
mock_get = mocker.patch("httpx.Client.get")

# Set up the mock response
mock_get.return_value = httpx.Response(
status_code=200,
json={
"metadata": {"guid": "mock_network_guid", "name": "test-network"},
},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
k: v
for k, v in {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}.items()
if v is not None
}

# Call the get_network method
response = client.get_network(name="test-network")

# Validate the response
assert isinstance(response, Munch)
assert response["metadata"]["guid"] == "mock_network_guid"


def test_delete_network_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.delete method
mock_delete = mocker.patch("httpx.Client.delete")

# Set up the mock response
mock_delete.return_value = httpx.Response(
status_code=204,
json={"success": True},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
k: v
for k, v in {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}.items()
if v is not None
}

# Call the delete_network method
response = client.delete_network(name="test-network")

# Validate the response
assert response["success"] is True
4 changes: 2 additions & 2 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from pytest_mock import MockerFixture

from tests.utils.test_util import (
client,
mock_response_project,
client, # noqa: F401
mock_response_project, # noqa: F401
project_body,
) # noqa: F401

Expand Down
190 changes: 190 additions & 0 deletions tests/test_secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import httpx
import pytest
from munch import Munch
from pytest_mock import MockerFixture

from tests.utils.test_util import client, secret_body # noqa: F401


def test_list_secrets_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.get method
mock_get = mocker.patch("httpx.Client.get")

# Set up mock responses for pagination
mock_get.return_value = httpx.Response(
status_code=200,
json={
"metadata": {"continue": 1},
"items": [{"name": "test-secret", "guid": "mock_secret_guid"}],
},
)

# Override get_headers to return mocked headers without None values
client.config.get_headers = lambda with_project=True: {
k: v
for k, v in {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}.items()
if v is not None
}

# Call the list_secrets method
response = client.list_secrets()

# Validate the response
assert isinstance(response, Munch)
assert response["items"] == [{"name": "test-secret", "guid": "mock_secret_guid"}]


def test_list_secrets_not_found(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.get method
mock_get = mocker.patch("httpx.Client.get")

# Set up the mock response
mock_get.return_value = httpx.Response(
status_code=404,
json={"error": "not found"},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}

with pytest.raises(Exception) as exc:
client.list_secrets()

assert str(exc.value) == "not found"


def test_create_secret_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.post method
mock_post = mocker.patch("httpx.Client.post")

# Set up the mock response
mock_post.return_value = httpx.Response(
status_code=201,
json={
"metadata": {"guid": "test_secret_guid", "name": "test_secret"},
},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}

# Call the create_secret method
response = client.create_secret(secret_body)

# Validate the response
assert isinstance(response, Munch)
assert response.metadata.guid == "test_secret_guid"


def test_create_secret_already_exists(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.post method
mock_post = mocker.patch("httpx.Client.post")

# Set up the mock response
mock_post.return_value = httpx.Response(
status_code=409,
json={"error": "secret already exists"},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}

with pytest.raises(Exception) as exc:
client.create_secret(secret_body)

assert str(exc.value) == "secret already exists"


def test_update_secret_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.put method
mock_put = mocker.patch("httpx.Client.put")

# Set up the mock response
mock_put.return_value = httpx.Response(
status_code=200,
json={
"metadata": {"guid": "test_secret_guid", "name": "test_secret"},
},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}

# Call the update_secret method
response = client.update_secret("mock_secret_guid", body=secret_body)

# Validate the response
assert isinstance(response, Munch)
assert response.metadata.guid == "test_secret_guid"


def test_delete_secret_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.delete method
mock_delete = mocker.patch("httpx.Client.delete")

# Set up the mock response
mock_delete.return_value = httpx.Response(
status_code=204,
json={"success": True},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}

# Call the delete_secret method
response = client.delete_secret("mock_secret_guid")

# Validate the response
assert response == {"success": True}


def test_get_secret_success(client, mocker: MockerFixture): # noqa: F811
# Mock the httpx.Client.get method
mock_get = mocker.patch("httpx.Client.get")

# Set up the mock response
mock_get.return_value = httpx.Response(
status_code=200,
json={
"metadata": {"guid": "test_secret_guid", "name": "test_secret"},
},
)

# Override get_headers to return the mocked headers without None values
client.config.get_headers = lambda with_project=True: {
"Authorization": f"Bearer {client.auth_token}",
"organizationguid": client.organization_guid,
"project": "mock_project_guid" if with_project else None,
}

# Call the get_secret method
response = client.get_secret("mock_secret_guid")

# Validate the response
assert isinstance(response, Munch)
assert response.metadata.guid == "test_secret_guid"
assert response.metadata.name == "test_secret"
Loading

0 comments on commit 98c11c3

Please sign in to comment.