text: // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  // See LICENSE.txt for license information.
  
  import {getRandomId} from '../../utils';
  import {getAdminAccount} from '../env';
  
  // *****************************************************************************
  // Users
  // https://api.mattermost.com/#tag/users
  // *****************************************************************************
  
  /**
   * Gets current user
   * This API assume that the user is logged
   * no params required because we are using /me to refer to current user
   */
  Cypress.Commands.add('apiGetMe', () => {
      return cy.request({
          headers: {'X-Requested-With': 'XMLHttpRequest'},
          url: 'api/v4/users/me',
          method: 'GET',
      }).then((response) => {
          expect(response.status).to.equal(200);
          cy.wrap({user: response.body});
      });
  });
  
  Cypress.Commands.add('apiGetUserByEmail', (email) => {
      return cy.request({
          headers: {'X-Requested-With': 'XMLHttpRequest'},
          url: '/api/v4/users/email/' + email,
      }).then((response) => {
          expect(response.status).to.equal(200);
          cy.wrap(response);
      });
  });
  
  Cypress.Commands.add('apiGetUsers', (usernames = []) => {
      return cy.request({
          headers: {'X-Requested-With': 'XMLHttpRequest'},
          url: '/api/v4/users/usernames',
          method: 'POST',
          body: usernames,
      });
  });
  
  Cypress.Commands.add('apiPatchUser', (userId, userData) => {
      return cy.request({
          headers: {'X-Requested-With': 'XMLHttpRequest'},
          method: 'PUT',
          url: `/api/v4/users/${userId}/patch`,
          body: userData,
      }).then((response) => {
          expect(response.status).to.equal(200);
          cy.wrap(response);
      });
  });
  
  Cypress.Commands.add('apiPatchMe', (data) => {
      return cy.request({
          headers: {'X-Requested-With': 'XMLHttpRequest'},
          url: '/api/v4/users/me/patch',
          method: 'PUT',
          body: data,
      }).then((response) => {
          expect(response.status).to.equal(200);
          cy.wrap(response);
      });
  });
  
  Cypress.Commands.add('apiCreateAdmin', () => {
      const {username, password} = getAdminAccount();
  
      const sysadminUser = {
          username,
          password,
          first_name: 'Kenneth',
          last_name: 'Moreno',
          email: 'sysadmin@sample.mattermost.com',
      };
  
      const options = {
          headers: {'X-Requested-With': 'XMLHttpRequest'},
          method: 'POST',
          url: '/api/v4/users',
          body: sysadminUser,
      };
  
      // # Create a new user
      return cy.request(options).then((res) => {
          expect(res.status).to.equal(201);
  
          cy.wrap({sysadmin: res.body});
      });
  });
  
  function generateRandomUser(prefix = 'user') {
      const randomId = getRandomId();
  
      return {
          email: `${prefix}${randomId}@sample.mattermost.com`,
          username: `${prefix}${randomId}`,
          password: 'passwd',
          first_name: `First${randomId}`,
          last_name: `Last${randomId}`,
          nickname: `Nickname${randomId}`,
      };
  }
  
  Cypress.Commands.add('apiCreateUser', ({prefix = 'user', bypassTutorial = true, user = null} = {}) => {
      const newUser = user || generateRandomUser(prefix);
  
      const createUserOption = {
          headers: {'X-Requested-With': 'XMLHttpRequest'},
          method: 'POST',
          url: '/api/v4/users',
          body: newUser,
      };
  
      return cy.request(createUserOption).then((userRes) => {
          expect(userRes.status).to.equal(201);
  
          const createdUser = userRes.body;
  
          if (bypassTutorial) {
              cy.apiSaveTutorialStep(createdUser.id, '999');
          }
  
          return cy.wrap({user: {...createdUser, password: newUser.password}});
      });
  });
  
  Cypress.Commands.add('apiCreateGuestUser', ({prefix = 'guest', activate = true} = {}) => {
      return cy.apiCreateUser({prefix}).then(({user}) => {
          cy.demoteUser(user.id);
          cy.apiActivateUser(user.id, activate);
  
          return cy.wrap({guest: user});
      });
  });
  
  /**
   * Saves channel display mode preference of a user directly via API
   * This API assume that the user is logged in and has cookie to access
   * @param {String} status - "online" (default), "offline", "away" or "dnd"
   */
  Cypress.Commands.add('apiUpdateUserStatus', (status = 'online') => {
      return cy.getCookie('MMUSERID').then((cookie) => {
          const data = {user_id: cookie.value, status};
  
          return cy.request({
              headers: {'X-Requested-With': 'XMLHttpRequest'},
              url: '/api/v4/users/me/status',
              method: 'PUT',
              body: data,
          }).then((response) => {
              expect(response.status).to.equal(200);
              cy.wrap({status: response.body});
          });
      });
  });
  
  /**
   * Revoke all active sessions for a user
   * @param {String} userId - ID of user to revoke sessions
   */
  Cypress.Commands.add('apiRevokeUserSessions', (userId) => {
      return cy.request({
          headers: {'X-Requested-With': 'XMLHttpRequest'},
          url: `/api/v4/users/${userId}/sessions/revoke/all`,
          method: 'POST',
      }).then((response) => {
          expect(response.status).to.equal(200);
          cy.wrap(response);
      });
  });
  
  /**
   * Activate/Deactivate a User directly via API
   * @param {String} userId - The user ID
   * @param {Boolean} active - Whether to activate or deactivate - true/false
   */
  Cypress.Commands.add('apiActivateUser', (userId, active = true) => {
      const baseUrl = Cypress.config('baseUrl');
      const admin = getAdminAccount();
  
      cy.externalRequest({user: admin, method: 'put', baseUrl, path: `users/${userId}/active`, data: {active}});
  });
  
  Cypress.Commands.add('apiGetUsersNotInTeam', ({teamId, page = 0, perPage = 60} = {}) => {
      return cy.request({
          method: 'GET',
          url: `/api/v4/users?not_in_team=${teamId}&page=${page}&per_page=${perPage}`,
          headers: {'X-Requested-With': 'XMLHttpRequest'},
      }).then((response) => {
          expect(response.status).to.equal(200);
          cy.wrap({users: response.body});
      });
  });
  