Skip to content

Commit

Permalink
Merge branch 'master' into demo-tails-server
Browse files Browse the repository at this point in the history
  • Loading branch information
swcurran authored May 11, 2020
2 parents 7149abf + e81a2b8 commit c214642
Show file tree
Hide file tree
Showing 33 changed files with 1,158 additions and 210 deletions.
2 changes: 1 addition & 1 deletion aries_cloudagent/config/default_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async def bind_providers(self, context: InjectionContext):
BaseVerifier,
ClassProvider(
"aries_cloudagent.verifier.indy.IndyVerifier",
ClassProvider.Inject(BaseWallet),
ClassProvider.Inject(BaseLedger),
),
)

Expand Down
Empty file.
118 changes: 118 additions & 0 deletions aries_cloudagent/messaging/credential_definitions/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock

from aiohttp import web as aio_web

from ....config.injection_context import InjectionContext
from ....issuer.base import BaseIssuer
from ....ledger.base import BaseLedger
from ....storage.base import BaseStorage
from ....messaging.request_context import RequestContext

from .. import routes as test_module


SCHEMA_ID = "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"
CRED_DEF_ID = "WgWxqztrNooG92RXvxSTWv:3:CL:20:tag"


class TestCredentialDefinitionRoutes(AsyncTestCase):
def setUp(self):
self.context = InjectionContext(enforce_typing=False)

self.ledger = async_mock.create_autospec(BaseLedger)
self.ledger.__aenter__ = async_mock.CoroutineMock(return_value=self.ledger)
self.ledger.create_and_send_credential_definition = async_mock.CoroutineMock(
return_value=(CRED_DEF_ID, {"cred": "def"})
)
self.ledger.get_credential_definition = async_mock.CoroutineMock(
return_value={"cred": "def"}
)
self.context.injector.bind_instance(BaseLedger, self.ledger)

self.issuer = async_mock.create_autospec(BaseIssuer)
self.context.injector.bind_instance(BaseIssuer, self.issuer)

self.storage = async_mock.create_autospec(BaseStorage)
self.storage.search_records = async_mock.MagicMock(
return_value=async_mock.MagicMock(
fetch_all=async_mock.CoroutineMock(
return_value=[async_mock.MagicMock(value=CRED_DEF_ID)]
)
)
)
self.context.injector.bind_instance(BaseStorage, self.storage)

self.app = {
"request_context": self.context,
}

async def test_send_credential_definition(self):
mock_request = async_mock.MagicMock(
app=self.app,
json=async_mock.CoroutineMock(
return_value={
"schema_id": "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
"support_revocation": False,
"tag": "tag",
}
),
)

with async_mock.patch.object(test_module.web, "json_response") as mock_response:
result = await test_module.credential_definitions_send_credential_definition(
mock_request
)
assert result == mock_response.return_value
mock_response.assert_called_once_with(
{"credential_definition_id": CRED_DEF_ID}
)

async def test_created(self):
mock_request = async_mock.MagicMock(
app=self.app,
json=async_mock.CoroutineMock(
return_value={
"schema_id": "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
"support_revocation": False,
"tag": "tag",
}
),
match_info={"cred_def_id": CRED_DEF_ID},
)

with async_mock.patch.object(test_module.web, "json_response") as mock_response:
result = await test_module.credential_definitions_created(mock_request)
assert result == mock_response.return_value
mock_response.assert_called_once_with(
{"credential_definition_ids": [CRED_DEF_ID]}
)

async def test_get_credential_definition(self):
mock_request = async_mock.MagicMock(
app=self.app,
json=async_mock.CoroutineMock(
return_value={
"schema_id": "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0",
"support_revocation": False,
"tag": "tag",
}
),
match_info={"cred_def_id": CRED_DEF_ID},
)

with async_mock.patch.object(test_module.web, "json_response") as mock_response:
result = await test_module.credential_definitions_get_credential_definition(
mock_request
)
assert result == mock_response.return_value
mock_response.assert_called_once_with(
{"credential_definition": {"cred": "def"}}
)

async def test_register(self):
mock_app = async_mock.MagicMock()
mock_app.add_routes = async_mock.MagicMock()

await test_module.register(mock_app)
mock_app.add_routes.assert_called_once()
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
catalogs: A list of URLs for localization resources
"""
super(LocalizationDecorator, self).__init__()
super().__init__()
self.locale = locale
self.localizable = list(localizable) if localizable else []
self.catalogs = list(catalogs) if catalogs else []
Expand Down
72 changes: 72 additions & 0 deletions aries_cloudagent/messaging/decorators/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json
import pytest
import uuid

from copy import deepcopy
from datetime import datetime, timezone
from time import time
from unittest import TestCase

from marshmallow import fields

from ....messaging.models.base import BaseModel, BaseModelSchema

from ..base import BaseDecoratorSet, DECORATOR_PREFIX


class SampleDecorator(BaseModel):
"""Sample model for base decorator tests."""

class Meta:
"""Sample decorator metadata."""

schema_class = "SampleDecoratorSchema"

def __init__(self, score: int, **kwargs):
"""Initializer."""
super().__init__(**kwargs)
self.score = score


class SampleDecoratorSchema(BaseModelSchema):
"""Sample schema decorator for base decorator tests."""

class Meta:
model_class = SampleDecorator

score = fields.Int(required=True)


class TestBaseDecoratorSet(TestCase):
def test_base_decorator_set(self):
MODELS = {"a": SampleDecorator}
deco_set = BaseDecoratorSet(MODELS)
assert type(deco_set) == BaseDecoratorSet
assert not deco_set.fields
assert deco_set.models == MODELS
assert deco_set.prefix == DECORATOR_PREFIX
assert BaseDecoratorSet.__name__ in str(deco_set)

deco_set_copy = deco_set.copy()
assert type(deco_set_copy) == BaseDecoratorSet
assert not deco_set_copy.fields
assert deco_set_copy.models == MODELS
assert deco_set_copy.prefix == DECORATOR_PREFIX

assert not deco_set.has_field("x")
deco_set.field("x")
assert not deco_set.has_field("x") # empty
assert not len(deco_set.field("x"))
deco_set.remove_field("x")
assert not deco_set.has_field("x")

deco_set.add_model("c", SampleDecorator)
assert "c" in deco_set.models
deco_set.remove_model("c")
assert "c" not in deco_set.models

with pytest.raises(ValueError):
deco_set["a"] = None
deco_set["a"] = {"score": 23}
deco_set["a"] = SampleDecorator(23)
deco_set.load_decorator("a", None)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ...models.base import BaseModel, BaseModelSchema

from ..base import BaseDecoratorSet
from ..default import DecoratorSet, DEFAULT_MODELS


class SimpleModel(BaseModel):
Expand All @@ -25,6 +26,10 @@ class Meta:


class TestDecoratorSet(TestCase):
def test_deco_set(self):
deco_set = DecoratorSet()
assert all(k in deco_set.models for k in DEFAULT_MODELS)

def test_extract(self):

decor_value = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from ..localization_decorator import LocalizationDecorator

from unittest import TestCase


class TestThreadDecorator(TestCase):

LOCALE = "en-ca"
LOCALIZABLE = ["a", "b"]
CATALOGS = ["http://192.168.56.111/my-project/catalog.json"]

def test_init(self):
decorator = LocalizationDecorator()
assert decorator.locale is None
assert decorator.localizable == []
assert decorator.catalogs == []

decorator = LocalizationDecorator(
locale=TestThreadDecorator.LOCALE,
localizable=TestThreadDecorator.LOCALIZABLE,
catalogs=TestThreadDecorator.CATALOGS,
)
assert decorator.locale == TestThreadDecorator.LOCALE
assert decorator.localizable == TestThreadDecorator.LOCALIZABLE
assert decorator.catalogs == TestThreadDecorator.CATALOGS

def test_serialize_load(self):
decorator = LocalizationDecorator(
locale=TestThreadDecorator.LOCALE,
localizable=TestThreadDecorator.LOCALIZABLE,
catalogs=TestThreadDecorator.CATALOGS,
)

dumped = decorator.serialize()
loaded = LocalizationDecorator.deserialize(dumped)

assert loaded.locale == self.LOCALE
assert loaded.localizable == self.LOCALIZABLE
assert loaded.catalogs == self.CATALOGS
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ def test_serialize_load(self):
assert loaded.pthid == self.parent_id
assert loaded.sender_order == self.sender_order
assert loaded.received_orders == self.received_orders

loaded.pthid = "dummy"
assert loaded.pthid == "dummy"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from datetime import datetime
from unittest import TestCase

from ...util import datetime_to_str
from ..timing_decorator import TimingDecorator, TimingDecoratorSchema


NOW = datetime.now()


class TestTimingDecorator(TestCase):
def test_serialize_load(self):
deco = TimingDecorator(in_time=NOW, out_time=NOW,)

assert deco.in_time == datetime_to_str(NOW)
assert deco.out_time == datetime_to_str(NOW)
assert not deco.stale_time
assert not deco.expires_time
assert not deco.delay_milli
assert not deco.wait_until_time

dumped = deco.serialize()
loaded = TimingDecorator.deserialize(dumped)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from unittest import TestCase

from ...valid import UUIDFour
from ..transport_decorator import TransportDecorator, TransportDecoratorSchema


class TestTransportDecorator(TestCase):
def test_serialize_load(self):
deco = TransportDecorator(
return_route="all",
return_route_thread=UUIDFour.EXAMPLE,
queued_message_count=23,
)

assert deco.return_route == "all"
assert deco.return_route_thread == UUIDFour.EXAMPLE
assert deco.queued_message_count == 23

dumped = deco.serialize()
loaded = TransportDecorator.deserialize(dumped)
6 changes: 3 additions & 3 deletions aries_cloudagent/messaging/decorators/transport_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from marshmallow import fields, validate

from ..models.base import BaseModel, BaseModelSchema
from ..valid import UUIDFour
from ..valid import UUIDFour, WHOLE_NUM


class TransportDecorator(BaseModel):
Expand All @@ -33,7 +33,7 @@ def __init__(
return_route_thread: Identify the thread to enable return routing for
queued_message_count: Indicate the number of queued messages
"""
super(TransportDecorator, self).__init__()
super().__init__()
self.return_route = return_route
self.return_route_thread = return_route_thread
self.queued_message_count = queued_message_count
Expand All @@ -59,5 +59,5 @@ class Meta:
example=UUIDFour.EXAMPLE,
)
queued_message_count = fields.Int(
required=False, description="Number of queued messages", example=3,
required=False, description="Number of queued messages", **WHOLE_NUM
)
14 changes: 10 additions & 4 deletions aries_cloudagent/protocols/connections/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,11 @@ async def create_request(

# Create connection request message
if not my_endpoint:
my_endpoints = [self.context.settings.get("default_endpoint")]
my_endpoints.extend(self.context.settings.get("additional_endpoints"))
my_endpoints = []
default_endpoint = self.context.settings.get("default_endpoint")
if default_endpoint:
my_endpoints.append(default_endpoint)
my_endpoints.extend(self.context.settings.get("additional_endpoints", []))
else:
my_endpoints = [my_endpoint]
did_doc = await self.create_did_document(
Expand Down Expand Up @@ -478,8 +481,11 @@ async def create_response(

# Create connection response message
if not my_endpoint:
my_endpoints = [self.context.settings.get("default_endpoint")]
my_endpoints.extend(self.context.settings.get("additional_endpoints"))
my_endpoints = []
default_endpoint = self.context.settings.get("default_endpoint")
if default_endpoint:
my_endpoints.append(default_endpoint)
my_endpoints.extend(self.context.settings.get("additional_endpoints", []))
did_doc = await self.create_did_document(
my_info, connection.inbound_connection_id, my_endpoints
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def setUp(self):
{
"default_endpoint": "http://aries.ca/endpoint",
"default_label": "This guy",
"additional_endpoints": [],
"additional_endpoints": ["http://aries.ca/another-endpoint"],
"debug.auto_accept_invites": True,
"debug.auto_accept_requests": True,
}
Expand Down
3 changes: 1 addition & 2 deletions aries_cloudagent/protocols/issue_credential/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ async def prepare_send(
Args:
connection_id: Connection to create offer for
credential_proposal: The credential proposal with preview on
attribute values to use if auto_issue is enabled
credential_proposal: The credential proposal with preview
auto_remove: Flag to automatically remove the record on completion
Returns:
Expand Down
Loading

0 comments on commit c214642

Please sign in to comment.