Skip to content

Commit

Permalink
Add cookie to customer login page
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoumpierre committed Feb 5, 2025
1 parent e39ac1b commit 7fce7fc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
26 changes: 2 additions & 24 deletions tests/e2e-pw/specs/auth.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* External dependencies
*/
import { test as setup, expect, Page, FullProject } from '@playwright/test';
import { test as setup, expect } from '@playwright/test';
import fs from 'fs';

/**
Expand All @@ -14,6 +14,7 @@ import {
customerStorageFile,
wpAdminLogin,
loginAsCustomer,
addSupportSessionDetectedCookie,
} from '../utils/helpers';

// See https://playwright.dev/docs/auth#multiple-signed-in-roles
Expand All @@ -35,29 +36,6 @@ const isAuthStateStale = ( authStateFile: string ) => {
return isStale;
};

/**
* Adds a special cookie during the session to avoid the support session detection page.
* This is temporarily displayed when navigating to the login page while Jetpack SSO and protect modules are disabled.
* Relevant for Atomic sites only.
*/
const addSupportSessionDetectedCookie = async (
page: Page,
project: FullProject
) => {
if ( process.env.NODE_ENV !== 'atomic' ) return;

const domain = new URL( project.use.baseURL ).hostname;

await page.context().addCookies( [
{
value: 'true',
name: '_wpcomsh_support_session_detected',
path: '/',
domain,
},
] );
};

setup( 'authenticate as admin', async ( { page }, { project } ) => {
// For local development, use existing state if it exists and isn't stale.
if ( ! process.env.CI ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test.describe( 'Saved cards', () => {
await getShopper( browser, true, project.use.baseURL )
).shopperPage;

await ensureCustomerIsLoggedIn( shopperPage );
await ensureCustomerIsLoggedIn( shopperPage, project );
} );

test( 'should save the card', async ( {} ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const cards: Array< CardType > = [
test.describe( 'Payment Methods', () => {
let shopperPage: Page;

test.beforeAll( async ( { browser } ) => {
test.beforeAll( async ( { browser }, { project } ) => {
shopperPage = ( await getShopper( browser ) ).shopperPage;
await ensureCustomerIsLoggedIn( shopperPage );
await ensureCustomerIsLoggedIn( shopperPage, project );
} );

test.beforeEach( async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test.describe( 'Shopper can save and delete cards', () => {
shopperPage = ( await getShopper( browser, true, project.use.baseURL ) )
.shopperPage;

await ensureCustomerIsLoggedIn( shopperPage );
await ensureCustomerIsLoggedIn( shopperPage, project );
} );

async function waitTwentySecondsSinceLastCardAdded( page: Page ) {
Expand Down Expand Up @@ -132,8 +132,8 @@ test.describe( 'Shopper can save and delete cards', () => {
Object.entries( cards ).forEach(
( [ cardName, { card, address, products } ] ) => {
test.describe( 'Testing card: ' + cardName, () => {
test.beforeAll( async () => {
await ensureCustomerIsLoggedIn( shopperPage );
test.beforeAll( async ( args, { project } ) => {
await ensureCustomerIsLoggedIn( shopperPage, project );
} );

test( `should add the ${ cardName } card as a new payment method`, async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describeif( shouldRunWCBlocksTests )(
await addWCBCheckoutPage( merchantPage );
}

await ensureCustomerIsLoggedIn( shopperPage );
await ensureCustomerIsLoggedIn( shopperPage, project );
} );

test( 'should be able to save basic card on Blocks checkout', async () => {
Expand Down
38 changes: 36 additions & 2 deletions tests/e2e-pw/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
* External dependencies
*/
import path from 'path';
import { test, Page, Browser, BrowserContext, expect } from '@playwright/test';
import {
test,
Page,
Browser,
BrowserContext,
expect,
FullProject,
} from '@playwright/test';

/**
* Internal dependencies
Expand Down Expand Up @@ -212,8 +219,35 @@ export const loginAsCustomer = async (
await page.context().storageState( { path: customerStorageFile } );
};

export const ensureCustomerIsLoggedIn = async ( page: Page ) => {
/**
* Adds a special cookie during the session to avoid the support session detection page.
* This is temporarily displayed when navigating to the login page while Jetpack SSO and protect modules are disabled.
* Relevant for Atomic sites only.
*/
export const addSupportSessionDetectedCookie = async (
page: Page,
project: FullProject
) => {
if ( process.env.NODE_ENV !== 'atomic' ) return;

const domain = new URL( project.use.baseURL ).hostname;

await page.context().addCookies( [
{
value: 'true',
name: '_wpcomsh_support_session_detected',
path: '/',
domain,
},
] );
};

export const ensureCustomerIsLoggedIn = async (
page: Page,
project: FullProject
) => {
if ( ! ( await isCustomerLoggedIn( page ) ) ) {
await addSupportSessionDetectedCookie( page, project );
await loginAsCustomer( page, config.users.customer );
}
};

0 comments on commit 7fce7fc

Please sign in to comment.