Skip to content

Commit

Permalink
CC-34716: Adjusted tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmiseev committed Nov 27, 2024
1 parent ac814ee commit 4ada73d
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 174 deletions.
2 changes: 0 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 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
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;
}
}
46 changes: 16 additions & 30 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 @@ -48,28 +49,8 @@ export class CartHelper {
'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: email,
password: password
}
}
}),
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,8 +59,13 @@ 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);

console.log(JSON.parse(getCartsResponse.body));
throw Error('test');

this.assertionsHelper.assertResponseStatus(getCartsResponse, 200, 'Get Carts');

Check failure on line 69 in helpers/cart-helper.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unreachable code

const getCartsResponseJson = JSON.parse(getCartsResponse.body);
Expand All @@ -98,13 +84,13 @@ export class CartHelper {
}
}

deleteCart(customerEmail, cartId) {
this.http.sendDeleteRequest(
this.http.url`${this.getCartsUrl()}/${cartId}`,
null,
this.getParamsWithAuthorization(customerEmail),
false
)
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) {
Expand Down
4 changes: 3 additions & 1 deletion tests/abstract-scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AssertionsHelper } from '../helpers/assertions-helper.js';
import { BapiHelper } from '../helpers/bapi-helper.js';
import AdminHelper from '../helpers/admin-helper.js';
import { DynamicFixturesHelper } from '../helpers/dynamic-fixtures-helper.js';
import { AuthTokenManager } from '../helpers/auth-token-manager.js';

export class AbstractScenario {
// eslint-disable-next-line no-unused-vars
Expand All @@ -30,7 +31,8 @@ export class AbstractScenario {
this.customerHelper = new CustomerHelper();
this.adminHelper = new AdminHelper();
this.assertionsHelper = new AssertionsHelper();
this.cartHelper = new CartHelper(this.urlHelper, this.http, this.customerHelper, this.assertionsHelper);
this.authTokenManager = AuthTokenManager.getInstance(this.http, this.urlHelper, this.assertionsHelper);
this.cartHelper = new CartHelper(this.urlHelper, this.http, this.customerHelper, this.assertionsHelper, this.authTokenManager);
this.bapiHelper = new BapiHelper(this.urlHelper, this.http, this.adminHelper, this.assertionsHelper);
this.storefrontHelper = new StorefrontHelper(this.urlHelper, this.http, this.customerHelper, this.assertionsHelper);
this.browserHelper = new BrowserHelper(this.urlHelper, this.customerHelper, this.assertionsHelper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import { AbstractScenario } from '../../../../abstract-scenario.js';
import { group } from 'k6';

export class SharedCartReorderScenario extends AbstractScenario {
execute(customerEmail, orderId) {
execute(customerEmail, orderId, thresholdTag = null) {
let self = this;
group('Cart Reorder', function () {
self.haveReorder(customerEmail, orderId);
self.haveReorder(customerEmail, orderId, thresholdTag);
});
}

haveReorder(customerEmail, orderId) {
haveReorder(customerEmail, orderId, thresholdTag = null) {
const requestParams = this.cartHelper.getParamsWithAuthorization(customerEmail);
if (thresholdTag) {
requestParams.tags = { name: thresholdTag };
}

const cartReorderResponse = this.http.sendPostRequest(
this.http.url`${this.getStorefrontApiBaseUrl()}/cart-reorder`,
JSON.stringify(this._getCartReorderAttributes(orderId)),
this.cartHelper.getParamsWithAuthorization(customerEmail),
requestParams,
false
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,27 @@ export class SharedCheckoutScenario extends AbstractScenario {
});
}

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

const checkoutResponse = this.http.sendPostRequest(
this.http.url`${this.getStorefrontApiBaseUrl()}/checkout?include=orders`,
JSON.stringify(this._getCheckoutData(cartId, customerEmail, isMpPaymentProvider)),
this.cartHelper.getParamsWithAuthorization(customerEmail),
requestParams,
false
);

this.assertionsHelper.assertResponseStatus(checkoutResponse, 201);

return JSON.parse(checkoutResponse.body);
try {
return JSON.parse(checkoutResponse.body);
} catch (e) {
console.log(checkoutResponse.body);
throw Error('Failed to parse response during SharedCheckoutScenario::placeOrder()');
}
}

_getCheckoutData(cartId, defaultCustomerEmail = this.customerHelper.getDefaultCustomerEmail(), isMpPaymentProvider = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@ import { AbstractScenario } from '../../../../abstract-scenario.js';
import { group } from 'k6';

export class SharedOrderAmendmentScenario extends AbstractScenario {
execute(customerEmail, orderId) {
execute(customerEmail, orderId, thresholdTag = null) {
let self = this;
group('Order Amendment', function () {
self.haveOrderAmendment(customerEmail, orderId);
self.haveOrderAmendment(customerEmail, orderId, thresholdTag);
});
}

haveOrderAmendment(customerEmail, orderId) {
haveOrderAmendment(customerEmail, orderId, thresholdTag = null) {
this._ensureOrderState(customerEmail, orderId, 'payment pending');

const requestParams = this.cartHelper.getParamsWithAuthorization(customerEmail);
if (thresholdTag) {
requestParams.tags = { name: thresholdTag };
}

const cartReorderResponse = this.http.sendPostRequest(
this.http.url`${this.getStorefrontApiBaseUrl()}/cart-reorder`,
JSON.stringify(this._getCartReorderAttributes(orderId)),
this.cartHelper.getParamsWithAuthorization(customerEmail),
requestParams,
false
);

this.assertionsHelper.assertResponseStatus(cartReorderResponse, 201);

return JSON.parse(cartReorderResponse.body);
try {
return JSON.parse(cartReorderResponse.body);
} catch (e) {
console.log(cartReorderResponse.body);
throw Error('Failed to parse response during SharedOrderAmendmentScenario::haveOrderAmendment()');
}
}

_ensureOrderState(customerEmail, orderId, state, maxRetries = 5) {
Expand Down Expand Up @@ -50,7 +60,12 @@ export class SharedOrderAmendmentScenario extends AbstractScenario {
);
self.assertionsHelper.assertResponseStatus(orderResponse, 200);

return JSON.parse(orderResponse.body);
try {
return JSON.parse(orderResponse.body);
} catch (e) {
console.log(orderResponse.body);
throw Error('Failed to parse response during SharedOrderAmendmentScenario::_getOrder()');
}
}

_getCartReorderAttributes(orderId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@ import {
import { SharedCheckoutScenario } from '../../../../cross-product/sapi/scenarios/checkout/shared-checkout-scenario.js';
export { handleSummary } from '../../../../../helpers/summary-helper.js';

const iterations = 10;
const virtualUsersCount = 10;
const vus = 10;
const iterations = 1;
const itemCount = 70;
const defaultItemPrice = 1000; // 10.00 EUR
const environment = 'SUITE';
const thresholdTag = 'cart_reorder';

const sharedCheckoutScenario = new SharedCheckoutScenario(environment);
const sharedCartReorderScenario = new SharedCartReorderScenario(environment);

export const options = loadDefaultOptions();
options.scenarios = {
TEST_ID_Cart_Reorder: {
SAPI15_cart_reorder: {
exec: 'execute',
executor: 'per-vu-iterations',
tags: {
testId: 'TEST_ID',
testId: 'SAPI15',
testGroup: 'Cart Reorder',
},
vus: vus,
iterations: iterations,
vus: virtualUsersCount,
},
};
options.thresholds[`http_req_duration{url:${sharedCartReorderScenario.getStorefrontApiBaseUrl()}/cart-reorder}`] = ['avg<409'];
options.thresholds[`http_req_duration{name:${thresholdTag}}`] = ['avg<300'];

export function setup() {
return sharedCheckoutScenario.dynamicFixturesHelper.haveCustomersWithQuotes(virtualUsersCount, iterations);
return sharedCheckoutScenario.dynamicFixturesHelper.haveCustomersWithQuotes(vus, iterations, itemCount, defaultItemPrice);
}

export function execute(data) {
const customerIndex = __VU % data.length;
const vus = __VU - 1;
const customerIndex = vus % data.length;
const { customerEmail, quoteIds } = data[customerIndex];
const quoteIndex = __ITER % quoteIds.length;

// Place an order
const checkoutResponseJson = sharedCheckoutScenario.haveOrder(customerEmail, quoteIds[quoteIndex], false);

// Reorder
sharedCartReorderScenario.execute(customerEmail, checkoutResponseJson.data.relationships.orders.data[0].id);
sharedCartReorderScenario.execute(customerEmail, checkoutResponseJson.data.relationships.orders.data[0].id, thresholdTag);
}
40 changes: 0 additions & 40 deletions tests/suite/sapi/tests/cart-reorder/SUITE-TEST_ID-cart-reorder.js

This file was deleted.

Loading

0 comments on commit 4ada73d

Please sign in to comment.