commands.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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:
  23. *
  24. * @param {string} method API Method in swagger doc, "get", "put", "post", "delete"
  25. * @param {integer} code Swagger doc endpoint response code, exactly as defined in swagger doc
  26. * @param {string} path Swagger doc endpoint path, exactly as defined in swagger doc
  27. * @param {*} data The API response data to check against the swagger schema
  28. */
  29. Cypress.Commands.add('validateSwaggerSchema', (method, code, path, data) => {
  30. cy.task('validateSwaggerSchema', {
  31. file: Cypress.env('swaggerBase'),
  32. endpoint: path,
  33. method: method,
  34. statusCode: code,
  35. responseSchema: data,
  36. verbose: true
  37. }).should('equal', null);
  38. });
  39. Cypress.Commands.add('createInitialUser', (defaultUser) => {
  40. let user = {
  41. name: 'Cypress McGee',
  42. nickname: 'Cypress',
  43. email: '[email protected]',
  44. auth: {
  45. type: 'password',
  46. secret: 'changeme'
  47. },
  48. };
  49. if (typeof defaultUser === 'object' && defaultUser) {
  50. user = Object.assign({}, user, defaultUser);
  51. }
  52. return cy.task('backendApiPost', {
  53. path: '/api/users',
  54. data: user,
  55. }).then((data) => {
  56. // Check the swagger schema:
  57. cy.validateSwaggerSchema('post', 201, '/users', data);
  58. expect(data).to.have.property('id');
  59. expect(data.id).to.be.greaterThan(0);
  60. cy.wrap(data);
  61. });
  62. });
  63. Cypress.Commands.add('getToken', (defaultUser, defaultAuth) => {
  64. if (typeof defaultAuth === 'object' && defaultAuth) {
  65. if (!defaultUser) {
  66. defaultUser = {};
  67. }
  68. defaultUser.auth = defaultAuth;
  69. }
  70. cy.task('backendApiGet', {
  71. path: '/api/',
  72. }).then((data) => {
  73. // Check the swagger schema:
  74. cy.validateSwaggerSchema('get', 200, '/', data);
  75. if (!data.setup) {
  76. cy.log('Setup = false');
  77. // create a new user
  78. cy.createInitialUser(defaultUser).then(() => {
  79. return cy.getToken(defaultUser);
  80. });
  81. } else {
  82. let auth = {
  83. identity: '[email protected]',
  84. secret: 'changeme',
  85. };
  86. if (typeof defaultUser === 'object' && defaultUser && typeof defaultUser.auth === 'object' && defaultUser.auth) {
  87. auth = Object.assign({}, auth, defaultUser.auth);
  88. }
  89. cy.log('Setup = true');
  90. // login with existing user
  91. cy.task('backendApiPost', {
  92. path: '/api/tokens',
  93. data: auth,
  94. }).then((res) => {
  95. cy.wrap(res.token);
  96. });
  97. }
  98. });
  99. });
  100. Cypress.Commands.add('resetUsers', () => {
  101. cy.task('backendApiDelete', {
  102. path: '/api/users'
  103. }).then((data) => {
  104. expect(data).to.be.equal(true);
  105. cy.wrap(data);
  106. });
  107. });
  108. // TODO: copied from v3, is this usable?
  109. Cypress.Commands.add('waitForCertificateStatus', (token, certID, expected, timeout = 60) => {
  110. cy.log(`Waiting for certificate (${certID}) status (${expected}) timeout (${timeout})`);
  111. cy.waitUntil(() => cy.task('backendApiGet', {
  112. token: token,
  113. path: `/api/certificates/${certID}`
  114. }).then((data) => {
  115. return data.result.status === expected;
  116. }), {
  117. errorMsg: 'Waiting for certificate status failed',
  118. timeout: timeout * 1000,
  119. interval: 5000
  120. });
  121. });