Skip to content

Commit

Permalink
Test locale is set even if not specified by user
Browse files Browse the repository at this point in the history
Signed-off-by: John Gomersall <[email protected]>
  • Loading branch information
john-gom committed Dec 11, 2024
1 parent c5fc3de commit f85733a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
7 changes: 6 additions & 1 deletion tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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');
});
29 changes: 4 additions & 25 deletions tests/register.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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();
});

35 changes: 30 additions & 5 deletions tests/test-helper.ts
Original file line number Diff line number Diff line change
@@ -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=${
Expand All @@ -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
Expand Down Expand Up @@ -145,4 +147,27 @@ export function generateRandomUser() {
const email = `${userName}@openfoodfacts.org`;

return {userName, password, email};
}
}

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;
}

0 comments on commit f85733a

Please sign in to comment.