Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2E Test Utils: Add getCurrentUser(), and use it for user switching #33050

Merged
merged 3 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/e2e-test-utils/src/get-current-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Get the username of the user that's currently logged into WordPress (if any).
*
* @return {string?} username The user that's currently logged into WordPress (if any).
*/
export async function getCurrentUser() {
const cookies = await page.cookies();
const cookie = cookies.find(
( c ) => !! c?.name?.startsWith( 'wordpress_logged_in_' )
);

if ( ! cookie?.value ) {
return;
}
return decodeURIComponent( cookie.value ).split( '|' )[ 0 ];
}
5 changes: 3 additions & 2 deletions packages/e2e-test-utils/src/switch-user-to-admin.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
* Internal dependencies
*/
import { getCurrentUser } from './get-current-user';
import { loginUser } from './login-user';
import { WP_USERNAME, WP_ADMIN_USER } from './shared/config';
import { WP_ADMIN_USER } from './shared/config';

/**
* Switches the current user to the admin user (if the user
* running the test is not already the admin user).
*/
export async function switchUserToAdmin() {
if ( WP_USERNAME === WP_ADMIN_USER.username ) {
if ( ( await getCurrentUser() ) === WP_ADMIN_USER.username ) {
return;
}
await loginUser( WP_ADMIN_USER.username, WP_ADMIN_USER.password );
Expand Down
5 changes: 3 additions & 2 deletions packages/e2e-test-utils/src/switch-user-to-test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
* Internal dependencies
*/
import { getCurrentUser } from './get-current-user';
import { loginUser } from './login-user';
import { WP_USERNAME, WP_ADMIN_USER } from './shared/config';
import { WP_USERNAME } from './shared/config';

/**
* Switches the current user to whichever user we should be
* running the tests as (if we're not already that user).
*/
export async function switchUserToTest() {
if ( WP_USERNAME === WP_ADMIN_USER.username ) {
if ( ( await getCurrentUser() ) === WP_USERNAME ) {
return;
}
await loginUser();
Expand Down