e2e.ts 1.8 KB

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