e2e.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /// <reference types="cypress" />
  2. // eslint-disable-next-line @typescript-eslint/triple-slash-reference
  3. /// <reference path="../support/index.d.ts" />
  4. export const adminUser = {
  5. name: 'Admin User',
  6. email: 'admin@example.com',
  7. password: 'password'
  8. };
  9. const login = (email: string, password: string) => {
  10. return cy.session(
  11. email,
  12. () => {
  13. // Make sure to test against us english to have stable tests,
  14. // regardless on local language preferences
  15. localStorage.setItem('locale', 'en-US');
  16. // Visit auth page
  17. cy.visit('/auth');
  18. // Fill out the form
  19. cy.get('input[autocomplete="email"]').type(email);
  20. cy.get('input[type="password"]').type(password);
  21. // Submit the form
  22. cy.get('button[type="submit"]').click();
  23. // Wait until the user is redirected to the home page
  24. cy.get('#chat-search').should('exist');
  25. // Get the current version to skip the changelog dialog
  26. if (localStorage.getItem('version') === null) {
  27. cy.get('button').contains("Okay, Let's Go!").click();
  28. }
  29. },
  30. {
  31. validate: () => {
  32. cy.request({
  33. method: 'GET',
  34. url: '/api/v1/auths/',
  35. headers: {
  36. Authorization: 'Bearer ' + localStorage.getItem('token')
  37. }
  38. });
  39. }
  40. }
  41. );
  42. };
  43. const register = (name: string, email: string, password: string) => {
  44. return cy
  45. .request({
  46. method: 'POST',
  47. url: '/api/v1/auths/signup',
  48. body: {
  49. name: name,
  50. email: email,
  51. password: password
  52. },
  53. failOnStatusCode: false
  54. })
  55. .then((response) => {
  56. expect(response.status).to.be.oneOf([200, 400]);
  57. });
  58. };
  59. const registerAdmin = () => {
  60. return register(adminUser.name, adminUser.email, adminUser.password);
  61. };
  62. const loginAdmin = () => {
  63. return login(adminUser.email, adminUser.password);
  64. };
  65. Cypress.Commands.add('login', (email, password) => login(email, password));
  66. Cypress.Commands.add('register', (name, email, password) => register(name, email, password));
  67. Cypress.Commands.add('registerAdmin', () => registerAdmin());
  68. Cypress.Commands.add('loginAdmin', () => loginAdmin());
  69. before(() => {
  70. cy.registerAdmin();
  71. });