Files
Harrison HealeyandGitHub daec1283e5 MM-69003 Mostly share ESLint config between web app and E2E tests (#36767)
* Switch Cypress to use shared ESLint config

* Run --fix in Cypress

* Manually fix remaining lint issues in Cypress

* Switch Playwright to use shared ESLint config

* Run --fix in Playwright

* Manually fix remaining lint issues in Playwright

* Install and cache web app deps during Cypress CI builds

This also caches the types and client package. That isn't needed currently
since it uses prepackaged versions of those, but I imagine we might change
that at some point.

* Run e2e-tests-check when ESLint plugin is updated

* Change E2E test GHA caching to cache all of web app node_modules

* Fix mismatch between cache save and restore

* Try bumping cache keys

* Copy step to install dependencies to server.run_cypress.sh

I don't know how this must've worked before, but if this fixes the issue,
it seems like neither Cypress nor Playwright actually use the cached
depenendencies.

* Try disabling caching entirely for Cypress tests

* Try bypassing makefile?

* Try also manually building dependencies in run_specs.sh

I don't know why this appears to duplicate run_cypress.sh and
run_playwright.sh, both of which are called run_test.sh which
might not be used any more as best I can tell.

* Try installing the web app dependencies in yet another place

* Disable the extra steps in server.prepare.sh specifically for Cypress

* Revert changes to update cache key and disable web app depenedency cache on Cypress builds
2026-06-09 20:50:58 +00:00

368 lines
11 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import * as TIMEOUTS from '../fixtures/timeouts';
import type {ChainableT} from '@/types';
const token = 'SSWS ' + Cypress.expose('oktaMMAppToken');
type UserId = string | undefined;
interface Profile {
firstName: string;
lastName: string;
email: string;
login: string;
userType: string;
isAdmin: string;
isGuest: string;
}
interface User {
firstname: string;
lastname: string;
email: string;
login: string;
userType: string;
isAdmin: string;
isGuest: string;
password: string;
}
export interface UserCollection {
admins: Record<string, Partial<User>>;
guests: Record<string, Partial<User>>;
regulars: Record<string, Partial<User>>;
}
/**
* Builds a user profile object
* @param {Object} user - the user data
* @returns {Object} profile: the user profile
*/
function buildProfile(user: User): Profile {
const profile: Profile = {
firstName: user.firstname,
lastName: user.lastname,
email: user.email,
login: user.email,
userType: user.userType,
isAdmin: user.isAdmin,
isGuest: user.isGuest,
};
return profile;
}
/**
* creates a user
* @param {Object} user - the user to create
* @returns {String} userId: the user id
*/
function oktaCreateUser(user: Partial<User> = {}): ChainableT<UserId> {
const profile = buildProfile(user as User);
return cy.task('oktaRequest', {
baseUrl: Cypress.expose('oktaApiUrl'),
urlSuffix: '/users/',
method: 'post',
token,
data: {
profile,
credentials: {
password: {value: user.password},
recovery_question: {
question: 'What is the best open source messaging platform for developers?',
answer: 'Mattermost',
},
},
},
// cy.task() returns untyped data
}).then((response: any) => {
expect(response.status).to.equal(200);
const userId = response.data.id;
return cy.wrap(userId as UserId);
});
}
Cypress.Commands.add('oktaCreateUser', oktaCreateUser);
/**
* gets a user by user id
* @param {String} userId - the user id
* @returns {String} userId: the user id or null if not found
*/
function oktaGetUser(userId: string = ''): ChainableT<UserId> {
return cy.task('oktaRequest', {
baseUrl: Cypress.expose('oktaApiUrl'),
urlSuffix: '/users?q=' + userId,
method: 'get',
token,
// cy.task() returns untyped data
}).then((response: any) => {
expect(response.status).to.be.equal(200);
if (response.data.length > 0) {
return cy.wrap(response.data[0].id as UserId);
}
return cy.wrap(undefined as UserId);
});
}
Cypress.Commands.add('oktaGetUser', oktaGetUser);
/**
* Updates the user data
* @param {String} userId - the user id
* @param {Object} user - the user data
* @returns {Object} data: the user data as a response
*/
function oktaUpdateUser(userId: string = '', user: Partial<User> = {}) {
const profile = buildProfile(user as User);
return cy.task('oktaRequest', {
baseUrl: Cypress.expose('oktaApiUrl'),
urlSuffix: '/users/' + userId,
method: 'post',
token,
data: {
profile,
},
// cy.task() returns untyped data
}).then((response: any) => {
expect(response.status).to.equal(201);
return cy.wrap(response.data);
});
}
Cypress.Commands.add('oktaUpdateUser', oktaUpdateUser);
/**
* deletes a user by user id
* @param {String} userId - the user id
*/
function oktaDeleteUser(userId: string = '') {
// cy.task() returns untyped data
cy.task('oktaRequest', {
baseUrl: Cypress.expose('oktaApiUrl'),
urlSuffix: '/users/' + userId,
method: 'delete',
token,
}).then((response: any) => {
expect(response.status).to.equal(204);
cy.task('oktaRequest', {
baseUrl: Cypress.expose('oktaApiUrl'),
urlSuffix: '/users/' + userId,
method: 'delete',
token,
}).then((_response: any) => {
expect(_response.status).to.equal(204);
});
});
}
//first we deactivate the user, then we actually delete it
Cypress.Commands.add('oktaDeleteUser', oktaDeleteUser);
/**
* deletes a user's session
* @param {String} userId - the user id
*/
function oktaDeleteSession(userId: string = '') {
// cy.task() returns untyped data
cy.task('oktaRequest', {
baseUrl: Cypress.expose('oktaApiUrl'),
urlSuffix: '/users/' + userId + '/sessions',
method: 'delete',
token,
}).then((response: any) => {
expect(response.status).to.equal(204);
// Ensure we clear out these specific cookies
['JSESSIONID'].forEach((cookie) => {
cy.clearCookie(cookie);
});
});
}
Cypress.Commands.add('oktaDeleteSession', oktaDeleteSession);
/**
* Assigns a user to an application
* @param {String} userId - the user id
* @param {Object} user - the user data
* @returns {Object} data: the user data as response
*/
function oktaAssignUserToApplication(userId: string = '', user: Partial<User> = {}) {
return cy.task('oktaRequest', {
baseUrl: Cypress.expose('oktaApiUrl'),
urlSuffix: '/apps/' + Cypress.expose('oktaMMAppId') + '/users',
method: 'post',
token,
data: {
id: userId,
scope: 'USER',
profile: {
firstName: user.firstname,
lastName: user.lastname,
email: user.email,
},
},
// cy.task() returns untyped data
}).then((response: any) => {
expect(response.status).to.be.equal(200);
return cy.wrap(response.data);
});
}
Cypress.Commands.add('oktaAssignUserToApplication', oktaAssignUserToApplication);
/**
* Gets the user data if exists or create one if does not exists
* @param {Object} user - the user data
* @returns {String} userId: the user id
*/
function oktaGetOrCreateUser(user: User): ChainableT<UserId> {
let userId: UserId;
return cy.oktaGetUser(user.email).then((uId) => {
userId = uId;
if (userId == null) {
cy.oktaCreateUser(user).then((_uId) => {
userId = _uId;
cy.oktaAssignUserToApplication(userId as string, user);
});
} else {
cy.oktaAssignUserToApplication(userId as string, user);
}
return cy.wrap(userId as UserId);
});
}
Cypress.Commands.add('oktaGetOrCreateUser', oktaGetOrCreateUser);
/**
* Add users given a collection of users
* @param {Object} users - a collection of users
*/
function oktaAddUsers(users: UserCollection) {
let userId: UserId;
Object.values(users.regulars).forEach((_user) => {
cy.oktaGetUser(_user.email).then((uId) => {
userId = uId;
if (userId == null) {
cy.oktaCreateUser(_user).then((_uId) => {
userId = _uId;
cy.oktaAssignUserToApplication(userId as string, _user);
cy.oktaDeleteSession(userId as string);
});
}
});
});
Object.values(users.guests).forEach((_user) => {
cy.oktaGetUser(_user.email).then((uId) => {
userId = uId;
if (userId == null) {
cy.oktaCreateUser(_user).then((_uId) => {
userId = _uId;
cy.oktaAssignUserToApplication(userId as string, _user);
cy.oktaDeleteSession(userId as string);
});
}
});
});
Object.values(users.admins).forEach((_user) => {
cy.oktaGetUser(_user.email).then((uId) => {
userId = uId;
if (userId == null) {
cy.oktaCreateUser(_user).then((_uId) => {
userId = _uId;
cy.oktaAssignUserToApplication(userId as string, _user);
cy.oktaDeleteSession(userId as string);
});
}
});
});
}
Cypress.Commands.add('oktaAddUsers', oktaAddUsers);
/**
* Remove users given a collection of users
* @param {Object} users - a collection of users
*/
function oktaRemoveUsers(users: UserCollection) {
let userId: UserId;
Object.values(users.regulars).forEach((_user) => {
cy.oktaGetUser(_user.email).then((_uId) => {
userId = _uId;
if (userId != null) {
cy.oktaDeleteUser(userId as string);
}
});
});
Object.values(users.guests).forEach((_user) => {
cy.oktaGetUser(_user.email).then((_uId) => {
userId = _uId;
if (userId != null) {
cy.oktaDeleteUser(userId as string);
}
});
});
Object.values(users.admins).forEach((_user) => {
cy.oktaGetUser(_user.email).then((_uId) => {
userId = _uId;
if (userId != null) {
cy.oktaDeleteUser(userId as string);
}
});
});
}
Cypress.Commands.add('oktaRemoveUsers', oktaRemoveUsers);
/**
* check if okta login page is visible
*/
function checkOktaLoginPage() {
cy.findByText('Powered by').should('be.visible');
cy.findAllByText('Sign In').should('be.visible');
cy.get('#okta-signin-password').should('be.visible');
cy.get('#okta-signin-submit').should('be.visible');
}
Cypress.Commands.add('checkOktaLoginPage', checkOktaLoginPage);
/**
* performs an okta login
*/
function doOktaLogin(user: User) {
cy.checkOktaLoginPage();
cy.get('#okta-signin-username').type(user.email);
cy.get('#okta-signin-password').type(user.password);
cy.findAllByText('Sign In').last().click().wait(TIMEOUTS.FIVE_SEC);
}
Cypress.Commands.add('doOktaLogin', doOktaLogin);
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface Chainable {
oktaCreateUser: typeof oktaCreateUser;
oktaGetUser: typeof oktaGetUser;
oktaUpdateUser: typeof oktaUpdateUser;
oktaDeleteUser: typeof oktaDeleteUser;
oktaDeleteSession: typeof oktaDeleteSession;
oktaAssignUserToApplication: typeof oktaAssignUserToApplication;
oktaGetOrCreateUser: typeof oktaGetOrCreateUser;
oktaAddUsers: typeof oktaAddUsers;
oktaRemoveUsers: typeof oktaRemoveUsers;
checkOktaLoginPage: typeof checkOktaLoginPage;
doOktaLogin(user: User): ChainableT<void>;
}
}
}