Skip to content

Commit

Permalink
test: add logout admin e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Apr 19, 2024
1 parent 0d7fcf7 commit 8a12342
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
15 changes: 7 additions & 8 deletions pwa/components/admin/authProvider.tsx
@@ -1,25 +1,24 @@
import { AuthProvider } from "react-admin";
import { signIn, signOut } from "next-auth/react";
import { signIn, signOut, useSession } from "next-auth/react";

import { auth } from "../../app/auth";
import { OIDC_SERVER_URL } from "../../config/keycloak";

const authProvider: AuthProvider = {
// Nothing to do here, this function will never be called
login: async () => Promise.resolve(),
logout: async () => {
const session = await auth();
const { data: session } = await useSession();
if (!session) {
return;
}

await signOut(/*{
await signOut({
// @ts-ignore
callbackUrl: `${OIDC_SERVER_URL}/protocol/openid-connect/logout?id_token_hint=${session.idToken}&post_logout_redirect_uri=${window.location.origin}`,
}*/);
});
},
checkError: async (error) => {
const session = await auth();
const { data: session } = await useSession();
const status = error.status;
// @ts-ignore
if (!session || session?.error === "RefreshAccessTokenError" || status === 401) {
Expand All @@ -33,7 +32,7 @@ const authProvider: AuthProvider = {
}
},
checkAuth: async () => {
const session = await auth();
const { data: session } = await useSession();
// @ts-ignore
if (!session || session?.error === "RefreshAccessTokenError") {
await signIn("keycloak");
Expand All @@ -46,7 +45,7 @@ const authProvider: AuthProvider = {
getPermissions: () => Promise.resolve(),
// @ts-ignore
getIdentity: async () => {
const session = await auth();
const { data: session } = await useSession();

return session ? Promise.resolve(session.user) : Promise.reject();
},
Expand Down
4 changes: 2 additions & 2 deletions pwa/tests/User.spec.ts
Expand Up @@ -5,7 +5,7 @@ test.describe("User authentication", () => {
await bookPage.gotoList();
});

test("I can log in @login", async ({ userPage, page }) => {
test("I can log in Books Store @login", async ({ userPage, page }) => {
await expect(page.getByText("Log in")).toBeVisible();
await expect(page.getByText("Sign out")).toHaveCount(0);

Expand All @@ -22,7 +22,7 @@ test.describe("User authentication", () => {
await expect(page.getByText("Sign out")).toBeVisible();
});

test("I can sign out @login", async ({ userPage, page }) => {
test("I can sign out of Books Store @login", async ({ userPage, page }) => {
await page.getByText("Log in").click();
await userPage.login();
await page.getByText("Sign out").click();
Expand Down
41 changes: 41 additions & 0 deletions pwa/tests/admin/User.spec.ts
@@ -0,0 +1,41 @@
import { expect, test } from "./test";

test.describe("User authentication", () => {
test.beforeEach(async ({ bookPage }) => {
await bookPage.gotoList();
});

test("I can log in Admin @login", async ({ userPage, page }) => {
await expect(page.getByText("Log in")).toBeVisible();
await expect(page.getByText("Sign out")).toHaveCount(0);

await page.getByText("Log in").click();
await page.getByText("Log in").waitFor({ state: "hidden" });
// @ts-ignore assert declared on test.ts
await expect(page).toBeOnLoginPage();
await expect(page.locator("#kc-header-wrapper")).toContainText("API Platform - Demo");
await expect(page.locator("#kc-form-login")).toContainText("Login as user: john.doe@example.com");
await expect(page.locator("#kc-form-login")).toContainText("Login as admin: chuck.norris@example.com");
await userPage.login();

await expect(page.getByText("Log in")).toHaveCount(0);
await expect(page.getByText("Sign out")).toBeVisible();
});

test("I can sign out of Admin @login", async ({ userPage, page }) => {
await page.getByText("Log in").click();
await userPage.login();
await page.getByText("Sign out").click();

await expect(page.getByText("Log in")).toBeVisible();
await expect(page.getByText("Sign out")).toHaveCount(0);

// I should be logged out from Keycloak also
await page.getByText("Log in").click();
// @ts-ignore assert declared on test.ts
await expect(page).toBeOnLoginPage();
await expect(page.locator("#kc-header-wrapper")).toContainText("API Platform - Demo");
await expect(page.locator("#kc-form-login")).toContainText("Login as user: john.doe@example.com");
await expect(page.locator("#kc-form-login")).toContainText("Login as admin: chuck.norris@example.com");
});
});
4 changes: 4 additions & 0 deletions pwa/tests/admin/pages/UserPage.ts
@@ -0,0 +1,4 @@
import { AbstractPage } from "./AbstractPage";

export class UserPage extends AbstractPage {
}
5 changes: 5 additions & 0 deletions pwa/tests/admin/test.ts
Expand Up @@ -3,10 +3,12 @@ import { test as playwrightTest } from "@playwright/test";
import { expect } from "../test";
import { BookPage } from "./pages/BookPage";
import { ReviewPage } from "./pages/ReviewPage";
import { UserPage } from "./pages/UserPage";

type Test = {
bookPage: BookPage,
reviewPage: ReviewPage,
userPage: UserPage,
}

export const test = playwrightTest.extend<Test>({
Expand All @@ -16,6 +18,9 @@ export const test = playwrightTest.extend<Test>({
reviewPage: async ({ page }, use) => {
await use(new ReviewPage(page));
},
userPage: async ({ page }, use) => {
await use(new UserPage(page));
},
});

export { expect };

0 comments on commit 8a12342

Please sign in to comment.