Skip to content

Commit 2159d4f

Browse files
committed
increase coverage
1 parent e71b68c commit 2159d4f

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

services/web/server/src/simcore_service_webserver/login/cli.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def invitations(
1616
user_id: int = 1,
1717
num_codes: int = 15,
1818
code_length: int = 30,
19-
):
19+
) -> None:
2020
"""Generates a list of invitation links for registration"""
2121

2222
invitation = ConfirmedInvitationData(issuer=issuer_email, trial_account_days=trial_days) # type: ignore[call-arg] # guest field is deprecated
2323

24-
codes = [generate_password(code_length) for _ in range(num_codes)]
24+
codes: list[str] = [generate_password(code_length) for _ in range(num_codes)]
2525

2626
typer.secho(
2727
"{:-^100}".format("invitations.md"),
@@ -48,19 +48,19 @@ def invitations(
4848

4949
utcnow = datetime.now(tz=UTC)
5050
today: datetime = utcnow.today()
51-
print("code,user_id,action,data,created_at", file=sys.stdout)
51+
print("code,user_id,action,data,created_at", file=sys.stdout) # noqa: T201
5252
for n, code in enumerate(codes, start=1):
53-
print(f'{code},{user_id},INVITATION,"{{', file=sys.stdout)
54-
print(
53+
print(f'{code},{user_id},INVITATION,"{{', file=sys.stdout) # noqa: T201
54+
print( # noqa: T201
5555
f'""guest"": ""invitation-{today.year:04d}{today.month:02d}{today.day:02d}-{n}"" ,',
5656
file=sys.stdout,
5757
)
58-
print(f'""issuer"" : ""{invitation.issuer}"" ,', file=sys.stdout)
59-
print(
58+
print(f'""issuer"" : ""{invitation.issuer}"" ,', file=sys.stdout) # noqa: T201
59+
print( # noqa: T201
6060
f'""trial_account_days"" : ""{invitation.trial_account_days}""',
6161
file=sys.stdout,
6262
)
63-
print('}",%s' % utcnow.isoformat(sep=" "), file=sys.stdout)
63+
print('}}",{}'.format(utcnow.isoformat(sep=" ")), file=sys.stdout) # noqa: T201
6464

6565
typer.secho(
6666
"-" * 100,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from io import StringIO
2+
3+
import simcore_service_webserver.login.cli
4+
from pytest_mock import MockerFixture
5+
from simcore_service_webserver.login.cli import invitations
6+
from yarl import URL
7+
8+
9+
def test_invitations(mocker: MockerFixture):
10+
base_url = "http://example.com"
11+
issuer_email = "[email protected]"
12+
trial_days = 7
13+
user_id = 42
14+
num_codes = 3
15+
code_length = 10
16+
17+
# Spy on generate_password to track generated codes
18+
spy_generate_password = mocker.spy(
19+
simcore_service_webserver.login.cli, "generate_password"
20+
)
21+
22+
# Mock sys.stdout to capture printed output
23+
mock_stdout = StringIO()
24+
mocker.patch("sys.stdout", new=mock_stdout)
25+
26+
invitations(
27+
base_url=base_url,
28+
issuer_email=issuer_email,
29+
trial_days=trial_days,
30+
user_id=user_id,
31+
num_codes=num_codes,
32+
code_length=code_length,
33+
)
34+
35+
output = mock_stdout.getvalue()
36+
37+
# Assert that the correct number of passwords were generated
38+
assert spy_generate_password.call_count == num_codes
39+
40+
# Collect generated codes
41+
generated_codes = spy_generate_password.spy_return_list
42+
43+
# Assert that the invitation links are correctly generated
44+
for i, code in enumerate(generated_codes, start=1):
45+
expected_url = URL(base_url).with_fragment(f"/registration/?invitation={code}")
46+
assert f"{i:2d}. {expected_url}" in output

0 commit comments

Comments
 (0)