From a4814bca2d926e75c55563144dfb22cc59d6c0b1 Mon Sep 17 00:00:00 2001 From: Solomon Victorino Date: Sun, 18 Aug 2024 17:20:54 -0600 Subject: [PATCH] fix(ui): prevent exceptions on other users' repo migration pages - don't expect the retry button to always be attached - don't parse status response as JSON when it was a login redirect - add E2E test --- tests/e2e/repo-migrate.test.e2e.js | 32 +++++++++++++++++++++++++++++ web_src/js/features/repo-migrate.js | 3 ++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/e2e/repo-migrate.test.e2e.js diff --git a/tests/e2e/repo-migrate.test.e2e.js b/tests/e2e/repo-migrate.test.e2e.js new file mode 100644 index 0000000000..63328e0900 --- /dev/null +++ b/tests/e2e/repo-migrate.test.e2e.js @@ -0,0 +1,32 @@ +// @ts-check +import {expect} from '@playwright/test'; +import {test, login_user, load_logged_in_context} from './utils_e2e.js'; + +test.beforeAll(({browser}, workerInfo) => login_user(browser, workerInfo, 'user2')); + +test('Migration Progress Page', async ({page: unauthedPage, browser}, workerInfo) => { + test.skip(workerInfo.project.name === 'Mobile Safari', 'Flaky actionability checks on Mobile Safari'); + + const page = await (await load_logged_in_context(browser, workerInfo, 'user2')).newPage(); + + await expect((await page.goto('/user2/invalidrepo'))?.status(), 'repo should not exist yet').toBe(404); + + await page.goto('/repo/migrate?service_type=1'); + + const form = page.locator('form'); + await form.getByRole('textbox', {name: 'Repository Name'}).fill('invalidrepo'); + await form.getByRole('textbox', {name: 'Migrate / Clone from URL'}).fill('https://codeberg.org/forgejo/invalidrepo'); + await form.locator('button.primary').click({timeout: 5000}); + await expect(page).toHaveURL('user2/invalidrepo'); + + await expect((await unauthedPage.goto('/user2/invalidrepo'))?.status(), 'public migration page should be accessible').toBe(200); + await expect(unauthedPage.locator('#repo_migrating_progress')).toBeVisible(); + + await page.reload(); + await expect(page.locator('#repo_migrating_failed')).toBeVisible(); + await page.getByRole('button', {name: 'Delete this repository'}).click(); + const deleteModal = page.locator('#delete-repo-modal'); + await deleteModal.getByRole('textbox', {name: 'Confirmation string'}).fill('user2/invalidrepo'); + await deleteModal.getByRole('button', {name: 'Delete repository'}).click(); + await expect(page).toHaveURL('/'); +}); diff --git a/web_src/js/features/repo-migrate.js b/web_src/js/features/repo-migrate.js index 490e7df0e4..fc42ce840b 100644 --- a/web_src/js/features/repo-migrate.js +++ b/web_src/js/features/repo-migrate.js @@ -7,13 +7,14 @@ export function initRepoMigrationStatusChecker() { const repoMigrating = document.getElementById('repo_migrating'); if (!repoMigrating) return; - document.getElementById('repo_migrating_retry').addEventListener('click', doMigrationRetry); + document.getElementById('repo_migrating_retry')?.addEventListener('click', doMigrationRetry); const task = repoMigrating.getAttribute('data-migrating-task-id'); // returns true if the refresh still needs to be called after a while const refresh = async () => { const res = await GET(`${appSubUrl}/user/task/${task}`); + if (res.url.endsWith('/login')) return false; // stop refreshing if redirected to login if (res.status !== 200) return true; // continue to refresh if network error occurs const data = await res.json();