Skip to content

Commit 8e87fb0

Browse files
committed
tests
1 parent e406b3e commit 8e87fb0

File tree

1 file changed

+65
-65
lines changed

1 file changed

+65
-65
lines changed

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

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,71 @@ def client(
5050
return event_loop.run_until_complete(aiohttp_client(web_server))
5151

5252

53+
async def test_reset_password_two_steps_action_confirmation_workflow(
54+
client: TestClient, login_options: LoginOptions, capsys: pytest.CaptureFixture
55+
):
56+
assert client.app
57+
58+
async with NewUser(app=client.app) as user:
59+
reset_url = client.app.router["initiate_reset_password"].url_for()
60+
response = await client.post(
61+
f"{reset_url}",
62+
json={
63+
"email": user["email"],
64+
},
65+
)
66+
assert response.url.path == reset_url.path
67+
await assert_status(response, status.HTTP_200_OK, MSG_EMAIL_SENT.format(**user))
68+
69+
out, err = capsys.readouterr()
70+
confirmation_url = parse_link(out)
71+
code = URL(confirmation_url).parts[-1]
72+
73+
# emulates user click on email url
74+
response = await client.get(confirmation_url)
75+
assert response.status == 200
76+
assert (
77+
response.url.path_qs
78+
== URL(login_options.LOGIN_REDIRECT)
79+
.with_fragment(f"reset-password?code={code}")
80+
.path_qs
81+
)
82+
83+
# api/specs/webserver/v0/components/schemas/auth.yaml#/ResetPasswordForm
84+
reset_allowed_url = client.app.router["complete_reset_password"].url_for(
85+
code=code
86+
)
87+
new_password = generate_password(10)
88+
response = await client.post(
89+
f"{reset_allowed_url}",
90+
json={
91+
"password": new_password,
92+
"confirm": new_password,
93+
},
94+
)
95+
payload = await response.json()
96+
assert response.status == 200, payload
97+
assert response.url.path == reset_allowed_url.path
98+
await assert_status(response, status.HTTP_200_OK, MSG_PASSWORD_CHANGED)
99+
100+
# Try new password
101+
logout_url = client.app.router["auth_logout"].url_for()
102+
response = await client.post(f"{logout_url}")
103+
assert response.url.path == logout_url.path
104+
await assert_status(response, status.HTTP_401_UNAUTHORIZED, "Unauthorized")
105+
106+
login_url = client.app.router["auth_login"].url_for()
107+
response = await client.post(
108+
f"{login_url}",
109+
json={
110+
"email": user["email"],
111+
"password": new_password,
112+
},
113+
)
114+
assert response.url.path == login_url.path
115+
await assert_status(response, status.HTTP_200_OK, MSG_LOGGED_IN)
116+
117+
53118
async def test_unknown_email(
54119
client: TestClient,
55120
capsys: pytest.CaptureFixture,
@@ -151,68 +216,3 @@ async def test_too_often(
151216

152217
out, _ = capsys.readouterr()
153218
assert parse_test_marks(out)["reason"] == MSG_OFTEN_RESET_PASSWORD
154-
155-
156-
async def test_reset_and_confirm(
157-
client: TestClient, login_options: LoginOptions, capsys: pytest.CaptureFixture
158-
):
159-
assert client.app
160-
161-
async with NewUser(app=client.app) as user:
162-
reset_url = client.app.router["initiate_reset_password"].url_for()
163-
response = await client.post(
164-
f"{reset_url}",
165-
json={
166-
"email": user["email"],
167-
},
168-
)
169-
assert response.url.path == reset_url.path
170-
await assert_status(response, status.HTTP_200_OK, MSG_EMAIL_SENT.format(**user))
171-
172-
out, err = capsys.readouterr()
173-
confirmation_url = parse_link(out)
174-
code = URL(confirmation_url).parts[-1]
175-
176-
# emulates user click on email url
177-
response = await client.get(confirmation_url)
178-
assert response.status == 200
179-
assert (
180-
response.url.path_qs
181-
== URL(login_options.LOGIN_REDIRECT)
182-
.with_fragment(f"reset-password?code={code}")
183-
.path_qs
184-
)
185-
186-
# api/specs/webserver/v0/components/schemas/auth.yaml#/ResetPasswordForm
187-
reset_allowed_url = client.app.router["complete_reset_password"].url_for(
188-
code=code
189-
)
190-
new_password = generate_password(10)
191-
response = await client.post(
192-
f"{reset_allowed_url}",
193-
json={
194-
"password": new_password,
195-
"confirm": new_password,
196-
},
197-
)
198-
payload = await response.json()
199-
assert response.status == 200, payload
200-
assert response.url.path == reset_allowed_url.path
201-
await assert_status(response, status.HTTP_200_OK, MSG_PASSWORD_CHANGED)
202-
203-
# Try new password
204-
logout_url = client.app.router["auth_logout"].url_for()
205-
response = await client.post(f"{logout_url}")
206-
assert response.url.path == logout_url.path
207-
await assert_status(response, status.HTTP_401_UNAUTHORIZED, "Unauthorized")
208-
209-
login_url = client.app.router["auth_login"].url_for()
210-
response = await client.post(
211-
f"{login_url}",
212-
json={
213-
"email": user["email"],
214-
"password": new_password,
215-
},
216-
)
217-
assert response.url.path == login_url.path
218-
await assert_status(response, status.HTTP_200_OK, MSG_LOGGED_IN)

0 commit comments

Comments
 (0)