Skip to content

Commit ad39981

Browse files
Redirect to calypsso pages after confirmation (#722)
### Description After an operation that involve opening an url, redirect to a nice Success or Error page from CalypSSO instead of returning directly a string. For errors, this pull request only redirect for errors that are expected to happen. For example clicking an activation link a second times, or an expired link should show a nice error. On the other hand we can expect that opening a link with an nonexisting token should never happen under normal conditions.
1 parent 12b62d2 commit ad39981

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

app/core/myeclpay/endpoints_myeclpay.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ async def init_transfer_structure_manager(
321321
async def confirm_structure_manager_transfer(
322322
token: str,
323323
db: AsyncSession = Depends(get_db),
324+
settings: Settings = Depends(get_settings),
324325
):
325326
"""
326327
Update a manager for an association
@@ -385,6 +386,13 @@ async def confirm_structure_manager_transfer(
385386
)
386387
await db.commit()
387388

389+
return RedirectResponse(
390+
url=settings.CLIENT_URL
391+
+ calypsso.get_message_relative_url(
392+
message_type=calypsso.TypeMessage.myeclpay_structure_transfer_success,
393+
),
394+
)
395+
388396

389397
@router.post(
390398
"/myeclpay/structures/{structure_id}/stores",
@@ -1379,6 +1387,7 @@ async def activate_user_device(
13791387
token: str,
13801388
db: AsyncSession = Depends(get_db),
13811389
notification_tool: NotificationTool = Depends(get_notification_tool),
1390+
settings: Settings = Depends(get_settings),
13821391
):
13831392
"""
13841393
Activate a wallet device
@@ -1396,9 +1405,11 @@ async def activate_user_device(
13961405
)
13971406

13981407
if wallet_device.status != WalletDeviceStatus.INACTIVE:
1399-
raise HTTPException(
1400-
status_code=400,
1401-
detail="Wallet device is already activated or revoked",
1408+
return RedirectResponse(
1409+
url=settings.CLIENT_URL
1410+
+ calypsso.get_message_relative_url(
1411+
message_type=calypsso.TypeMessage.myeclpay_wallet_device_already_activated_or_revoked,
1412+
),
14021413
)
14031414

14041415
await cruds_myeclpay.update_wallet_device_status(
@@ -1437,7 +1448,12 @@ async def activate_user_device(
14371448
else:
14381449
raise UnexpectedError(f"Activated wallet device {wallet_device.id} has no user") # noqa: TRY003
14391450

1440-
return "Wallet device activated"
1451+
return RedirectResponse(
1452+
url=settings.CLIENT_URL
1453+
+ calypsso.get_message_relative_url(
1454+
message_type=calypsso.TypeMessage.myeclpay_wallet_device_activation_success,
1455+
),
1456+
)
14411457

14421458

14431459
@router.post(

app/core/users/endpoints_users.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Query,
1616
UploadFile,
1717
)
18-
from fastapi.responses import FileResponse
18+
from fastapi.responses import FileResponse, RedirectResponse
1919
from fastapi.templating import Jinja2Templates
2020
from sqlalchemy.exc import IntegrityError
2121
from sqlalchemy.ext.asyncio import AsyncSession
@@ -346,6 +346,7 @@ async def activate_user(
346346
user: schemas_users.CoreUserActivateRequest,
347347
db: AsyncSession = Depends(get_db),
348348
request_id: str = Depends(get_request_id),
349+
settings: Settings = Depends(get_settings),
349350
):
350351
"""
351352
Activate the previously created account.
@@ -364,7 +365,12 @@ async def activate_user(
364365

365366
# We need to make sure the unconfirmed user is still valid
366367
if unconfirmed_user.expire_on < datetime.now(UTC):
367-
raise HTTPException(status_code=400, detail="Expired activation token")
368+
return RedirectResponse(
369+
url=settings.CLIENT_URL
370+
+ calypsso.get_message_relative_url(
371+
message_type=calypsso.TypeMessage.token_expired,
372+
),
373+
)
368374

369375
# An account with the same email may exist if:
370376
# - the user called two times the user creation endpoints and got two activation token
@@ -547,6 +553,7 @@ async def recover_user(
547553
async def reset_password(
548554
reset_password_request: schemas_users.ResetPasswordRequest,
549555
db: AsyncSession = Depends(get_db),
556+
settings: Settings = Depends(get_settings),
550557
):
551558
"""
552559
Reset the user password, using a **reset_token** provided by `/users/recover` endpoint.
@@ -560,7 +567,12 @@ async def reset_password(
560567

561568
# We need to make sure the unconfirmed user is still valid
562569
if recover_request.expire_on < datetime.now(UTC):
563-
raise HTTPException(status_code=400, detail="Expired reset token")
570+
return RedirectResponse(
571+
url=settings.CLIENT_URL
572+
+ calypsso.get_message_relative_url(
573+
message_type=calypsso.TypeMessage.token_expired,
574+
),
575+
)
564576

565577
new_password_hash = security.get_password_hash(reset_password_request.new_password)
566578
await cruds_users.update_user_password_by_id(

tests/test_myeclpay.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,9 +1157,13 @@ async def test_create_and_activate_user_device(
11571157
response = client.get(
11581158
f"/myeclpay/devices/activate?token={UNIQUE_TOKEN}",
11591159
headers={"Authorization": f"Bearer {ecl_user_access_token}"},
1160+
follow_redirects=False,
1161+
)
1162+
assert response.status_code == 307
1163+
assert response.next_request is not None
1164+
assert str(response.next_request.url).endswith(
1165+
"calypsso/message?type=myeclpay_wallet_device_activation_success",
11601166
)
1161-
assert response.status_code == 200
1162-
assert response.json() == "Wallet device activated"
11631167

11641168

11651169
async def test_activate_non_existing_device(
@@ -1179,9 +1183,13 @@ async def test_activate_already_activated_device(
11791183
response = client.get(
11801184
"/myeclpay/devices/activate?token=activation_token_ecl_user_wallet_device",
11811185
headers={"Authorization": f"Bearer {ecl_user_access_token}"},
1186+
follow_redirects=False,
1187+
)
1188+
assert response.status_code == 307
1189+
assert response.next_request is not None
1190+
assert str(response.next_request.url).endswith(
1191+
"calypsso/message?type=myeclpay_wallet_device_already_activated_or_revoked",
11821192
)
1183-
assert response.status_code == 400
1184-
assert response.json()["detail"] == "Wallet device is already activated or revoked"
11851193

11861194

11871195
async def test_revoke_user_device_unregistered_user(

0 commit comments

Comments
 (0)