|
| 1 | +"""Test universal resolver with http bindings.""" |
| 2 | + |
| 3 | +import re |
| 4 | +from typing import Dict, Union |
| 5 | + |
| 6 | +from asynctest import mock as async_mock |
| 7 | +import pytest |
| 8 | + |
| 9 | +from aries_cloudagent.config.settings import Settings |
| 10 | + |
| 11 | +from .. import universal as test_module |
| 12 | +from ...base import DIDNotFound, ResolverError |
| 13 | +from ..universal import UniversalResolver |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +async def resolver(): |
| 18 | + """Resolver fixture.""" |
| 19 | + yield UniversalResolver( |
| 20 | + endpoint="https://example.com", supported_did_regex=re.compile("^did:sov:.*$") |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def profile(): |
| 26 | + """Profile fixture.""" |
| 27 | + yield async_mock.MagicMock() |
| 28 | + |
| 29 | + |
| 30 | +class MockResponse: |
| 31 | + """Mock http response.""" |
| 32 | + |
| 33 | + def __init__(self, status: int, body: Union[str, Dict]): |
| 34 | + self.status = status |
| 35 | + self.body = body |
| 36 | + |
| 37 | + async def json(self): |
| 38 | + return self.body |
| 39 | + |
| 40 | + async def text(self): |
| 41 | + return self.body |
| 42 | + |
| 43 | + async def __aenter__(self): |
| 44 | + """For use as async context.""" |
| 45 | + return self |
| 46 | + |
| 47 | + async def __aexit__(self, err_type, err_value, err_exc): |
| 48 | + """For use as async context.""" |
| 49 | + |
| 50 | + |
| 51 | +class MockClientSession: |
| 52 | + """Mock client session.""" |
| 53 | + |
| 54 | + def __init__(self, response: MockResponse = None): |
| 55 | + self.response = response |
| 56 | + |
| 57 | + def __call__(self): |
| 58 | + return self |
| 59 | + |
| 60 | + async def __aenter__(self): |
| 61 | + """For use as async context.""" |
| 62 | + return self |
| 63 | + |
| 64 | + async def __aexit__(self, err_type, err_value, err_exc): |
| 65 | + """For use as async context.""" |
| 66 | + |
| 67 | + def get(self, endpoint): |
| 68 | + """Return response.""" |
| 69 | + return self.response |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +def mock_client_session(): |
| 74 | + temp = test_module.aiohttp.ClientSession |
| 75 | + session = MockClientSession() |
| 76 | + test_module.aiohttp.ClientSession = session |
| 77 | + yield session |
| 78 | + test_module.aiohttp.ClientSession = temp |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.asyncio |
| 82 | +async def test_resolve(profile, resolver, mock_client_session): |
| 83 | + mock_client_session.response = MockResponse( |
| 84 | + 200, |
| 85 | + { |
| 86 | + "didDocument": { |
| 87 | + "id": "did:example:123", |
| 88 | + "@context": "https://www.w3.org/ns/did/v1", |
| 89 | + } |
| 90 | + }, |
| 91 | + ) |
| 92 | + doc = await resolver.resolve(profile, "did:sov:WRfXPg8dantKVubE3HX8pw") |
| 93 | + assert doc.get("id") == "did:example:123" |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_resolve_not_found(profile, resolver, mock_client_session): |
| 98 | + mock_client_session.response = MockResponse(404, "Not found") |
| 99 | + with pytest.raises(DIDNotFound): |
| 100 | + await resolver.resolve(profile, "did:sov:WRfXPg8dantKVubE3HX8pw") |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.asyncio |
| 104 | +async def test_resolve_unexpeceted_status(profile, resolver, mock_client_session): |
| 105 | + mock_client_session.response = MockResponse( |
| 106 | + 500, "Server failed to complete request" |
| 107 | + ) |
| 108 | + with pytest.raises(ResolverError): |
| 109 | + await resolver.resolve(profile, "did:sov:WRfXPg8dantKVubE3HX8pw") |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.asyncio |
| 113 | +async def test_fetch_resolver_props(mock_client_session: MockClientSession): |
| 114 | + mock_client_session.response = MockResponse(200, {"test": "json"}) |
| 115 | + assert await test_module._fetch_resolver_props("test") == {"test": "json"} |
| 116 | + mock_client_session.response = MockResponse(404, "Not found") |
| 117 | + with pytest.raises(ResolverError): |
| 118 | + await test_module._fetch_resolver_props("test") |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.asyncio |
| 122 | +async def test_get_supported_did_regex(): |
| 123 | + props = {"example": {"http": {"pattern": "match a test string"}}} |
| 124 | + with async_mock.patch.object( |
| 125 | + test_module, |
| 126 | + "_fetch_resolver_props", |
| 127 | + async_mock.CoroutineMock(return_value=props), |
| 128 | + ): |
| 129 | + pattern = await test_module._get_supported_did_regex("test") |
| 130 | + assert pattern.fullmatch("match a test string") |
| 131 | + |
| 132 | + |
| 133 | +def test_compile_supported_did_regex(): |
| 134 | + patterns = ["one", "two", "three"] |
| 135 | + compiled = test_module._compile_supported_did_regex(patterns) |
| 136 | + assert compiled.match("one") |
| 137 | + assert compiled.match("two") |
| 138 | + assert compiled.match("three") |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.asyncio |
| 142 | +async def test_setup_endpoint_regex_set(resolver: UniversalResolver): |
| 143 | + settings = Settings( |
| 144 | + { |
| 145 | + "resolver.universal": "http://example.com", |
| 146 | + "resolver.universal.supported": "test", |
| 147 | + } |
| 148 | + ) |
| 149 | + context = async_mock.MagicMock() |
| 150 | + context.settings = settings |
| 151 | + with async_mock.patch.object( |
| 152 | + test_module, |
| 153 | + "_compile_supported_did_regex", |
| 154 | + async_mock.MagicMock(return_value="pattern"), |
| 155 | + ): |
| 156 | + await resolver.setup(context) |
| 157 | + |
| 158 | + assert resolver._endpoint == "http://example.com" |
| 159 | + assert resolver._supported_did_regex == "pattern" |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.asyncio |
| 163 | +async def test_setup_endpoint_set(resolver: UniversalResolver): |
| 164 | + settings = Settings( |
| 165 | + { |
| 166 | + "resolver.universal": "http://example.com", |
| 167 | + } |
| 168 | + ) |
| 169 | + context = async_mock.MagicMock() |
| 170 | + context.settings = settings |
| 171 | + with async_mock.patch.object( |
| 172 | + test_module, |
| 173 | + "_get_supported_did_regex", |
| 174 | + async_mock.CoroutineMock(return_value="pattern"), |
| 175 | + ): |
| 176 | + await resolver.setup(context) |
| 177 | + |
| 178 | + assert resolver._endpoint == "http://example.com" |
| 179 | + assert resolver._supported_did_regex == "pattern" |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.asyncio |
| 183 | +async def test_setup_endpoint_default(resolver: UniversalResolver): |
| 184 | + settings = Settings( |
| 185 | + { |
| 186 | + "resolver.universal": "DEFAULT", |
| 187 | + } |
| 188 | + ) |
| 189 | + context = async_mock.MagicMock() |
| 190 | + context.settings = settings |
| 191 | + with async_mock.patch.object( |
| 192 | + test_module, |
| 193 | + "_get_supported_did_regex", |
| 194 | + async_mock.CoroutineMock(return_value="pattern"), |
| 195 | + ): |
| 196 | + await resolver.setup(context) |
| 197 | + |
| 198 | + assert resolver._endpoint == test_module.DEFAULT_ENDPOINT |
| 199 | + assert resolver._supported_did_regex == "pattern" |
| 200 | + |
| 201 | + |
| 202 | +@pytest.mark.asyncio |
| 203 | +async def test_setup_endpoint_unset(resolver: UniversalResolver): |
| 204 | + settings = Settings() |
| 205 | + context = async_mock.MagicMock() |
| 206 | + context.settings = settings |
| 207 | + with async_mock.patch.object( |
| 208 | + test_module, |
| 209 | + "_get_supported_did_regex", |
| 210 | + async_mock.CoroutineMock(return_value="pattern"), |
| 211 | + ): |
| 212 | + await resolver.setup(context) |
| 213 | + |
| 214 | + assert resolver._endpoint == test_module.DEFAULT_ENDPOINT |
| 215 | + assert resolver._supported_did_regex == "pattern" |
| 216 | + |
| 217 | + |
| 218 | +@pytest.mark.asyncio |
| 219 | +async def test_supported_did_regex_not_setup(): |
| 220 | + resolver = UniversalResolver() |
| 221 | + with pytest.raises(ResolverError): |
| 222 | + resolver.supported_did_regex |
0 commit comments