From ab2aa0d3374dcd64752dbfbf488ae39c521ffd01 Mon Sep 17 00:00:00 2001 From: cl0ete Date: Wed, 13 Nov 2024 17:24:11 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Test=20limit=20and=20offset=20param?= =?UTF-8?q?s=20on=20get=20credentials=20(#1169)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * issue 10 creds to alice * update fixture import * test limit/offset params * :art: * :art: * :art: * fix test * update name * import testMode * skip if regression * update checks * update fixture with param * :art: * fix fixture * rework test * :art: * rework fixture * add one more case * :art: --- app/tests/e2e/conftest.py | 1 + app/tests/e2e/test_wallet_credentials.py | 44 ++++++++++++++ app/tests/fixtures/credentials.py | 74 ++++++++++++++++++++++++ 3 files changed, 119 insertions(+) diff --git a/app/tests/e2e/conftest.py b/app/tests/e2e/conftest.py index 544f373af..8b5c464ac 100644 --- a/app/tests/e2e/conftest.py +++ b/app/tests/e2e/conftest.py @@ -16,6 +16,7 @@ get_or_issue_regression_cred_revoked, get_or_issue_regression_cred_valid, issue_alice_creds, + issue_alice_many_creds, issue_credential_to_alice, meld_co_issue_credential_to_alice, revoke_alice_creds, diff --git a/app/tests/e2e/test_wallet_credentials.py b/app/tests/e2e/test_wallet_credentials.py index 2f3e3705c..183355946 100644 --- a/app/tests/e2e/test_wallet_credentials.py +++ b/app/tests/e2e/test_wallet_credentials.py @@ -1,9 +1,11 @@ import logging +from typing import List import pytest from fastapi import HTTPException from app.routes.wallet.credentials import router +from app.tests.util.regression_testing import TestMode from shared import RichAsyncClient from shared.models.credential_exchange import CredentialExchange @@ -60,3 +62,45 @@ async def test_get_and_delete_credential_record( f"{WALLET_CREDENTIALS_PATH}/{credential_id}" ) assert exc.value.status_code == 404 + + +@pytest.mark.anyio +@pytest.mark.skipif( + TestMode.regression_run in TestMode.fixture_params, + reason="Skipping due to regression run", +) +@pytest.mark.parametrize( + "issue_alice_many_creds", [3], indirect=True +) # issue alice 3 creds +async def test_get_credential_record_with_limit( + alice_member_client: RichAsyncClient, + issue_alice_many_creds: List[CredentialExchange], # pylint: disable=unused-argument +): + valid_params = [ + {"limit": 1}, + {"limit": 2}, + {"limit": 3}, + {"limit": 4}, + {"limit": 1, "offset": 4}, + {"limit": 2, "offset": 2}, + ] + + expected_length = [1, 2, 3, 3, 0, 1] + + for params, length in zip(valid_params, expected_length): + response = ( + await alice_member_client.get(WALLET_CREDENTIALS_PATH, params=params) + ).json() + assert len(response["results"]) == length + + 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 alice_member_client.get(WALLET_CREDENTIALS_PATH, params=params) + assert exc.value.status_code == 422 diff --git a/app/tests/fixtures/credentials.py b/app/tests/fixtures/credentials.py index 4157a50b8..d0f64de4a 100644 --- a/app/tests/fixtures/credentials.py +++ b/app/tests/fixtures/credentials.py @@ -433,3 +433,77 @@ async def get_or_issue_regression_cred_valid( referent=valid_credential["referent"], cred_def_revocable=valid_credential["cred_def_id"], ) + + +@pytest.fixture(scope="function") +async def issue_alice_many_creds( + request, + faber_client: RichAsyncClient, + alice_member_client: RichAsyncClient, + credential_definition_id: str, + faber_and_alice_connection: FaberAliceConnect, +) -> List[CredentialExchange]: + + faber_conn_id = faber_and_alice_connection.faber_connection_id + + faber_cred_ex_ids = [] + num_creds = request.param if hasattr(request, "param") else 3 + for i in range(num_creds): + credential = { + "connection_id": faber_conn_id, + "save_exchange_record": True, + "indy_credential_detail": { + "credential_definition_id": credential_definition_id, + "attributes": {"speed": str(i), "name": "Alice", "age": "44"}, + }, + } + response = ( + await faber_client.post( + CREDENTIALS_BASE_PATH, + json=credential, + ) + ).json() + + faber_cred_ex_id = response["credential_exchange_id"] + faber_cred_ex_ids += [faber_cred_ex_id] + + thread_id = response["thread_id"] + + alice_event = await check_webhook_state( + client=alice_member_client, + topic="credentials", + state="offer-received", + filter_map={ + "thread_id": thread_id, + }, + ) + alice_cred_ex_id = alice_event["credential_exchange_id"] + + await alice_member_client.post( + f"{CREDENTIALS_BASE_PATH}/{alice_cred_ex_id}/request", + json={}, + ) + + await check_webhook_state( + client=alice_member_client, + topic="credentials", + state="done", + filter_map={ + "thread_id": thread_id, + }, + ) + + cred_ex_response = ( + await faber_client.get( + CREDENTIALS_BASE_PATH + "?connection_id=" + faber_conn_id + ) + ).json() + cred_ex_response = [ + record + for record in cred_ex_response + if record["credential_exchange_id"] in faber_cred_ex_ids + ] + + assert len(cred_ex_response) == num_creds + + return [CredentialExchange(**cred) for cred in cred_ex_response]