n8n/packages/testing/playwright/composables/MfaComposer.ts
Declan Carroll f96cdb17db
Some checks are pending
CI: Master (Build, Test, Lint) / Build for Github Cache (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (22.x) (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (24.14.1) (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (25.x) (push) Waiting to run
CI: Master (Build, Test, Lint) / Lint (push) Waiting to run
CI: Master (Build, Test, Lint) / Performance (push) Waiting to run
CI: Master (Build, Test, Lint) / Notify Slack on failure (push) Blocked by required conditions
test: Resolve 20 janitor scope-lockdown and dead-code violations (#27948)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-02 17:32:27 +00:00

70 lines
2.3 KiB
TypeScript

import { expect } from '@playwright/test';
import { authenticator } from 'otplib';
import type { n8nPage } from '../pages/n8nPage';
export class MfaComposer {
constructor(private readonly n8n: n8nPage) {}
/**
* Enable MFA for a user using predefined secret
* @param email - User email
* @param password - User password
* @param mfaSecret - Known MFA secret to use for token generation
*/
async enableMfa(email: string, password: string, mfaSecret: string): Promise<void> {
await this.n8n.signIn.loginWithEmailAndPassword(email, password, true);
await this.n8n.settingsPersonal.goto();
await this.n8n.settingsPersonal.clickEnableMfa();
await this.n8n.mfaSetupModal.container.waitFor({ state: 'visible' });
await this.n8n.mfaSetupModal.clickCopySecretToClipboard();
const token = authenticator.generate(mfaSecret);
await this.n8n.mfaSetupModal.fillToken(token);
await expect(this.n8n.mfaSetupModal.getDownloadRecoveryCodesButton()).toBeVisible();
await this.n8n.mfaSetupModal.clickDownloadRecoveryCodes();
await this.n8n.mfaSetupModal.clickSave();
await this.n8n.mfaSetupModal.waitForHidden();
}
/**
* Login with MFA code
* @param email - User email
* @param password - User password
* @param mfaSecret - Known MFA secret for token generation
*/
async loginWithMfaCode(email: string, password: string, mfaSecret: string): Promise<void> {
await this.n8n.signIn.fillEmail(email);
await this.n8n.signIn.fillPassword(password);
await this.n8n.signIn.clickSubmit();
await expect(this.n8n.mfaLogin.getForm()).toBeVisible();
const loginMfaCode = authenticator.generate(mfaSecret);
await this.n8n.mfaLogin.submitMfaCode(loginMfaCode);
await expect(this.n8n.page).toHaveURL(/workflows/);
}
/**
* Login with MFA recovery code
* @param email - User email
* @param password - User password
* @param recoveryCode - Known recovery code
*/
async loginWithMfaRecoveryCode(
email: string,
password: string,
recoveryCode: string,
): Promise<void> {
await this.n8n.signIn.fillEmail(email);
await this.n8n.signIn.fillPassword(password);
await this.n8n.signIn.clickSubmit();
await expect(this.n8n.mfaLogin.getForm()).toBeVisible();
await this.n8n.mfaLogin.submitMfaRecoveryCode(recoveryCode);
await expect(this.n8n.page).toHaveURL(/workflows/);
}
}