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

feat: create coupon code via Magento REST api #45

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
66 changes: 62 additions & 4 deletions cypress/support/magento2-rest-api.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {getWebsites} from './utils'

export class Magento2RestApi {
static createCustomerAccount(customer) {
cy.request({
method: 'POST',
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')) {
Expand All @@ -24,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)
Expand All @@ -43,11 +43,69 @@ export class Magento2RestApi {
}
},
headers: {
authorization: "Bearer " + Cypress.env('MAGENTO2_ADMIN_TOKEN')
authorization: `Bearer ${Cypress.env('MAGENTO2_ADMIN_TOKEN')}`
},
timeout: 100000
}).then((response) => {
console.log(response.body);
});
}

/**
* Creates a sales rule with randomly generated coupon code in a promise.
*
* @param discountPercent
* @returns {PromiseLike<any> | Promise<any>}
*/
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')}`
},
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')}`
},
body: {
couponSpec: {
rule_id: rule.body.rule_id,
format: 'alphanum',
quantity: 1,
length: 10,
prefix: 'GEN',
},
},
})
.then(coupon => {
return coupon.body[0]
})
})
})
}
}
21 changes: 17 additions & 4 deletions cypress/support/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
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')}`
},
})
.then(response => {
return response.body
})
}