commands.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // ***********************************************
  2. // This example commands.js shows you how to
  3. // create various custom commands and overwrite
  4. // existing commands.
  5. //
  6. // For more comprehensive examples of custom
  7. // commands please read more here:
  8. // https://on.cypress.io/custom-commands
  9. // ***********************************************
  10. //
  11. import 'cypress-wait-until';
  12. Cypress.Commands.add('randomString', (length) => {
  13. let result = '';
  14. const characters = 'ABCDEFGHIJK LMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  15. const charactersLength = characters.length;
  16. for (let i = 0; i < length; i++) {
  17. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  18. }
  19. return result;
  20. });
  21. /**
  22. * Check the swagger schema file:
  23. *
  24. * @param {string} url
  25. * @param {string} savePath
  26. */
  27. Cypress.Commands.add("validateSwaggerFile", (url, savePath) => {
  28. cy.task('log', `validateSwaggerFile: ${url} -- ${savePath}`)
  29. .then(() => {
  30. return cy
  31. .request(url)
  32. .then((response) => cy.writeFile(savePath, response.body, { log: false }))
  33. .then(() => cy.exec(`yarn swagger-lint '${savePath}'`, { failOnNonZeroExit: false }))
  34. .then((result) => cy.task('log', `Swagger Vacuum Results:\n${result.stdout || ''}`)
  35. .then(() => expect(result.code).to.eq(0)));
  36. });
  37. });
  38. /**
  39. * Check the swagger schema for a specific endpoint:
  40. *
  41. * @param {string} method API Method in swagger doc, "get", "put", "post", "delete"
  42. * @param {integer} code Swagger doc endpoint response code, exactly as defined in swagger doc
  43. * @param {string} path Swagger doc endpoint path, exactly as defined in swagger doc
  44. * @param {*} data The API response data to check against the swagger schema
  45. */
  46. Cypress.Commands.add('validateSwaggerSchema', (method, code, path, data) => {
  47. cy.task('validateSwaggerSchema', {
  48. file: Cypress.env('swaggerBase'),
  49. endpoint: path,
  50. method: method,
  51. statusCode: code,
  52. responseSchema: data,
  53. verbose: true
  54. }).should('equal', null);
  55. });
  56. Cypress.Commands.add('createInitialUser', (defaultUser) => {
  57. let user = {
  58. name: 'Cypress McGee',
  59. nickname: 'Cypress',
  60. email: '[email protected]',
  61. auth: {
  62. type: 'password',
  63. secret: 'changeme'
  64. },
  65. };
  66. if (typeof defaultUser === 'object' && defaultUser) {
  67. user = Object.assign({}, user, defaultUser);
  68. }
  69. return cy.task('backendApiPost', {
  70. path: '/api/users',
  71. data: user,
  72. }).then((data) => {
  73. // Check the swagger schema:
  74. cy.validateSwaggerSchema('post', 201, '/users', data);
  75. expect(data).to.have.property('id');
  76. expect(data.id).to.be.greaterThan(0);
  77. cy.wrap(data);
  78. });
  79. });
  80. Cypress.Commands.add('getToken', (defaultUser, defaultAuth) => {
  81. if (typeof defaultAuth === 'object' && defaultAuth) {
  82. if (!defaultUser) {
  83. defaultUser = {};
  84. }
  85. defaultUser.auth = defaultAuth;
  86. }
  87. cy.task('backendApiGet', {
  88. path: '/api/',
  89. }).then((data) => {
  90. // Check the swagger schema:
  91. cy.validateSwaggerSchema('get', 200, '/', data);
  92. if (!data.setup) {
  93. cy.log('Setup = false');
  94. // create a new user
  95. cy.createInitialUser(defaultUser).then(() => {
  96. return cy.getToken(defaultUser);
  97. });
  98. } else {
  99. let auth = {
  100. identity: '[email protected]',
  101. secret: 'changeme',
  102. };
  103. if (typeof defaultUser === 'object' && defaultUser && typeof defaultUser.auth === 'object' && defaultUser.auth) {
  104. auth = Object.assign({}, auth, defaultUser.auth);
  105. }
  106. cy.log('Setup = true');
  107. // login with existing user
  108. cy.task('backendApiPost', {
  109. path: '/api/tokens',
  110. data: auth,
  111. }).then((res) => {
  112. cy.wrap(res.token);
  113. });
  114. }
  115. });
  116. });
  117. Cypress.Commands.add('resetUsers', () => {
  118. cy.task('backendApiDelete', {
  119. path: '/api/users'
  120. }).then((data) => {
  121. expect(data).to.be.equal(true);
  122. cy.wrap(data);
  123. });
  124. });
  125. // TODO: copied from v3, is this usable?
  126. Cypress.Commands.add('waitForCertificateStatus', (token, certID, expected, timeout = 60) => {
  127. cy.log(`Waiting for certificate (${certID}) status (${expected}) timeout (${timeout})`);
  128. cy.waitUntil(() => cy.task('backendApiGet', {
  129. token: token,
  130. path: `/api/certificates/${certID}`
  131. }).then((data) => {
  132. return data.result.status === expected;
  133. }), {
  134. errorMsg: 'Waiting for certificate status failed',
  135. timeout: timeout * 1000,
  136. interval: 5000
  137. });
  138. });