Files
upstream-rocketchat/apps/meteor/tests/e2e/utils/user-helpers.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

136 lines
3.6 KiB
TypeScript

import { faker } from '@faker-js/faker';
import type { APIResponse } from '@playwright/test';
import type { IUser } from '@rocket.chat/core-typings';
import type { BaseTest } from './test';
import { BASE_URL, DEFAULT_USER_CREDENTIALS } from '../config/constants';
import type { IUserState } from '../fixtures/userStates';
export interface ICreateUserOptions {
username?: string;
email?: string;
name?: string;
password?: string;
roles?: string[];
data?: Record<string, any>;
}
export interface ITestUser {
response: APIResponse;
data: IUser & { username: string };
deleted: boolean;
delete: () => Promise<APIResponse | undefined>;
markAsDeleted: () => void;
}
/**
* Creates a test user with optional customizations
*/
export async function createTestUser(api: BaseTest['api'], options: ICreateUserOptions = {}): Promise<ITestUser> {
const userData = {
email: options.email || faker.internet.email(),
name: options.name || faker.person.fullName(),
password: options.password || DEFAULT_USER_CREDENTIALS.password,
username: options.username || `test-user-${faker.string.uuid()}`,
roles: options.roles || ['user'],
...options.data,
};
const response = await api.post('/users.create', userData);
if (response.status() !== 200) {
throw new Error(`Failed to create user: ${response.status()}, response: ${await response.text()}`);
}
const { user } = await response.json();
return {
response,
data: user,
deleted: false,
markAsDeleted(this: ITestUser) {
this.deleted = true;
},
async delete(this: ITestUser) {
if (this.deleted) {
return;
}
const response = await api.post('/users.delete', { userId: user._id });
this.markAsDeleted();
return response;
},
};
}
/**
* Logs in a test user via the REST API and returns an IUserState
* suitable for use with createAuxContext.
*
* Use this instead of the pre-baked Users.userN fixtures whenever the test
* will deactivate (or otherwise invalidate the session of) the user, so that
* shared fixture tokens are never corrupted.
*/
export async function loginTestUser(api: BaseTest['api'], user: ITestUser): Promise<IUserState> {
const response = await api.post('/login', {
username: user.data.username,
password: DEFAULT_USER_CREDENTIALS.password,
});
const {
data: { userId, authToken },
} = await response.json();
const expires = new Date();
expires.setFullYear(expires.getFullYear() + 1);
return {
data: {
_id: userId,
username: user.data.username,
loginToken: authToken,
loginExpire: expires,
hashedToken: '',
},
state: {
cookies: [
{ sameSite: 'Lax', name: 'rc_uid', value: userId, domain: 'localhost', path: '/', expires: -1, httpOnly: false, secure: false },
{
sameSite: 'Lax',
name: 'rc_token',
value: authToken,
domain: 'localhost',
path: '/',
expires: -1,
httpOnly: false,
secure: false,
},
],
origins: [
{
origin: BASE_URL,
localStorage: [
{ name: 'userLanguage', value: 'en-US' },
{ name: 'Meteor.loginToken', value: authToken },
{ name: 'Meteor.loginTokenExpires', value: expires.toISOString() },
{ name: 'Meteor.userId', value: userId },
],
},
],
},
};
}
/**
* Creates multiple test users at once
*/
export async function createTestUsers(api: BaseTest['api'], count: number, options: ICreateUserOptions = {}): Promise<ITestUser[]> {
const promises = Array.from({ length: count }, (_, i) =>
createTestUser(api, {
...options,
username: options.username ? `${options.username}-${i + 1}` : undefined,
}),
);
return Promise.all(promises);
}