| | import fs from 'fs/promises'; |
| | import path from 'path'; |
| | import chalk from 'chalk'; |
| | import { BrowserContext, Page } from 'playwright'; |
| | import { TestAccountName } from '..'; |
| | import { getAccountSiteURL, getCalypsoURL } from '../data-helper'; |
| | import { EmailClient } from '../email-client'; |
| | import envVariables from '../env-variables'; |
| | import { SecretsManager } from '../secrets'; |
| | import { TOTPClient } from '../totp-client'; |
| | import { SidebarComponent } from './components/sidebar-component'; |
| | import { LoginPage } from './pages/login-page'; |
| | import type { TestAccountCredentials } from '../secrets'; |
| |
|
| | |
| | |
| | |
| | export class TestAccount { |
| | readonly accountName: TestAccountName; |
| | readonly credentials: TestAccountCredentials; |
| |
|
| | |
| | |
| | |
| | |
| | constructor( accountName: TestAccountName ) { |
| | this.accountName = accountName; |
| | this.credentials = SecretsManager.secrets.testAccounts[ accountName ]; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | async authenticate( |
| | page: Page, |
| | { url, waitUntilStable }: { url?: string | RegExp; waitUntilStable?: boolean } = {} |
| | ): Promise< void > { |
| | const browserContext = page.context(); |
| | await browserContext.clearCookies(); |
| |
|
| | if ( await this.hasFreshAuthCookies() ) { |
| | this.log( 'Found fresh cookies, skipping log in' ); |
| | await browserContext.addCookies( await this.getAuthCookies() ); |
| | await page.goto( getCalypsoURL( '/' ) ); |
| | } else { |
| | this.log( 'Logging in via Login Page' ); |
| | await this.logInViaLoginPage( page ); |
| | } |
| |
|
| | if ( url ) { |
| | await page.waitForURL( url, { timeout: 20 * 1000 } ); |
| | } |
| | if ( waitUntilStable ) { |
| | const sidebarComponent = new SidebarComponent( page ); |
| | await sidebarComponent.waitForSidebarInitialization(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | async logInViaLoginPage( page: Page ): Promise< void > { |
| | const loginPage = new LoginPage( page ); |
| |
|
| | await loginPage.visit(); |
| | await loginPage.logInWithCredentials( this.credentials.username, this.credentials.password ); |
| |
|
| | |
| | if ( this.credentials.totpKey ) { |
| | return await loginPage.submitVerificationCode( this.getTOTP() ); |
| | } |
| | if ( this.credentials.smsNumber ) { |
| | return await loginPage.submitVerificationCode( await this.getSMSOTP() ); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | async logInViaPopupPage( page: Page ): Promise< void > { |
| | const loginPage = new LoginPage( page ); |
| |
|
| | const { username, password } = this.credentials; |
| | await loginPage.fillUsername( username ); |
| | await loginPage.clickSubmit(); |
| | await loginPage.fillPassword( password ); |
| |
|
| | |
| | await Promise.all( [ page.waitForEvent( 'close' ), loginPage.clickSubmit() ] ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | getTOTP(): string { |
| | if ( ! this.credentials.totpKey ) { |
| | throw new Error( |
| | `Unable to generate TOTP verification code for user ${ this.credentials.username } due to missing TOTP key.` |
| | ); |
| | } |
| |
|
| | const totpClient = new TOTPClient( this.credentials.totpKey ); |
| | return totpClient.getToken(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | async getSMSOTP(): Promise< string > { |
| | if ( ! this.credentials.smsNumber ) { |
| | throw new Error( `User ${ this.credentials.username } has no SMS 2FA.` ); |
| | } |
| |
|
| | const emailClient = new EmailClient(); |
| | const message = await emailClient.getLastMatchingMessage( { |
| | inboxId: SecretsManager.secrets.mailosaur.totpUserInboxId, |
| | sentTo: this.credentials.smsNumber.number, |
| | body: 'WordPress.com verification code', |
| | } ); |
| | return emailClient.get2FACodeFromMessage( message ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | getSiteURL( { protocol = true }: { protocol?: boolean } = {} ): string { |
| | return getAccountSiteURL( this.accountName, { protocol } ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | async hasFreshAuthCookies(): Promise< boolean > { |
| | try { |
| | const { birthtimeMs } = await fs.stat( this.getAuthCookiesPath() ); |
| | const nowMs = Date.now(); |
| | const twoDaysMs = 2 * 24 * 60 * 60 * 1000; |
| |
|
| | return nowMs < birthtimeMs + twoDaysMs; |
| | } catch { |
| | return false; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | async saveAuthCookies( browserContext: BrowserContext ): Promise< void > { |
| | const cookiesPath = this.getAuthCookiesPath(); |
| |
|
| | |
| | |
| | |
| | |
| | await fs.rm( cookiesPath, { force: true } ); |
| |
|
| | this.log( `Saving auth cookies to ${ cookiesPath }` ); |
| | await browserContext.storageState( { path: cookiesPath } ); |
| | } |
| |
|
| | |
| | |
| | |
| | async getAuthCookies(): Promise< [ { name: string; value: string } ] > { |
| | const cookiesPath = this.getAuthCookiesPath(); |
| | const storageStateFile = await fs.readFile( cookiesPath, { encoding: 'utf8' } ); |
| | const { cookies } = JSON.parse( storageStateFile ); |
| |
|
| | return cookies; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | private getAuthCookiesPath(): string { |
| | return path.join( envVariables.COOKIES_PATH, `${ this.accountName }.json` ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | private log( message: string ) { |
| | const { DEBUG } = process.env; |
| | if ( DEBUG && DEBUG !== 'false' ) { |
| | console.log( |
| | `${ chalk.bold( chalk.magenta( `TestAccount:${ this.accountName }` ) ) } => ${ message }` |
| | ); |
| | } |
| | } |
| | } |
| |
|