Skip to content

Commit d651983

Browse files
committed
frontend/api: update tests + local dev setup for changes in registration + edit profile
1 parent 11a6321 commit d651983

8 files changed

Lines changed: 137 additions & 9 deletions

File tree

packages/divbase-api/src/divbase_api/db.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ async def create_first_admin_user() -> None:
110110
name="First Admin",
111111
email=admin_email,
112112
password=admin_password,
113+
confirm_password=admin_password,
113114
organisation="DivBase University",
114115
organisation_role="Developer",
115116
)

packages/divbase-api/src/divbase_api/templates/components/registration_form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ <h2>Create an account </h2>
8585
</div>
8686

8787
<div class="mb-3">
88-
<label for="role_dropdown" class="form-label">Role at Organisation</label>
88+
<label for="role_dropdown" class="form-label">Role</label>
8989
<div class="input-group">
9090
<span class="input-group-text">
9191
<i class="bi bi-briefcase"></i>

packages/divbase-api/src/divbase_api/templates/profile_pages/edit_profile.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ <h5 class="mb-0"><i class="bi bi-person-gear me-2"></i>Personal Information</h5>
7777
</div>
7878

7979
<div class="mb-3">
80-
<label for="role_dropdown" class="form-label">Role at Organisation</label>
80+
<label for="role_dropdown" class="form-label">Role</label>
8181
<div class="input-group">
8282
<span class="input-group-text">
8383
<i class="bi bi-briefcase"></i>

scripts/local_dev_setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ def create_users(token: str) -> dict[str, int]:
240240
"organisation": user_data["organisation"],
241241
"organisation_role": user_data["organisation_role"],
242242
"password": user_data["password"],
243+
"confirm_password": user_data["password"],
243244
},
244245
params={"email_verified": True},
245246
)

tests/e2e_integration/helpers/setup_test_data.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
"password": "badpassword",
6464
"organisation": "European Bioinformatics Institute",
6565
"organisation_role": "Research Assistant",
66+
}, # users name, organisation and role can be changed in some tests
67+
"profile_edit user": {
68+
"email": "profile_edit_user@divbase.se",
69+
"password": "badpassword",
70+
"organisation": "Stockholm University",
71+
"organisation_role": "Researcher",
6672
},
6773
}
6874

@@ -225,6 +231,7 @@ def create_users(token: str) -> dict[str, int]:
225231
"organisation": creds["organisation"],
226232
"organisation_role": creds["organisation_role"],
227233
"password": creds["password"],
234+
"confirm_password": creds["password"],
228235
},
229236
params={"email_verified": True},
230237
)

tests/e2e_integration/playwright/conftest.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88
from playwright.sync_api import Browser, BrowserContext, Page, expect
99

10-
from divbase_api.api_constants import SWEDISH_UNIVERSITIES
10+
from divbase_api.api_constants import KNOWN_JOB_ROLES, SWEDISH_UNIVERSITIES
1111

1212
FRONTEND_BASE_URL = "http://localhost:8001"
1313
MAILPIT_BASE_URL = "http://localhost:8026"
@@ -50,9 +50,11 @@ def register_new_user(
5050
We provide a dropdown of Swedish universities, but also allow users to enter their own if not in the list.
5151
This function handles both cases for all tests that need to perform a user registration.
5252
"""
53-
# This displays only if user selects "Other" in the dropdown
53+
# These display only if user selects "Other" in the dropdown
5454
other_organisation_input = page.get_by_role("textbox", name="Your organisation")
55+
other_role_input = page.get_by_role("textbox", name="Your role")
5556
expect(other_organisation_input).to_be_hidden()
57+
expect(other_role_input).to_be_hidden()
5658

5759
navigate_to(page, "/register")
5860
expect(page).to_have_url(f"{FRONTEND_BASE_URL}/register")
@@ -68,7 +70,14 @@ def register_new_user(
6870
expect(other_organisation_input).to_be_visible()
6971
other_organisation_input.fill(organisation)
7072

71-
page.get_by_role("textbox", name="Role").fill(role)
73+
if role in KNOWN_JOB_ROLES:
74+
page.get_by_label("Role", exact=True).select_option(role)
75+
expect(other_role_input).to_be_hidden()
76+
else:
77+
page.get_by_label("Role", exact=True).select_option("Other")
78+
expect(other_role_input).to_be_visible()
79+
other_role_input.fill(role)
80+
7281
page.get_by_role("textbox", name="Password", exact=True).fill(password)
7382
page.get_by_role("textbox", name="Confirm Password").fill(password)
7483
page.get_by_role("button", name=" Create Account").click()
@@ -90,6 +99,7 @@ def EXISTING_ACCOUNTS(CONSTANTS):
9099
"MANAGE_USER": CONSTANTS["TEST_USERS"]["manage user"],
91100
"FORGOT_PASSWORD_USER": CONSTANTS["TEST_USERS"]["forgot_password user"],
92101
"PASSWORD_RESET_USER": CONSTANTS["TEST_USERS"]["password_reset user"],
102+
"PROFILE_EDIT_USER": CONSTANTS["TEST_USERS"]["profile_edit user"],
93103
}
94104

95105

@@ -159,3 +169,11 @@ def logged_in_read_page(browser: Browser, EXISTING_ACCOUNTS):
159169
page = _create_logged_in_user_page(browser, EXISTING_ACCOUNTS["READ_USER"])
160170
yield page
161171
page.close()
172+
173+
174+
@pytest.fixture
175+
def logged_in_edit_profile_user_page(browser: Browser, EXISTING_ACCOUNTS):
176+
"""Logged in edit profile user at home page."""
177+
page = _create_logged_in_user_page(browser, EXISTING_ACCOUNTS["PROFILE_EDIT_USER"])
178+
yield page
179+
page.close()
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""
2+
Test edit profile page functionality using Playwright.
3+
"""
4+
5+
import pytest
6+
from playwright.sync_api import Page, expect
7+
8+
from divbase_api.api_constants import KNOWN_JOB_ROLES, SWEDISH_UNIVERSITIES
9+
10+
from .conftest import FRONTEND_BASE_URL, navigate_to
11+
12+
13+
def _edit_profile(
14+
page: Page,
15+
new_name: str,
16+
new_organisation: str,
17+
new_role: str,
18+
):
19+
"""
20+
Helper function to edit the profile of a user.
21+
Handles the "Other" option for organisation and role.
22+
"""
23+
navigate_to(page=page, path="/profile/edit")
24+
25+
organisation_input = page.get_by_label("Organisation", exact=True)
26+
role_input = page.get_by_label("Role", exact=True)
27+
other_organisation_input = page.get_by_role("textbox", name="Your organisation")
28+
other_role_input = page.get_by_role("textbox", name="Your Role")
29+
30+
page.get_by_role("textbox", name="Full Name").fill(new_name)
31+
32+
if new_organisation in SWEDISH_UNIVERSITIES:
33+
organisation_input.select_option(new_organisation)
34+
expect(other_organisation_input).to_be_hidden()
35+
else:
36+
organisation_input.select_option("Other")
37+
expect(other_organisation_input).to_be_visible()
38+
other_organisation_input.fill(new_organisation)
39+
40+
if new_role in KNOWN_JOB_ROLES:
41+
role_input.select_option(new_role)
42+
expect(other_role_input).to_be_hidden()
43+
else:
44+
role_input.select_option("Other")
45+
expect(other_role_input).to_be_visible()
46+
other_role_input.fill(new_role)
47+
48+
page.get_by_role("button", name="Update Profile").click()
49+
expect(page).to_have_url(f"{FRONTEND_BASE_URL}/profile/")
50+
51+
expect(page.get_by_text(new_name)).to_be_visible()
52+
expect(page.get_by_text(new_organisation)).to_be_visible()
53+
expect(page.get_by_text(new_role)).to_be_visible()
54+
55+
56+
@pytest.mark.parametrize(
57+
"new_name, new_organisation, new_role",
58+
[
59+
("Updated Profile User", "Uppsala University", "Postdoctoral Researcher"),
60+
("Another User", "Other Organisation", "Other Role"),
61+
("Testing User", "Lund University", "PhD Student"),
62+
("Onenameuser", "Lund University", "Non-dropdown Role"),
63+
("Test Test User", "Other Organisation", "PhD Student"),
64+
],
65+
)
66+
def test_edit_profile(
67+
logged_in_edit_profile_user_page: Page,
68+
new_name: str,
69+
new_organisation: str,
70+
new_role: str,
71+
):
72+
"""
73+
Test editing the profile of a user, including handling of "Other" option for organisation and role.
74+
"""
75+
_edit_profile(
76+
page=logged_in_edit_profile_user_page,
77+
new_name=new_name,
78+
new_organisation=new_organisation,
79+
new_role=new_role,
80+
)
81+
82+
83+
def test_edit_profile_cancel_does_not_save_changes(logged_in_edit_profile_user_page: Page):
84+
"""
85+
Test canceling the profile edit with changes made on the form does not save the changes.
86+
"""
87+
page = logged_in_edit_profile_user_page
88+
89+
navigate_to(page=page, path="/profile/edit")
90+
91+
page.get_by_role("textbox", name="Full Name").fill("Cancelled name")
92+
page.get_by_label("Organisation", exact=True).select_option("Other")
93+
page.get_by_role("textbox", name="Your organisation").fill("Cancelled Organisation")
94+
page.get_by_label("Role", exact=True).select_option("Other")
95+
page.get_by_role("textbox", name="Your Role").fill("Cancelled Role")
96+
97+
page.get_by_role("link", name="Cancel").click()
98+
expect(page).to_have_url(f"{FRONTEND_BASE_URL}/profile/")
99+
100+
for cancelled_text in ["Cancelled Update", "Cancelled Organisation", "Cancelled Role"]:
101+
expect(page.get_by_text(cancelled_text)).not_to_be_visible()

tests/e2e_integration/playwright/test_register.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"New Test User EMBL",
2727
"newuser@embl.de",
2828
"European Bioinformatics Institute (EMBL-EBI)",
29-
"Developer",
29+
"A job role not in the dropdown",
3030
),
3131
],
3232
)
@@ -41,8 +41,8 @@ def test_register_new_user_complete_flow(
4141
"""
4242
Test complete user registration flow including email verification step.
4343
44-
We test creating a user from a Swedish university (in the organisation dropdown)
45-
and a user from an organisation not in the dropdown to ensure both flows work correctly.
44+
We test creating a user whose organisation and role are in the dropdowns AND
45+
a user with an organisation and role not in the dropdowns (so they select "Other" and manully fill out there roles).
4646
"""
4747
test_password = "badpassword"
4848
register_new_user(
@@ -90,7 +90,7 @@ def test_register_with_existing_email(page: Page, EXISTING_ACCOUNTS):
9090
email=existing_email,
9191
organisation="Uppsala University",
9292
role="Lecturer",
93-
password="newpassword",
93+
password="badpassword",
9494
expect_success=False,
9595
)
9696
expect(page.get_by_text(GENERIC_REGISTRATION_ERROR_MESSAGE)).to_be_visible()

0 commit comments

Comments
 (0)