Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
MM-29278 Make database error message easier to understand on Cypress …
Browse files Browse the repository at this point in the history
…test (#6630)

* make database error message easier to understand on Cypress test

* Update e2e/cypress/support/db_commands.d.ts

Co-authored-by: Alejandro García Montoro <[email protected]>

Co-authored-by: Mattermod <[email protected]>
Co-authored-by: Alejandro García Montoro <[email protected]>
  • Loading branch information
3 people authored Oct 6, 2020
1 parent d17b29a commit 486e807
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe('MM-T2575 Extend Session - Email Login', () => {
before(() => {
// # Check if with license and has matching database
cy.apiRequireLicense();
cy.requireServerDBToMatch();

cy.apiInitSetup().then(({team, user}) => {
testUser = user;
Expand Down
12 changes: 8 additions & 4 deletions e2e/cypress/plugins/db_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const dbGetActiveUserSessions = async ({dbConfig, params: {username, userId, lim
sessions: sessions.map((session) => convertKeysToLowercase(session)),
};
} catch (error) {
return {errorMessage: 'Failed to get active user sessions from the database'};
const errorMessage = 'Failed to get active user sessions from the database';
return {error, errorMessage};
}
};

Expand All @@ -63,7 +64,8 @@ const dbGetUser = async ({dbConfig, params: {username}}) => {

return {user: convertKeysToLowercase(user)};
} catch (error) {
return {errorMessage: 'Failed to get a user from the database'};
const errorMessage = 'Failed to get a user from the database';
return {error, errorMessage};
}
};

Expand All @@ -79,7 +81,8 @@ const dbGetUserSession = async ({dbConfig, params: {sessionId}}) => {

return {session: convertKeysToLowercase(session)};
} catch (error) {
return {errorMessage: 'Failed to get a user session from the database'};
const errorMessage = 'Failed to get a user session from the database';
return {error, errorMessage};
}
};

Expand Down Expand Up @@ -111,7 +114,8 @@ const dbUpdateUserSession = async ({dbConfig, params: {sessionId, userId, fields

return {session: convertKeysToLowercase(session)};
} catch (error) {
return {errorMessage: 'Failed to update a user session from the database'};
const errorMessage = 'Failed to update a user session from the database';
return {error, errorMessage};
}
};

Expand Down
75 changes: 75 additions & 0 deletions e2e/cypress/support/db_commands.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

/// <reference types="cypress" />

// ***************************************************************
// Each command should be properly documented using JSDoc.
// See https://jsdoc.app/index.html for reference.
// Basic requirements for documentation are the following:
// - Meaningful description
// - Each parameter with `@params`
// - Return value with `@returns`
// - Example usage with `@example`
// Custom command should follow naming convention of having `db` prefix, e.g. `dbGetUser`.
// ***************************************************************

declare namespace Cypress {
interface Chainable<Subject = any> {

/**
* Gets server config, and assert if it matches with the database connection being used by Cypress
*
* @example
* cy.apiRequireServerDBToMatch();
*/
apiRequireServerDBToMatch(): void;

/**
* Gets active sessions of a user on a given username or user ID directly from the database
* @param {String} username
* @param {String} userId
* @param {String} limit - maximum number of active sessions to return, e.g. 50 (default)
* @returns {Object} user - user object
* @returns {[Object]} sessions - an array of active sessions
*/
dbGetActiveUserSessions({username: string, userId, limit}): Chainable<Record<string, any>>;

/**
* Gets active sessions of a user on a given username or user ID directly from the database
* @param {Object} options
* @param {String} options.username
* @param {String} options.userId
* @param {String} options.limit - maximum number of active sessions to return, e.g. 50 (default)
* @returns {UserProfile} user - user object
* @returns {[Object]} sessions - an array of active sessions
*/
dbGetActiveUserSessions(options: Record<string, any>): Chainable<Record<string, any>>;

/**
* Gets user on a given username directly from the database
* @param {Object} options
* @param {String} options.username
* @returns {UserProfile} user - user object
*/
dbGetUser(options: Record<string, string>): Chainable<UserProfile>;

/**
* Gets session of a user on a given session ID directly from the database
* @param {Object} options
* @param {String} options.sessionId
* @returns {Session} session
*/
dbGetUserSession(options: Record<string, string>): Chainable<Session>;

/**
* Updates session of a user on a given user ID and session ID with fields to update directly from the database
* @param {Object} options
* @param {String} options.sessionId
* @param {String} options.userId
* @param {Object} options.fieldsToUpdate - will update all except session ID and user ID
* @returns {Session} session
*/
dbUpdateUserSession(options: Record<string, any>): Chainable<Session>;
}
}
14 changes: 10 additions & 4 deletions e2e/cypress/support/db_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const dbConfig = {
connection: Cypress.env('dbConnection'),
};

Cypress.Commands.add('requireServerDBToMatch', () => {
const message = 'Compare "cypress.json" against "config.json" of mattermost-server. It should match database driver and connection string.';

Cypress.Commands.add('apiRequireServerDBToMatch', () => {
cy.apiGetConfig().then(({config}) => {
expect(config.SqlSettings.DriverName, 'Should match server DB. Also manually check that the connection string is correct and match the one being used by the server.').to.equal(Cypress.env('dbClient'));
expect(config.SqlSettings.DriverName, message).to.equal(Cypress.env('dbClient'));
});
});

Expand All @@ -34,8 +36,8 @@ Cypress.Commands.add('dbGetActiveUserSessions', ({username, userId, limit}) => {
* @returns {Object} user - user object
*/
Cypress.Commands.add('dbGetUser', ({username}) => {
cy.task('dbGetUser', {dbConfig, params: {username}}).then(({user, errorMessage}) => {
expect(errorMessage).to.be.undefined;
cy.task('dbGetUser', {dbConfig, params: {username}}).then(({user, errorMessage, error}) => {
verifyError(error, errorMessage);

cy.wrap({user});
});
Expand Down Expand Up @@ -68,3 +70,7 @@ Cypress.Commands.add('dbUpdateUserSession', ({sessionId, userId, fieldsToUpdate}
cy.wrap({session});
});
});

function verifyError(error, errorMessage) {
expect(errorMessage, `${errorMessage}\n\n${message}\n\n${JSON.stringify(error)}`).to.be.undefined;
}
1 change: 1 addition & 0 deletions e2e/cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare namespace Cypress {
type PreferenceType = import('mattermost-redux/types/preferences').PreferenceType;
type Role = import('mattermost-redux/types/roles').Role;
type Scheme = import('mattermost-redux/types/schemes').Scheme;
type Session = import('mattermost-redux/types/sessions').Session;
type Team = import('mattermost-redux/types/teams').Team;
type TeamMembership = import('mattermost-redux/types/teams').TeamMembership;
type TermsOfService = import('mattermost-redux/types/terms_of_service').TermsOfService;
Expand Down
3 changes: 3 additions & 0 deletions e2e/cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ before(() => {
cy.apiAdminLogin().then(() => sysadminSetup(sysadmin));
});
}

// * Verify that the server database matches with the DB client and config at "cypress.json"
cy.apiRequireServerDBToMatch();
});
});

Expand Down

0 comments on commit 486e807

Please sign in to comment.