Skip to content

Commit

Permalink
improve cypress log (mattermost#6835)
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril committed Oct 20, 2020
1 parent f62c757 commit ecce64d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 20 deletions.
4 changes: 2 additions & 2 deletions components/widgets/badges/badges_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Widgets - Badges', () => {
// * Verify .Badge is visible and its CSS properties
cy.get('.Badge').should('be.visible').
and('have.css', 'margin', '0px 0px 0px 4px').
and('have.css', 'display', 'flex').
and('have.css', 'display', 'inline-flex').
and('have.css', 'align-items', 'center');

// * Verify .Badge__box is visible, text and its CSS properties
Expand All @@ -44,7 +44,7 @@ describe('Widgets - Badges', () => {
and('have.css', 'font-weight', '600').
and('have.css', 'padding', '1px 4px').
and('have.css', 'line-height', '14px').
and('have.css', 'background-color', 'rgba(61, 60, 64, 0.15)');
and('have.css', 'background-color', 'rgba(61, 60, 64, 0.16)');
});
});

Expand Down
10 changes: 5 additions & 5 deletions e2e/cypress/plugins/db_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const dbGetActiveUserSessions = async ({dbConfig, params: {username, userId, lim
sessions: sessions.map((session) => convertKeysToLowercase(session)),
};
} catch (error) {
const 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 @@ -64,7 +64,7 @@ const dbGetUser = async ({dbConfig, params: {username}}) => {

return {user: convertKeysToLowercase(user)};
} catch (error) {
const errorMessage = 'Failed to get a user from the database';
const errorMessage = 'Failed to get a user from the database.';
return {error, errorMessage};
}
};
Expand All @@ -81,7 +81,7 @@ const dbGetUserSession = async ({dbConfig, params: {sessionId}}) => {

return {session: convertKeysToLowercase(session)};
} catch (error) {
const 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 All @@ -94,7 +94,7 @@ const dbUpdateUserSession = async ({dbConfig, params: {sessionId, userId, fields
try {
let user = await knexClient(toLowerCase(dbConfig, 'Users')).where('id', userId).first();
if (!user) {
return {errorMessage: 'No user found with id:', userId};
return {errorMessage: `No user found with id: ${userId}.`};
}

delete fieldsToUpdate.id;
Expand All @@ -114,7 +114,7 @@ const dbUpdateUserSession = async ({dbConfig, params: {sessionId, userId, fields

return {session: convertKeysToLowercase(session)};
} catch (error) {
const 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
13 changes: 10 additions & 3 deletions e2e/cypress/support/api/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ Cypress.Commands.add('apiGetClientLicense', () => {
});

Cypress.Commands.add('apiRequireLicenseForFeature', (key = '') => {
Cypress.log({name: 'EE License', message: `Checking if server has license for feature: __${key}__.`});

return uploadLicenseIfNotExist().then(({license}) => {
expect(license.IsLicensed, 'Server has no Enterprise license.').to.equal('true');
const hasLicenseMessage = `Server ${license.IsLicensed === 'true' ? 'has' : 'has no'} EE license.`;
expect(license.IsLicensed, hasLicenseMessage).to.equal('true');

let hasLicenseKey = false;
for (const [k, v] of Object.entries(license)) {
Expand All @@ -29,15 +32,19 @@ Cypress.Commands.add('apiRequireLicenseForFeature', (key = '') => {
}
}

expect(hasLicenseKey, `No license for feature: ${key}`).to.equal(true);
const hasLicenseKeyMessage = `Server ${hasLicenseKey ? 'has' : 'has no'} EE license for feature: __${key}__`;
expect(hasLicenseKey, hasLicenseKeyMessage).to.equal(true);

return cy.wrap({license});
});
});

Cypress.Commands.add('apiRequireLicense', () => {
Cypress.log({name: 'EE License', message: 'Checking if server has license.'});

return uploadLicenseIfNotExist().then(({license}) => {
expect(license.IsLicensed, 'Server has no Enterprise license.').to.equal('true');
const hasLicenseMessage = `Server ${license.IsLicensed === 'true' ? 'has' : 'has no'} EE license.`;
expect(license.IsLicensed, hasLicenseMessage).to.equal('true');

return cy.wrap({license});
});
Expand Down
23 changes: 18 additions & 5 deletions e2e/cypress/support/db_commands.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

const dbClient = Cypress.env('dbClient');
const dbConnection = Cypress.env('dbConnection');
const dbConfig = {
client: Cypress.env('dbClient'),
connection: Cypress.env('dbConnection'),
client: dbClient,
connection: dbConnection,
};

const message = 'Compare "cypress.json" against "config.json" of mattermost-server. It should match database driver and connection string.';
const message = `Compare "cypress.json" against "config.json" of mattermost-server. It should match database driver and connection string.
The value at "cypress.json" is based on default mattermost-server's local database:
{"dbClient": "${dbClient}", "dbConnection": "${dbConnection}"}
If your server is using database other than the default, you may export those as env variables, like:
"__CYPRESS_dbClient=[dbClient] CYPRESS_dbConnection=[dbConnection] npm run cypress:open__"
`;

Cypress.Commands.add('apiRequireServerDBToMatch', () => {
cy.apiGetConfig().then(({config}) => {
expect(config.SqlSettings.DriverName, message).to.equal(Cypress.env('dbClient'));
if (config.SqlSettings.DriverName !== dbClient) {
expect(config.SqlSettings.DriverName, message).to.equal(dbClient);
}
});
});

Expand Down Expand Up @@ -72,5 +83,7 @@ Cypress.Commands.add('dbUpdateUserSession', ({sessionId, userId, fieldsToUpdate}
});

function verifyError(error, errorMessage) {
expect(errorMessage, `${errorMessage}\n\n${message}\n\n${JSON.stringify(error)}`).to.be.undefined;
if (errorMessage) {
expect(errorMessage, `${errorMessage}\n\n${message}\n\n${JSON.stringify(error)}`).to.be.undefined;
}
}
47 changes: 42 additions & 5 deletions e2e/cypress/support/task_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,64 @@ Cypress.Commands.add('postBotMessage', ({token, message, props, channelId, rootI
/**
* urlHealthCheck is a task wrapped as command that checks whether
* a URL is healthy and reachable.
* @param {String} name - name of service to check
* @param {String} url - URL to check
* @param {String} helperMessage - a message to display on error to help resolve the issue
* @param {String} method - a request using a specific method
* @param {String} httpStatus - expected HTTP status
*/
Cypress.Commands.add('urlHealthCheck', ({url, method = 'get', httpStatus}) => {
Cypress.Commands.add('urlHealthCheck', ({name, url, helperMessage, method, httpStatus}) => {
Cypress.log({name, message: `Checking URL health at ${url}`});

cy.task('urlHealthCheck', {url, method}).then(({data, errorCode, status, success}) => {
expect(success, `Requires ${url} to be reachable: ${errorCode}`).to.equal(true);
expect(status, `Expect ${httpStatus} to match returned ${status} HTTP status`).to.equal(httpStatus);
const urlService = `__${name}__ at ${url}`;

const successMessage = success ?
`${urlService}: reachable` :
`${errorCode}: The test you're running requires ${urlService} to be reachable. \n${helperMessage}`;
expect(success, successMessage).to.equal(true);

const statusMessage = status === httpStatus ?
`${urlService}: responded with ${status} HTTP status` :
`${urlService}: expected to respond with ${httpStatus} but got ${status} HTTP status`;
expect(status, statusMessage).to.equal(httpStatus);

cy.wrap({data, status});
});
});

Cypress.Commands.add('requireWebhookServer', () => {
const webhookBaseUrl = Cypress.env().webhookBaseUrl;
cy.urlHealthCheck({url: webhookBaseUrl, method: 'get', httpStatus: 200});
const helperMessage = `
__Tips:__
1. In local development, you may run "__npm run start:webhook__" at "/e2e" folder.
2. If reachable from remote host, you may export it as env variable, like "__CYPRESS_webhookBaseUrl=[url] npm run cypress:open__".
`;

cy.urlHealthCheck({
name: 'Webhook Server',
url: webhookBaseUrl,
helperMessage,
method: 'get',
httpStatus: 200,
});
});

Cypress.Commands.add('requireStorybookServer', () => {
const storybookUrl = Cypress.env().storybookUrl;
cy.urlHealthCheck({url: storybookUrl, method: 'get', httpStatus: 200});
const helperMessage = `
__Tips:__
1. In local development, you may run "__npm run storybook__" at root folder.
2. If reachable from remote host, you may export it as env variable, like "__CYPRESS_storybookUrl=[url] npm run cypress:open__".
`;

cy.urlHealthCheck({
name: 'Storybook Server',
url: storybookUrl,
helperMessage,
method: 'get',
httpStatus: 200,
});
});

Cypress.Commands.overwrite('log', (subject, message) => cy.task('log', message));

0 comments on commit ecce64d

Please sign in to comment.