Skip to content
Prev Previous commit
Next Next commit
Don't add users if they exist
  • Loading branch information
WunderBart authored and fullofcaffeine committed Feb 6, 2024
commit 65d7211ad5a8a0939c4b58bec8725f10153e49c7
23 changes: 16 additions & 7 deletions packages/e2e-test-utils-playwright/src/request-utils/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { RequestUtils } from './index';

export interface User {
id: number;
name: string;
email: string;
}

Expand Down Expand Up @@ -73,13 +74,21 @@ async function createUser( this: RequestUtils, user: UserData ) {
userData.roles = user.roles;
}

const response = await this.rest< User >( {
method: 'POST',
path: '/wp/v2/users',
data: userData,
} );
try {
const response = await this.rest< User >( {
method: 'POST',
path: '/wp/v2/users',
data: userData,
} );

return response;
return response;
} catch ( error: any ) {
if ( error.code === 'existing_user_login' ) {
return null;
}

throw error;
}
}

/**
Expand Down Expand Up @@ -111,7 +120,7 @@ async function deleteAllUsers( this: RequestUtils ) {
const responses = await Promise.all(
users
// Do not delete root user.
.filter( ( user: User ) => user.id !== 1 )
.filter( ( user: User ) => user.name !== process.env.WP_USERNAME )
.map( ( user: User ) => deleteUser.bind( this )( user.id ) )
);

Expand Down