Skip to content

Commit

Permalink
Change more error responses to BadRequest / Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <[email protected]>
  • Loading branch information
jamshale committed Jan 23, 2025
1 parent 82fefd9 commit b44542f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
24 changes: 12 additions & 12 deletions acapy_agent/wallet/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ async def wallet_did_list(request: web.BaseRequest):
filter_key_type = key_types.from_key_type(request.query.get("key_type", ""))
wallet: BaseWallet | None = session.inject_or(BaseWallet)
if not wallet:
raise web.HTTPForbidden(reason="No wallet available")
raise web.HTTPBadRequest(reason="No wallet available")
if filter_posture is DIDPosture.PUBLIC:
public_did_info = await wallet.get_public_did()
if (
Expand Down Expand Up @@ -698,7 +698,7 @@ async def wallet_get_public_did(request: web.BaseRequest):
async with context.session() as session:
wallet = session.inject_or(BaseWallet)
if not wallet:
raise web.HTTPForbidden(reason="No wallet available")
raise web.HTTPBadRequest(reason="No wallet available")
try:
info = await wallet.get_public_did()
except WalletError as err:
Expand Down Expand Up @@ -749,7 +749,7 @@ async def wallet_set_public_did(request: web.BaseRequest):
async with context.session() as session:
wallet = session.inject_or(BaseWallet)
if not wallet:
raise web.HTTPForbidden(reason="No wallet available")
raise web.HTTPBadRequest(reason="No wallet available")
did = request.query.get("did")
if not did:
raise web.HTTPBadRequest(reason="Request query must include DID")
Expand Down Expand Up @@ -837,7 +837,7 @@ async def promote_wallet_public_did(
if isinstance(context, InjectionContext):
is_ctx_admin_request = False
if not profile:
raise web.HTTPForbidden(
raise web.HTTPBadRequest(
reason=(
"InjectionContext is provided but no profile is provided. "
"InjectionContext does not have profile attribute but "
Expand Down Expand Up @@ -897,14 +897,14 @@ async def promote_wallet_public_did(
)

if not endorser_info:
raise web.HTTPForbidden(
raise web.HTTPBadRequest(
reason=(
"Endorser Info is not set up in "
"connection metadata for this connection record"
)
)
if "endorser_did" not in endorser_info.keys():
raise web.HTTPForbidden(
raise web.HTTPBadRequest(
reason=(
' "endorser_did" is not set in "endorser_info"'
" in connection metadata for this connection record"
Expand Down Expand Up @@ -1018,14 +1018,14 @@ async def wallet_set_did_endpoint(request: web.BaseRequest):
async with context.session() as session:
endorser_info = await connection_record.metadata_get(session, "endorser_info")
if not endorser_info:
raise web.HTTPForbidden(
raise web.HTTPBadRequest(
reason=(
"Endorser Info is not set up in "
"connection metadata for this connection record"
)
)
if "endorser_did" not in endorser_info.keys():
raise web.HTTPForbidden(
raise web.HTTPBadRequest(
reason=(
' "endorser_did" is not set in "endorser_info"'
" in connection metadata for this connection record"
Expand All @@ -1036,7 +1036,7 @@ async def wallet_set_did_endpoint(request: web.BaseRequest):
async with context.session() as session:
wallet = session.inject_or(BaseWallet)
if not wallet:
raise web.HTTPForbidden(reason="No wallet available")
raise web.HTTPBadRequest(reason="No wallet available")
try:
endpoint = mediator_endpoint or endpoint
ledger = context.profile.inject_or(BaseLedger)
Expand All @@ -1052,7 +1052,7 @@ async def wallet_set_did_endpoint(request: web.BaseRequest):
except WalletNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except LedgerConfigError as err:
raise web.HTTPForbidden(reason=err.roll_up) from err
raise web.HTTPBadRequest(reason=err.roll_up) from err
except (LedgerError, WalletError) as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

Expand Down Expand Up @@ -1257,7 +1257,7 @@ async def wallet_get_did_endpoint(request: web.BaseRequest):
async with context.session() as session:
wallet = session.inject_or(BaseWallet)
if not wallet:
raise web.HTTPForbidden(reason="No wallet available")
raise web.HTTPBadRequest(reason="No wallet available")
did = request.query.get("did")
if not did:
raise web.HTTPBadRequest(reason="Request query must include DID")
Expand Down Expand Up @@ -1295,7 +1295,7 @@ async def wallet_rotate_did_keypair(request: web.BaseRequest):
async with context.session() as session:
wallet = session.inject_or(BaseWallet)
if not wallet:
raise web.HTTPForbidden(reason="No wallet available")
raise web.HTTPBadRequest(reason="No wallet available")
try:
did_info: Optional[DIDInfo] = None
did_info = await wallet.get_local_did(did)
Expand Down
26 changes: 12 additions & 14 deletions acapy_agent/wallet/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from unittest import IsolatedAsyncioTestCase

from aiohttp.web import HTTPForbidden

from ...admin.request_context import AdminRequestContext
from ...ledger.base import BaseLedger
from ...protocols.coordinate_mediation.v1_0.route_manager import RouteManager
Expand Down Expand Up @@ -64,19 +62,19 @@ async def asyncSetUp(self):
async def test_missing_wallet(self):
self.session_inject[BaseWallet] = None

with self.assertRaises(HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_create_did(self.request)

with self.assertRaises(HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_did_list(self.request)

with self.assertRaises(HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_get_public_did(self.request)

with self.assertRaises(HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_set_public_did(self.request)

with self.assertRaises(HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
self.request.json = mock.CoroutineMock(
return_value={
"did": self.test_did,
Expand All @@ -85,7 +83,7 @@ async def test_missing_wallet(self):
)
await test_module.wallet_set_did_endpoint(self.request)

with self.assertRaises(HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_get_did_endpoint(self.request)

def test_format_did_info(self):
Expand Down Expand Up @@ -157,21 +155,21 @@ async def test_create_did_unsupported_method(self):
}
)

with self.assertRaises(test_module.web.HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_create_did(self.request)

async def test_create_did_unsupported_key_type(self):
self.request.json = mock.CoroutineMock(
return_value={"method": "sov", "options": {"key_type": "bls12381g2"}}
)
with self.assertRaises(test_module.web.HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_create_did(self.request)

async def test_create_did_indy(self):
self.request.json = mock.CoroutineMock(
return_value={"method": "indy", "options": {"key_type": ED25519.key_type}}
)
with self.assertRaises(test_module.web.HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_create_did(self.request)

async def test_create_did_method_requires_user_defined_did(self):
Expand Down Expand Up @@ -213,7 +211,7 @@ async def test_create_did_method_doesnt_support_user_defined_did(self):
)

# then
with self.assertRaises(test_module.web.HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_create_did(self.request)

async def test_create_did_x(self):
Expand Down Expand Up @@ -815,7 +813,7 @@ async def test_set_did_endpoint_public_did_no_ledger(self):
)
self.wallet.set_did_endpoint.side_effect = test_module.LedgerConfigError()

with self.assertRaises(test_module.web.HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_set_did_endpoint(self.request)

async def test_set_did_endpoint_x(self):
Expand Down Expand Up @@ -921,7 +919,7 @@ async def test_rotate_did_keypair_missing_wallet(self):
self.request.query = {"did": "did"}
self.session_inject[BaseWallet] = None

with self.assertRaises(HTTPForbidden):
with self.assertRaises(test_module.web.HTTPBadRequest):
await test_module.wallet_rotate_did_keypair(self.request)

async def test_rotate_did_keypair_no_query_did(self):
Expand Down

0 comments on commit b44542f

Please sign in to comment.