Skip to content

Commit

Permalink
Fix unit tests using php-wasm/node to manage their own cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton committed Dec 27, 2024
1 parent 5b17006 commit eaf55c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import { loadNodeRuntime } from '@php-wasm/node';
import { readFileSync } from 'fs';
import { join } from 'path';
import { login } from './login';
import { PHPRequest, PHPRequestHandler } from '@php-wasm/universal';
import {
HttpCookieStore,
PHPRequest,
PHPRequestHandler,
} from '@php-wasm/universal';

describe('Blueprint step enableMultisite', () => {
let handler: PHPRequestHandler;
let cookieStore: HttpCookieStore;
async function doBootWordPress(options: { absoluteUrl: string }) {
handler = await bootWordPress({
createPhpRuntime: async () =>
Expand All @@ -28,16 +33,23 @@ describe('Blueprint step enableMultisite', () => {
),
},
});
cookieStore = new HttpCookieStore();
const php = await handler.getPrimaryPhp();

return { php, handler };
}

const requestFollowRedirects = async (request: PHPRequest) => {
const requestFollowRedirectsWithCookies = async (request: PHPRequest) => {
let response = await handler.request(request);
while (response.httpStatusCode === 302) {
cookieStore.rememberCookiesFromResponseHeaders(response.headers);

const cookieHeader = cookieStore.getCookieRequestHeader();
response = await handler.request({
url: response.headers['location'][0],
headers: {
...(cookieHeader && { cookie: cookieHeader }),
},
});
}
return response;
Expand Down Expand Up @@ -81,7 +93,7 @@ describe('Blueprint step enableMultisite', () => {
* the admin bar includes the multisite menu.
*/
await login(php, {});
const response = await requestFollowRedirects({
const response = await requestFollowRedirectsWithCookies({
url: '/',
});
expect(response.httpStatusCode).toEqual(200);
Expand Down
20 changes: 14 additions & 6 deletions packages/playground/blueprints/src/lib/steps/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getWordPressModule,
} from '@wp-playground/wordpress-builds';
import { login } from './login';
import { PHPRequestHandler } from '@php-wasm/universal';
import { PHPRequestHandler, HttpCookieStore } from '@php-wasm/universal';
import { bootWordPress } from '@wp-playground/wordpress';
import { loadNodeRuntime } from '@php-wasm/node';
import { defineWpConfigConsts } from './define-wp-config-consts';
Expand All @@ -14,6 +14,7 @@ import { joinPaths, phpVar } from '@php-wasm/util';
describe('Blueprint step login', () => {
let php: PHP;
let handler: PHPRequestHandler;
let cookieStore: HttpCookieStore;
beforeEach(async () => {
handler = await bootWordPress({
createPhpRuntime: async () =>
Expand All @@ -23,22 +24,29 @@ describe('Blueprint step login', () => {
wordPressZip: await getWordPressModule(),
sqliteIntegrationPluginZip: await getSqliteDatabaseModule(),
});
cookieStore = new HttpCookieStore();
php = await handler.getPrimaryPhp();
});

const requestFollowRedirects = async (request: PHPRequest) => {
const requestFollowRedirectsWithCookies = async (request: PHPRequest) => {
let response = await handler.request(request);
while (response.httpStatusCode === 302) {
cookieStore.rememberCookiesFromResponseHeaders(response.headers);

const cookieHeader = cookieStore.getCookieRequestHeader();
response = await handler.request({
url: response.headers['location'][0],
headers: {
...(cookieHeader && { cookie: cookieHeader }),
},
});
}
return response;
};

it('should log the user in', async () => {
await login(php, {});
const response = await requestFollowRedirects({
const response = await requestFollowRedirectsWithCookies({
url: '/',
});
expect(response.httpStatusCode).toBe(200);
Expand All @@ -47,7 +55,7 @@ describe('Blueprint step login', () => {

it('should log the user into wp-admin', async () => {
await login(php, {});
const response = await requestFollowRedirects({
const response = await requestFollowRedirectsWithCookies({
url: '/wp-admin/',
});
expect(response.httpStatusCode).toBe(200);
Expand All @@ -60,7 +68,7 @@ describe('Blueprint step login', () => {
PLAYGROUND_FORCE_AUTO_LOGIN_ENABLED: true,
},
});
const response = await requestFollowRedirects({
const response = await requestFollowRedirectsWithCookies({
url: '/?playground_force_auto_login_as_user=admin',
});
expect(response.httpStatusCode).toBe(200);
Expand All @@ -81,7 +89,7 @@ describe('Blueprint step login', () => {
}
`
);
const response = await requestFollowRedirects({
const response = await requestFollowRedirectsWithCookies({
url: '/nonce-test.php',
});
expect(response.text).toBe('1');
Expand Down

0 comments on commit eaf55c3

Please sign in to comment.