Files
upstream-mattermost/e2e-tests/cypress/tests/support/index.js
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

267 lines
9.8 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// ***********************************************************
// Read more at: https://on.cypress.io/configuration
// ***********************************************************
import dayjs from 'dayjs';
import localforage from 'localforage';
import '@testing-library/cypress/add-commands';
import 'cypress-file-upload';
import 'cypress-wait-until';
import 'cypress-plugin-tab';
import 'cypress-real-events';
import addContext from 'mochawesome/addContext';
import './api';
import './api_commands'; // soon to deprecate
import './client';
import './common_login_commands';
import './db_commands';
import './email';
import './external_commands';
import './extended_commands';
import './fetch_commands';
import './keycloak_commands';
import './ldap_commands';
import './ldap_server_commands';
import './notification_commands';
import './okta_commands';
import './saml_commands';
import './shell';
import './task_commands';
import './ui';
import './ui_commands'; // soon to deprecate
import {DEFAULT_TEAM} from './constants';
import {getDefaultConfig} from './api/system';
Cypress.dayjs = dayjs;
Cypress.on('test:after:run', (test, runnable) => {
// Only if the test is failed do we want to add
// the additional context of the screenshot.
if (test.state === 'failed') {
let parentNames = '';
// Define our starting parent
let parent = runnable.parent;
// If the test failed due to a hook, we have to handle
// getting our starting parent to form the correct filename.
if (test.failedFromHookId) {
// Failed from hook Id is always something like 'h2'
// We just need the trailing number to match with parent id
const hookId = test.failedFromHookId.split('')[1];
// If the current parentId does not match our hook id
// start digging upwards until we get the parent that
// has the same hook id, or until we get to a tile of ''
// (which means we are at the top level)
if (parent.id !== `r${hookId}`) {
while (parent.parent && parent.parent.id !== `r${hookId}`) {
if (parent.title === '') {
// If we have a title of '' we have reached the top parent
break;
} else {
parent = parent.parent;
}
}
}
}
// Now we can go from parent to parent to generate the screenshot filename
while (parent) {
// Only append parents that have actual content for their titles
if (parent.title !== '') {
parentNames = parent.title + ' -- ' + parentNames;
}
parent = parent.parent;
}
// Clean up strings of characters that Cypress strips out
const charactersToStrip = /[;:"<>/]/g;
parentNames = parentNames.replace(charactersToStrip, '');
const testTitle = test.title.replace(charactersToStrip, '');
// If the test has a hook name, that means it failed due to a hook
// and consequently Cypress appends some text to the file name
const hookName = test.hookName ? ' -- ' + test.hookName + ' hook' : '';
const filename = encodeURIComponent(`${parentNames}${testTitle}${hookName} (failed).png`);
// Add context to the mochawesome report which includes the screenshot
addContext({test}, {
title: 'Failing Screenshot: >> screenshots/' + Cypress.spec.name + '/' + filename,
value: 'screenshots/' + Cypress.spec.name + '/' + filename,
});
}
});
// Turn off all uncaught exception handling
Cypress.on('uncaught:exception', () => {
return false;
});
before(() => {
// # Clear localforage state
localforage.clear();
// # Try to login using existing sysadmin account
cy.apiAdminLogin({failOnStatusCode: false}).then((response) => {
if (response.user) {
sysadminSetup(response.user);
} else {
// # Create and login a newly created user as sysadmin
cy.apiCreateAdmin().then(({sysadmin}) => {
cy.apiAdminLogin().then(() => sysadminSetup(sysadmin));
});
}
switch (Cypress.expose('serverEdition')) {
case 'Cloud':
cy.apiRequireLicenseForFeature('Cloud');
break;
case 'E20':
cy.apiRequireLicense();
break;
default:
break;
}
if (Cypress.expose('serverClusterEnabled')) {
cy.log('Checking cluster information...');
// * Ensure cluster is set up properly when enabled
cy.shouldHaveClusterEnabled();
cy.apiGetClusterStatus().then(({clusterInfo}) => {
const sameCount = clusterInfo?.length === Cypress.expose('serverClusterHostCount');
expect(sameCount, sameCount ? '' : `Should match number of hosts in a cluster as expected. Got "${clusterInfo?.length}" but expected "${Cypress.expose('serverClusterHostCount')}"`).to.equal(true);
clusterInfo.forEach((info) => cy.log(`hostname: ${info.hostname}, version: ${info.version}, config_hash: ${info.config_hash}`));
});
}
// Log license status and server details before test
printLicenseStatus();
printServerDetails();
});
});
beforeEach(() => {
// Temporary fix for error related to this.get('prev') being undefined with @testing-library/cypress@9.0.0
cy.then(() => null);
});
function printLicenseStatus() {
cy.apiGetClientLicense().then(({license}) => {
cy.log(`Server License:
- IsLicensed = ${license.IsLicensed}
- IsTrial = ${license.IsTrial}
- SkuName = ${license.SkuName}
- SkuShortName = ${license.SkuShortName}
- Cloud = ${license.Cloud}
- Users = ${license.Users}`);
});
}
function printServerDetails() {
cy.apiGetConfig(true).then(({config}) => {
cy.log(`Build Info:
- BuildNumber = ${config.BuildNumber}
- BuildDate = ${config.BuildDate}
- Version = ${config.Version}
- BuildHash = ${config.BuildHash}
- BuildHashEnterprise = ${config.BuildHashEnterprise}
- BuildEnterpriseReady = ${config.BuildEnterpriseReady}
- TelemetryId = ${config.TelemetryId}
- ServiceEnvironment = ${config.ServiceEnvironment}`);
});
cy.apiGetConfig().then(({config}) => {
cy.log(`Notable Server Config:
- ServiceSettings.EnableSecurityFixAlert = ${config.ServiceSettings.EnableSecurityFixAlert}
- LogSettings.EnableDiagnostics = ${config.LogSettings?.EnableDiagnostics}`);
});
}
function sysadminSetup(user) {
if (Cypress.expose('firstTest')) {
// Sends dummy call to update the config to server
// Without this, first call to `cy.apiUpdateConfig()` consistently getting time out error in CI against remote server.
cy.externalRequest({user, method: 'put', path: 'config', data: getDefaultConfig(), failOnStatusCode: false});
}
// # Reset config to default
cy.apiUpdateConfig();
if (!user.email_verified) {
cy.apiVerifyUserEmailById(user.id);
}
// # Reset admin preference, online status and locale
resetUserPreference(user.id);
cy.apiUpdateUserStatus('online');
cy.apiPatchMe({
locale: 'en',
timezone: {automaticTimezone: '', manualTimezone: 'UTC', useAutomaticTimezone: 'false'},
});
// # Reset roles
cy.apiGetClientLicense().then(({isLicensed}) => {
if (isLicensed) {
cy.apiResetRoles();
}
});
// # Disable plugins not included in prepackaged
cy.apiDisableNonPrepackagedPlugins();
// # Deactivate test bots if any
cy.apiDeactivateTestBots();
// # Disable welcome tours if any
cy.apiDisableTutorials(user.id);
// # Check if default team is present; create if not found.
cy.apiGetTeamsForUser().then(({teams}) => {
const defaultTeam = teams && teams.length > 0 && teams.find((team) => team.name === DEFAULT_TEAM.name);
if (!defaultTeam) {
cy.apiCreateTeam(DEFAULT_TEAM.name, DEFAULT_TEAM.display_name, 'O', false);
} else if (defaultTeam && Cypress.expose('resetBeforeTest')) {
teams.forEach((team) => {
if (team.name !== DEFAULT_TEAM.name) {
cy.apiDeleteTeam(team.id);
}
});
cy.apiGetChannelsForUser('me', defaultTeam.id).then(({channels}) => {
channels.forEach((channel) => {
if (
(channel.team_id === defaultTeam.id || channel.team_name === defaultTeam.name) &&
(channel.name !== 'town-square' && channel.name !== 'off-topic')
) {
cy.apiDeleteChannel(channel.id);
}
});
});
}
});
}
function resetUserPreference(userId) {
cy.apiSaveTeammateNameDisplayPreference('username');
cy.apiSaveMessageDisplayPreference('clean');
cy.apiSaveLinkPreviewsPreference('true');
cy.apiSaveCollapsePreviewsPreference('false');
cy.apiSaveClockDisplayModeTo24HourPreference(false);
cy.apiSaveTutorialStep(userId, '999');
cy.apiSaveOnboardingTaskListPreference(userId, 'onboarding_task_list_open', 'false');
cy.apiSaveOnboardingTaskListPreference(userId, 'onboarding_task_list_show', 'false');
cy.apiSaveCloudTrialBannerPreference(userId, 'trial', 'max_days_banner');
cy.apiSaveSkipStepsPreference(userId, 'true');
cy.apiSaveStartTrialModal(userId, 'true');
cy.apiSaveUnreadScrollPositionPreference(userId, 'start_from_left_off');
}