|
| 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() |
0 commit comments