From f85733a36acc97bb38255f1f520386b814c79430 Mon Sep 17 00:00:00 2001 From: John Gomersall Date: Wed, 11 Dec 2024 13:06:30 +0000 Subject: [PATCH] Test locale is set even if not specified by user Signed-off-by: John Gomersall --- tests/login.spec.ts | 7 ++++++- tests/register.spec.ts | 29 ++++------------------------- tests/test-helper.ts | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/tests/login.spec.ts b/tests/login.spec.ts index e31a909c..ca8dcad4 100644 --- a/tests/login.spec.ts +++ b/tests/login.spec.ts @@ -1,7 +1,7 @@ // @ts-check import { test, expect, Locator } from "@playwright/test"; import { INPUT_FIELD, LINK, PRIMARY_BUTTON, PRIMARY_BUTTON_HOVER } from "./expected-styles"; -import { clickEmailVerifyLink, createRedisClient, getLastEmail, getLocaleSelector, gotoHome, gotoTestPage, matchStyles, populateRegistrationForm } from "./test-helper"; +import { clickEmailVerifyLink, createRedisClient, getKeycloakHeaders, getLastEmail, getLocaleSelector, gotoHome, gotoTestPage, keycloakUserUrl, matchStyles, populateRegistrationForm } from "./test-helper"; test("login page", async ({ page }) => { await gotoHome(page); @@ -87,4 +87,9 @@ test("locale from app is respected", async ({ page }) => { await page.getByRole("button", { name: "Account" }).click(); await expect(page.getByText('^personalInfoDescription^')).toBeVisible(); + + // Fetch the user via API and make sure locale is set correctly + const headers = await getKeycloakHeaders(); + const users = await (await fetch(`${keycloakUserUrl}?exact=true&username=${userName}`, {headers})).json(); + expect(users[0].attributes.locale[0]).toBe('xx'); }); diff --git a/tests/register.spec.ts b/tests/register.spec.ts index c9fa2e5f..9f425b4f 100644 --- a/tests/register.spec.ts +++ b/tests/register.spec.ts @@ -1,6 +1,6 @@ import { test, expect } from "@playwright/test"; import { HELPER_TEXT } from "./expected-styles"; -import { createAndVerifyUser, createRedisClient, createUser, generateRandomUser, getLastEmail, getLocaleSelector, gotoHome, matchStyles, registerLink, selectDummyLocale } from "./test-helper"; +import { createAndVerifyUser, createRedisClient, createUser, generateRandomUser, getKeycloakHeaders, getLastEmail, getLocaleSelector, gotoHome, keycloakUserUrl, matchStyles, registerLink, selectDummyLocale } from "./test-helper"; test("general layout", async ({ page }) => { await gotoHome(page); @@ -75,26 +75,6 @@ test("newsletter and producer fields", async ({ page }) => { test("user created by API doesn't need email verification", async ({page}) => { const redisClient = await createRedisClient('user-registered'); - // Create a user with email verified - const formData = new URLSearchParams(); - formData.append('grant_type', 'client_credentials'); - formData.append('client_id', process.env.TEST_CLIENT_ID ?? ''); - formData.append('client_secret', process.env.TEST_CLIENT_SECRET ?? ''); - - const baseUrl = process.env.KEYCLOAK_BASE_URL; - const realmName = process.env.KEYCLOAK_REALM_NAME; - const tokenUrl = `${baseUrl}/realms/${realmName}/protocol/openid-connect/token`; - const userUrl = `${baseUrl}/admin/realms/${realmName}/users`; - - const response = await fetch(tokenUrl, - { - method: 'POST', - body: formData, - } - ); - const jwt = await response.json(); - const accessToken = jwt.access_token; - const {userName, password, email} = generateRandomUser(); const user = JSON.stringify({ email: email, @@ -115,11 +95,9 @@ test("user created by API doesn't need email verification", async ({page}) => { } }); - const headers = new Headers(); - headers.set('Authorization', 'Bearer ' + accessToken); - headers.set('Content-Type', 'application/json'); + const headers = await getKeycloakHeaders(); - const createResponse = await fetch(userUrl, {method: 'POST', body: user, headers}); + const createResponse = await fetch(keycloakUserUrl, {method: 'POST', body: user, headers}); // Should send registration message immediately const myMessage = await redisClient.getMessageForUser(userName); @@ -169,3 +147,4 @@ test("five character password not accepted", async ({ page }) => { // Verify email page will now load. Extend timeout to avoid test issues await expect(page.getByText('^invalidPasswordMinLengthMessage 0=6^')).toBeVisible(); }); + diff --git a/tests/test-helper.ts b/tests/test-helper.ts index 4f51a34d..c29838ae 100644 --- a/tests/test-helper.ts +++ b/tests/test-helper.ts @@ -1,8 +1,10 @@ import { expect, Locator, Page } from "@playwright/test"; import { createClient } from "redis"; -const keycloak = `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM_NAME}`; -export const gotoHome = async (page: Page) => await page.goto(`${keycloak}/account/#/`); +const keycloakBaseUrl = process.env.KEYCLOAK_BASE_URL; +const keycloakRealm = process.env.KEYCLOAK_REALM_NAME; +const keycloakRealmUrl = `${keycloakBaseUrl}/realms/${keycloakRealm}`; +export const gotoHome = async (page: Page) => await page.goto(`${keycloakRealmUrl}/account/#/`); export const registerLink = (page: Page) => page.getByRole("link", { name: "Create an Open Food Facts account" }); export const forgotPasswordLink = (page: Page) => page.getByRole("link", { name: "^doForgotPassword^" }); export const gotoTestPage = async (page: Page, lang?: string) => await page.goto(`http://localhost:5604/index.html?clientId=${ @@ -11,9 +13,9 @@ export const gotoTestPage = async (page: Page, lang?: string) => await page.goto process.env.TEST_CLIENT_SECRET }&lang=${ lang -}&keycloak=${encodeURIComponent(keycloak)}`); +}&keycloak=${encodeURIComponent(keycloakRealmUrl)}`); const smtp4devApi = `http://localhost:${process.env.SMTP4DEV_PORT}/api/Messages`; - +export const keycloakUserUrl = `${keycloakBaseUrl}/admin/realms/${keycloakRealm}/users`; export const matchStyles = async ( locator: Locator, properties: any @@ -145,4 +147,27 @@ export function generateRandomUser() { const email = `${userName}@openfoodfacts.org`; return {userName, password, email}; -} \ No newline at end of file +} + +export async function getKeycloakHeaders() { + const formData = new URLSearchParams(); + formData.append('grant_type', 'client_credentials'); + formData.append('client_id', process.env.TEST_CLIENT_ID ?? ''); + formData.append('client_secret', process.env.TEST_CLIENT_SECRET ?? ''); + + const tokenUrl = `${keycloakRealmUrl}/protocol/openid-connect/token`; + + const response = await fetch(tokenUrl, + { + method: 'POST', + body: formData, + } + ); + const jwt = await response.json(); + const accessToken = jwt.access_token; + + const headers = new Headers(); + headers.set('Authorization', 'Bearer ' + accessToken); + headers.set('Content-Type', 'application/json'); + return headers; +}