Skip to content

Commit

Permalink
Test trust registry endpoints (#759)
Browse files Browse the repository at this point in the history
* test trustregistry actors endpoint

* test trust registry schemas endpoint

* is none

* assert is None

* omit test files and tests folders from coverage report

* undo omit

* fix pytest raises logic

* 🎨

* fix typo

* fix with pytest raises logic
  • Loading branch information
cl0ete authored Apr 15, 2024
1 parent 0d658e2 commit 9605066
Show file tree
Hide file tree
Showing 2 changed files with 363 additions and 0 deletions.
177 changes: 177 additions & 0 deletions trustregistry/tests/test_registry_actors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
from unittest.mock import patch

import pytest
from fastapi.exceptions import HTTPException

from shared.models.trustregistry import Actor
from trustregistry.crud import ActorAlreadyExistsException, ActorDoesNotExistException
from trustregistry.registry import registry_actors


@pytest.mark.anyio
async def test_get_actors():
with patch("trustregistry.registry.registry_actors.crud.get_actors") as mock_crud:
actor = Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")
mock_crud.return_value = [actor]
result = await registry_actors.get_actors()
mock_crud.assert_called_once()
assert result == [actor]


@pytest.mark.anyio
async def test_register_actor():
with patch("trustregistry.registry.registry_actors.crud.create_actor") as mock_crud:
actor = Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")
mock_crud.return_value = actor
result = await registry_actors.register_actor(actor)
mock_crud.assert_called_once()
assert result == actor


@pytest.mark.anyio
@pytest.mark.parametrize(
"exception, status_code", [(ActorAlreadyExistsException, 409), (Exception, 500)]
)
async def test_register_actor_x(exception, status_code):

with patch("trustregistry.registry.registry_actors.crud.create_actor") as mock_crud:
actor = Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")
mock_crud.side_effect = exception()
with pytest.raises(HTTPException) as ex:
await registry_actors.register_actor(actor)

mock_crud.assert_called_once()
assert ex.value.status_code == status_code


@pytest.mark.anyio
@pytest.mark.parametrize(
"actor_id, actor",
[
("1", Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")),
("2", Actor(id="1", name="Bob", roles=["role"], did="did:sov:5678")),
],
)
async def test_update_actor(actor_id, actor):
with patch("trustregistry.registry.registry_actors.crud.update_actor") as mock_crud:
actor = Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")
mock_crud.return_value = actor

if actor_id != actor.id:
with pytest.raises(HTTPException) as ex:
await registry_actors.update_actor(actor_id, actor)

mock_crud.assert_not_called()
assert ex.value.status_code == 400

else:
result = await registry_actors.update_actor(actor_id, actor)
mock_crud.assert_called_once()
assert result == actor


@pytest.mark.anyio
async def test_update_actor_x():
with patch("trustregistry.registry.registry_actors.crud.update_actor") as mock_crud:
actor = Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")
mock_crud.side_effect = ActorDoesNotExistException()
with pytest.raises(HTTPException) as ex:
await registry_actors.update_actor("1", actor)

mock_crud.assert_called_once()
assert ex.value.status_code == 404


@pytest.mark.anyio
async def test_get_actor_by_did():
with patch(
"trustregistry.registry.registry_actors.crud.get_actor_by_did"
) as mock_crud:
actor = Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")
mock_crud.return_value = actor
result = await registry_actors.get_actor_by_did("did:sov:1234")
mock_crud.assert_called_once()
assert result == actor


@pytest.mark.anyio
async def test_get_actor_by_did_x():
with patch(
"trustregistry.registry.registry_actors.crud.get_actor_by_did"
) as mock_crud:
mock_crud.side_effect = ActorDoesNotExistException()
with pytest.raises(HTTPException) as ex:
await registry_actors.get_actor_by_did("did:sov:1234")

mock_crud.assert_called_once()
assert ex.value.status_code == 404


@pytest.mark.anyio
async def test_get_actor_by_id():
with patch(
"trustregistry.registry.registry_actors.crud.get_actor_by_id"
) as mock_crud:
actor = Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")
mock_crud.return_value = actor
result = await registry_actors.get_actor_by_id("1")
mock_crud.assert_called_once()
assert result == actor


@pytest.mark.anyio
async def test_get_actor_by_id_x():
with patch(
"trustregistry.registry.registry_actors.crud.get_actor_by_id"
) as mock_crud:
mock_crud.side_effect = ActorDoesNotExistException()
with pytest.raises(HTTPException) as ex:
await registry_actors.get_actor_by_id("1")

mock_crud.assert_called_once()
assert ex.value.status_code == 404


@pytest.mark.anyio
async def test_get_actor_by_name():
with patch(
"trustregistry.registry.registry_actors.crud.get_actor_by_name"
) as mock_crud:
actor = Actor(id="1", name="Alice", roles=["role"], did="did:sov:1234")
mock_crud.return_value = actor
result = await registry_actors.get_actor_by_name("Alice")
mock_crud.assert_called_once()
assert result == actor


@pytest.mark.anyio
async def test_get_actor_by_name_x():
with patch(
"trustregistry.registry.registry_actors.crud.get_actor_by_name"
) as mock_crud:
mock_crud.side_effect = ActorDoesNotExistException()
with pytest.raises(HTTPException) as ex:
await registry_actors.get_actor_by_name("Alice")

mock_crud.assert_called_once()
assert ex.value.status_code == 404


@pytest.mark.anyio
async def test_delete_actor():
with patch("trustregistry.registry.registry_actors.crud.delete_actor") as mock_crud:
mock_crud.return_value = None
result = await registry_actors.remove_actor("1")
mock_crud.assert_called_once()
assert result is None


@pytest.mark.anyio
async def test_delete_actor_x():
with patch("trustregistry.registry.registry_actors.crud.delete_actor") as mock_crud:
mock_crud.side_effect = ActorDoesNotExistException()
with pytest.raises(HTTPException) as ex:
await registry_actors.remove_actor("1")

mock_crud.assert_called_once()
assert ex.value.status_code == 404
186 changes: 186 additions & 0 deletions trustregistry/tests/test_registry_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
from unittest.mock import patch

import pytest
from fastapi.exceptions import HTTPException

from shared.models.trustregistry import Schema
from trustregistry.crud import SchemaAlreadyExistsException, SchemaDoesNotExistException
from trustregistry.registry import registry_schemas


@pytest.mark.anyio
async def test_get_schemas():
with patch("trustregistry.registry.registry_schemas.crud.get_schemas") as mock_crud:
schema = Schema(
did="WgWxqztrNooG92RXvxSTWv",
name="schema_name",
version="1.0",
id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
)
mock_crud.return_value = [schema]
result = await registry_schemas.get_schemas()
mock_crud.assert_called_once()
assert result == [schema]


@pytest.mark.anyio
async def test_register_schema():
with patch(
"trustregistry.registry.registry_schemas.crud.create_schema"
) as mock_crud:
schema_id = registry_schemas.SchemaID(
schema_id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"
)
schema = Schema(
did="WgWxqztrNooG92RXvxSTWv",
name="schema_name",
version="1.0",
id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
)
mock_crud.return_value = schema

result = await registry_schemas.register_schema(schema_id)
mock_crud.assert_called_once()
assert result == schema


@pytest.mark.anyio
async def test_register_schema_x():
with patch(
"trustregistry.registry.registry_schemas.crud.create_schema"
) as mock_crud:
schema_id = registry_schemas.SchemaID(
schema_id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"
)
mock_crud.side_effect = SchemaAlreadyExistsException()
with pytest.raises(HTTPException) as ex:
await registry_schemas.register_schema(schema_id)

mock_crud.assert_called_once()
assert ex.value.status_code == 405


@pytest.mark.anyio
@pytest.mark.parametrize(
"schema_id, new_schema_id",
[
(
"WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
registry_schemas.SchemaID(
schema_id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.1"
),
),
(
"WgWxqztrNooG92RXvxSTWv:2:schema_name:1.1",
registry_schemas.SchemaID(
schema_id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.1"
),
),
],
)
async def test_update_schema(schema_id, new_schema_id):
with patch(
"trustregistry.registry.registry_schemas.crud.update_schema"
) as mock_crud:
schema = Schema(
did="WgWxqztrNooG92RXvxSTWv",
name="schema_name",
version="1.0",
id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
)
if schema_id == new_schema_id.schema_id:
with pytest.raises(HTTPException) as ex:
await registry_schemas.update_schema(schema_id, new_schema_id)

mock_crud.assert_not_called()
assert ex.value.status_code == 400
else:
mock_crud.return_value = schema
result = await registry_schemas.update_schema(schema_id, new_schema_id)
mock_crud.assert_called_once()
assert result == schema


@pytest.mark.anyio
async def test_update_schema_x():
with patch(
"trustregistry.registry.registry_schemas.crud.update_schema"
) as mock_crud:
schema_id = "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"
new_schema_id = registry_schemas.SchemaID(
schema_id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.1"
)
mock_crud.side_effect = SchemaDoesNotExistException()
with pytest.raises(HTTPException) as ex:
await registry_schemas.update_schema(schema_id, new_schema_id)

mock_crud.assert_called_once()
assert ex.value.status_code == 405


@pytest.mark.anyio
async def test_get_schema_by_id():
with patch(
"trustregistry.registry.registry_schemas.crud.get_schema_by_id"
) as mock_crud:
schema = Schema(
did="WgWxqztrNooG92RXvxSTWv",
name="schema_name",
version="1.0",
id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
)
mock_crud.return_value = schema
result = await registry_schemas.get_schema(
"WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"
)
mock_crud.assert_called_once()
assert result == schema


@pytest.mark.anyio
async def test_get_schema_by_id_x():
with patch(
"trustregistry.registry.registry_schemas.crud.get_schema_by_id"
) as mock_crud:
mock_crud.side_effect = SchemaDoesNotExistException()
with pytest.raises(HTTPException) as ex:
await registry_schemas.get_schema(
"WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"
)

mock_crud.assert_called_once()
assert ex.value.status_code == 404


@pytest.mark.anyio
async def test_remove_schema():
with patch(
"trustregistry.registry.registry_schemas.crud.delete_schema"
) as mock_crud:
schema = Schema(
did="WgWxqztrNooG92RXvxSTWv",
name="schema_name",
version="1.0",
id="WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
)
mock_crud.return_value = schema
result = await registry_schemas.remove_schema(
"WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"
)
mock_crud.assert_called_once()
assert result is None


@pytest.mark.anyio
async def test_remove_schema_x():
with patch(
"trustregistry.registry.registry_schemas.crud.delete_schema"
) as mock_crud:
mock_crud.side_effect = SchemaDoesNotExistException()
with pytest.raises(HTTPException) as ex:
await registry_schemas.remove_schema(
"WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"
)

mock_crud.assert_called_once()
assert ex.value.status_code == 404

0 comments on commit 9605066

Please sign in to comment.