Skip to content

Commit

Permalink
🎨 clean-up alice exchange records too
Browse files Browse the repository at this point in the history
  • Loading branch information
ff137 committed Aug 21, 2024
1 parent 73969f1 commit 37549da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/tests/e2e/issuer/test_get_records_paginated.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,6 @@ async def test_get_credential_exchange_records_paginated(
# Clean up created credentials
for cred_ex_id in faber_cred_ex_ids:
await faber_client.delete(f"{CREDENTIALS_BASE_PATH}/{cred_ex_id}")
for alice_credential in alice_prev_credentials:

Check failure on line 121 in app/tests/e2e/issuer/test_get_records_paginated.py

View workflow job for this annotation

GitHub Actions / JUnit Test Report

test_get_records_paginated.test_get_credential_exchange_records_paginated[clean-clean-clean-clean-clean]

UnboundLocalError: cannot access local variable 'alice_prev_credentials' where it is not associated with a value
Raw output
faber_client = <shared.util.rich_async_client.RichAsyncClient object at 0x7fb0fc9cabd0>
alice_member_client = <shared.util.rich_async_client.RichAsyncClient object at 0x7fb0fc9bc110>
credential_definition_id = 'LVKUvs2UpvQdw3nHjsWxUb:3:CL:2566:tag'
faber_and_alice_connection = FaberAliceConnect(alice_connection_id='2748562d-df18-4417-9fa2-61745fdd8876', faber_connection_id='d969c4ea-4a14-4da0-87d9-aaa4770a921a')

    @pytest.mark.anyio
    @pytest.mark.skipif(
        TestMode.regression_run in TestMode.fixture_params,
        reason="Temporarily skip; existing tests on dev don't clean up old records yet",
    )
    async def test_get_credential_exchange_records_paginated(
        faber_client: RichAsyncClient,
        alice_member_client: RichAsyncClient,
        credential_definition_id: str,
        faber_and_alice_connection: FaberAliceConnect,
    ):
        num_credentials_to_test = 5
        test_attributes = {"name": "Alice", "age": "44"}
    
        faber_cred_ex_ids = []
        try:
            # Create multiple credential exchanges
            for i in range(num_credentials_to_test):
                test_attributes["speed"] = str(i)
                credential_v2 = {
                    "protocol_version": "v2",
                    "connection_id": faber_and_alice_connection.faber_connection_id,
                    "indy_credential_detail": {
                        "credential_definition_id": credential_definition_id,
                        "attributes": test_attributes,
                    },
                    "save_exchange_record": True,
                }
    
                response = await faber_client.post(
                    CREDENTIALS_BASE_PATH, json=credential_v2
                )
    
                credential_exchange_id = response.json()["credential_exchange_id"]
                faber_cred_ex_ids.append(credential_exchange_id)
    
            # Test different limits
            for limit in range(1, num_credentials_to_test + 2):
                num_tries = 0
                retry = True
                while retry and num_tries < 5:  # Handle case where record doesn't exist yet
                    response = await alice_member_client.get(
                        CREDENTIALS_BASE_PATH,
                        params={
                            "state": "offer-sent",
                            "limit": limit,
                        },
                    )
                    credentials = response.json()
                    expected_num = min(limit, num_credentials_to_test)
                    if len(credentials) != expected_num:
                        num_tries += 1
                        await asyncio.sleep(0.2)
                    else:
                        retry = False
>               assert (
                    not retry
                ), f"Expected {expected_num} records, got {len(credentials)}: {credentials}"
E               AssertionError: Expected 1 records, got 0: []
E               assert not True

app/tests/e2e/issuer/test_get_records_paginated.py:69: AssertionError

During handling of the above exception, another exception occurred:

faber_client = <shared.util.rich_async_client.RichAsyncClient object at 0x7fb0fc9cabd0>
alice_member_client = <shared.util.rich_async_client.RichAsyncClient object at 0x7fb0fc9bc110>
credential_definition_id = 'LVKUvs2UpvQdw3nHjsWxUb:3:CL:2566:tag'
faber_and_alice_connection = FaberAliceConnect(alice_connection_id='2748562d-df18-4417-9fa2-61745fdd8876', faber_connection_id='d969c4ea-4a14-4da0-87d9-aaa4770a921a')

    @pytest.mark.anyio
    @pytest.mark.skipif(
        TestMode.regression_run in TestMode.fixture_params,
        reason="Temporarily skip; existing tests on dev don't clean up old records yet",
    )
    async def test_get_credential_exchange_records_paginated(
        faber_client: RichAsyncClient,
        alice_member_client: RichAsyncClient,
        credential_definition_id: str,
        faber_and_alice_connection: FaberAliceConnect,
    ):
        num_credentials_to_test = 5
        test_attributes = {"name": "Alice", "age": "44"}
    
        faber_cred_ex_ids = []
        try:
            # Create multiple credential exchanges
            for i in range(num_credentials_to_test):
                test_attributes["speed"] = str(i)
                credential_v2 = {
                    "protocol_version": "v2",
                    "connection_id": faber_and_alice_connection.faber_connection_id,
                    "indy_credential_detail": {
                        "credential_definition_id": credential_definition_id,
                        "attributes": test_attributes,
                    },
                    "save_exchange_record": True,
                }
    
                response = await faber_client.post(
                    CREDENTIALS_BASE_PATH, json=credential_v2
                )
    
                credential_exchange_id = response.json()["credential_exchange_id"]
                faber_cred_ex_ids.append(credential_exchange_id)
    
            # Test different limits
            for limit in range(1, num_credentials_to_test + 2):
                num_tries = 0
                retry = True
                while retry and num_tries < 5:  # Handle case where record doesn't exist yet
                    response = await alice_member_client.get(
                        CREDENTIALS_BASE_PATH,
                        params={
                            "state": "offer-sent",
                            "limit": limit,
                        },
                    )
                    credentials = response.json()
                    expected_num = min(limit, num_credentials_to_test)
                    if len(credentials) != expected_num:
                        num_tries += 1
                        await asyncio.sleep(0.2)
                    else:
                        retry = False
                assert (
                    not retry
                ), f"Expected {expected_num} records, got {len(credentials)}: {credentials}"
    
            # Test offset greater than number of records
            response = await alice_member_client.get(
                CREDENTIALS_BASE_PATH,
                params={
                    "state": "offer-received",
                    "limit": 1,
                    "offset": num_credentials_to_test,
                },
            )
            credentials = response.json()
            assert len(credentials) == 0
    
            # Test fetching unique records with pagination
            alice_prev_credentials = []
            for offset in range(num_credentials_to_test):
                response = await alice_member_client.get(
                    CREDENTIALS_BASE_PATH,
                    params={
                        "state": "offer-received",
                        "limit": 1,
                        "offset": offset,
                    },
                )
    
                credentials = response.json()
                assert len(credentials) == 1
    
                record = credentials[0]
                assert record not in alice_prev_credentials
                alice_prev_credentials.append(record)
    
            # Test invalid limit and offset values
            invalid_params = [
                {"limit": -1},  # must be positive
                {"offset": -1},  # must be positive
                {"limit": 0},  # must be greater than 0
                {"limit": 10001},  # must be less than or equal to max in ACA-Py: 10'000
            ]
    
            for params in invalid_params:
                with pytest.raises(HTTPException) as exc:
                    await faber_client.get(CREDENTIALS_BASE_PATH, params=params)
                assert exc.value.status_code == 422
    
        finally:
            # Clean up created credentials
            for cred_ex_id in faber_cred_ex_ids:
                await faber_client.delete(f"{CREDENTIALS_BASE_PATH}/{cred_ex_id}")
>           for alice_credential in alice_prev_credentials:
E           UnboundLocalError: cannot access local variable 'alice_prev_credentials' where it is not associated with a value

app/tests/e2e/issuer/test_get_records_paginated.py:121: UnboundLocalError
cred_ex_id = alice_credential["credential_exchange_id"]
await alice_member_client.delete(f"{CREDENTIALS_BASE_PATH}/{cred_ex_id}")
3 changes: 3 additions & 0 deletions app/tests/e2e/verifier/test_get_records_paginated.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@ async def test_get_presentation_exchange_records_paginated(
# Clean up created presentation requests
for proof_id in acme_proof_ids:
await acme_client.delete(f"{VERIFIER_BASE_PATH}/proofs/{proof_id}")
for alice_proof in alice_previous_proofs:
proof_id = alice_proof["proof_id"]
await alice_member_client.delete(f"{VERIFIER_BASE_PATH}/proofs/{proof_id}")

0 comments on commit 37549da

Please sign in to comment.