Skip to content

Commit

Permalink
Merge pull request openwallet-foundation#3338 from didx-xyz/fix-depre…
Browse files Browse the repository at this point in the history
…cations

🎨 Fix current test warnings
  • Loading branch information
dbluhm authored Nov 14, 2024
2 parents b7d27fa + d84a384 commit a41b4fb
Show file tree
Hide file tree
Showing 16 changed files with 441 additions and 460 deletions.
1 change: 0 additions & 1 deletion acapy_agent/anoncreds/tests/test_revocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,6 @@ async def test_create_credential_w3c(self, mock_supports_revocation):
assert isinstance(result, tuple)
assert mock_supports_revocation.call_count == 1

@pytest.mark.asyncio
@mock.patch.object(AskarAnoncredsProfileSession, "handle")
async def test_create_credential_w3c_keyerror(self, mock_handle):
mock_handle.fetch = mock.CoroutineMock(side_effect=[MockEntry(), MockEntry()])
Expand Down
2 changes: 1 addition & 1 deletion acapy_agent/config/logging/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def load_resource(path: str, encoding: Optional[str] = None):
else:
# Package resource
package, resource = components
bstream = resources.open_binary(package, resource)
bstream = resources.files(package).joinpath(resource).open("rb")
if encoding:
return io.TextIOWrapper(bstream, encoding=encoding)
return bstream
Expand Down
59 changes: 45 additions & 14 deletions acapy_agent/config/tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import contextlib
from io import StringIO
from io import BufferedReader, StringIO, TextIOWrapper
from tempfile import NamedTemporaryFile
from unittest import IsolatedAsyncioTestCase, mock

Expand Down Expand Up @@ -110,24 +110,55 @@ def test_banner_did(self):
def test_load_resource(self):
# Testing local file access
with mock.patch("builtins.open", mock.MagicMock()) as mock_open:
test_module.load_resource("abc", encoding="utf-8")
# First call succeeds
file_handle = mock.MagicMock(spec=TextIOWrapper)
mock_open.return_value = file_handle
result = test_module.load_resource("abc", encoding="utf-8")
mock_open.assert_called_once_with("abc", encoding="utf-8")
assert result == file_handle # Verify the returned file handle

mock_open.reset_mock()
# Simulate IOError on second call
mock_open.side_effect = IOError("insufficient privilege")
# load_resource should absorb IOError
test_module.load_resource("abc", encoding="utf-8")
result = test_module.load_resource("abc", encoding="utf-8")
mock_open.assert_called_once_with("abc", encoding="utf-8")
assert result is None

# Testing package resource access with encoding (text mode)
with mock.patch(
"importlib.resources.open_binary", mock.MagicMock()
) as mock_open_binary, mock.patch(
with mock.patch("importlib.resources.files") as mock_files, mock.patch(
"io.TextIOWrapper", mock.MagicMock()
) as mock_text_io_wrapper:
test_module.load_resource("abc:def", encoding="utf-8")
mock_open_binary.assert_called_once_with("abc", "def")
mock_text_io_wrapper.assert_called_once()
# Setup the mocks
mock_resource_path = mock.MagicMock()
mock_files.return_value.joinpath.return_value = mock_resource_path
mock_resource_handle = mock.MagicMock(spec=BufferedReader)
mock_resource_path.open.return_value = mock_resource_handle
mock_text_io_wrapper.return_value = mock.MagicMock(spec=TextIOWrapper)

result = test_module.load_resource("abc:def", encoding="utf-8")

# Assertions
mock_files.assert_called_once_with("abc")
mock_files.return_value.joinpath.assert_called_once_with("def")
mock_resource_path.open.assert_called_once_with("rb")
mock_text_io_wrapper.assert_called_once_with(
mock_resource_handle, encoding="utf-8"
)
assert result is mock_text_io_wrapper.return_value

# Testing package resource access without encoding (binary mode)
with mock.patch(
"importlib.resources.open_binary", mock.MagicMock()
) as mock_open_binary:
test_module.load_resource("abc:def", encoding=None)
mock_open_binary.assert_called_once_with("abc", "def")
with mock.patch("importlib.resources.files") as mock_files:
# Setup the mocks
mock_resource_path = mock.MagicMock()
mock_files.return_value.joinpath.return_value = mock_resource_path
mock_resource_handle = mock.MagicMock(spec=BufferedReader)
mock_resource_path.open.return_value = mock_resource_handle

result = test_module.load_resource("abc:def", encoding=None)

# Assertions
mock_files.assert_called_once_with("abc")
mock_files.return_value.joinpath.assert_called_once_with("def")
mock_resource_path.open.assert_called_once_with("rb")
assert result == mock_resource_handle # Verify the returned binary stream
6 changes: 3 additions & 3 deletions acapy_agent/connections/tests/test_base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import IsolatedAsyncioTestCase
from unittest.mock import call

import pytest
from pydid import DID, DIDDocument, DIDDocumentBuilder
from pydid.doc.builder import ServiceBuilder
from pydid.verification_method import (
Expand All @@ -27,9 +28,7 @@
from ...protocols.connections.v1_0.messages.connection_invitation import (
ConnectionInvitation,
)
from ...protocols.coordinate_mediation.v1_0.models.mediation_record import (
MediationRecord,
)
from ...protocols.coordinate_mediation.v1_0.models.mediation_record import MediationRecord
from ...protocols.coordinate_mediation.v1_0.route_manager import (
CoordinateMediationV1RouteManager,
RouteManager,
Expand Down Expand Up @@ -1065,6 +1064,7 @@ async def test_resolve_connection_targets_x_bad_key_material(self):
await self.manager.resolve_connection_targets(did)
assert "not supported" in str(cm.exception)

@pytest.mark.filterwarnings("ignore::UserWarning")
async def test_resolve_connection_targets_x_unsupported_key(self):
did = "did:sov:" + self.test_did
doc_builder = DIDDocumentBuilder(did)
Expand Down
1 change: 1 addition & 0 deletions acapy_agent/core/tests/test_conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ async def test_dispatch_complete_fatal_x(self):
conductor.dispatch_complete(message, mock_task)
mock_notify.assert_called_once_with()

@pytest.mark.filterwarnings("ignore:Aries RFC 0160.*:DeprecationWarning")
async def test_print_invite_connection(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
builder.update_settings(
Expand Down
6 changes: 3 additions & 3 deletions acapy_agent/didcomm_v2/tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..adapters import ResolverAdapter, SecretsAdapter, SecretsAdapterError


class TestDIDResolver(BaseDIDResolver):
class MockDIDResolver(BaseDIDResolver):
async def setup(self, context: InjectionContext):
return await super().setup(context)

Expand All @@ -26,7 +26,7 @@ async def resolve(
self,
profile,
did,
sercive_accept=None,
service_accept=None,
):
return {"did": did, "test": "didDoc"}

Expand All @@ -46,7 +46,7 @@ async def asyncSetUp(self):
self.test_did = "did:test:0"
self.invalid_did = "this shouldn't work"
resolver = DIDResolver()
resolver.register_resolver(TestDIDResolver())
resolver.register_resolver(MockDIDResolver())
self.res_adapter = ResolverAdapter(profile=self.profile, resolver=resolver)

async def test_resolver_adapter_resolve_did(self):
Expand Down
3 changes: 0 additions & 3 deletions acapy_agent/multitenant/admin/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from unittest import IsolatedAsyncioTestCase

import pytest
from marshmallow.exceptions import ValidationError

from ....admin.request_context import AdminRequestContext
Expand Down Expand Up @@ -143,7 +142,6 @@ async def test_wallets_list_query(self):
}
)

@pytest.mark.asyncio(scope="function")
async def test_wallet_create_tenant_settings(self):
body = {
"wallet_name": "test",
Expand Down Expand Up @@ -796,7 +794,6 @@ async def test_wallet_create_token_x(self):
)
await test_module.wallet_create_token(self.request)

@pytest.mark.asyncio(scope="function")
async def test_wallet_remove_managed(self):
self.request.has_body = False
self.request.match_info = {"wallet_id": "dummy"}
Expand Down
3 changes: 3 additions & 0 deletions acapy_agent/protocols/connections/v1_0/tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest import IsolatedAsyncioTestCase

import pytest

from .....cache.base import BaseCache
from .....cache.in_memory import InMemoryCache
from .....connections.models.conn_record import ConnRecord
Expand Down Expand Up @@ -29,6 +31,7 @@
from ..models.connection_detail import ConnectionDetail


@pytest.mark.filterwarnings("ignore:Aries RFC 0160.*:DeprecationWarning")
class TestConnectionManager(IsolatedAsyncioTestCase):
def make_did_doc(self, did, verkey):
doc = DIDDoc(did=did)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from unittest import IsolatedAsyncioTestCase

import pytest

from ......connections.models import connection_target
from ......connections.models.diddoc import DIDDoc, PublicKey, PublicKeyType, Service
from ......messaging.decorators.attach_decorator import AttachDecorator
Expand Down Expand Up @@ -75,7 +73,6 @@ async def asyncSetUp(self):
did_doc_attach=self.did_doc_attach,
)

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
async def test_called(self, mock_didx_mgr):
mock_didx_mgr.return_value.accept_response = mock.CoroutineMock()
Expand All @@ -89,7 +86,6 @@ async def test_called(self, mock_didx_mgr):
)
assert not responder.messages

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
async def test_called_auto_ping(self, mock_didx_mgr):
self.ctx.update_settings({"auto_ping_connection": True})
Expand All @@ -107,7 +103,6 @@ async def test_called_auto_ping(self, mock_didx_mgr):
result, _ = messages[0]
assert isinstance(result, Ping)

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
@mock.patch.object(connection_target, "ConnectionTarget")
async def test_problem_report(self, mock_conn_target, mock_didx_mgr):
Expand Down Expand Up @@ -144,7 +139,6 @@ async def test_problem_report(self, mock_conn_target, mock_didx_mgr):
)
assert target == {"target_list": [mock_conn_target]}

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
@mock.patch.object(connection_target, "ConnectionTarget")
async def test_problem_report_did_doc(
Expand Down Expand Up @@ -191,7 +185,6 @@ async def test_problem_report_did_doc(
)
assert target == {"target_list": [mock_conn_target]}

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
@mock.patch.object(connection_target, "ConnectionTarget")
async def test_problem_report_did_doc_no_conn_target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,6 @@ async def test_issue_credential_non_revocable(self):
# assert data is encoded as base64
assert attachment.data.base64

@pytest.mark.asyncio
async def test_match_sent_cred_def_id_error(self):
tag_query = {"tag": "test_tag"}

Expand All @@ -737,7 +736,6 @@ async def test_match_sent_cred_def_id_error(self):
context.exception
)

@pytest.mark.asyncio
async def test_store_credential(self):
attr_values = {
"legalName": "value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,6 @@ async def test_filter_string(self):
)
assert len(tmp_vp.get("verifiableCredential")) == 1

@pytest.mark.asyncio
@pytest.mark.ursa_bbs_signatures
async def test_filter_schema(self):
cred_list, _ = await self.setup_tuple(self.profile)
Expand Down Expand Up @@ -2384,7 +2383,6 @@ async def test_no_filter(self):
)
assert len(tmp_vp.get("verifiableCredential")) == 6

@pytest.mark.asyncio
@pytest.mark.ursa_bbs_signatures
async def test_filter_with_only_string_type(self):
cred_list, _ = await self.setup_tuple(self.profile)
Expand Down Expand Up @@ -2436,7 +2434,6 @@ async def test_filter_with_only_string_type(self):
)
assert len(tmp_vp.get("verifiableCredential")) == 6

@pytest.mark.asyncio
@pytest.mark.ursa_bbs_signatures
async def test_filter_with_only_num_type(self):
await self.setup_tuple(self.profile)
Expand Down Expand Up @@ -2850,7 +2847,6 @@ async def test_filter_by_field_path_match_on_proof(self):
with pytest.raises(DIFPresExchError):
await dif_pres_exch_handler.filter_by_field(field, cred)

@pytest.mark.asyncio
async def test_filter_creds_record_id(self):
dif_pres_exch_handler = DIFPresExchHandler(self.profile)
cred_list = [
Expand Down Expand Up @@ -3214,7 +3210,6 @@ async def test_apply_constraint_received_cred_path_update(self):
constraint=constraint, cred_dict=cred_dict
)

@pytest.mark.asyncio
@pytest.mark.ursa_bbs_signatures
async def test_apply_constraint_received_cred_invalid(self):
dif_pres_exch_handler = DIFPresExchHandler(
Expand Down Expand Up @@ -3270,7 +3265,6 @@ async def test_apply_constraint_received_cred_invalid(self):
constraint=constraint, cred_dict=cred_dict
)

@pytest.mark.asyncio
@pytest.mark.ursa_bbs_signatures
async def test_apply_constraint_received_cred_valid(self):
dif_pres_exch_handler = DIFPresExchHandler(
Expand Down Expand Up @@ -3464,7 +3458,6 @@ async def test_apply_constraint_received_cred_no_sel_disc(self):
constraint=constraint, cred_dict=cred_dict
)

@pytest.mark.asyncio
async def test_get_updated_path(self):
dif_pres_exch_handler = DIFPresExchHandler(
self.profile, proof_type=BbsBlsSignature2020.signature_type
Expand Down Expand Up @@ -3532,7 +3525,6 @@ def test_get_dict_keys_from_path(self):
cred_dict, "credentialSubject.Patient.address"
) == ["@id"]

@pytest.mark.asyncio
async def test_filter_by_field_keyerror(self):
dif_pres_exch_handler = DIFPresExchHandler(
self.profile, proof_type=BbsBlsSignature2020.signature_type
Expand All @@ -3555,7 +3547,6 @@ async def test_filter_by_field_keyerror(self):
)
assert not await dif_pres_exch_handler.filter_by_field(field, vc_record_cred)

@pytest.mark.asyncio
async def test_filter_by_field_xsd_parser(self):
dif_pres_exch_handler = DIFPresExchHandler(
self.profile, proof_type=BbsBlsSignature2020.signature_type
Expand Down
8 changes: 4 additions & 4 deletions acapy_agent/transport/tests/test_pack_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ async def test_get_recipient_keys_fails(self):
serializer.get_recipient_keys(json.dumps(enc_message))


class TestDIDCommMessaging(DIDCommMessaging):
class MockDIDCommMessaging(DIDCommMessaging):
def __init__(
self,
):
Expand Down Expand Up @@ -255,7 +255,7 @@ async def test_errors(self):
message_json = json.dumps(message)
async with self.profile.session() as session:
session.context.injector.bind_instance(
DIDCommMessaging, TestDIDCommMessaging()
DIDCommMessaging, MockDIDCommMessaging()
)
with self.assertRaises(WireFormatParseError) as context:
await wire_format.parse_message(session, message_json)
Expand All @@ -273,7 +273,7 @@ async def test_errors(self):
async def test_fallback(self):
serializer = V2PackWireFormat()

test_dm = TestDIDCommMessaging()
test_dm = MockDIDCommMessaging()
test_dm.packaging.unpack = mock.AsyncMock(side_effect=CryptoServiceError())
async with self.profile.session() as session:
session.context.injector.bind_instance(DIDCommMessaging, test_dm)
Expand All @@ -289,7 +289,7 @@ async def test_fallback(self):
async def test_encode(self):
serializer = V2PackWireFormat()

test_dm = TestDIDCommMessaging()
test_dm = MockDIDCommMessaging()
test_dm.pack = mock.AsyncMock(
return_value=PackResult(
message=self.test_message, target_services=mock.MagicMock()
Expand Down
Loading

0 comments on commit a41b4fb

Please sign in to comment.