Skip to content

Commit

Permalink
test: add tests for routes
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Nov 16, 2023
1 parent dc6bfe9 commit 49757f3
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
7 changes: 5 additions & 2 deletions oid4vci/oid4vci/v1_0/models/supported_cred.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,18 @@ def to_issuer_metadata(self) -> dict:
the spec).
"""
issuer_metadata = {
prop: getattr(self, prop)
prop: value
for prop in (
"format",
"identifier",
"cryptographic_binding_methods_supported",
"cryptographic_suites_supported",
"display",
)
if (value := getattr(self, prop)) is not None
}

issuer_metadata["id"] = self.identifier

# Flatten the format specific metadata into the object
issuer_metadata = {
**issuer_metadata,
Expand Down
3 changes: 2 additions & 1 deletion oid4vci/oid4vci/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,18 @@ async def credential_supported_create(request: web.Request):

body: Dict[str, Any] = await request.json()
LOGGER.info(f"body: {body}")
print(body)
known = {
k: body.pop(k)
for k in (
"format",
"identifier",
"cryptographic_binding_methods_supported",
"cryptographic_suites_supported",
"display",
)
if k in body
}
known["identifier"] = body.pop("id")
format_specific = body
LOGGER.info(f"format_data: {format_specific}")
LOGGER.info(f"known: {known}")
Expand Down
9 changes: 9 additions & 0 deletions oid4vci/tests/models/test_supported_cred.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ async def test_save(profile: Profile, record: SupportedCredential):
session, record.supported_cred_id
)
assert loaded == record


def test_to_issuer_metadata(record: SupportedCredential):
assert record.to_issuer_metadata() == {
"format": "jwt_vc_json",
"id": "MyCredential",
"cryptographic_suites_supported": ["EdDSA"],
"credentialSubject": {"name": "alice"},
}
Empty file.
18 changes: 18 additions & 0 deletions oid4vci/tests/routes/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from aries_cloudagent.admin.request_context import AdminRequestContext
import pytest
from unittest.mock import MagicMock


@pytest.fixture
def context():
"""Test AdminRequestContext."""
yield AdminRequestContext.test_context()


@pytest.fixture
def req(context: AdminRequestContext):
"""Test web.Request."""
items = {"context": context}
mock = MagicMock()
mock.__getitem__ = lambda _, k: items[k]
yield mock
42 changes: 42 additions & 0 deletions oid4vci/tests/routes/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from aiohttp import web
from unittest.mock import AsyncMock
from aries_cloudagent.admin.request_context import AdminRequestContext
from oid4vci.v1_0 import routes as test_module
import pytest

from oid4vci.v1_0.models.supported_cred import SupportedCredential


@pytest.mark.asyncio
async def test_credential_supported_create(
context: AdminRequestContext, req: web.Request
):
"""Test credential_supported_create endpoint."""
req.json = AsyncMock(
return_value={
"format": "jwt_vc_json",
"id": "MyCredential",
"credentialSubject": {"name": "alice"},
"type": ["VerifiableCredential", "MyCredential"],
"cryptographic_binding_methods_supported": ["proof"],
"cryptographic_suites_supported": ["ES256"],
"display": {"some nonsense": "here"},
}
)

await test_module.credential_supported_create(req)

async with context.session() as session:
records = await SupportedCredential.query(
session, {"identifier": "MyCredential"}
)

assert records
record = records[0]
assert record
assert record.format == "jwt_vc_json"
assert record.identifier == "MyCredential"
assert record.format_data == {
"credentialSubject": {"name": "alice"},
"type": ["VerifiableCredential", "MyCredential"],
}
40 changes: 40 additions & 0 deletions oid4vci/tests/routes/test_public_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest.mock import patch

from aiohttp import web
from aries_cloudagent.admin.request_context import AdminRequestContext
import pytest

from oid4vci.v1_0 import public_routes as test_module


@pytest.mark.asyncio
async def test_issuer_metadata(context: AdminRequestContext, req: web.Request):
"""Test issuer metadata endpoint."""
supported = test_module.SupportedCredential(
format="jwt_vc_json",
identifier="MyCredential",
format_data={
"credentialSubject": {"name": "alice"},
},
)

async with context.session() as session:
await supported.save(session)

with patch.object(
test_module, "OID4VCI_ENDPOINT", "http://localhost:8020"
), patch.object(test_module, "web", autospec=True) as mock_web:
await test_module.oid_cred_issuer(req)
mock_web.json_response.assert_called_once_with(
{
"credential_issuer": "http://localhost:8020/",
"credential_endpoint": "http://localhost:8020/credential",
"credentials_supported": [
{
"format": "jwt_vc_json",
"id": "MyCredential",
"credentialSubject": {"name": "alice"},
}
],
}
)

0 comments on commit 49757f3

Please sign in to comment.