From 79b80350702e6bb85a0615a031cc9a69ef128e8d Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 29 Mar 2022 08:41:02 +0200 Subject: [PATCH 1/3] feat: create coupon code via Magento REST api --- cypress/support/magento2-rest-api.js | 69 ++++++++++++++++++++++++++-- cypress/support/utils.js | 22 +++++++-- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/cypress/support/magento2-rest-api.js b/cypress/support/magento2-rest-api.js index 28165e0..aa625f5 100644 --- a/cypress/support/magento2-rest-api.js +++ b/cypress/support/magento2-rest-api.js @@ -1,3 +1,5 @@ +import {getWebsites} from './utils' + export class Magento2RestApi { static createCustomerAccount(customer) { cy.request({ @@ -5,7 +7,7 @@ export class Magento2RestApi { url: '/rest/all/V1/customers', body: customer, failOnStatusCode: false, - timeout: 100000 + timeout: 100000, }).then((response) => { // Conditional testing is bad mmkay, I just haven't found a way to expect oneOf in different properties yet if (response.body.hasOwnProperty('message')) { @@ -43,11 +45,72 @@ export class Magento2RestApi { } }, headers: { - authorization: "Bearer " + Cypress.env('MAGENTO2_ADMIN_TOKEN') + authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}` }, - timeout: 100000 + timeout: 100000, }).then((response) => { console.log(response.body); }); } + + /** + * Creates a sales rule with randomly generated coupon code in a promise. + * + * @param discountPercent + * @returns {PromiseLike | Promise} + */ + static createRandomCouponCode(discountPercent = 10) { + return getWebsites().then(websites => { + return cy.request({ + method: 'POST', + url: '/rest/V1/salesRules', + headers: { + authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}` + }, + timeout: 100000, + body: { + rule: { + apply_to_shipping: true, + coupon_type: 'SPECIFIC_COUPON', + customer_group_ids: [0], + discount_amount: discountPercent, + discount_step: 1, + is_active: true, + is_advanced: false, + is_rss: false, + name: 'autogenerated', + simple_action: 'by_percent', + sort_order: 0, + stop_rules_processing: false, + times_used: 0, + use_auto_generation: true, + uses_per_coupon: 9999999, + uses_per_customer: 9999999, + website_ids: Object.values(websites.map(w => w.id)), + }, + }, + }).then(rule => { + cy.request({ + method: 'POST', + url: '/rest/V1/coupons/generate', + headers: { + authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}` + }, + timeout: 100000, + body: { + couponSpec: { + rule_id: rule.body.rule_id, + format: 'alphanum', + quantity: 1, + length: 10, + prefix: 'GEN', + }, + }, + }) + .then(coupon => { + return coupon.body[0] + }) + }) + }) + } } diff --git a/cypress/support/utils.js b/cypress/support/utils.js index aa7324c..904ad69 100644 --- a/cypress/support/utils.js +++ b/cypress/support/utils.js @@ -1,7 +1,21 @@ export const isMobile = () => { - return Cypress.config("viewportWidth") < Cypress.env("mobileViewportWidthBreakpoint"); -}; + return Cypress.config('viewportWidth') < Cypress.env('mobileViewportWidthBreakpoint') +} export const isMobileHyva = () => { - return Cypress.config("viewportWidth") < Cypress.env("mobileViewportWidthBreakpointHyva"); -}; + return Cypress.config('viewportWidth') < Cypress.env('mobileViewportWidthBreakpointHyva') +} + +export function getWebsites() { + return cy.request({ + method: 'GET', + url: '/rest/V1/store/websites', + headers: { + authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}` + }, + timeout: 100000, + }) + .then(response => { + return response.body + }) +} From 5b87fa18780eabe0f057c79441e9571a3bd4f29f Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 29 Mar 2022 09:10:50 +0200 Subject: [PATCH 2/3] chore: remove explicit timeout for creating coupon codes --- cypress/support/magento2-rest-api.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/cypress/support/magento2-rest-api.js b/cypress/support/magento2-rest-api.js index aa625f5..11ff51d 100644 --- a/cypress/support/magento2-rest-api.js +++ b/cypress/support/magento2-rest-api.js @@ -67,7 +67,6 @@ export class Magento2RestApi { headers: { authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}` }, - timeout: 100000, body: { rule: { apply_to_shipping: true, @@ -96,7 +95,6 @@ export class Magento2RestApi { headers: { authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}` }, - timeout: 100000, body: { couponSpec: { rule_id: rule.body.rule_id, From 3b2cf921aca0d08f83e395814356480326796597 Mon Sep 17 00:00:00 2001 From: klg Date: Tue, 29 Mar 2022 09:49:38 +0200 Subject: [PATCH 3/3] chore: remove all timeouts --- cypress/support/magento2-rest-api.js | 3 --- cypress/support/utils.js | 1 - 2 files changed, 4 deletions(-) diff --git a/cypress/support/magento2-rest-api.js b/cypress/support/magento2-rest-api.js index 11ff51d..f5a3d6a 100644 --- a/cypress/support/magento2-rest-api.js +++ b/cypress/support/magento2-rest-api.js @@ -7,7 +7,6 @@ export class Magento2RestApi { url: '/rest/all/V1/customers', body: customer, failOnStatusCode: false, - timeout: 100000, }).then((response) => { // Conditional testing is bad mmkay, I just haven't found a way to expect oneOf in different properties yet if (response.body.hasOwnProperty('message')) { @@ -26,7 +25,6 @@ export class Magento2RestApi { method: 'POST', url: '/rest/all/V1/integration/customer/token', body: customer, - timeout: 50000 }).then((response) => { // TODO: do something with the token... cy.log(response.body) @@ -47,7 +45,6 @@ export class Magento2RestApi { headers: { authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}` }, - timeout: 100000, }).then((response) => { console.log(response.body); }); diff --git a/cypress/support/utils.js b/cypress/support/utils.js index 904ad69..e53a3c3 100644 --- a/cypress/support/utils.js +++ b/cypress/support/utils.js @@ -13,7 +13,6 @@ export function getWebsites() { headers: { authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}` }, - timeout: 100000, }) .then(response => { return response.body