e2e.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. Cypress.Commands.add('uploadTestDocument', (suffix: any) => {
  70. // Login as admin
  71. cy.loginAdmin();
  72. // upload example document
  73. cy.visit('/workspace/documents');
  74. // Create a document
  75. cy.get("button[aria-label='Add Docs']").click();
  76. cy.readFile('cypress/data/example-doc.txt').then((text) => {
  77. // select file
  78. cy.get('#upload-doc-input').selectFile(
  79. {
  80. contents: Cypress.Buffer.from(text + Date.now()),
  81. fileName: `document-test-initial-${suffix}.txt`,
  82. mimeType: 'text/plain',
  83. lastModified: Date.now()
  84. },
  85. {
  86. force: true
  87. }
  88. );
  89. // open tag input
  90. cy.get("button[aria-label='Add Tag']").click();
  91. cy.get("input[placeholder='Add a tag']").type('cypress-test');
  92. cy.get("button[aria-label='Save Tag']").click();
  93. // submit to upload
  94. cy.get("button[type='submit']").click();
  95. // wait for upload to finish
  96. cy.get('button').contains('#cypress-test').should('exist');
  97. cy.get('div').contains(`document-test-initial-${suffix}.txt`).should('exist');
  98. });
  99. });
  100. Cypress.Commands.add('deleteTestDocument', (suffix: any) => {
  101. cy.loginAdmin();
  102. cy.visit('/workspace/documents');
  103. // clean up uploaded documents
  104. cy.get('div')
  105. .contains(`document-test-initial-${suffix}.txt`)
  106. .find("button[aria-label='Delete Doc']")
  107. .click();
  108. });
  109. before(() => {
  110. cy.registerAdmin();
  111. });