From fa1dae33b9df9bc105252fdadee489d6fd6c830d Mon Sep 17 00:00:00 2001 From: Sebastian Seggewiss Date: Tue, 10 Jan 2023 11:09:44 +0100 Subject: [PATCH] 7.0.0 --- .github/workflows/npm-publish.yml | 2 +- README.md | 6 - cypress/support/CHANGELOG.md | 14 ++ cypress/support/commands/api-commands.js | 141 +++++++------------ cypress/support/commands/commands.js | 8 +- cypress/support/commands/fixture-commands.js | 2 +- package.json | 2 +- 7 files changed, 73 insertions(+), 102 deletions(-) create mode 100644 cypress/support/CHANGELOG.md diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 8988ace..6d88e9e 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -4,7 +4,7 @@ on: push jobs: publish: - if: github.ref == 'refs/heads/6.x' + if: github.ref == 'refs/heads/7.x' runs-on: ubuntu-latest steps: - name: Checkout diff --git a/README.md b/README.md index 0d9fcfe..f289518 100644 --- a/README.md +++ b/README.md @@ -317,12 +317,6 @@ cy.openInitialPage(); cy.authenticate() ``` -#### Logs in silently using Shopware API - -```js -cy.loginViaApi() -``` - #### Search for an existing entity using Shopware API at the given endpoint ```js diff --git a/cypress/support/CHANGELOG.md b/cypress/support/CHANGELOG.md new file mode 100644 index 0000000..14b92e7 --- /dev/null +++ b/cypress/support/CHANGELOG.md @@ -0,0 +1,14 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## [7.0.0] - 10.01.2023 + +### Changed + +- Changed `typeSingleSelectAndCheck` to work with Cypress 12 +- Changed `authenticate` to utilize `cy.session()` + +### Removed + +- Removed `loginViaApi` use `authenticate` instead diff --git a/cypress/support/commands/api-commands.js b/cypress/support/commands/api-commands.js index 783ba89..d6122c2 100644 --- a/cypress/support/commands/api-commands.js +++ b/cypress/support/commands/api-commands.js @@ -6,94 +6,57 @@ const Fixture = require('../service/administration/fixture.service'); * @name authenticate * @function */ - Cypress.Commands.add('authenticate', () => { - return cy.getCookie('refreshBearerAuth').then((refreshBearerAuthCookie) => { - if (refreshBearerAuthCookie) { - cy.log('Delete existing bearerAuth because it is not admin') - cy.clearCookie('bearerAuth') - cy.clearCookie('refreshBearerAuth') - } else { - cy.log('Existing bearerAuth is admin') - } - - return cy.getCookie('bearerAuth').then((cookie) => { - const cookieValue = JSON.parse(decodeURIComponent(cookie && cookie.value)); - - return cy.request({ - method: 'GET', - url: '/api/_info/version', - failOnStatusCode: false, - headers: { - Authorization: `Bearer ${cookieValue && cookieValue.access}` - }, - }).then((response) => { - // when token is valid - if (response.status === 200) { - return cy.log('cached /api/oauth/token') - .then(() => cookieValue); +Cypress.Commands.add('authenticate', () => { + cy.session( + 'bearerAuth', + () => { + cy.request( + 'POST', + '/api/oauth/token', + { + grant_type: Cypress.env('grant') ? Cypress.env('grant') : 'password', + client_id: Cypress.env('client_id') ? Cypress.env('client_id') : 'administration', + scopes: Cypress.env('scope') ? Cypress.env('scope') : 'write', + username: Cypress.env('username') || Cypress.env('user') || 'admin', + password: Cypress.env('password') || Cypress.env('pass') || 'shopware' } + ).then((responseData) => { + let result = responseData.body; + result.access = result.access_token; + result.refresh = result.refresh_token; - // delete existing cookies - cy.clearCookie('bearerAuth') - cy.clearCookie('refreshBearerAuth') + cy.log('request /api/oauth/token') - // request new token when it is invalid - return cy.request( - 'POST', - '/api/oauth/token', + return cy.setCookie( + 'bearerAuth', + JSON.stringify(result), { - grant_type: Cypress.env('grant') ? Cypress.env('grant') : 'password', - client_id: Cypress.env('client_id') ? Cypress.env('client_id') : 'administration', - scopes: Cypress.env('scope') ? Cypress.env('scope') : 'write', - username: Cypress.env('username') || Cypress.env('user') || 'admin', - password: Cypress.env('password') || Cypress.env('pass') || 'shopware' + path: Cypress.env('admin'), + sameSite: "strict" } - ).then((responseData) => { - let result = responseData.body; - result.access = result.access_token; - result.refresh = result.refresh_token; - result.expiry = Math.round(+new Date() / 1000) + responseData.body.expires_in; - - cy.log('request /api/oauth/token') - - // auth token needs to have at least x seconds of lifetime left - const minimumLifetime = Cypress.env('minAuthTokenLifetime') || 60; - - return cy.setCookie( - 'bearerAuth', - JSON.stringify(result), - { - expiry: result.expiry - minimumLifetime, - path: Cypress.env('admin'), - sameSite: "strict" - } - ).then(() => result) + ); + }); + }, + { + validate: () => { + return cy.getCookie('bearerAuth').then((cookie) => { + const cookieValue = JSON.parse(decodeURIComponent(cookie && cookie.value)); + + return cy.request({ + method: 'GET', + url: '/api/_info/version', + failOnStatusCode: true, + headers: { + Authorization: `Bearer ${cookieValue && cookieValue.access}` + }, + }).then(() => true); }); - }) - }) - }) -}); - -/** - * Logs in silently using Shopware API - * @memberOf Cypress.Chainable# - * @name loginViaApi - * @function - */ -Cypress.Commands.add('loginViaApi', () => { - return cy.authenticate().then((result) => { - // we do not need to expire this because the refresh token is valid for a week and this cookie is not persisted - cy.setCookie( - 'bearerAuth', - JSON.stringify(result), - { - path: Cypress.env('admin'), - sameSite: "strict" } - ); + } + ); - // Return bearer token - return cy.getCookie('bearerAuth'); + return cy.getCookie('bearerAuth').then((cookie) => { + return JSON.parse(decodeURIComponent(cookie && cookie.value)); }); }); @@ -171,14 +134,16 @@ Cypress.Commands.add('requestAdminApi', (method, url, requestData = {}) => { */ Cypress.Commands.add('clearCacheAdminApi', (method, url) => { return cy.authenticate().then((result) => { - const requestConfig = { - headers: { - Authorization: `Bearer ${result.access}` - }, - method: method, - url: url - }; - return cy.request(requestConfig); + return cy.getCookie('bearerAuth').then((cookie) => { + const requestConfig = { + headers: { + Authorization: `Bearer ${JSON.parse(cookie.value).access}` + }, + method: method, + url: url + }; + return cy.request(requestConfig); + }) }); }); diff --git a/cypress/support/commands/commands.js b/cypress/support/commands/commands.js index 1918391..d32fb7b 100644 --- a/cypress/support/commands/commands.js +++ b/cypress/support/commands/commands.js @@ -341,13 +341,11 @@ Cypress.Commands.add( prevSubject: 'element', }, (subject, value, selector) => { - cy.get(subject).typeSingleSelect(value, selector); + cy.wrap(subject).typeSingleSelect(value, selector); // expect the placeholder for an empty select field not be shown and search for the value - cy.get( - `${subject.selector} .sw-select__selection .is--placeholder` - ).should("not.exist"); - cy.get(`${subject.selector} .sw-select__selection`).contains(value); + cy.wrap(subject).get(`${selector} .sw-select__selection .is--placeholder`).should("not.exist"); + cy.wrap(subject).get(`${selector} .sw-select__selection`).contains(value); } ); diff --git a/cypress/support/commands/fixture-commands.js b/cypress/support/commands/fixture-commands.js index 7855828..9495326 100644 --- a/cypress/support/commands/fixture-commands.js +++ b/cypress/support/commands/fixture-commands.js @@ -771,4 +771,4 @@ Cypress.Commands.add('createProductVariantFixture', () => { }], }, 'product-variants.json'); }); -}); \ No newline at end of file +}); diff --git a/package.json b/package.json index eabcfd9..66db1da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shopware-ag/e2e-testsuite-platform", - "version": "6.0.0", + "version": "7.0.0", "description": "E2E Testsuite for Shopware 6 using Cypress.js", "keywords": [ "e2e",