Skip to content

Commit

Permalink
add required header for cy.apiLogin (mattermost#2985)
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril authored and Bob Lubecker committed Jun 19, 2019
1 parent 9842408 commit 4891deb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
10 changes: 8 additions & 2 deletions cypress/plugins/external_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ module.exports = async ({user, method = 'get', path, data = {}}) => {
const loginUrl = `${cypressConfig.baseUrl}/api/v4/users/login`;

// First we need to login with our external user to get cookies/tokens
const loginResponse = await axios({url: loginUrl, method: 'post', data: {login_id: user.username, password: user.password}});
const loginResponse = await axios({
url: loginUrl,
headers: {'X-Requested-With': 'XMLHttpRequest'},
method: 'post',
data: {login_id: user.username, password: user.password},
});

const setCookie = loginResponse.headers['set-cookie'];

let cookieString = '';
Expand Down Expand Up @@ -41,4 +47,4 @@ module.exports = async ({user, method = 'get', path, data = {}}) => {
}

return {status: response.status, data: response.data, error: response.error};
};
};
10 changes: 8 additions & 2 deletions cypress/plugins/post_message_as.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ const cypressConfig = require('../../cypress.json');
module.exports = async ({sender, message, channelId, createAt = 0}) => {
const url = `${cypressConfig.baseUrl}/api/v4/users/login`;

const response = await axios({url, method: 'post', data: {login_id: sender.username, password: sender.password}});
const response = await axios({
url,
headers: {'X-Requested-With': 'XMLHttpRequest'},
method: 'post',
data: {login_id: sender.username, password: sender.password},
});

const token = response.headers.token;

Client4.setUrl(cypressConfig.baseUrl);
Expand All @@ -26,4 +32,4 @@ module.exports = async ({sender, message, channelId, createAt = 0}) => {
};

return Client4.createPost(options);
};
};
45 changes: 28 additions & 17 deletions cypress/support/api_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Cypress.Commands.add('apiLogin', (username = 'user-1') => {
const user = users[username];

return cy.request({
headers: {'X-Requested-With': 'XMLHttpRequest'},
url: '/api/v4/users/login',
method: 'POST',
body: {login_id: user.username, password: user.password},
Expand Down Expand Up @@ -159,6 +160,23 @@ Cypress.Commands.add('apiGetTeams', () => {
});
});

/**
* Add user into a team directly via API
* This API assume that the user is logged in and has cookie to access
* @param {String} teamId - The team ID
* @param {String} userId - ID of user to be added into a team
* All parameter required
*/
Cypress.Commands.add('apiAddUserToTeam', (teamId, userId) => {
return cy.request({
method: 'POST',
url: `/api/v4/teams/${teamId}/members`,
headers: {'X-Requested-With': 'XMLHttpRequest'},
body: {team_id: teamId, user_id: userId},
qs: {team_id: teamId},
});
});

// *****************************************************************************
// Preferences
// https://api.mattermost.com/#tag/preferences
Expand Down Expand Up @@ -243,7 +261,7 @@ Cypress.Commands.add('apiSaveThemePreference', (value = JSON.stringify(theme.def
@param {Object} user - Object of user email, username, and password that you can optionally set. Otherwise use default values
@returns {Object} Returns object containing email, username, id and password if you need it further in the test
*/
Cypress.Commands.add('createNewUser', (user = {}, teams = null) => {
Cypress.Commands.add('createNewUser', (user = {}, teamIds = []) => {
const timestamp = Date.now();

const {email = `newe2etestuser${timestamp}@sample.mattermost.com`, username = `NewE2ETestUser${timestamp}`, password = 'password123'} = user;
Expand All @@ -258,31 +276,23 @@ Cypress.Commands.add('createNewUser', (user = {}, teams = null) => {

const userId = userResponse.body.id;

let teamsToAdd;

if (teams) {
teamsToAdd = teams;
if (teamIds && teamIds.length > 0) {
teamIds.forEach((teamId) => {
cy.apiAddUserToTeam(teamId, userId);
});
} else {
// Get teams, select the first three, and add new user to that team
cy.request('GET', '/api/v4/teams').then((teamsResponse) => {
// Verify we have at least 2 teams in the response to add the user to
expect(teamsResponse).to.have.property('body').to.have.length.greaterThan(1);

// Pull out only the first 2 teams
teamsToAdd = teamsResponse.body.slice(0, 2).map((t) => t.id);
teamsResponse.body.slice(0, 2).map((t) => t.id).forEach((teamId) => {
cy.apiAddUserToTeam(teamId, userId);
});
});
}

// Add user to several teams by default
teamsToAdd.forEach((team) => {
cy.request({
method: 'POST',
url: `/api/v4/teams/${team}/members`,
headers: {'X-Requested-With': 'XMLHttpRequest'},
body: {team_id: team, user_id: userId},
qs: {team_id: team}});
});

// # Update new user preferences to bypass tutorial
const preferences = [{
user_id: userId,
Expand All @@ -293,7 +303,7 @@ Cypress.Commands.add('createNewUser', (user = {}, teams = null) => {

cy.apiSaveUserPreference(preferences, userId);

// Wrap our user object so it gets returnd from our cypress command
// Wrap our user object so it gets returned from our cypress command
cy.wrap({email, username, password, id: userId});
});
});
Expand All @@ -307,6 +317,7 @@ Cypress.Commands.add('createNewUser', (user = {}, teams = null) => {
Cypress.Commands.add('loginAsNewUser', (user = {}) => {
return cy.createNewUser(user).then((newUser) => {
cy.request({
headers: {'X-Requested-With': 'XMLHttpRequest'},
url: '/api/v4/users/login',
method: 'POST',
body: {login_id: newUser.username, password: newUser.password},
Expand Down

0 comments on commit 4891deb

Please sign in to comment.