PluginProbe
Hello Plus / trunk
Hello Plus vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / tests / playwright / wp-authentication.ts

wp-authentication.ts in Hello Plus trunk, at tests/playwright/wp-authentication.ts

61 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { APIRequest, APIRequestContext, Page, chromium, APIResponse } from '@playwright/test';
2
3 export async function login( apiRequest: APIRequest, user: string, password: string, baseUrl: string ) {
4 // Important: make sure we authenticate in a clean environment by unsetting storage state.
5 const context = await apiRequest.newContext( { storageState: undefined } );
6
7 await context.post( `${ baseUrl }/wp-login.php`, {
8 form: {
9 log: user,
10 pwd: password,
11 'wp-submit': 'Log In',
12 redirect_to: `${ baseUrl }/wp-admin/`,
13 testcookie: '1',
14 },
15 } );
16 return context;
17 }
18
19 export async function fetchNonce( context: APIRequestContext, baseUrl: string ) {
20 const response = await context.get( `${ baseUrl }/wp-admin/post-new.php` );
21
22 await validateResponse( response, 'Failed to fetch page' );
23
24 let pageText = await response.text();
25 if ( pageText.includes( 'WordPress has been updated!' ) ) {
26 pageText = await updateDatabase( context, baseUrl );
27 }
28
29 const nonceMatch = pageText.match( /var wpApiSettings = .*;/ );
30 if ( ! nonceMatch ) {
31 throw new Error( `Nonce not found on the page:\n"${ pageText }"` );
32 }
33
34 return nonceMatch[ 0 ].replace( /^.*"nonce":"([^"]*)".*$/, '$1' );
35 }
36
37 async function updateDatabase( context: APIRequestContext, baseUrl: string ): Promise<string> {
38 const browser = await chromium.launch();
39 const browserContext = await browser.newContext();
40 const page: Page = await browserContext.newPage();
41 await page.goto( `${ baseUrl }/wp-admin/post-new.php` );
42 await page.getByText( 'Update WordPress Database' ).click();
43 await page.getByText( 'Continue' ).click();
44
45 const retryResponse = await context.get( `${ baseUrl }/wp-admin/post-new.php` );
46
47 const pageText = await retryResponse.text();
48 await browser.close();
49 return pageText;
50 }
51
52 async function validateResponse( response: APIResponse, errorMessage: string ) {
53 if ( ! response.ok() ) {
54 throw new Error( `
55 ${ errorMessage }: ${ response.status }.
56 ${ await response.text() }
57 ${ response.url() }
58 ` );
59 }
60 }
61