commands.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /**
  12. * Check the swagger schema:
  13. *
  14. * @param {string} method API Method in swagger doc, "get", "put", "post", "delete"
  15. * @param {string} path Swagger doc endpoint path, exactly as defined in swagger doc
  16. * @param {*} data The API response data to check against the swagger schema
  17. */
  18. Cypress.Commands.add('validateSwaggerSchema', (method, path, data) => {
  19. cy.task('validateSwaggerSchema', {
  20. file: Cypress.env('swaggerBase'),
  21. endpoint: path,
  22. method: method,
  23. statusCode: 200,
  24. responseSchema: data,
  25. verbose: true
  26. }).should('equal', null);
  27. });
  28. Cypress.Commands.add('getToken', () => {
  29. cy.task('backendApiGet', {
  30. path: '/api/',
  31. }).then((data) => {
  32. // Check the swagger schema:
  33. cy.task('validateSwaggerSchema', {
  34. endpoint: '/',
  35. method: 'get',
  36. statusCode: 200,
  37. responseSchema: data,
  38. verbose: true,
  39. }).should('equal', null);
  40. if (!data.result.setup) {
  41. cy.log('Setup = false');
  42. // create a new user
  43. cy.createInitialUser().then(() => {
  44. return cy.getToken();
  45. });
  46. } else {
  47. cy.log('Setup = true');
  48. // login with existing user
  49. cy.task('backendApiPost', {
  50. path: '/api/tokens',
  51. data: {
  52. type: 'password',
  53. identity: '[email protected]',
  54. secret: 'changeme'
  55. }
  56. }).then(res => {
  57. cy.wrap(res.result.token);
  58. });
  59. }
  60. });
  61. });
  62. Cypress.Commands.add('createInitialUser', () => {
  63. return cy.task('backendApiPost', {
  64. path: '/api/users',
  65. data: {
  66. name: 'Jamie Curnow',
  67. nickname: 'James',
  68. email: '[email protected]',
  69. roles: [],
  70. is_disabled: false,
  71. auth: {
  72. type: 'password',
  73. secret: 'changeme'
  74. }
  75. }
  76. }).then((data) => {
  77. // Check the swagger schema:
  78. cy.task('validateSwaggerSchema', {
  79. endpoint: '/users',
  80. method: 'post',
  81. statusCode: 201,
  82. responseSchema: data,
  83. verbose: true
  84. }).should('equal', null);
  85. expect(data.result).to.have.property('id');
  86. expect(data.result.id).to.be.greaterThan(0);
  87. cy.wrap(data.result);
  88. });
  89. });