Skip to content

Commit dd0179e

Browse files
committed
Test error
1 parent d46f019 commit dd0179e

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

services/web/server/tests/unit/with_dbs/03/login/test_login_2fa.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# pylint: disable=unused-variable
44

55
import asyncio
6+
import logging
67
from contextlib import AsyncExitStack
78
from unittest.mock import Mock
89

@@ -12,6 +13,7 @@
1213
from aiohttp.test_utils import TestClient, make_mocked_request
1314
from faker import Faker
1415
from pytest import CaptureFixture, MonkeyPatch
16+
from pytest_mock import MockerFixture
1517
from pytest_simcore.helpers.utils_assert import assert_status
1618
from pytest_simcore.helpers.utils_envs import EnvVarsDict, setenvs_from_dict
1719
from pytest_simcore.helpers.utils_login import NewUser, parse_link, parse_test_marks
@@ -27,8 +29,10 @@
2729
get_redis_validation_code_client,
2830
send_email_code,
2931
)
32+
from simcore_service_webserver.login._constants import MSG_2FA_UNAVAILABLE_OEC
3033
from simcore_service_webserver.login.storage import AsyncpgStorage
3134
from simcore_service_webserver.products.plugin import get_current_product
35+
from twilio.base.exceptions import TwilioRestException
3236

3337

3438
@pytest.fixture
@@ -65,7 +69,7 @@ def postgres_db(postgres_db: sa.engine.Engine):
6569

6670

6771
@pytest.fixture
68-
def mocked_twilio_service(mocker) -> dict[str, Mock]:
72+
def mocked_twilio_service(mocker: MockerFixture) -> dict[str, Mock]:
6973
return {
7074
"send_sms_code_for_registration": mocker.patch(
7175
"simcore_service_webserver.login.handlers_registration.send_sms_code",
@@ -322,3 +326,56 @@ async def test_send_email_code(
322326
assert parsed_context["code"] == f"{code}"
323327
assert parsed_context["name"] == user_name.capitalize()
324328
assert parsed_context["support_email"] == support_email
329+
330+
331+
async def test_2fa_sms_failure_during_login(
332+
client: TestClient,
333+
fake_user_email: str,
334+
fake_user_password: str,
335+
fake_user_phone_number: str,
336+
caplog: pytest.LogCaptureFixture,
337+
mocker: MockerFixture,
338+
):
339+
assert client.app
340+
341+
# Mocks error in graylog https://monitoring.osparc.io/graylog/search/649e7619ce6e0838a96e9bf1?q=%222FA%22&rangetype=relative&from=172800
342+
mocker.patch(
343+
"simcore_service_webserver.login.handlers_auth.send_sms_code",
344+
autospec=True,
345+
side_effect=TwilioRestException(
346+
status=400,
347+
uri="https://www.twilio.com/doc",
348+
msg="Unable to create record: A 'From' phone number is required",
349+
),
350+
)
351+
352+
# A registered user ...
353+
async with NewUser(
354+
params={
355+
"email": fake_user_email,
356+
"password": fake_user_password,
357+
"phone": fake_user_phone_number,
358+
},
359+
app=client.app,
360+
):
361+
# ... logs in, but fails to send SMS !
362+
with caplog.at_level(logging.ERROR):
363+
url = client.app.router["auth_login"].url_for()
364+
response = await client.post(
365+
f"{url}",
366+
json={
367+
"email": fake_user_email,
368+
"password": fake_user_password,
369+
},
370+
)
371+
372+
# Expects failure:
373+
# HTTPServiceUnavailable: Currently we cannot use 2FA, please try again later (OEC:140558738809344)
374+
data, error = await assert_status(response, web.HTTPServiceUnavailable)
375+
assert not data
376+
assert error["errors"][0]["message"].startswith(
377+
MSG_2FA_UNAVAILABLE_OEC[:10]
378+
)
379+
380+
# Expects logs like 'Failed while setting up 2FA code and sending SMS to 157XXXXXXXX3 [OEC:140392495277888]'
381+
assert f"{fake_user_phone_number[:3]}" in caplog.text

0 commit comments

Comments
 (0)