-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_send_credential.py
131 lines (112 loc) · 4.33 KB
/
test_send_credential.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from unittest.mock import AsyncMock, patch
import pytest
from aries_cloudcontroller.exceptions import (
ApiException,
BadRequestException,
NotFoundException,
)
from fastapi import HTTPException
from app.exceptions.cloudapi_exception import CloudApiException
from app.models.issuer import CredentialType, SendCredential
from app.routes.issuer import send_credential
from app.tests.routes.issuer.test_create_offer import indy_cred, ld_cred
@pytest.mark.anyio
@pytest.mark.parametrize(
"credential",
[
SendCredential(
protocol_version="v2",
type=CredentialType.INDY,
indy_credential_detail=indy_cred,
connection_id="abc",
),
SendCredential(
protocol_version="v2",
type=CredentialType.LD_PROOF,
ld_credential_detail=ld_cred,
connection_id="abc",
),
],
)
async def test_send_credential_success(credential):
mock_aries_controller = AsyncMock()
mock_aries_controller.issue_credential_v2_0.issue_credential_automated = AsyncMock()
with patch("app.routes.issuer.client_from_auth") as mock_client_from_auth, patch(
"app.routes.issuer.assert_public_did", return_value="public_did"
), patch(
"app.services.issuer.acapy_issuer_v2.credential_record_to_model_v2"
), patch(
"app.routes.issuer.schema_id_from_credential_definition_id",
return_value="schema_id",
), patch(
"app.routes.issuer.assert_valid_issuer"
):
mock_client_from_auth.return_value.__aenter__.return_value = (
mock_aries_controller
)
await send_credential(credential=credential, auth="mocked_auth")
mock_aries_controller.issue_credential_v2_0.issue_credential_automated.assert_awaited_once()
@pytest.mark.anyio
@pytest.mark.parametrize(
"exception_class, expected_status_code, expected_detail",
[
(BadRequestException, 400, "Bad request"),
(NotFoundException, 404, "Not found"),
(ApiException, 500, "Internal Server Error"),
],
)
async def test_send_credential_fail_acapy_error(
exception_class, expected_status_code, expected_detail
):
mock_aries_controller = AsyncMock()
mock_aries_controller.issue_credential_v2_0.issue_credential_automated = AsyncMock(
side_effect=exception_class(status=expected_status_code, reason=expected_detail)
)
with patch(
"app.routes.issuer.client_from_auth"
) as mock_client_from_auth, pytest.raises(
HTTPException, match=expected_detail
) as exc, patch(
"app.routes.issuer.assert_public_did", return_value="public_did"
), patch(
"app.routes.issuer.schema_id_from_credential_definition_id",
return_value="schema_id",
), patch(
"app.routes.issuer.assert_valid_issuer"
):
mock_client_from_auth.return_value.__aenter__.return_value = (
mock_aries_controller
)
await send_credential(
credential=SendCredential(
protocol_version="v2",
type=CredentialType.INDY,
indy_credential_detail=indy_cred,
connection_id="abc",
),
auth="mocked_auth",
)
assert exc.value.status_code == expected_status_code
@pytest.mark.anyio
async def test_send_credential_fail_bad_public_did():
credential = SendCredential(
protocol_version="v2",
type=CredentialType.INDY,
indy_credential_detail=indy_cred,
connection_id="abc",
)
mock_aries_controller = AsyncMock()
mock_aries_controller.issue_credential_v2_0.issue_credential_automated = AsyncMock()
with patch("app.routes.issuer.client_from_auth") as mock_client_from_auth, patch(
"app.routes.issuer.assert_public_did",
AsyncMock(side_effect=CloudApiException(status_code=404, detail="Not found")),
), pytest.raises(
HTTPException,
match="Wallet making this request has no public DID. Only issuers with a public DID can make this request.",
) as exc:
mock_client_from_auth.return_value.__aenter__.return_value = (
mock_aries_controller
)
await send_credential(credential=credential, auth="mocked_auth")
mock_aries_controller.issue_credential_v2_0.issue_credential_automated.assert_awaited_once()
assert exc.value.status_code == 403