"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const SM_8_9_1 = require("../../fixtures/SM-8.9");
const _setup_1 = require("../../test-setup.js");
const resetSession_1 = require("../../utils/resetSession");
const sleep_1 = require("../../utils/sleep");
const node_process_1 = __importDefault(require("node:process"));
const KeycloakUtils_1 = require("../../pages/SM-8.9/KeycloakUtils");
const expectLocatorWithPagination_1 = require("../../utils/assertionHelpers/expectLocatorWithPagination");
const expectTextWithPagination_1 = require("../../utils/assertionHelpers/expectTextWithPagination");
SM_8_9_1.test.describe('Identity User Flow Tests', () => {
    SM_8_9_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    //Skipped due to bug 35735: https://github.com/camunda/camunda/issues/35735
    SM_8_9_1.test.skip('User Roles User Flow on Management Identity @tasklistV2', async ({ page, navigationPage, optimizeHomePage, managementIdentityPage, keycloakLoginPage, keycloakAdminPage, modelerHomePage, consoleHomePage, settingsPage, browser, }) => {
        if (node_process_1.default.env.IS_RBA === 'true' ||
            node_process_1.default.env.IS_MT === 'true' ||
            node_process_1.default.env.IS_LICENSE_KEY === 'true') {
            SM_8_9_1.test.skip(true, 'Skipping test because test not required in this test environment');
        }
        SM_8_9_1.test.slow();
        const testUser = (await (0, _setup_1.generateRandomStringAsync)(6)).toLowerCase();
        const password = node_process_1.default.env.DISTRO_QA_E2E_TESTS_IDENTITY_FIRSTUSER_PASSWORD;
        const credentials = {
            username: testUser,
            password: password,
        };
        await SM_8_9_1.test.step('Navigate to Keycloak and Create New User', async () => {
            await (0, KeycloakUtils_1.createUser)(navigationPage, managementIdentityPage, keycloakLoginPage, keycloakAdminPage, testUser, password);
        });
        await SM_8_9_1.test.step('Log out from Project Admin', async () => {
            await settingsPage.clickOpenUserSideBarButton();
            await settingsPage.clickLogoutButton();
            await (0, resetSession_1.resetSession)(browser, page);
        });
        await SM_8_9_1.test.step('Navigate to Identity', async () => {
            await navigationPage.goToManagementIdentity(undefined, credentials);
        });
        await SM_8_9_1.test.step('Navigate to Roles for Test User', async () => {
            await managementIdentityPage.clickUsersLink();
            await managementIdentityPage.clickUser(testUser);
            await managementIdentityPage.clickAssignedRolesTab();
        });
        if (node_process_1.default.env.IS_OPTIMIZE !== 'false') {
            await SM_8_9_1.test.step('Remove Access from Optimize', async () => {
                await managementIdentityPage.clickDeleteAccessButton('Grants full access to Optimize');
                await managementIdentityPage.clickConfirmDeleteButton();
            });
        }
        await SM_8_9_1.test.step('Remove Access from Web Modeler', async () => {
            await managementIdentityPage.clickDeleteAccessButton('Grants full access to Web Modeler');
            await managementIdentityPage.clickConfirmDeleteButton();
        });
        await SM_8_9_1.test.step('Remove Access from Console', async () => {
            await managementIdentityPage.clickDeleteAccessButton('Grants full access to Console');
            await managementIdentityPage.clickConfirmDeleteButton();
        });
        await SM_8_9_1.test.step('Reset Session', async () => {
            await (0, resetSession_1.resetSession)(browser, page);
            await (0, sleep_1.sleep)(40000);
        });
        await SM_8_9_1.test.step('Ensure Console is not Accessible', async () => {
            await navigationPage.goToConsole(undefined, credentials);
            await (0, test_1.expect)(page.getByText('Something went wrong')).toBeVisible({
                timeout: 60000,
            });
            await (0, test_1.expect)(consoleHomePage.consoleBanner).not.toBeVisible({
                timeout: 60000,
            });
        });
        await SM_8_9_1.test.step('Ensure Modeler is not Accessible', async () => {
            await navigationPage.goToModeler(undefined, credentials);
            await (0, test_1.expect)(modelerHomePage.modelerPageBanner).not.toBeVisible({
                timeout: 60000,
            });
        });
        if (node_process_1.default.env.IS_OPTIMIZE !== 'false') {
            await SM_8_9_1.test.step('Ensure Optimize is not Accessible', async () => {
                await navigationPage.goToOptimize(undefined, credentials);
                await (0, test_1.expect)(optimizeHomePage.optimizeBanner).not.toBeVisible({
                    timeout: 60000,
                });
                await (0, test_1.expect)(page.getByText('User has no authorization to access Optimize. Please check your Identity configuration')).toBeVisible({
                    timeout: 60000,
                });
            });
        }
        await SM_8_9_1.test.step('Grant Optimize, Web Modeler and Console App Permission to Test User', async () => {
            await navigationPage.goToManagementIdentity();
            await managementIdentityPage.clickUsersLink();
            await managementIdentityPage.clickUser(testUser);
            await managementIdentityPage.clickAssignedRolesTab();
            await managementIdentityPage.clickAssignRolesButton();
            if (node_process_1.default.env.IS_OPTIMIZE !== 'false') {
                await managementIdentityPage.clickOptimizeCheckbox();
            }
            await managementIdentityPage.clickModelerCheckbox();
            await managementIdentityPage.clickConsoleCheckbox();
            await managementIdentityPage.clickAddButton();
            await (0, sleep_1.sleep)(60000);
        });
        if (node_process_1.default.env.IS_OPTIMIZE !== 'false') {
            await SM_8_9_1.test.step('Ensure Optimize is Accessible', async () => {
                await navigationPage.goToOptimize(undefined, credentials);
                await (0, test_1.expect)(optimizeHomePage.optimizeBanner).toBeVisible({
                    timeout: 180000,
                });
            });
        }
        await SM_8_9_1.test.step('Ensure Modeler is Accessible', async () => {
            await navigationPage.goToModeler(undefined, credentials);
            await (0, test_1.expect)(modelerHomePage.modelerPageBanner).toBeVisible({
                timeout: 180000,
            });
        });
        await SM_8_9_1.test.step('Ensure Console is Accessible', async () => {
            await navigationPage.goToConsole(undefined, credentials);
            await (0, test_1.expect)(consoleHomePage.consoleBanner).toBeVisible({
                timeout: 180000,
            });
        });
    });
    (0, SM_8_9_1.test)('User Roles User Flow on Orchestration Cluster', async ({ page, navigationPage, ocIdentityMappingRulesPage, ocIdentityHomePage, ocIdentityRolesPage, taskPanelPage, operateHomePage, settingsPage, browser, keycloakLoginPage, keycloakAdminPage, managementIdentityPage, }) => {
        if (node_process_1.default.env.IS_RBA === 'true' ||
            node_process_1.default.env.IS_MT === 'true' ||
            node_process_1.default.env.IS_LICENSE_KEY === 'true') {
            SM_8_9_1.test.skip(true, 'Skipping test because test not required in this test environment');
        }
        SM_8_9_1.test.slow();
        const testUser = (await (0, _setup_1.generateRandomStringAsync)(6)).toLowerCase();
        const password = node_process_1.default.env.DISTRO_QA_E2E_TESTS_IDENTITY_FIRSTUSER_PASSWORD;
        const credentials = {
            username: testUser,
            password: password,
        };
        const mappingRuleId = `${testUser}-user-mapping`;
        const claimName = 'preferred_username';
        await SM_8_9_1.test.step('Navigate to Keycloak and Create New User', async () => {
            await (0, KeycloakUtils_1.createUser)(navigationPage, managementIdentityPage, keycloakLoginPage, keycloakAdminPage, testUser, password);
        });
        await SM_8_9_1.test.step('Navigate to OC Identity', async () => {
            await navigationPage.goToOCIdentity();
        });
        await SM_8_9_1.test.step('Assert Admin Mapping and No Mapping Defined for Test User', async () => {
            await ocIdentityHomePage.clickRolesTab();
            await ocIdentityRolesPage.clickRole('admin');
            await (0, test_1.expect)(ocIdentityRolesPage.roleCell('demo')).toBeVisible();
            await ocIdentityHomePage.clickMappingRulesTab();
            await (0, sleep_1.sleep)(1000);
            await (0, expectTextWithPagination_1.expectTextWithPagination)(page, testUser, { shouldBeVisible: false });
        });
        await SM_8_9_1.test.step('Log out from Project Admin', async () => {
            await settingsPage.clickOpenSettingsButton();
            await settingsPage.clickLogoutButton(); //identity bug https://github.com/camunda/camunda/issues/32382
            await (0, resetSession_1.resetSession)(browser, page); //a workaround for the bug
        });
        await SM_8_9_1.test.step('Login with Test User and Ensure Identity is not Accessible', async () => {
            await navigationPage.goToOCIdentity(undefined, credentials);
            await (0, test_1.expect)(page.getByText('You don’t have access to this component')).toBeVisible({
                timeout: 120000,
            });
            await (0, test_1.expect)(ocIdentityHomePage.adminBanner).toBeVisible({
                timeout: 10000,
            });
        });
        await SM_8_9_1.test.step('Ensure Operate is not Accessible', async () => {
            await navigationPage.goToOperate();
            await (0, test_1.expect)(page.getByText('You don’t have access to this component')).toBeVisible({
                timeout: 120000,
            });
            await (0, test_1.expect)(operateHomePage.operateBanner).toBeVisible({
                timeout: 60000,
            });
        });
        await SM_8_9_1.test.step('Ensure Tasklist is not Accessible', async () => {
            await navigationPage.goToTasklist();
            await (0, test_1.expect)(page.getByText('You don’t have access to this component')).toBeVisible({
                timeout: 120000,
            });
            await (0, test_1.expect)(taskPanelPage.tasklistBanner).toBeVisible({
                timeout: 60000,
            });
        });
        await SM_8_9_1.test.step('Log out from Test User and Log in as Main User', async () => {
            await (0, resetSession_1.resetSession)(browser, page); //a workaround for https://github.com/camunda/camunda/issues/34513
            await navigationPage.goToOCIdentity();
        });
        await SM_8_9_1.test.step('Create Mapping Rule for Test User', async () => {
            await ocIdentityHomePage.clickMappingRulesTab();
            await ocIdentityMappingRulesPage.createMappingRule(mappingRuleId, mappingRuleId, claimName, testUser);
            await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(page, ocIdentityMappingRulesPage.mappingRuleRow(mappingRuleId).first());
            await (0, test_1.expect)(ocIdentityMappingRulesPage.mappingRuleRow(mappingRuleId)).toContainText(`${mappingRuleId}${mappingRuleId}${claimName}${testUser}`);
        });
        await SM_8_9_1.test.step('Assign Admin Role Permission to Test User', async () => {
            await ocIdentityHomePage.clickRolesTab();
            await ocIdentityRolesPage.assignMappingRuleToRole('admin', mappingRuleId);
            await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(page, ocIdentityRolesPage.mappingRuleRow(mappingRuleId).first());
            await (0, test_1.expect)(ocIdentityRolesPage.mappingRuleRow(mappingRuleId)).toContainText(`${mappingRuleId}${mappingRuleId}${claimName}${testUser}`);
        });
        await SM_8_9_1.test.step('Clear cookies and reset session', async () => {
            await (0, resetSession_1.resetSession)(browser, page); //a workaround for https://github.com/camunda/camunda/issues/34513
            await (0, sleep_1.sleep)(30000);
        });
        await SM_8_9_1.test.step('Log in with Test User and Ensure Identity is Accessible', async () => {
            await navigationPage.goToOCIdentity(undefined, credentials);
            await (0, test_1.expect)(ocIdentityHomePage.adminBanner).toBeVisible({
                timeout: 10000,
            });
        });
        await SM_8_9_1.test.step('Ensure Operate is Accessible', async () => {
            await navigationPage.goToOperate();
            await (0, test_1.expect)(operateHomePage.operateBanner).toBeVisible({
                timeout: 180000,
            });
            await (0, test_1.expect)(page.getByText('You don’t have access to this component')).not.toBeVisible({
                timeout: 120000,
            });
        });
        await SM_8_9_1.test.step('Ensure Tasklist is Accessible', async () => {
            await navigationPage.goToTasklist();
            await (0, test_1.expect)(taskPanelPage.tasklistBanner).toBeVisible({ timeout: 180000 });
            await (0, test_1.expect)(page.getByText('You don’t have access to this component')).not.toBeVisible({
                timeout: 120000,
            });
        });
    });
});
