Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CC-34716: Cart Reorder + Order Amendment features. #27

Merged
merged 9 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
settings.json
result.html
/node_modules/
/.env
/results/
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.4'

services:
k6:
container_name: "loadtesting_environment"
Expand All @@ -8,6 +6,8 @@ services:
dockerfile: Dockerfile
networks:
- loadtesting
# - spryker_private
# - spryker_public
# - spryker_demo_private
# - spryker_demo_public
# - spryker_b2b_marketplace_dev_private
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.4'

services:
k6:
container_name: "loadtesting_environment"
Expand Down
2 changes: 1 addition & 1 deletion docs/Executing-Tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This repository contains a folder `shell` that contains a lot of scripts that help with running tests. The important part is that they must be executed from the project root!

```bash
./shell/run-a-single-test <path/to/the/test-file.js>
./shell/run-a-single-test.sh <path/to/the/test-file.js>
```

Or run all tests for a product.
Expand Down
10 changes: 10 additions & 0 deletions environments/SUITE.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"local": {
"storefrontUrl": "http://yves.%store%.spryker.local",
"storefrontApiUrl": "http://glue.%store%.spryker.local",
"backendApiUrl": "http://glue-backend.%store%.spryker.local",
"backofficeUrl": "http://backoffice.%store%.spryker.local",
"backofficeApiUrl": "http://backend-api.%store%.spryker.local",
"stores": ["eu", "us"]
}
}
63 changes: 63 additions & 0 deletions helpers/auth-token-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export class AuthTokenManager {
constructor(http, urlHelper, assertionsHelper) {
if (AuthTokenManager.instance) {
return AuthTokenManager.instance;
}

if (!http || !urlHelper || !assertionsHelper) {
throw new Error('Http, UrlHelper, and AssertionsHelper must be provided.');
}

this.http = http;
this.urlHelper = urlHelper;
this.assertionsHelper = assertionsHelper;

// Token cache: { '<email>:<password>': 'Bearer <token>' }
this.tokenCache = {};

AuthTokenManager.instance = this;
}

static getInstance(http, urlHelper, assertionsHelper) {
if (!AuthTokenManager.instance) {
AuthTokenManager.instance = new AuthTokenManager(http, urlHelper, assertionsHelper);
}
return AuthTokenManager.instance;
}

getAuthToken(email, password) {
const cacheKey = `${email}:${password}`;

// Return cached token if exists
if (this.tokenCache[cacheKey]) {
return this.tokenCache[cacheKey];
}

// Fetch new token from the API
const urlAccessTokens = `${this.urlHelper.getStorefrontApiBaseUrl()}/access-tokens`;
const response = this.http.sendPostRequest(
this.http.url`${urlAccessTokens}`,
JSON.stringify({
data: {
type: 'access-tokens',
attributes: {
username: email,
password: password,
},
},
}),
{ headers: { Accept: 'application/json' } },
false
);

this.assertionsHelper.assertResponseStatus(response, 201, 'Auth Token');

const responseJson = JSON.parse(response.body);
this.assertionsHelper.assertSingleResourceResponseBodyStructure(responseJson, 'Auth Token');

const token = `${responseJson.data.attributes.tokenType} ${responseJson.data.attributes.accessToken}`;
this.tokenCache[cacheKey] = token;

return token;
}
}
39 changes: 15 additions & 24 deletions helpers/cart-helper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export class CartHelper {
constructor(urlHelper, http, customerHelper, assertionsHelper) {
constructor(urlHelper, http, customerHelper, assertionsHelper, authTokenManager) {
this.urlHelper = urlHelper;
this.http = http;
this.customerHelper = customerHelper;
this.assertionsHelper = assertionsHelper;
this.authTokenManager = authTokenManager;
}

haveCartWithProducts(quantity = 1, sku = '100429') {
Expand Down Expand Up @@ -42,34 +43,14 @@ export class CartHelper {
return cartsResponseJson.data.id;
}

getParamsWithAuthorization() {
getParamsWithAuthorization(email = this.customerHelper.getDefaultCustomerEmail(), password = this.customerHelper.getDefaultCustomerPassword()) {
const defaultParams = {
headers: {
'Accept': 'application/json'
},
};
const urlAccessTokens = `${this.urlHelper.getStorefrontApiBaseUrl()}/access-tokens`;

const response = this.http.sendPostRequest(
this.http.url`${urlAccessTokens}`,
JSON.stringify({
data: {
type: 'access-tokens',
attributes: {
username: this.customerHelper.getDefaultCustomerEmail(),
password: this.customerHelper.getDefaultCustomerPassword()
}
}
}),
defaultParams,
false
);
this.assertionsHelper.assertResponseStatus(response, 201, 'Auth Token');

const responseJson = JSON.parse(response.body);
this.assertionsHelper.assertSingleResourceResponseBodyStructure(responseJson, 'Auth Token');

defaultParams.headers.Authorization = `${responseJson.data.attributes.tokenType} ${responseJson.data.attributes.accessToken}`;
defaultParams.headers.Authorization = this.authTokenManager.getAuthToken(email, password);

return defaultParams;
}
Expand All @@ -78,7 +59,8 @@ export class CartHelper {
return `${this.urlHelper.getStorefrontApiBaseUrl()}/carts`;
}

getCarts(params) {
getCarts(email) {
const params = this.getParamsWithAuthorization(email);
const getCartsResponse = this.http.sendGetRequest(this.http.url`${this.getCartsUrl()}`, params, false);
this.assertionsHelper.assertResponseStatus(getCartsResponse, 200, 'Get Carts');

Expand All @@ -98,6 +80,15 @@ export class CartHelper {
}
}

deleteCart(customerEmail, cartId, thresholdTag = null) {
const requestParams = this.getParamsWithAuthorization(customerEmail);
if (thresholdTag) {
requestParams.tags = { name: thresholdTag };
}

this.http.sendDeleteRequest(this.http.url`${this.getCartsUrl()}/${cartId}`, null, requestParams, false);
}

addItemToCart(cartId, quantity, params, sku) {
const addItemToCartResponse = this.http.sendPostRequest(
this.http.url`${this.getCartsUrl()}/${cartId}/items`,
Expand Down
182 changes: 182 additions & 0 deletions helpers/dynamic-fixtures-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
export class DynamicFixturesHelper {
constructor(backendApiUrl, http) {
this.backendApiUrl = backendApiUrl;
this.http = http;
}

haveCustomersWithQuotes(customerCount, quoteCount = 1, itemCount = 10, defaultItemPrice = 1000) {
const defaultParams = {
headers: {
'Content-Type': 'application/vnd.api+json',
},
};

const dynamicFixturesResponse = this.http.sendPostRequest(
this.http.url`${this.backendApiUrl}/dynamic-fixtures`,
JSON.stringify(this._getCustomersWithQuotesAttributes(customerCount, quoteCount, itemCount, defaultItemPrice)),
defaultParams,
false
);

const dynamicFixturesResponseJson = JSON.parse(dynamicFixturesResponse.body);
const customerData = dynamicFixturesResponseJson.data.filter(item => /^customer\d+$/.test(item.attributes.key));

return customerData.map(customer => {
const associatedCustomerQuotes = dynamicFixturesResponseJson.data
.filter(item => item.attributes.key.startsWith(`${customer.attributes.key}Quote`))
.map(quote => quote.attributes.data.uuid);

return {
customerEmail: customer.attributes.data.email,
quoteIds: associatedCustomerQuotes
};
});
}

haveConsoleCommands(commands) {
const params = {
headers: {
'Content-Type': 'application/vnd.api+json',
},
timeout: 600000,
};

this.http.sendPostRequest(
this.http.url`${this.backendApiUrl}/dynamic-fixtures`,
JSON.stringify(this._getConsoleCommandsAttributes(commands)),
params,
false
);
}

_getConsoleCommandsAttributes(commands) {
const operations = commands.map((command) => {
return {
type: 'cli-command',
name: command,
};
});

return {
data: {
type: 'dynamic-fixtures',
attributes: {
operations: operations,
},
},
};
}

_getCustomersWithQuotesAttributes(customerCount = 1, quoteCount = 1, itemCount = 10, defaultItemPrice = 100) {
const baseOperations = [
{
type: 'transfer',
name: 'LocaleTransfer',
key: 'locale',
arguments: { id_locale: 66, locale_name: 'en_US' }
},
{
type: 'transfer',
name: 'ProductImageTransfer',
key: 'productImage',
arguments: {
externalUrlSmall: 'https://images.icecat.biz/img/gallery_mediums/30691822_1486.jpg',
externalUrlLarge: 'https://images.icecat.biz/img/gallery/30691822_1486.jpg'
}
}
];

// Generate products dynamically
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: generate products, customers, etc would be better to move to a separate methods just for readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be reworked in next phase. For now I would keep it as it is.

const products = Array.from({ length: itemCount }, (_, i) => {
const productKey = `product${i + 1}`;
return [
{
type: 'helper',
name: 'haveFullProduct',
key: productKey,
arguments: [{}, { idTaxSet: 1 }]
},
{
type: 'helper',
name: 'haveProductImageSet',
arguments: [
{
name: 'default',
idProduct: `#${productKey}.id_product_concrete`,
idProductAbstract: `#${productKey}.fk_product_abstract`,
productImages: ['#productImage']
}
]
},
{
type: 'helper',
name: 'havePriceProduct',
arguments: [
{
skuProductAbstract: `#${productKey}.abstract_sku`,
skuProduct: `#${productKey}.sku`,
moneyValue: { netAmount: defaultItemPrice, grossAmount: defaultItemPrice }
}
]
},
{
type: 'helper',
name: 'haveProductInStock',
arguments: [{ sku: `#${productKey}.sku`, isNeverOutOfStock: '1', fkStock: 1, stockType: 'Warehouse1' }]
}
];
}).flat();

// Generate items dynamically for quotes
const generateItems = () => {
return Array.from({ length: itemCount }, (_, i) => ({
sku: `#product${i + 1}.sku`,
abstractSku: `#product${i + 1}.abstract_sku`,
quantity: 1,
unitPrice: defaultItemPrice
}));
};

// Generate customers dynamically
const customers = Array.from({ length: customerCount }, (_, customerIndex) => {
const customerKey = `customer${customerIndex + 1}`;
return [
{
type: 'helper',
name: 'haveCustomer',
key: customerKey,
arguments: [{ locale: '#locale', password: 'change123' }]
},
{
type: 'helper',
name: 'confirmCustomer',
key: `confirmed${customerKey}`,
arguments: [`#${customerKey}`]
},
// Generate quotes for each customer
...Array.from({ length: quoteCount }, (_, quoteIndex) => ({
type: 'helper',
name: 'havePersistentQuote',
key: `${customerKey}Quote${quoteIndex + 1}`,
arguments: [
{
customer: `#${customerKey}`,
items: generateItems()
}
]
}))
];
}).flat();

return {
data: {
type: 'dynamic-fixtures',
attributes: {
synchronize: true,
operations: [...baseOperations, ...products, ...customers]
}
}
};
}

}
12 changes: 12 additions & 0 deletions helpers/summary-helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { textSummary } from '../lib/external/k6-summary.js';
import { htmlReport } from '../lib/external/k6-html-report.js';

export function handleSummary(data) {
if (__ENV.K6_HOSTENV === 'local') {
return {
'result.html': htmlReport(data),
stdout: textSummary(data, { indent: ' ', enableColors: true }),
};
} else {
return handleSummaryTesting(data);
}
}

function handleSummaryTesting(data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor:

Suggested change
function handleSummaryTesting(data) {
function _handleSummaryTesting(data) {

As I understand it is used in this class only

const failedMetrics = getFailedMetrics(data);

const summary = {
Expand Down
1 change: 1 addition & 0 deletions lib/external/k6-html-report.js

Large diffs are not rendered by default.

Loading