mattermost/e2e-tests/playwright/support/server/init.ts
Saturnino Abril 53471c7e8c
E2E/Playwright: Upgrade playwright/dependencies and test server config (#26464)
* chore: upgrade playwright and test server config

* add post install script

* update config types

* fix visual test
2024-03-19 08:09:25 +08:00

109 lines
3.9 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import path from 'node:path';
import {expect} from '@playwright/test';
import chalk from 'chalk';
import {ClientError} from '@mattermost/client/client4';
import {PreferenceType} from '@mattermost/types/preferences';
import testConfig from '@e2e-test.config';
import {makeClient} from '.';
import {getOnPremServerConfig} from './default_config';
import {createRandomTeam} from './team';
import {createRandomUser} from './user';
export async function initSetup({
userPrefix = 'user',
teamPrefix = {name: 'team', displayName: 'Team'},
withDefaultProfileImage = true,
} = {}) {
try {
// Login the admin user via API
const {adminClient, adminUser} = await getAdminClient();
if (!adminClient) {
throw new Error(
"Failed to setup admin: Check that you're able to access the server using the same admin credential.",
);
}
// Reset server config
const adminConfig = await adminClient.updateConfig(getOnPremServerConfig());
// Create new team
const team = await adminClient.createTeam(createRandomTeam(teamPrefix.name, teamPrefix.displayName));
// Create new user and add to newly created team
const randomUser = createRandomUser(userPrefix);
const user = await adminClient.createUser(randomUser, '', '');
user.password = randomUser.password;
await adminClient.addToTeam(team.id, user.id);
// Log in new user via API
const {client: userClient} = await makeClient(user);
if (withDefaultProfileImage) {
// Set user profile image
const fullPath = path.join(path.resolve(__dirname), '../', 'asset/mattermost-icon_128x128.png');
await userClient.uploadProfileImageX(user.id, fullPath);
}
// Update user preference
const preferences: PreferenceType[] = [
{user_id: user.id, category: 'tutorial_step', name: user.id, value: '999'},
{
user_id: user.id,
category: 'drafts',
name: 'drafts_tour_tip_showed',
value: JSON.stringify({drafts_tour_tip_showed: true}),
},
{user_id: user.id, category: 'crt_thread_pane_step', name: user.id, value: '999'},
];
await userClient.savePreferences(user.id, preferences);
return {
adminClient,
adminUser,
adminConfig,
user,
userClient,
team,
offTopicUrl: getUrl(team.name, 'off-topic'),
townSquareUrl: getUrl(team.name, 'town-square'),
};
} catch (error) {
// log an error for debugging
// eslint-disable-next-line no-console
const err = error as ClientError;
if (err.message === 'Could not parse multipart form.') {
// eslint-disable-next-line no-console
console.log(chalk.yellow(`node version: ${process.version}\nNODE_OPTIONS: ${process.env.NODE_OPTIONS}`));
// eslint-disable-next-line no-console
console.log(
chalk.green(
`This failed due to the experimental fetch support in Node.js starting v18.0.0.\nYou may set environment variable: "export NODE_OPTIONS='--no-experimental-fetch'", then try again.'`,
),
);
}
expect(err, 'Should not throw an error').toBeFalsy();
throw err;
}
}
export async function getAdminClient(opts: {skipLog: boolean} = {skipLog: false}) {
const {client: adminClient, user: adminUser} = await makeClient(
{
username: testConfig.adminUsername,
password: testConfig.adminPassword,
},
opts,
);
return {adminClient, adminUser};
}
function getUrl(teamName: string, channelName: string) {
return `/${teamName}/channels/${channelName}`;
}