From 95be7b5163a7cbc691a70b511fc166b5dbfb9ffa Mon Sep 17 00:00:00 2001 From: Karolina Rakoczy Date: Wed, 27 Jan 2021 10:41:55 +0100 Subject: [PATCH] order ready to fulfill created --- cypress/api/Customer.js | 84 ++++++++++++++++++ cypress/api/Order.js | 60 +++++++++++++ cypress/api/Product.js | 55 ++++++++++++ cypress/api/ShippingMethod.js | 97 +++++++++++++++++++++ cypress/integration/dashboard.js | 111 ++++++++++++++++++++++-- cypress/support/customer/index.js | 37 -------- cypress/support/index.js | 17 +++- cypress/support/shippingMethod/index.js | 51 ----------- 8 files changed, 414 insertions(+), 98 deletions(-) create mode 100644 cypress/api/Customer.js create mode 100644 cypress/api/Order.js create mode 100644 cypress/api/Product.js create mode 100644 cypress/api/ShippingMethod.js delete mode 100644 cypress/support/customer/index.js delete mode 100644 cypress/support/shippingMethod/index.js diff --git a/cypress/api/Customer.js b/cypress/api/Customer.js new file mode 100644 index 000000000..c9beb7042 --- /dev/null +++ b/cypress/api/Customer.js @@ -0,0 +1,84 @@ +export class Customer { + createCustomer(email, customerName, address, isActive = false) { + const mutation = ` + mutation{ + customerCreate(input:{ + firstName: "${customerName}" + lastName: "${customerName}" + email: "${email}" + isActive: ${isActive} + defaultBillingAddress: { + companyName: "${address.companyName}" + streetAddress1: "${address.streetAddress1}" + streetAddress2: "${address.streetAddress2}" + city: "${address.city}" + postalCode: "${address.postalCode}" + country: ${address.country} + phone: "${address.phone}" + } + defaultShippingAddress: { + companyName: "${address.companyName}" + streetAddress1: "${address.streetAddress1}" + streetAddress2: "${address.streetAddress2}" + city: "${address.city}" + postalCode: "${address.postalCode}" + country: ${address.country} + phone: "${address.phone}" + } + }){ + user{ + id + } + accountErrors{ + code + message + } + } + } + `; + return cy.sendRequestWithQuery(mutation); + } + + deleteCustomers(startsWith) { + this.getCustomers(startsWith).then(resp => { + if (resp.body.data.customers) { + const customers = resp.body.data.customers.edges; + customers.forEach(element => { + if (element.node.email.includes(startsWith)) { + this.deleteCustomer(element.node.id); + } + }); + } + }); + } + + deleteCustomer(customerId) { + const mutation = `mutation{ + customerDelete(id:"${customerId}"){ + accountErrors{ + code + message + } + } + }`; + return cy.sendRequestWithQuery(mutation); + } + + getCustomers(startsWith) { + const query = `query{ + customers(first:100, filter: { + search: "${startsWith}" + }){ + edges{ + node{ + id + email + } + } + } + } + `; + return cy.sendRequestWithQuery(query); + } +} +export default Customer; diff --git a/cypress/api/Order.js b/cypress/api/Order.js new file mode 100644 index 000000000..571f27fb0 --- /dev/null +++ b/cypress/api/Order.js @@ -0,0 +1,60 @@ +class Order { + markOrderAsPaid(orderId) { + const mutation = `mutation{ + orderMarkAsPaid(id:"${orderId}"){ + orderErrors{ + message + } + } + }`; + cy.sendRequestWithQuery(mutation); + } + + addProductToOrder(orderId, variantId, quantity = 1) { + const mutation = `mutation{ + draftOrderLinesCreate(id:"${orderId}", input:{ + quantity:${quantity} + variantId: "${variantId}" + }){ + orderErrors{ + message + } + } + }`; + return cy.sendRequestWithQuery(mutation); + } + + createDraftOrder(customerId, shippingMethodId, channelId) { + const mutation = ` + mutation{ + draftOrderCreate(input:{ + user:"${customerId}" + shippingMethod:"${shippingMethodId}" + channel: "${channelId}" + }){ + orderErrors{ + message + } + order{ + id + } + } + } + `; + return cy.sendRequestWithQuery(mutation); + } + completeOrder(orderId) { + const mutation = `mutation{ + draftOrderComplete(id:"${orderId}"){ + order{ + id + } + orderErrors{ + message + } + } + }`; + return cy.sendRequestWithQuery(mutation); + } +} +export default Order; diff --git a/cypress/api/Product.js b/cypress/api/Product.js new file mode 100644 index 000000000..bbffa4312 --- /dev/null +++ b/cypress/api/Product.js @@ -0,0 +1,55 @@ +class Product { + getFirstProducts(first) { + const query = `query{ + products(first:${first}){ + edges{ + node{ + id + name + variants{ + id + } + } + } + } + } + `; + return cy.sendRequestWithQuery(query); + } + + updateChannelInProduct(productId, channelId) { + const mutation = `mutation{ + productChannelListingUpdate(id:"${productId}", input:{ + addChannels:{ + channelId:"${channelId}" + isPublished: true + isAvailableForPurchase:true + visibleInListings:true + } + }){ + product{ + id + name + } + } + }`; + return cy.sendRequestWithQuery(mutation); + } + + updateChannelPriceInVariant(variantId, channelId) { + const mutation = `mutation{ + productVariantChannelListingUpdate(id: "${variantId}", input:{ + channelId: "${channelId}" + price: 10 + costPrice: 10 + }){ + productChannelListingErrors{ + message + } + } + }`; + return cy.sendRequestWithQuery(mutation); + } +} + +export default Product; diff --git a/cypress/api/ShippingMethod.js b/cypress/api/ShippingMethod.js new file mode 100644 index 000000000..179f1b824 --- /dev/null +++ b/cypress/api/ShippingMethod.js @@ -0,0 +1,97 @@ +class ShippingMethod { + createShippingRate(name, shippingZone) { + const mutation = ` + mutation{ + shippingPriceCreate(input:{ + name: "${name}" + shippingZone: "${shippingZone}" + type: PRICE + }){ + shippingMethod{ + id + } + } + } + `; + return cy.sendRequestWithQuery(mutation); + } + + createShippingZone(name, country) { + const mutation = ` + mutation{ + shippingZoneCreate(input:{ + name: "${name}" + countries: "${country}" + }){ + shippingZone{ + id + } + } + } + `; + return cy.sendRequestWithQuery(mutation); + } + + addChannelToShippingMethod(shippingRateId, channelId) { + const mutation = ` + mutation{ + shippingMethodChannelListingUpdate(id:"${shippingRateId}", input:{ + addChannels: { + channelId:"${channelId}" + price:10 + } + }){ + shippingMethod{ + id + } + shippingErrors{ + code + message + } + } + } + `; + return cy.sendRequestWithQuery(mutation); + } + + deleteShippingZones(startsWith) { + this.getShippingZones().then(resp => { + if (resp.body.data.shippingZones) { + const shippingZone = resp.body.data.shippingZones.edges; + shippingZone.forEach(element => { + if (element.node.name.includes(startsWith)) { + this.deleteShippingZone(element.node.id); + } + }); + } + }); + } + + deleteShippingZone(shippingZoneId) { + const mutation = `mutation{ + shippingZoneDelete(id:"${shippingZoneId}"){ + shippingErrors{ + message + } + } + } + `; + return cy.sendRequestWithQuery(mutation); + } + + getShippingZones() { + const query = `query{ + shippingZones(first:100){ + edges{ + node{ + name + id + } + } + } + } + `; + return cy.sendRequestWithQuery(query); + } +} +export default ShippingMethod; diff --git a/cypress/integration/dashboard.js b/cypress/integration/dashboard.js index d4f1c0749..44213df2e 100644 --- a/cypress/integration/dashboard.js +++ b/cypress/integration/dashboard.js @@ -1,12 +1,30 @@ +import faker from "faker"; + +import Customer from "../api/Customer"; +import Order from "../api/Order"; +import Product from "../api/Product"; +import ShippingMethod from "../api/ShippingMethod"; import { DASHBOARD_SELECTORS } from "../elements/dashboard/dashboard-selectors"; // describe("User authorization", () => { + const startsWith = "Cy-"; + + const customer = new Customer(); + const product = new Product(); + const order = new Order(); + const shippingMethod = new ShippingMethod(); + + before(() => { + customer.deleteCustomers(startsWith); + shippingMethod.deleteShippingZones(startsWith); + }); + beforeEach(() => { cy.clearSessionData().loginUserViaRequest(); }); - it("should all elements be visible on the dashboard", () => { + xit("should all elements be visible on the dashboard", () => { cy.visit("/"); softAssertVisibility(DASHBOARD_SELECTORS.sales); softAssertVisibility(DASHBOARD_SELECTORS.orders); @@ -17,17 +35,94 @@ describe("User authorization", () => { softAssertVisibility(DASHBOARD_SELECTORS.productsOutOfStock); }); - xit("aa", () => { - cy.fixture("addresses").then(json => { - cy.createCustomer("Test9", "Test9", json.plAddress); + it("should correct amount of orders be displayed", () => { + faker = require("faker"); + const randomName = startsWith + faker.random.number(); + const randomEmail = randomName + "@example.com"; + product.getFirstProducts(3).then(productsResp => { + const productsList = productsResp.body.data.products.edges; + productsList.forEach(productElement => { + product.updateChannelInProduct( + "Q2hhbm5lbDoxNzk=", + productElement.node.id + ); + const variants = productElement.node.variants; + variants.forEach(variant => { + product.updateChannelPriceInVariant(variant.id, "Q2hhbm5lbDoxNzk="); + }); + }); + cy.fixture("addresses").then(json => { + customer + .createCustomer(randomEmail, randomName, json.plAddress) + .as("createCustomerResponse") + .then(resp => { + const customerId = resp.body.data.customerCreate.user.id; + shippingMethod + .createShippingZone(randomName, "PL") + .then(shippingZoneResp => { + shippingMethod + .createShippingRate( + randomName, + shippingZoneResp.body.data.shippingZoneCreate.shippingZone + .id + ) + .then(rateResp => { + const shippingMethodId = + rateResp.body.data.shippingPriceCreate.shippingMethod.id; + shippingMethod + .addChannelToShippingMethod( + shippingMethodId, + "Q2hhbm5lbDoxNzk=" + ) + .then(shippingMethodResp => { + createReadyToFullfillOrder( + customerId, + shippingMethodId, + "Q2hhbm5lbDoxNzk=", + productsList + ); + }); + }); + }); + }); + }); }); - createChannel(); - createRateShipping(); - addChannelToProduct(); - createOrder(); + cy.visit("/"); + softAssertMatch(DASHBOARD_SELECTORS.orders, /^0/); + softAssertMatch(DASHBOARD_SELECTORS.ordersReadyToFulfill, /^Brak/); + softAssertMatch(DASHBOARD_SELECTORS.paymentsWaitingForCapture, /^Brak/); + softAssertMatch(DASHBOARD_SELECTORS.productsOutOfStock, /^Brak/); }); + function createReadyToFullfillOrder( + customerId, + shippingMethodId, + channelId, + productsList + ) { + order + .createDraftOrder(customerId, shippingMethodId, channelId) + .then(draftOrderResp => { + const orderId = draftOrderResp.body.data.draftOrderCreate.order.id; + productsList.forEach(productElement => { + productElement.node.variants.forEach(variantElement => { + order.addProductToOrder(orderId, variantElement.id); + }); + }); + order.markOrderAsPaid(orderId); + order.completeOrder(orderId); + }); + } + function softAssertVisibility(selector) { cy.get(selector).then(element => chai.softExpect(element).to.be.visible); } + + function softAssertMatch(selector, regexp) { + cy.get(selector) + .invoke("text") + .then(text => + chai.softExpect(assert.match(text, regexp, "regexp matches")) + ); + } }); diff --git a/cypress/support/customer/index.js b/cypress/support/customer/index.js deleted file mode 100644 index 0a900581c..000000000 --- a/cypress/support/customer/index.js +++ /dev/null @@ -1,37 +0,0 @@ -Cypress.Commands.add( - "createCustomer", - (email, name, address, isActive = false) => { - const mustation = ` - mutation{ - customerCreate(input:{ - firstName: "${name}" - lastName: "${name}" - email: "${email}" - isActive: ${isActive} - defaultBillingAddress: { - companyName: "${address.companyName}" - streetAddress1: "${address.streetAddress1}" - streetAddress2: "${address.streetAddress2}" - city: "${address.city}" - postalCode: "${address.postalCode}" - country: ${address.country} - phone: "${address.phone}" - } - defaultShippingAddress: { - companyName: "${address.companyName}" - streetAddress1: "${address.streetAddress1}" - streetAddress2: "${address.streetAddress2}" - city: "${address.city}" - postalCode: "${address.postalCode}" - country: ${address.country} - phone: "${address.phone}" - } - }){ - accountErrors{ - code - } - } - } - `; - } -); diff --git a/cypress/support/index.js b/cypress/support/index.js index c863c2167..3cfc0f3cf 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -1,7 +1,5 @@ import "./user"; import "./softAsserations"; -import "./customer"; -import "./shippingMethod"; Cypress.Commands.add("clearSessionData", () => { // Because of known cypress bug, not all local storage data are cleared. @@ -20,3 +18,18 @@ Cypress.Commands.add("clearSessionData", () => { } }); }); + +Cypress.Commands.add("sendRequestWithQuery", query => + cy.request({ + method: "POST", + body: { + method: "POST", + url: Cypress.env("API_URI"), + query + }, + headers: { + Authorization: `JWT ${window.sessionStorage.getItem("auth")}` + }, + url: Cypress.env("API_URI") + }) +); diff --git a/cypress/support/shippingMethod/index.js b/cypress/support/shippingMethod/index.js deleted file mode 100644 index 949b97651..000000000 --- a/cypress/support/shippingMethod/index.js +++ /dev/null @@ -1,51 +0,0 @@ -Cypress.Commands.add("createShippingRate", (name, shippingZone) => { - const mutation = ` - mutation{ - CreateShippingRate(input:{ - maximumDeliveryDays: null - minimumDeliveryDays: null - name: "${name}" - shippingZone: "${shippingZone}" - type: "PRICE" - }) - } - `; - return mutation; -}); - -Cypress.Commands.add("createShippingZone", (name, country) => { - const mutation = ` - mutation{ - shippingZoneCreate(input:{ - name: "${name}" - countries: "${country}" - }){ - shippingZone{ - id - } - } - } - `; - return mutation; -}); - -Cypress.Commands.add("", (shippingRateId, channelId) => { - const mutation = ` - mutation{ - shippingMethodChannelListingUpdate(id:"${shippingRateId}", input:{ - addChannels: { - channelId:"${channelId}" - } - }){ - shippingMethod{ - id - } - shippingErrors{ - code - message - } - } - } - `; - return mutation; -});