diff --git a/known_failures.txt b/known_failures.txt index d07dbbf3..d9cc75ee 100644 --- a/known_failures.txt +++ b/known_failures.txt @@ -7,18 +7,6 @@ tests/api/smoke/test_api_067_org_member_permissions.py::test_api_067_only_an_adm tests/api/smoke/test_api_072_user_lookup_privacy.py::test_api_072_anonymous_user_lookup_hides_the_email # GHSA-v29h-63vw-gw4f tests/accessibility/test_a11y_008_public_layout.py::test_public_page_does_not_scroll_sideways[home-tablet] # home scrolls sideways at 768px tests/provider/smoke/test_prv_026_stored_description_html.py::test_description_html_is_not_run_on_the_public_page # stored description HTML runs on the public page -tests/consumer/smoke/test_con_auth_pages.py::TestErrorPage::test_bad_redirect_uri_is_not_blamed_on_google # DataSpaceKeycloakTheme#32 -tests/consumer/smoke/test_con_auth_pages.py::TestErrorPage::test_unknown_client_is_not_blamed_on_google # DataSpaceKeycloakTheme#32 -tests/consumer/smoke/test_con_auth_pages.py::TestForgotPasswordPage::test_email_carries_over_from_sign_in # DataSpaceKeycloakTheme#40 -tests/consumer/smoke/test_con_auth_pages.py::TestForgotPasswordPage::test_empty_request_message_matches_design # DataSpaceKeycloakTheme#40 -tests/consumer/smoke/test_con_auth_pages.py::TestForgotPasswordPage::test_request_shows_check_your_email # DataSpaceKeycloakTheme#33 -tests/consumer/smoke/test_con_auth_pages.py::TestLegalLinks::test_footer_has_privacy_terms_legal # DataSpaceKeycloakTheme#43 -tests/consumer/smoke/test_con_auth_pages.py::TestLegalLinks::test_mobile_shows_legal_links # DataSpaceKeycloakTheme#43 -tests/consumer/smoke/test_con_auth_pages.py::TestRegistrationPage::test_consent_covers_terms_and_privacy # DataSpaceKeycloakTheme#42 -tests/consumer/smoke/test_con_auth_pages.py::TestRegistrationPage::test_password_requirements_are_shown # DataSpaceKeycloakTheme#41 -tests/consumer/smoke/test_con_auth_pages.py::TestRegistrationPage::test_registration_form_renders_design_fields # DataSpaceKeycloakTheme#44 -tests/consumer/smoke/test_con_auth_pages.py::TestSignInPage::test_empty_sign_in_is_validated_inline # DataSpaceKeycloakTheme#39 -tests/consumer/smoke/test_con_auth_pages.py::TestSignInPage::test_wrong_credentials_message_matches_design # DataSpaceKeycloakTheme#39 tests/api/smoke/test_api_073_anonymous_mutations.py::test_api_073_anonymous_caller_is_refused[update-sector] # GHSA-jcj9-qw6c-98h3 tests/api/smoke/test_api_073_anonymous_mutations.py::test_api_073_anonymous_caller_is_refused[delete-sector] # GHSA-jcj9-qw6c-98h3 tests/api/smoke/test_api_073_anonymous_mutations.py::test_api_073_anonymous_caller_is_refused[update-sdg] # GHSA-jcj9-qw6c-98h3 diff --git a/locators/consumer/keycloak_auth_locators.py b/locators/consumer/keycloak_auth_locators.py index 6b73ec89..bd60425a 100644 --- a/locators/consumer/keycloak_auth_locators.py +++ b/locators/consumer/keycloak_auth_locators.py @@ -18,6 +18,7 @@ class KeycloakShellLocators: LEGAL_LINKS = (By.CSS_SELECTOR, ".civic-legal-links a") ALERT = (By.CSS_SELECTOR, ".pf-c-alert") FIELD_ERROR = (By.CSS_SELECTOR, ".civic-field-error") + PASSWORD_RULES = (By.CSS_SELECTOR, "#password-requirements li") ANY_LINK = (By.TAG_NAME, "a") @@ -55,10 +56,12 @@ class KeycloakRegisterLocators: FIRST_NAME = (By.ID, "firstName") LAST_NAME = (By.ID, "lastName") PASSWORD = (By.ID, "password") + PASSWORD_CONFIRM = (By.ID, "password-confirm") SIGN_IN_PROMPT = (By.ID, "kc-registration") GOOGLE_BROKER_LINK = (By.CSS_SELECTOR, "a[href*='/broker/google/login']") TERMS_ROW = (By.CSS_SELECTOR, ".civic-terms") TERMS_ERROR = (By.ID, "input-error-termsAccepted") + EMAIL_ERROR_SIGN_IN_LINK = (By.CSS_SELECTOR, "#input-error-email a") # The privacy consent checkbox. Note it carries required=false in the DOM, # so nothing about the markup guarantees consent is enforced - the tests @@ -68,3 +71,9 @@ class KeycloakRegisterLocators: SUBMIT = (By.CSS_SELECTOR, "input[type=submit], button[type=submit]") PRIVACY_LINK = (By.CSS_SELECTOR, "a[href*='/privacy']") BACK_TO_LOGIN = (By.CSS_SELECTOR, "a[href*='login-actions'], a[href*='protocol/openid-connect/auth']") + + +class KeycloakUpdatePasswordLocators: + NEW_PASSWORD = (By.ID, "password-new") + CONFIRM = (By.ID, "password-confirm") + SUBMIT = (By.CSS_SELECTOR, "#kc-passwd-update-form [type=submit]:not([name=cancel-aia])") diff --git a/pages/consumer/keycloak_auth_page.py b/pages/consumer/keycloak_auth_page.py index 0ac04864..18088f55 100644 --- a/pages/consumer/keycloak_auth_page.py +++ b/pages/consumer/keycloak_auth_page.py @@ -2,6 +2,7 @@ import os from selenium.common.exceptions import TimeoutException +from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait @@ -11,6 +12,7 @@ KeycloakRegisterLocators, KeycloakResetLocators, KeycloakShellLocators, + KeycloakUpdatePasswordLocators, ) from pages.base_page import BasePage @@ -21,6 +23,8 @@ VERIFY_EMAIL_MARKER = "VERIFY_EMAIL" REGISTRATION_PATH = "registrations" +# Meets the realm policy planned for #41: 8+ characters, an uppercase letter and a digit. +REGISTER_PASSWORD = "QaAuto-Register-2026" def _kc_settings(): @@ -77,6 +81,39 @@ def field_errors(self): if _squash(e.text) ] + def field_error(self, field): + """The inline error under one field, or "" when it has none.""" + return self._text((By.ID, f"input-error-{field}")) + + def password_rules(self): + """The "Password must contain" checklist as (label, state); state is met, invalid or "".""" + rules = [] + for li in self.driver.find_elements(*KeycloakShellLocators.PASSWORD_RULES): + label = _squash(li.text).replace("Not met: ", "").replace("Met: ", "") + classes = (li.get_attribute("class") or "").split() + state = "met" if "is-met" in classes else "invalid" if "is-invalid" in classes else "" + rules.append((label, state)) + return rules + + def _retype(self, locator, text): + el = self._find(locator) + el.clear() + el.send_keys(text) + + def submit_in_browser(self, submit_locator): + """Click submit; True when the theme kept the form in the browser (nothing was posted).""" + self.mark_document() + self._find(submit_locator).click() + self._wait_ready() + return not self.document_was_replaced() + + def wait_until_url_contains(self, fragment, timeout=20): + try: + WebDriverWait(self.driver, timeout).until(lambda d: fragment in d.current_url) + return True + except TimeoutException: + return False + def alert_text(self): return self._text(KeycloakShellLocators.ALERT) @@ -203,6 +240,19 @@ def submit(self): self._await(KeycloakLoginLocators.USERNAME) return self + def submit_stays_in_browser(self): + return self.submit_in_browser(KeycloakLoginLocators.SUBMIT) + + def sign_in_as(self, email, password): + """Sign in as a real (throwaway) account. Waits for Keycloak to move on from the form.""" + self.type_email(email) + self._find(KeycloakLoginLocators.PASSWORD).send_keys(password) + url_before = self.driver.current_url + self._find(KeycloakLoginLocators.SUBMIT).click() + WebDriverWait(self.driver, 20).until(lambda d: d.current_url != url_before) + self._wait_ready() + return self + def go_to_forgot_password(self): self._find(KeycloakLoginLocators.FORGOT_LINK).click() WebDriverWait(self.driver, 20).until(lambda d: "reset-credentials" in d.current_url) @@ -245,6 +295,9 @@ def request_reset(self, email): el.send_keys(email) self.mark_document() self._find(KeycloakResetLocators.SUBMIT).click() + if email: + # A filled form goes to the server; without this wait the old page's heading is read. + WebDriverWait(self.driver, 20).until(lambda d: self.document_was_replaced()) self._wait_ready() self._await(KeycloakShellLocators.HEADING) return self @@ -285,10 +338,15 @@ def has_terms_checkbox(self): def privacy_hrefs(self): return [e.get_attribute("href") for e in self.driver.find_elements(*KeycloakRegisterLocators.PRIVACY_LINK)] - def fill(self, email, first_name="QA", last_name="Automation"): + def fill(self, email, first_name="QA", last_name="Automation", password=REGISTER_PASSWORD): self._find(KeycloakRegisterLocators.EMAIL).send_keys(email) self._find(KeycloakRegisterLocators.FIRST_NAME).send_keys(first_name) self._find(KeycloakRegisterLocators.LAST_NAME).send_keys(last_name) + # The realm decides whether register asks for a password; fill it only when shown. + for locator in (KeycloakRegisterLocators.PASSWORD, KeycloakRegisterLocators.PASSWORD_CONFIRM): + el = self._find(locator) + if el is not None: + el.send_keys(password) return self def accept_terms(self): @@ -380,9 +438,54 @@ def type_password(self, text): el.send_keys(text) return True + def type_register_email(self, text): + self._retype(KeycloakRegisterLocators.EMAIL, text) + return self + + def retype_password(self, text): + self._retype(KeycloakRegisterLocators.PASSWORD, text) + return self + + def type_confirm(self, text): + self._retype(KeycloakRegisterLocators.PASSWORD_CONFIRM, text) + return self + + def submit_stays_in_browser(self): + return self.submit_in_browser(KeycloakRegisterLocators.SUBMIT) + + def email_error_sign_in_href(self): + el = self._find(KeycloakRegisterLocators.EMAIL_ERROR_SIGN_IN_LINK) + return el.get_attribute("href") if el else None + def submit_without_consent(self): """Click Create Account with consent unticked; the theme should block it.""" self.mark_document() self._find(KeycloakRegisterLocators.SUBMIT).click() self._wait_ready() return self + + +class KeycloakUpdatePasswordPage(_KeycloakPage): + """login-update-password.ftl, shown when an account has the UPDATE_PASSWORD action.""" + + def wait_loaded(self): + self._await(KeycloakUpdatePasswordLocators.NEW_PASSWORD) + return self + + def type_new(self, text): + self._retype(KeycloakUpdatePasswordLocators.NEW_PASSWORD, text) + return self + + def type_confirm(self, text): + self._retype(KeycloakUpdatePasswordLocators.CONFIRM, text) + return self + + def submit_stays_in_browser(self): + return self.submit_in_browser(KeycloakUpdatePasswordLocators.SUBMIT) + + def submit(self): + url_before = self.driver.current_url + self._find(KeycloakUpdatePasswordLocators.SUBMIT).click() + WebDriverWait(self.driver, 20).until(lambda d: d.current_url != url_before) + self._wait_ready() + return self diff --git a/tests/api/smoke/test_api_035_website_title.py b/tests/api/smoke/test_api_035_website_title.py deleted file mode 100644 index 88c0d165..00000000 --- a/tests/api/smoke/test_api_035_website_title.py +++ /dev/null @@ -1,55 +0,0 @@ -# tests/api/smoke/test_api_035_website_title.py -# -# DataSpaceFrontend#470 ("Provider Flow Update") added a new, unauthenticated -# API route, GET /api/website-title?url=, that fetches an arbitrary -# caller-supplied URL server-side and returns its . It validates the -# `url` param (present, http/https only) before fetching. -# -# The two validation tests are `readonly`: `deployed_pr` skips them on any site -# whose X-Git-Sha doesn't include #470, so they sit in the prod gate safely and -# switch on once #470 ships. The title fetch stays out of the prod gate: it -# depends on example.com being reachable from the server. - -import pytest - -pytestmark = [pytest.mark.api, pytest.mark.smoke] - - -@pytest.mark.readonly -@pytest.mark.deployed_pr("DataSpaceFrontend#470", site="HOME_URL_DEV") -def test_website_title_rejects_missing_url(dev_frontend_client): - """No `url` param must 400, not 500 or a silent null-title 200.""" - resp = dev_frontend_client.get("/api/website-title") - assert resp.status_code == 400, ( - f"expected 400 for a missing url param, got {resp.status_code}: {resp.text}" - ) - assert resp.json().get("title") is None - - -@pytest.mark.readonly -@pytest.mark.deployed_pr("DataSpaceFrontend#470", site="HOME_URL_DEV") -def test_website_title_rejects_non_http_protocol(dev_frontend_client): - """A non-http(s) URL (e.g. javascript:) must 400, confirming the protocol check runs.""" - resp = dev_frontend_client.get( - "/api/website-title", params={"url": "javascript:alert(1)"} - ) - assert resp.status_code == 400, ( - f"expected 400 for a non-http(s) url, got {resp.status_code}: {resp.text}" - ) - assert resp.json().get("title") is None - - -@pytest.mark.deployed_pr("DataSpaceFrontend#470", site="HOME_URL_DEV") -def test_website_title_fetches_a_real_page_title(dev_frontend_client): - """A valid http(s) URL must 200 with the target page's actual <title>. - - This is the path that didn't exist before #470: proven red (404) against - the pre-merge commit locally, see the PR body for both runs. - """ - resp = dev_frontend_client.get( - "/api/website-title", params={"url": "https://example.com"} - ) - assert resp.status_code == 200, f"expected 200, got {resp.status_code}: {resp.text}" - assert resp.json().get("title") == "Example Domain", ( - f"expected the fetched page's real title, got {resp.json()!r}" - ) diff --git a/tests/consumer/functional/test_con_auth_flows.py b/tests/consumer/functional/test_con_auth_flows.py new file mode 100644 index 00000000..4f995b21 --- /dev/null +++ b/tests/consumer/functional/test_con_auth_flows.py @@ -0,0 +1,195 @@ +# tests/consumer/functional/test_con_auth_flows.py +# +# End-to-end auth flows on the redesigned Keycloak pages (DataSpaceKeycloakTheme#46), +# against the realm as configured for the release: password on the register form, +# policy of 8 characters + 1 uppercase + 1 digit, and a user-writable termsAccepted. +# +# Two kinds of test live here: +# +# - TestRegisterValidation / TestSignInValidation are readonly: they type into the +# forms and submit only what the theme blocks in the browser, so nothing reaches +# the server. Safe against production. +# - The rest create throwaway qa-auto-*@example.invalid accounts in the realm dev and +# prod share. They depend on `keycloak_admin`, which skips without a service account, +# and every account is deleted in teardown whether the test passed or not. +# +# Google sign-in, linking and review profile need a real Google account and are not +# automated here. + +import os +import uuid + +import pytest + +from pages.consumer.keycloak_auth_page import ( + REGISTER_PASSWORD, + KeycloakLoginPage, + KeycloakRegisterPage, + KeycloakUpdatePasswordPage, +) +from utils.keycloak_admin import KeycloakAdmin + +pytestmark = [pytest.mark.functional, pytest.mark.regression] + +BASE_URL = os.getenv("HOME_URL_DEV", "https://dev.civicdataspace.in") + +RULES = ("At least 8 characters", "One uppercase letter", "One number") + + +def assert_handed_to_app(driver, keycloak_admin, email): + """Keycloak signed the account in and sent the browser back to the app. + + The app's NextAuth callback then rejects the code, because these tests start the flow + with their own state, so the final URL is the app's sign-in page. The session is the proof. + """ + host = driver.current_url.split("/")[2] + assert host == BASE_URL.split("/")[2], f"Still on {driver.current_url!r}, not back at the app" + assert keycloak_admin.user_session_count(email) >= 1, f"No Keycloak session for {email}" + + +@pytest.fixture +def keycloak_admin(): + if os.getenv("REGISTRATION_TESTS") == "off": + pytest.skip("REGISTRATION_TESTS=off: no accounts created in the realm dev and prod share") + admin = KeycloakAdmin() + if not admin.available(): + pytest.skip("No Keycloak service-account access: skipped rather than risk undeletable accounts") + return admin + + +@pytest.fixture +def throwaway_email(keycloak_admin): + email = f"qa-auto-{uuid.uuid4().hex[:12]}@example.invalid" + yield email + keycloak_admin.delete_user_by_email(email) + + +@pytest.fixture +def register_page(driver): + return KeycloakRegisterPage(driver).load(BASE_URL) + + +@pytest.fixture +def login_page(driver): + return KeycloakLoginPage(driver).load(BASE_URL) + + +@pytest.mark.readonly +class TestRegisterValidation: + @pytest.mark.parametrize( + "password, met", + [ + ("abc", ()), + ("abcdefgh", ("At least 8 characters",)), + ("Abcdefgh", ("At least 8 characters", "One uppercase letter")), + ("Abcdefg1", RULES), + ], + ids=["short", "length-only", "length-and-upper", "all-rules"], + ) + def test_checklist_ticks_each_rule_as_it_is_met(self, register_page, password, met): + register_page.type_password(password) + rules = dict(register_page.password_rules()) + assert tuple(rules) == RULES, f"Checklist shows {list(rules)}, realm policy is {RULES}" + ticked = tuple(label for label, state in rules.items() if state == "met") + assert ticked == met, f"For {password!r} the checklist ticks {ticked}, expected {met}" + + def test_weak_password_is_stopped_in_the_browser(self, register_page): + register_page.fill(f"qa-no-account-{uuid.uuid4().hex[:8]}@example.invalid", password="abcdefgh") + register_page.accept_terms() + assert register_page.submit_stays_in_browser(), "A password that fails the checklist was posted" + assert register_page.field_error("password") == "Password does not meet all requirements." + unmet = [label for label, state in register_page.password_rules() if state == "invalid"] + assert unmet == ["One uppercase letter", "One number"], unmet + + def test_mismatched_confirmation_is_stopped_in_the_browser(self, register_page): + register_page.fill(f"qa-no-account-{uuid.uuid4().hex[:8]}@example.invalid") + register_page.type_confirm(REGISTER_PASSWORD + "x") + register_page.accept_terms() + assert register_page.submit_stays_in_browser(), "Mismatched passwords were posted" + assert register_page.field_error("password-confirm") == "Passwords do not match." + + def test_invalid_email_is_stopped_in_the_browser(self, register_page): + register_page.fill("not-an-email") + register_page.accept_terms() + assert register_page.submit_stays_in_browser(), "An invalid email was posted" + assert register_page.field_error("email"), "No error under an invalid email" + + def test_empty_form_flags_every_field(self, register_page): + assert register_page.submit_stays_in_browser(), "An empty form was posted" + for field in ("email", "firstName", "lastName", "password", "password-confirm", "termsAccepted"): + assert register_page.field_error(field), f"No error under {field} on an empty submit" + + +@pytest.mark.readonly +class TestSignInValidation: + def test_invalid_email_is_stopped_in_the_browser(self, login_page): + login_page.type_email("not-an-email") + assert login_page.submit_stays_in_browser(), "An invalid email was posted" + assert login_page.field_error("username") == "Enter a valid email address." + + +class TestRegistration: + def test_consent_is_recorded_on_the_account(self, register_page, throwaway_email, keycloak_admin): + register_page.fill(throwaway_email).accept_terms().submit() + attributes = keycloak_admin.user_attributes(throwaway_email) + assert attributes is not None, f"No account created. Landed at {register_page.driver.current_url!r}" + assert attributes.get("termsAccepted") == ["on"], ( + f"Consent was ticked but the account has termsAccepted={attributes.get('termsAccepted')!r}. " + "The realm attribute must be editable by users or Keycloak drops it on registration." + ) + + def test_existing_email_offers_sign_in(self, register_page, throwaway_email, keycloak_admin): + assert keycloak_admin.create_user(throwaway_email, REGISTER_PASSWORD), "Could not seed the account" + register_page.fill(throwaway_email).accept_terms().submit() + assert "already exists" in register_page.field_error("email"), register_page.field_error("email") + href = register_page.email_error_sign_in_href() or "" + assert "/protocol/openid-connect/auth" in href or "login-actions/authenticate" in href, href + + +class TestSignIn: + def test_verified_account_reaches_the_app(self, login_page, throwaway_email, keycloak_admin): + assert keycloak_admin.create_user(throwaway_email, REGISTER_PASSWORD), "Could not seed the account" + login_page.sign_in_as(throwaway_email, REGISTER_PASSWORD) + assert_handed_to_app(login_page.driver, keycloak_admin, throwaway_email) + + def test_unverified_account_is_asked_to_verify(self, login_page, throwaway_email, keycloak_admin): + assert keycloak_admin.create_user( + throwaway_email, REGISTER_PASSWORD, email_verified=False, required_actions=["VERIFY_EMAIL"] + ), "Could not seed the account" + login_page.sign_in_as(throwaway_email, REGISTER_PASSWORD) + assert login_page.wait_until_url_contains("VERIFY_EMAIL"), ( + f"An unverified account was not sent to Verify email: {login_page.driver.current_url!r}" + ) + + +class TestUpdatePassword: + """The custom update-password page, reached through the UPDATE_PASSWORD required action.""" + + @pytest.fixture + def update_page(self, login_page, throwaway_email, keycloak_admin): + assert keycloak_admin.create_user( + throwaway_email, REGISTER_PASSWORD, required_actions=["UPDATE_PASSWORD"] + ), "Could not seed the account" + login_page.sign_in_as(throwaway_email, REGISTER_PASSWORD) + return KeycloakUpdatePasswordPage(login_page.driver).wait_loaded() + + def test_page_matches_design(self, update_page): + assert update_page.heading() == "Update your password" + assert update_page.subtitle() == "Choose a new password for your CivicDataSpace account." + + def test_weak_password_is_stopped_in_the_browser(self, update_page): + update_page.type_new("abcdefgh").type_confirm("abcdefgh") + assert update_page.submit_stays_in_browser(), "A password that fails the checklist was posted" + assert update_page.field_error("password-new") == "Password does not meet all requirements." + unmet = [label for label, state in update_page.password_rules() if state == "invalid"] + assert unmet == ["One uppercase letter", "One number"], unmet + + def test_mismatch_is_stopped_in_the_browser(self, update_page): + update_page.type_new("Abcdefg1").type_confirm("Abcdefg2") + assert update_page.submit_stays_in_browser(), "Mismatched passwords were posted" + assert update_page.field_error("password-confirm") == "Passwords do not match." + + def test_valid_password_continues_to_the_app(self, update_page, throwaway_email, keycloak_admin): + update_page.type_new("Newpass-2026A").type_confirm("Newpass-2026A").submit() + assert keycloak_admin.required_actions(throwaway_email) == [], "UPDATE_PASSWORD is still pending" + assert_handed_to_app(update_page.driver, keycloak_admin, throwaway_email) diff --git a/tests/consumer/smoke/test_con_auth_pages.py b/tests/consumer/smoke/test_con_auth_pages.py index dd0b0a38..6d162aba 100644 --- a/tests/consumer/smoke/test_con_auth_pages.py +++ b/tests/consumer/smoke/test_con_auth_pages.py @@ -20,9 +20,8 @@ # covers the page they point at. # # Design gaps: where production does not yet match the design, the test asserts -# the design and is marked strict xfail with the tracking issue. When a fix -# ships the test XPASSes, strict turns that into a failure, and the marker has -# to come off - at which point it guards the fix. +# the design, so it fails until the fix ships and then guards it (tracked in +# CivicDataLab/DataSpaceKeycloakTheme#32-44). import os import uuid @@ -40,14 +39,8 @@ BASE_URL = os.getenv("HOME_URL_DEV", "https://dev.civicdataspace.in") -DESIGN_REGISTER_FIELDS = ("email", "password", "password-confirm") - - -def design_gap(issue, what): - return pytest.mark.xfail( - strict=True, - reason=f"CivicDataLab/DataSpaceKeycloakTheme#{issue}: {what}", - ) +# First and last name stay on the form: the dashboard builds its links from the first name. +DESIGN_REGISTER_FIELDS = ("email", "firstName", "lastName", "password", "password-confirm") def no_account_email(): @@ -141,7 +134,6 @@ def test_password_visibility_toggle(self, login_page): login_page.toggle_password() assert login_page.password_state() == ("password", "Show password") - @design_gap(39, "empty sign-in goes to the server instead of inline required-field errors") def test_empty_sign_in_is_validated_inline(self, login_page): login_page.submit() assert not login_page.document_was_replaced(), ( @@ -150,7 +142,6 @@ def test_empty_sign_in_is_validated_inline(self, login_page): errors = login_page.field_errors() assert "Email is required." in errors and "Password is required." in errors, errors - @design_gap(39, "credential error reads 'Invalid username or password.' under Email") def test_wrong_credentials_message_matches_design(self, login_page): login_page.sign_in(no_account_email(), "NotARealPassword1") assert login_page.field_errors() == ["Email or password is incorrect."] @@ -165,18 +156,15 @@ def test_copy_matches_design(self, reset_page): assert reset_page.submit_label() == "Send Reset Link" assert reset_page.back_to_sign_in_text() == "Back to Sign In" - @design_gap(40, "email typed on Sign In is not carried to the reset form") def test_email_carries_over_from_sign_in(self, login_page): email = no_account_email() reset_page = login_page.type_email(email).go_to_forgot_password() assert reset_page.email_value() == email - @design_gap(40, "empty reset request reads 'Please specify username.'") def test_empty_request_message_matches_design(self, reset_page): reset_page.request_reset("") assert "Email is required." in reset_page.field_errors() - @design_gap(33, "reset request returns to Sign In with a banner instead of 'Check your email'") def test_request_shows_check_your_email(self, reset_page): reset_page.request_reset(no_account_email()) assert reset_page.heading() == "Check your email" @@ -215,45 +203,37 @@ def test_consent_is_enforced_in_the_browser(self, register_page): ) assert register_page.terms_error_text(), "No error shown for missing consent" - @design_gap(44, "realm renders email, firstName, lastName and no password fields") def test_registration_form_renders_design_fields(self, register_page): names = register_page.field_names() missing = [f for f in DESIGN_REGISTER_FIELDS if f not in names] - extra = [f for f in ("firstName", "lastName") if f in names] - assert not missing and not extra, ( - f"Register form fields differ from the design. Missing {missing}, " - f"not in design {extra}. Present: {names}" - ) + assert not missing, f"Register form is missing {missing}. Present: {names}" - @design_gap(41, "no live password requirements checklist (and no password field until #44)") def test_password_requirements_are_shown(self, register_page): assert register_page.type_password("abc"), "No password field on the register form" text = register_page.card_text() for line in ("Password must contain", "At least 8 characters", "One uppercase letter", "One number"): assert line in text, f"{line!r} not shown while typing a password" - @design_gap(42, "consent covers the Privacy Policy only, not Terms & Conditions") - def test_consent_covers_terms_and_privacy(self, register_page): + def test_consent_names_the_privacy_policy(self, register_page): + """Terms & Conditions are covered by the Privacy Policy, so consent names only that.""" text = register_page.terms_text() - assert "Terms & Conditions" in text and "Privacy Policy" in text, text + assert "Privacy Policy" in text, text class TestErrorPage: - @design_gap(32, "every error page is titled 'Google sign-in failed'") def test_bad_redirect_uri_is_not_blamed_on_google(self, driver): page = KeycloakErrorPage(driver).load_with_redirect(BASE_URL, "https://example.invalid/cb") assert page.heading() != "Google sign-in failed" - @design_gap(32, "every error page is titled 'Google sign-in failed'") def test_unknown_client_is_not_blamed_on_google(self, driver): page = KeycloakErrorPage(driver).load_with_client(BASE_URL, "qa-no-such-client") assert page.heading() != "Google sign-in failed" class TestLegalLinks: - @design_gap(43, "brand panel footer shows only 'Privacy'") - def test_footer_has_privacy_terms_legal(self, login_page): - assert login_page.legal_link_texts() == ["Privacy", "Terms", "Legal"] + def test_footer_has_privacy_link(self, login_page): + """Terms & Conditions and Legal are covered by the Privacy page, so it is the only link.""" + assert login_page.legal_link_texts() == ["Privacy"] @pytest.mark.mobile def test_mobile_sign_in_has_no_horizontal_scroll(self, login_page): @@ -261,7 +241,6 @@ def test_mobile_sign_in_has_no_horizontal_scroll(self, login_page): assert not login_page.has_horizontal_scroll() @pytest.mark.mobile - @design_gap(43, "no legal links at all on mobile") def test_mobile_shows_legal_links(self, login_page): login_page.use_mobile_viewport() - assert login_page.visible_legal_link_texts(), "No Privacy/Terms/Legal link visible at 390px" + assert "Privacy" in login_page.visible_legal_link_texts(), "No Privacy link visible at 390px" diff --git a/utils/keycloak_admin.py b/utils/keycloak_admin.py index 6fd7c7cf..33be940a 100644 --- a/utils/keycloak_admin.py +++ b/utils/keycloak_admin.py @@ -76,6 +76,46 @@ def find_users_by_email(self, email): def user_exists(self, email): return len(self.find_users_by_email(email)) > 0 + def user_attributes(self, email): + """Custom attributes of the user with this email, or None when there is no such user.""" + users = self.find_users_by_email(email) + return (users[0].get("attributes") or {}) if users else None + + def user_session_count(self, email): + """Active sessions of the user with this email: proof that a sign-in went through.""" + users = self.find_users_by_email(email) + if not users: + return 0 + resp = requests.get( + f"{self.base_url}/admin/realms/{self.realm}/users/{users[0]['id']}/sessions", + headers=self._headers(), + timeout=TIMEOUT, + ) + return len(resp.json()) if resp.status_code == 200 else 0 + + def required_actions(self, email): + users = self.find_users_by_email(email) + return users[0].get("requiredActions", []) if users else None + + def create_user(self, email, password, email_verified=True, required_actions=()): + """Create a signed-in-able user. The caller must delete it (delete_user_by_email).""" + resp = requests.post( + f"{self.base_url}/admin/realms/{self.realm}/users", + headers=self._headers(), + json={ + "username": email, + "email": email, + "firstName": "QA", + "lastName": "Automation", + "enabled": True, + "emailVerified": email_verified, + "requiredActions": list(required_actions), + "credentials": [{"type": "password", "value": password, "temporary": False}], + }, + timeout=TIMEOUT, + ) + return resp.status_code == 201 + def delete_user_by_email(self, email): """Delete every user matching the email. Returns how many were removed.