From 83ba3633aef49a9b31c7227bde89778b6484a3ab Mon Sep 17 00:00:00 2001 From: Aniket Katkar Date: Thu, 28 Aug 2025 18:34:58 +0530 Subject: [PATCH] Chore(UI): Refactor auth setup to avoid unnecessary logout and login operation (#23064) * Refactor auth setup to avoid unnecessary logout and login operation * Revert initial setup refactoring * Remove the auth.teardown and logout code from auth.setup * Cleanup logic --------- Co-authored-by: Shailesh Parmar --- .../main/resources/ui/playwright.config.ts | 5 - .../resources/ui/playwright/e2e/auth.setup.ts | 60 ++++++------ .../ui/playwright/e2e/auth.teardown.ts | 22 ----- .../ui/playwright/utils/authTeardown.ts | 91 ------------------- .../ui/playwright/utils/initialSetup.ts | 10 +- 5 files changed, 34 insertions(+), 154 deletions(-) delete mode 100644 openmetadata-ui/src/main/resources/ui/playwright/e2e/auth.teardown.ts delete mode 100644 openmetadata-ui/src/main/resources/ui/playwright/utils/authTeardown.ts diff --git a/openmetadata-ui/src/main/resources/ui/playwright.config.ts b/openmetadata-ui/src/main/resources/ui/playwright.config.ts index 3f23e40e442..9a1ffdc6cf6 100644 --- a/openmetadata-ui/src/main/resources/ui/playwright.config.ts +++ b/openmetadata-ui/src/main/resources/ui/playwright.config.ts @@ -64,11 +64,6 @@ export default defineConfig({ { name: 'setup', testMatch: '**/*.setup.ts', - teardown: 'restore-policies', - }, - { - name: 'restore-policies', - testMatch: '**/auth.teardown.ts', }, { name: 'chromium', diff --git a/openmetadata-ui/src/main/resources/ui/playwright/e2e/auth.setup.ts b/openmetadata-ui/src/main/resources/ui/playwright/e2e/auth.setup.ts index 7b31aed5c0a..24a584f7b6c 100644 --- a/openmetadata-ui/src/main/resources/ui/playwright/e2e/auth.setup.ts +++ b/openmetadata-ui/src/main/resources/ui/playwright/e2e/auth.setup.ts @@ -10,7 +10,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Page, test as setup } from '@playwright/test'; +import { test as setup } from '@playwright/test'; import { EDIT_DESCRIPTION_RULE, EDIT_GLOSSARY_TERM_RULE, @@ -71,20 +71,41 @@ const ownerUser = new UserClass({ setup('authenticate all users', async ({ browser }) => { setup.setTimeout(120 * 1000); - - let adminPage: Page; - let dataConsumerPage: Page; - let dataStewardPage: Page; - let editDescriptionPage: Page; - let editTagsPage: Page; - let editGlossaryTermPage: Page; - let ownerPage: Page; + // Create separate pages for each user + const [ + adminPage, + dataConsumerPage, + dataStewardPage, + editDescriptionPage, + editTagsPage, + editGlossaryTermPage, + ownerPage, + ] = await Promise.all([ + browser.newPage(), + browser.newPage(), + browser.newPage(), + browser.newPage(), + browser.newPage(), + browser.newPage(), + browser.newPage(), + ]); try { // Create admin page and context - adminPage = await browser.newPage(); const admin = new AdminClass(); + await loginAsAdmin(adminPage, admin); + + // Create a new page to login with admin user after token expiry is set to 4 hours + // This is done to avoid logging out the user to get the new token + const newAdminPage = await browser.newPage(); + await admin.login(newAdminPage); + + // Close the leftside bar to run tests smoothly + await newAdminPage.getByTestId('sidebar-toggle').click(); + + await newAdminPage.waitForURL('**/my-data'); + const { apiContext, afterAction } = await getApiContext(adminPage); // Create all users, Using allSettled to avoid failing the setup if one of the users fails to create @@ -120,24 +141,7 @@ setup('authenticate all users', async ({ browser }) => { ]); // Save admin state - await adminPage.context().storageState({ path: adminFile }); - - // Create separate pages for each user - const [ - dataConsumerPage, - dataStewardPage, - editDescriptionPage, - editTagsPage, - editGlossaryTermPage, - ownerPage, - ] = await Promise.all([ - browser.newPage(), - browser.newPage(), - browser.newPage(), - browser.newPage(), - browser.newPage(), - browser.newPage(), - ]); + await newAdminPage.context().storageState({ path: adminFile }); // Save states for each user sequentially to avoid file operation conflicts await dataConsumer.login(dataConsumerPage); diff --git a/openmetadata-ui/src/main/resources/ui/playwright/e2e/auth.teardown.ts b/openmetadata-ui/src/main/resources/ui/playwright/e2e/auth.teardown.ts deleted file mode 100644 index bba61a4ba67..00000000000 --- a/openmetadata-ui/src/main/resources/ui/playwright/e2e/auth.teardown.ts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2025 Collate. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import { test as teardown } from '@playwright/test'; -import { AdminClass } from '../support/user/AdminClass'; -import { resetPolicyChanges } from '../utils/authTeardown'; - -teardown('restore the organization roles and policies', async ({ page }) => { - const admin = new AdminClass(); - - // Reset the default organization roles and policies - await resetPolicyChanges(page, admin); -}); diff --git a/openmetadata-ui/src/main/resources/ui/playwright/utils/authTeardown.ts b/openmetadata-ui/src/main/resources/ui/playwright/utils/authTeardown.ts deleted file mode 100644 index aa496ab961c..00000000000 --- a/openmetadata-ui/src/main/resources/ui/playwright/utils/authTeardown.ts +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2025 Collate. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import { APIRequestContext, Page } from '@playwright/test'; -import { - ORGANIZATION_POLICY_RULES, - VIEW_ALL_RULE, -} from '../constant/permission'; -import { AdminClass } from '../support/user/AdminClass'; -import { enableDisableAutoPilotApplication } from './applications'; -import { getApiContext } from './common'; - -export const restoreOrganizationDefaultRole = async ( - apiContext: APIRequestContext -) => { - const organizationTeamResponse = await apiContext - .get(`/api/v1/teams/name/Organization`) - .then((res) => res.json()); - - const dataConsumerRoleResponse = await apiContext - .get('/api/v1/roles/name/DataConsumer') - .then((res) => res.json()); - - await apiContext.patch(`/api/v1/teams/${organizationTeamResponse.id}`, { - data: [ - { - op: 'replace', - path: '/defaultRoles', - value: [ - { - id: dataConsumerRoleResponse.id, - type: 'role', - }, - ], - }, - ], - headers: { - 'Content-Type': 'application/json-patch+json', - }, - }); -}; - -export const updateDefaultOrganizationPolicy = async ( - apiContext: APIRequestContext -) => { - const orgPolicyResponse = await apiContext - .get('/api/v1/policies/name/OrganizationPolicy') - .then((response) => response.json()); - - await apiContext.patch(`/api/v1/policies/${orgPolicyResponse.id}`, { - data: [ - { - op: 'replace', - path: '/rules', - value: [...ORGANIZATION_POLICY_RULES, ...VIEW_ALL_RULE], - }, - ], - headers: { - 'Content-Type': 'application/json-patch+json', - }, - }); -}; - -const restoreRolesAndPolicies = async (page: Page) => { - const { apiContext, afterAction } = await getApiContext(page); - // Remove organization policy and role - await restoreOrganizationDefaultRole(apiContext); - // update default Organization policy - await updateDefaultOrganizationPolicy(apiContext); - // enable the AutoPilot application - await enableDisableAutoPilotApplication(apiContext); - - await afterAction(); -}; - -export const resetPolicyChanges = async (page: Page, admin: AdminClass) => { - await admin.login(page); - await page.waitForURL('**/my-data'); - await restoreRolesAndPolicies(page); - await admin.logout(page); - await page.waitForURL('**/signin'); -}; diff --git a/openmetadata-ui/src/main/resources/ui/playwright/utils/initialSetup.ts b/openmetadata-ui/src/main/resources/ui/playwright/utils/initialSetup.ts index 3bb08c29577..b7ad57bc98e 100644 --- a/openmetadata-ui/src/main/resources/ui/playwright/utils/initialSetup.ts +++ b/openmetadata-ui/src/main/resources/ui/playwright/utils/initialSetup.ts @@ -44,13 +44,7 @@ const initialSetup = async (page: Page) => { export const loginAsAdmin = async (page: Page, admin: AdminClass) => { await admin.login(page); await page.waitForURL('**/my-data'); + + // Setup policy and increase the token expiry time await initialSetup(page); - await admin.logout(page); - await page.waitForURL('**/signin'); - await admin.login(page); - - // Close the leftside bar to run tests smoothly - await page.getByTestId('sidebar-toggle').click(); - - await page.waitForURL('**/my-data'); };