forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_commands.js
89 lines (75 loc) · 3.13 KB
/
db_commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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: dbClient,
connection: dbConnection,
};
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}) => {
if (config.SqlSettings.DriverName !== dbClient) {
expect(config.SqlSettings.DriverName, message).to.equal(dbClient);
}
});
});
/**
* 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
*/
Cypress.Commands.add('dbGetActiveUserSessions', ({username, userId, limit}) => {
cy.task('dbGetActiveUserSessions', {dbConfig, params: {username, userId, limit}}).then(({user, sessions, errorMessage}) => {
expect(errorMessage).to.be.undefined;
cy.wrap({user, sessions});
});
});
/**
* Gets user on a given username directly from the database
* @param {String} username
* @returns {Object} user - user object
*/
Cypress.Commands.add('dbGetUser', ({username}) => {
cy.task('dbGetUser', {dbConfig, params: {username}}).then(({user, errorMessage, error}) => {
verifyError(error, errorMessage);
cy.wrap({user});
});
});
/**
* Gets session of a user on a given session ID directly from the database
* @param {String} sessionId
* @returns {Object} session
*/
Cypress.Commands.add('dbGetUserSession', ({sessionId}) => {
cy.task('dbGetUserSession', {dbConfig, params: {sessionId}}).then(({session, errorMessage}) => {
expect(errorMessage).to.be.undefined;
cy.wrap({session});
});
});
/**
* Updates session of a user on a given user ID and session ID with fields to update directly from the database
* @param {String} sessionId
* @param {String} userId
* @param {Object} fieldsToUpdate - will update all except session ID and user ID
* @returns {Object} session
*/
Cypress.Commands.add('dbUpdateUserSession', ({sessionId, userId, fieldsToUpdate}) => {
cy.task('dbUpdateUserSession', {dbConfig, params: {sessionId, userId, fieldsToUpdate}}).then(({session, errorMessage}) => {
expect(errorMessage).to.be.undefined;
cy.wrap({session});
});
});
function verifyError(error, errorMessage) {
if (errorMessage) {
expect(errorMessage, `${errorMessage}\n\n${message}\n\n${JSON.stringify(error)}`).to.be.undefined;
}
}