"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const SM_8_7_1 = require("../../fixtures/SM-8.7");
const _setup_1 = require("../../test-setup.js");
const sleep_1 = require("../../utils/sleep");
const UtlitiesPage_1 = require("../../pages/SM-8.7/UtlitiesPage");
const env_1 = require("../../utils/env");
const KeycloakUtils_1 = require("../../pages/SM-8.7/KeycloakUtils");
const loggingUtils_1 = require("../../utils/loggingUtils");
SM_8_7_1.test.describe('SM Setup Tests', () => {
    SM_8_7_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    (0, SM_8_7_1.test)('User Roles User Flow', async ({ page, context, navigationPage, taskPanelPage, operateHomePage, optimizeHomePage, identityPage, modelerHomePage, consoleHomePage, keycloakLoginPage, keycloakAdminPage, }, testInfo) => {
        (0, loggingUtils_1.setupTestLogging)(testInfo);
        if (process.env.IS_RBA === 'true' ||
            process.env.IS_MT === 'true' ||
            process.env.IS_LICENSE_KEY === 'true' ||
            process.env.IS_SMOKE === 'true') {
            SM_8_7_1.test.skip(true, 'Skipping test because test not required in this test environment');
        }
        const adminUser = 'demo';
        const adminPassword = process.env.DISTRO_QA_E2E_TESTS_IDENTITY_FIRSTUSER_PASSWORD;
        const testUsername = `test-user-${new Date().getTime()}-${Math.random()
            .toString(36)
            .substring(7)}`;
        const testPassword = 'test-password';
        const setAdminUserCredentials = () => {
            navigationPage.activeUser = adminUser;
            navigationPage.activePassword = adminPassword;
        };
        const setTestUserCredentials = () => {
            navigationPage.activeUser = testUsername;
            navigationPage.activePassword = testPassword;
        };
        SM_8_7_1.test.slow();
        await SM_8_7_1.test.step('Create user in keycloak', async () => {
            await navigationPage.goToKeycloak();
            await keycloakLoginPage.toBeVisible();
            await keycloakLoginPage.fillUsername((0, KeycloakUtils_1.keycloakAdminCredentials)().username);
            await keycloakLoginPage.fillPassword((0, KeycloakUtils_1.keycloakAdminCredentials)().password);
            await keycloakLoginPage.clickLogin();
            await keycloakAdminPage.toBeVisible();
            await keycloakAdminPage.switchToCamundaPlatform();
            await keycloakAdminPage.clickUsersTab();
            await keycloakAdminPage.clickAddUser();
            await keycloakAdminPage.fillUser(testUsername);
            await keycloakAdminPage.saveUser();
            await keycloakAdminPage.fillPassword(testPassword);
            await keycloakAdminPage.savePassword();
        });
        await SM_8_7_1.test.step('Grant Apps Permission to New User', async () => {
            setAdminUserCredentials();
            await navigationPage.goToIdentity();
            await identityPage.clickUsersLink();
            await identityPage.clickUser(testUsername);
            await identityPage.clickAssignedRolesTab();
            await identityPage.clickAssignRolesButton();
            await identityPage.clickOperateCheckbox();
            await identityPage.clickTasklistCheckbox();
            await identityPage.clickOptimizeCheckbox();
            await identityPage.clickModelerCheckbox();
            await identityPage.clickConsoleCheckbox();
            await identityPage.clickAddButton();
        });
        await SM_8_7_1.test.step('Ensure Apps are Accessible', async () => {
            await (0, sleep_1.sleep)(100); // lets not clear the cookies before the Ajax has completed
            await context.clearCookies();
            setTestUserCredentials();
            await navigationPage.goToOperate();
            await (0, test_1.expect)(operateHomePage.operateBanner).toBeVisible({
                timeout: 180000,
            });
            await navigationPage.goToTasklist();
            await (0, test_1.expect)(taskPanelPage.tasklistBanner).toBeVisible({ timeout: 180000 });
            await navigationPage.goToOptimize();
            await (0, test_1.expect)(optimizeHomePage.optimizeBanner).toBeVisible({
                timeout: 180000,
            });
            await navigationPage.goToModeler();
            await (0, test_1.expect)(modelerHomePage.modelerPageBanner).toBeVisible({
                timeout: 180000,
            });
            await navigationPage.goToConsole();
            await (0, test_1.expect)(consoleHomePage.consoleBanner).toBeVisible({
                timeout: 180000,
            });
        });
        await SM_8_7_1.test.step('Remove Access from Operate,Optimize, Tasklist, Web Modeler and Console from Main User', async () => {
            await context.clearCookies();
            setAdminUserCredentials();
            await navigationPage.goToIdentity();
            await identityPage.clickUsersLink();
            await identityPage.clickUser(testUsername);
            await identityPage.clickAssignedRolesTab();
            await identityPage.clickDeleteAccessButton('Grants full access to Operate');
            await identityPage.clickConfirmDeleteButton();
            if (!env_1.isOpenSearch) {
                await identityPage.clickDeleteAccessButton('Grants full access to Optimize');
                await identityPage.clickConfirmDeleteButton();
            }
            await identityPage.clickDeleteAccessButton('Grants full access to Tasklist');
            await identityPage.clickConfirmDeleteButton();
            await identityPage.clickDeleteAccessButton('Grants full access to Web Modeler');
            await identityPage.clickConfirmDeleteButton();
            await identityPage.clickDeleteAccessButton('Grants elevated access to Web Modeler');
            await identityPage.clickConfirmDeleteButton();
            await identityPage.clickDeleteAccessButton('Grants full access to Console');
            await identityPage.clickConfirmDeleteButton();
        });
        await SM_8_7_1.test.step('Ensure Apps are not Accessible', async () => {
            await context.clearCookies();
            //No permission for Optimize
            setTestUserCredentials();
            if (!env_1.isOpenSearch) {
                await navigationPage.goToOptimize(undefined);
                await (0, UtlitiesPage_1.assertPageTextWithRetry)(page, 'User has no authorization to access Optimize. Please check your Identity configuration', false, 30000);
                await (0, test_1.expect)(optimizeHomePage.optimizeBanner).not.toBeVisible({
                    timeout: 60000,
                });
            }
            //No permission for Tasklist
            await navigationPage.goToTasklist(undefined);
            await (0, UtlitiesPage_1.assertPageTextWithRetry)(page, 'No permission for Tasklist - Please check your configuration.', false, 30000);
            await (0, test_1.expect)(taskPanelPage.tasklistBanner).not.toBeVisible({
                timeout: 60000,
            });
            //No permission for Operate
            await navigationPage.goToOperate(undefined);
            await (0, test_1.expect)(page.getByText('404 Not Found')).toBeVisible({
                timeout: 120000,
            });
            await (0, test_1.expect)(operateHomePage.operateBanner).not.toBeVisible({
                timeout: 60000,
            });
            //No permission for Console
            await navigationPage.goToConsole(undefined);
            await (0, test_1.expect)(page.getByText('Something went wrong')).toBeVisible({
                timeout: 60000,
            });
            await (0, test_1.expect)(consoleHomePage.consoleBanner).not.toBeVisible({
                timeout: 60000,
            });
            //No permission for Modeler
            await navigationPage.goToModeler(undefined);
            await (0, test_1.expect)(page.getByText('Log in to camunda-platform')).toBeVisible({
                timeout: 60000,
            });
        });
        await SM_8_7_1.test.step('Grant Apps Permission to Main User', async () => {
            await navigationPage.goToIdentity();
            await identityPage.clickUsersLink();
            await identityPage.clickDemoUser();
            await identityPage.clickAssignedRolesTab();
            await identityPage.clickAssignRolesButton();
            await identityPage.clickOperateCheckbox();
            await identityPage.clickTasklistCheckbox();
            if (!env_1.isOpenSearch) {
                await identityPage.clickOptimizeCheckbox();
            }
            await identityPage.clickModelerCheckbox();
            await identityPage.clickConsoleCheckbox();
            await identityPage.clickAddButton();
            // Role propagation is verified by the next step's 180s-timeout assertions.
            // A brief pause ensures the Identity API call completes before navigation.
            await (0, sleep_1.sleep)(5000);
        });
        await SM_8_7_1.test.step('Ensure Apps are Accessible', async () => {
            await navigationPage.goToOperate();
            await (0, test_1.expect)(operateHomePage.operateBanner).toBeVisible({
                timeout: 180000,
            });
            await navigationPage.goToTasklist();
            await (0, test_1.expect)(taskPanelPage.tasklistBanner).toBeVisible({ timeout: 180000 });
            if (!env_1.isOpenSearch) {
                await navigationPage.goToOptimize();
                await (0, test_1.expect)(optimizeHomePage.optimizeBanner).toBeVisible({
                    timeout: 180000,
                });
            }
            await navigationPage.goToModeler();
            await (0, test_1.expect)(modelerHomePage.modelerPageBanner).toBeVisible({
                timeout: 180000,
            });
            await navigationPage.goToConsole();
            await (0, test_1.expect)(consoleHomePage.consoleBanner).toBeVisible({
                timeout: 180000,
            });
            await (0, test_1.expect)(modelerHomePage.modelerPageBanner).not.toBeVisible({
                timeout: 60000,
            });
        });
    });
});
