Skip to content

Commit

Permalink
Migrate nonce.test.js to Playwright (#44929)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooja Killekar <poojakillekar@Poojas-MacBook-Air.local>
  • Loading branch information
kevin940726 and Pooja Killekar authored Oct 13, 2022
1 parent 96e5dc7 commit a75c1bd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 50 deletions.
16 changes: 0 additions & 16 deletions packages/e2e-tests/plugins/nonce.php

This file was deleted.

34 changes: 0 additions & 34 deletions packages/e2e-tests/specs/editor/plugins/nonce.test.js

This file was deleted.

85 changes: 85 additions & 0 deletions test/e2e/specs/editor/plugins/nonce.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'Nonce', () => {
test.afterEach( async ( { requestUtils } ) => {
await requestUtils.deleteAllPosts();
} );

test( 'should refresh when expired', async ( {
page,
admin,
requestUtils,
} ) => {
await admin.createNewPost();
await page.keyboard.press( 'Enter' );
// Wait until the network is idle.
await page.waitForLoadState( 'networkidle' );
await page.keyboard.type( 'test' );

/**
* Mock network and manually expire the API nonce until refreshed.
*/
{
let refreshed = false;

page.on( 'response', ( response ) => {
if (
response
.url()
.includes(
'/wp-admin/admin-ajax.php?action=rest-nonce'
) &&
response.status() === 200
) {
refreshed = true;
}
} );

await page.route(
// Intercept every REST API endpoint.
( url ) =>
url.href.startsWith(
requestUtils.storageState.rootURL.slice( 0, -1 )
),
( route ) => {
if ( refreshed ) {
route.continue();
} else {
route.fulfill( {
status: 403,
contentType: 'application/json; charset=UTF-8',
body: JSON.stringify( {
code: 'rest_cookie_invalid_nonce',
data: { status: 403 },
message: 'Cookie check failed',
} ),
} );
}
}
);
}

const saveDraftResponses = [];
page.on( 'response', async ( response ) => {
const request = response.request();
if (
request.method() === 'POST' &&
request.postDataJSON()?.status === 'draft'
) {
saveDraftResponses.push( response.status() );
}
} );

await page.click( 'role=button[name=/Save draft/i]' );
// Saving draft should still succeed after retrying.
await expect(
page.locator( 'role=button[name="Dismiss this notice"i]' )
).toContainText( /Draft saved/i );

// We expect a 403 status only once.
expect( saveDraftResponses ).toEqual( [ 403, 200 ] );
} );
} );

0 comments on commit a75c1bd

Please sign in to comment.