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 <shailesh.parmar.webdev@gmail.com>
This commit is contained in:
Aniket Katkar 2025-08-28 18:34:58 +05:30
parent 92682f6992
commit 83ba3633ae
5 changed files with 34 additions and 154 deletions

View File

@ -64,11 +64,6 @@ export default defineConfig({
{
name: 'setup',
testMatch: '**/*.setup.ts',
teardown: 'restore-policies',
},
{
name: 'restore-policies',
testMatch: '**/auth.teardown.ts',
},
{
name: 'chromium',

View File

@ -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);

View File

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

View File

@ -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');
};

View File

@ -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');
};