Skip to content

Commit

Permalink
Merge pull request #276 from shopware/7.0.0
Browse files Browse the repository at this point in the history
7.0.0
  • Loading branch information
jleifeld authored Jan 10, 2023
2 parents f5e6403 + fa1dae3 commit 3f5b1d6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions cypress/support/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
141 changes: 53 additions & 88 deletions cypress/support/commands/api-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});

Expand Down Expand Up @@ -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);
})
});
});

Expand Down
8 changes: 3 additions & 5 deletions cypress/support/commands/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);

Expand Down
2 changes: 1 addition & 1 deletion cypress/support/commands/fixture-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,4 +771,4 @@ Cypress.Commands.add('createProductVariantFixture', () => {
}],
}, 'product-variants.json');
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 3f5b1d6

Please sign in to comment.