From 0512af273c26a837bc2a4a4d80abe5edc05f9d2c Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Fri, 16 Aug 2024 14:51:42 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=82(frontend)=20redirect=20to=20correc?= =?UTF-8?q?t=20url=20after=20login?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a user wanted to access a doc but was not logged in, they would be redirected to the login page. After logging in, they would be redirected to the home page. This change makes it so that they are redirected to the doc they originally wanted to access. Usefull from the mail sent to the user to access the doc they were invited to. --- .github/workflows/impress-frontend.yml | 2 +- .../apps/e2e/__tests__/app-impress/common.ts | 2 +- .../__tests__/app-impress/doc-editor.spec.ts | 13 ------ .../__tests__/app-impress/doc-export.spec.ts | 40 ++++++------------- .../__tests__/app-impress/doc-routing.spec.ts | 25 +++++++++--- .../e2e/__tests__/app-impress/header.spec.ts | 8 ++-- .../apps/impress/src/core/auth/Auth.tsx | 1 + .../apps/impress/src/core/auth/conf.ts | 1 + .../impress/src/core/auth/useAuthStore.tsx | 18 +++++++++ 9 files changed, 58 insertions(+), 52 deletions(-) create mode 100644 src/frontend/apps/impress/src/core/auth/conf.ts diff --git a/.github/workflows/impress-frontend.yml b/.github/workflows/impress-frontend.yml index d88b86554..9a607a125 100644 --- a/.github/workflows/impress-frontend.yml +++ b/.github/workflows/impress-frontend.yml @@ -99,7 +99,7 @@ jobs: test-e2e: runs-on: ubuntu-latest needs: build-front - timeout-minutes: 15 + timeout-minutes: 20 steps: - name: Checkout repository uses: actions/checkout@v4 diff --git a/src/frontend/apps/e2e/__tests__/app-impress/common.ts b/src/frontend/apps/e2e/__tests__/app-impress/common.ts index 8ca94fc09..84ce892f4 100644 --- a/src/frontend/apps/e2e/__tests__/app-impress/common.ts +++ b/src/frontend/apps/e2e/__tests__/app-impress/common.ts @@ -147,7 +147,7 @@ export const mockedDocument = async (page: Page, json: object) => { if (request.method().includes('GET') && !request.url().includes('page=')) { await route.fulfill({ json: { - id: 'b0df4343-c8bd-4c20-9ff6-fbf94fc94egg', + id: 'mocked-document-id', content: '', title: 'Mocked document', accesses: [], diff --git a/src/frontend/apps/e2e/__tests__/app-impress/doc-editor.spec.ts b/src/frontend/apps/e2e/__tests__/app-impress/doc-editor.spec.ts index 34a9a96c2..1bdde8a6f 100644 --- a/src/frontend/apps/e2e/__tests__/app-impress/doc-editor.spec.ts +++ b/src/frontend/apps/e2e/__tests__/app-impress/doc-editor.spec.ts @@ -7,19 +7,6 @@ test.beforeEach(async ({ page }) => { }); test.describe('Doc Editor', () => { - test('checks the Doc Editor interact correctly', async ({ - page, - browserName, - }) => { - const randomDoc = await createDoc(page, 'doc-editor', browserName, 1); - - await expect(page.locator('h2').getByText(randomDoc[0])).toBeVisible(); - - await page.locator('.ProseMirror.bn-editor').click(); - await page.locator('.ProseMirror.bn-editor').fill('Hello World'); - await expect(page.getByText('Hello World')).toBeVisible(); - }); - test('checks the Doc is connected to the webrtc server', async ({ page, browserName, diff --git a/src/frontend/apps/e2e/__tests__/app-impress/doc-export.spec.ts b/src/frontend/apps/e2e/__tests__/app-impress/doc-export.spec.ts index 4ec1243eb..065f84882 100644 --- a/src/frontend/apps/e2e/__tests__/app-impress/doc-export.spec.ts +++ b/src/frontend/apps/e2e/__tests__/app-impress/doc-export.spec.ts @@ -122,22 +122,14 @@ test.describe('Doc Export', () => { // Add a list await page.locator('.bn-block-outer').last().click(); await page.keyboard.press('Enter'); - await page.locator('.bn-block-outer').last().fill('Test List 1'); - await page.getByText('Test List 1').dblclick(); + await page.locator('.bn-block-outer').last().fill('/'); + await page.getByText('Bullet List').click(); await page - .getByRole('button', { - name: 'Paragraph', - }) - .click(); - await page - .getByRole('menuitem', { - name: 'Bullet List', - }) - .click(); - await page - .locator('.bn-block-content[data-content-type="bulletListItem"]') + .locator('.bn-block-content[data-content-type="bulletListItem"] p') .last() - .click(); + .fill('Test List 1'); + // eslint-disable-next-line playwright/no-wait-for-timeout + await page.waitForTimeout(300); await page.keyboard.press('Enter'); await page .locator('.bn-block-content[data-content-type="bulletListItem"] p') @@ -155,22 +147,14 @@ test.describe('Doc Export', () => { // Add a number list await page.locator('.bn-block-outer').last().click(); await page.keyboard.press('Enter'); - await page.locator('.bn-block-outer').last().fill('Test Number 1'); - await page.getByText('Test Number 1').dblclick(); + await page.locator('.bn-block-outer').last().fill('/'); + await page.getByText('Numbered List').click(); await page - .getByRole('button', { - name: 'Paragraph', - }) - .click(); - await page - .getByRole('menuitem', { - name: 'Numbered List', - }) - .click(); - await page - .locator('.bn-block-content[data-content-type="numberedListItem"]') + .locator('.bn-block-content[data-content-type="numberedListItem"] p') .last() - .click(); + .fill('Test Number 1'); + // eslint-disable-next-line playwright/no-wait-for-timeout + await page.waitForTimeout(300); await page.keyboard.press('Enter'); await page .locator('.bn-block-content[data-content-type="numberedListItem"] p') diff --git a/src/frontend/apps/e2e/__tests__/app-impress/doc-routing.spec.ts b/src/frontend/apps/e2e/__tests__/app-impress/doc-routing.spec.ts index cebbb11be..1d602a9ae 100644 --- a/src/frontend/apps/e2e/__tests__/app-impress/doc-routing.spec.ts +++ b/src/frontend/apps/e2e/__tests__/app-impress/doc-routing.spec.ts @@ -1,10 +1,12 @@ import { expect, test } from '@playwright/test'; -test.beforeEach(async ({ page }) => { - await page.goto('/'); -}); +import { keyCloakSignIn } from './common'; test.describe('Doc Routing', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/'); + }); + test('checks alias docs url with homepage', async ({ page }) => { await expect(page).toHaveURL('/'); @@ -14,9 +16,9 @@ test.describe('Doc Routing', () => { await expect(buttonCreateHomepage).toBeVisible(); - await page.goto('/docs'); + await page.goto('/docs/'); await expect(buttonCreateHomepage).toBeVisible(); - await expect(page).toHaveURL(/\/docs$/); + await expect(page).toHaveURL(/\/docs\/$/); }); test('checks 404 on docs/[id] page', async ({ page }) => { @@ -33,3 +35,16 @@ test.describe('Doc Routing', () => { }); }); }); + +test.describe('Doc Routing: Not loggued', () => { + test.use({ storageState: { cookies: [], origins: [] } }); + + test('checks redirect to a doc after login', async ({ + page, + browserName, + }) => { + await page.goto('/docs/mocked-document-id/'); + await keyCloakSignIn(page, browserName); + await expect(page).toHaveURL(/\/docs\/mocked-document-id\/$/); + }); +}); diff --git a/src/frontend/apps/e2e/__tests__/app-impress/header.spec.ts b/src/frontend/apps/e2e/__tests__/app-impress/header.spec.ts index a451f5604..6dc2a4c55 100644 --- a/src/frontend/apps/e2e/__tests__/app-impress/header.spec.ts +++ b/src/frontend/apps/e2e/__tests__/app-impress/header.spec.ts @@ -2,11 +2,11 @@ import { expect, test } from '@playwright/test'; import { keyCloakSignIn } from './common'; -test.beforeEach(async ({ page }) => { - await page.goto('/'); -}); - test.describe('Header', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/'); + }); + test('checks all the elements are visible', async ({ page }) => { const header = page.locator('header').first(); diff --git a/src/frontend/apps/impress/src/core/auth/Auth.tsx b/src/frontend/apps/impress/src/core/auth/Auth.tsx index ff89021b3..865a815fc 100644 --- a/src/frontend/apps/impress/src/core/auth/Auth.tsx +++ b/src/frontend/apps/impress/src/core/auth/Auth.tsx @@ -4,6 +4,7 @@ import { PropsWithChildren, useEffect } from 'react'; import { Box } from '@/components'; import { useAuthStore } from './useAuthStore'; + export const Auth = ({ children }: PropsWithChildren) => { const { authenticated, initAuth } = useAuthStore(); diff --git a/src/frontend/apps/impress/src/core/auth/conf.ts b/src/frontend/apps/impress/src/core/auth/conf.ts new file mode 100644 index 000000000..63a3701f0 --- /dev/null +++ b/src/frontend/apps/impress/src/core/auth/conf.ts @@ -0,0 +1 @@ +export const PATH_AUTH_LOCAL_STORAGE = 'docs-path-auth'; diff --git a/src/frontend/apps/impress/src/core/auth/useAuthStore.tsx b/src/frontend/apps/impress/src/core/auth/useAuthStore.tsx index 90dde94fb..8db59f967 100644 --- a/src/frontend/apps/impress/src/core/auth/useAuthStore.tsx +++ b/src/frontend/apps/impress/src/core/auth/useAuthStore.tsx @@ -3,6 +3,7 @@ import { create } from 'zustand'; import { baseApiUrl } from '@/core/conf'; import { User, getMe } from './api'; +import { PATH_AUTH_LOCAL_STORAGE } from './conf'; interface AuthStore { authenticated: boolean; @@ -23,9 +24,26 @@ export const useAuthStore = create((set) => ({ initAuth: () => { getMe() .then((data: User) => { + // If a path is stored in the local storage, we redirect to it + const path_auth = localStorage.getItem(PATH_AUTH_LOCAL_STORAGE); + if (path_auth) { + localStorage.removeItem(PATH_AUTH_LOCAL_STORAGE); + window.location.replace(path_auth); + return; + } + set({ authenticated: true, userData: data }); }) .catch(() => { + // If we try to access a specific page and we are not authenticated + // we store the path in the local storage to redirect to it after login + if (window.location.pathname !== '/') { + localStorage.setItem( + PATH_AUTH_LOCAL_STORAGE, + window.location.pathname, + ); + } + window.location.replace(new URL('authenticate/', baseApiUrl()).href); }); },