diff --git a/cypress/apiRequests/Metadata.js b/cypress/apiRequests/Metadata.js new file mode 100644 index 000000000..f3f509187 --- /dev/null +++ b/cypress/apiRequests/Metadata.js @@ -0,0 +1,31 @@ +export function updateMetadata(id, key, value) { + const mutation = `mutation{ + updateMetadata(id:"${id}" input:{ + key:"${key}", + value:"${value}" + }){ + errors{ + field + message + } + } + }`; + return cy.sendRequestWithQuery(mutation).its("body.data.updateMetadata"); +} + +export function updatePrivateMetadata(id, key, value) { + const mutation = `mutation{ + updatePrivateMetadata(id:"${id}" input:{ + key:"${key}", + value:"${value}" + }){ + errors{ + field + message + } + } + }`; + return cy + .sendRequestWithQuery(mutation) + .its("body.data.updatePrivateMetadata"); +} diff --git a/cypress/apiRequests/Order.js b/cypress/apiRequests/Order.js index 9714ee456..88fec7252 100644 --- a/cypress/apiRequests/Order.js +++ b/cypress/apiRequests/Order.js @@ -1,4 +1,4 @@ -import { getDefaultAddress } from "./utils/Utils"; +import { getDefaultAddress, getValueWithDefault } from "./utils/Utils"; export function markOrderAsPaid(orderId) { const mutation = `mutation{ @@ -25,16 +25,22 @@ export function addProductToOrder(orderId, variantId, quantity = 1) { return cy.sendRequestWithQuery(mutation); } -export function createDraftOrder( +export function createDraftOrder({ customerId, shippingMethodId, channelId, address -) { +}) { + const user = getValueWithDefault(customerId, `user:"${customerId}"`); + const shippingMethod = getValueWithDefault( + shippingMethodId, + `shippingMethod:"${shippingMethodId}"` + ); + const mutation = `mutation{ draftOrderCreate(input:{ - user:"${customerId}" - shippingMethod:"${shippingMethodId}" + ${user} + ${shippingMethod} channelId: "${channelId}" ${getDefaultAddress(address, "shippingAddress")} ${getDefaultAddress(address, "billingAddress")} @@ -80,6 +86,14 @@ export function getOrder(orderId) { shippingMethod{ id } + metadata{ + key + value + } + privateMetadata{ + key + value + } } }`; cy.sendRequestWithQuery(query).its("body.data.order"); diff --git a/cypress/apiRequests/storeFront/ProductDetails.js b/cypress/apiRequests/storeFront/ProductDetails.js index 67450994f..45df10b68 100644 --- a/cypress/apiRequests/storeFront/ProductDetails.js +++ b/cypress/apiRequests/storeFront/ProductDetails.js @@ -69,3 +69,29 @@ export function getProductDetails(productId, channelId, auth = "token") { }`; return cy.sendRequestWithQuery(query, auth); } + +export function getProductMetadata({ + productId, + channelSlug, + auth, + withPrivateMetadata +}) { + const privateMetadata = getValueWithDefault( + withPrivateMetadata, + `privateMetadata{ + key + value + }` + ); + + const query = `query{ + product(id:"${productId}" channel:"${channelSlug}"){ + metadata{ + key + value + } + ${privateMetadata} + } + }`; + return cy.sendRequestWithQuery(query, auth).its("body"); +} diff --git a/cypress/integration/allEnv/metadata.js b/cypress/integration/allEnv/metadata.js new file mode 100644 index 000000000..1a149dfb8 --- /dev/null +++ b/cypress/integration/allEnv/metadata.js @@ -0,0 +1,100 @@ +import faker from "faker"; + +import { + updateMetadata, + updatePrivateMetadata +} from "../../apiRequests/Metadata"; +import { createDraftOrder, getOrder } from "../../apiRequests/Order"; +import { getProductMetadata } from "../../apiRequests/storeFront/ProductDetails"; +import { getDefaultChannel } from "../../utils/channelsUtils"; +import { + createProductInChannel, + createTypeAttributeAndCategoryForProduct +} from "../../utils/products/productsUtils"; + +describe("Test for metadata", () => { + const startsWith = "Metadata"; + const name = `${startsWith}${faker.datatype.number()}`; + const metadata = { key: "metadataKey", value: "metadataValue" }; + let channel; + let product; + + before(() => { + cy.clearSessionData().loginUserViaRequest(); + getDefaultChannel() + .then(channelResp => { + channel = channelResp; + createTypeAttributeAndCategoryForProduct(name); + }) + .then(({ attribute, category, productType }) => { + createProductInChannel({ + attributeId: attribute.id, + categoryId: category.id, + channelId: channel.id, + name, + productTypeId: productType.id + }); + }) + .then(({ product: productResp }) => { + product = productResp; + }); + }); + + it("should create metadata for product", () => { + cy.clearSessionData().loginUserViaRequest(); + updateMetadata(product.id, metadata.key, metadata.value); + updatePrivateMetadata(product.id, metadata.key, metadata.value) + .then(() => { + getProductMetadata({ + productId: product.id, + channelSlug: channel.slug, + auth: "auth", + withPrivateMetadata: true + }).its("data"); + }) + .then(({ product: productResp }) => { + expect(productResp.metadata[0].key).to.eq(metadata.key); + expect(productResp.metadata[0].value).to.eq(metadata.value); + expect(productResp.privateMetadata[0].key).to.eq(metadata.key); + expect(productResp.privateMetadata[0].value).to.eq(metadata.value); + getProductMetadata({ + productId: product.id, + channelSlug: channel.slug, + auth: "token", + withPrivateMetadata: true + }); + }) + .then(({ errors }) => { + expect(errors[0].extensions.exception.code).to.eq("PermissionDenied"); + getProductMetadata({ + productId: product.id, + channelSlug: channel.slug, + auth: "token", + withPrivateMetadata: false + }).its("data"); + }) + .then(({ product: productResp }) => { + expect(productResp.metadata[0].key).to.eq(metadata.key); + expect(productResp.metadata[0].value).to.eq(metadata.value); + }); + }); + it("should create metadata for order", () => { + let order; + cy.clearSessionData().loginUserViaRequest(); + createDraftOrder({ channelId: channel.id }) + .then(orderResp => { + order = orderResp; + updateMetadata(order.id, metadata.key, metadata.value); + updatePrivateMetadata(order.id, metadata.key, metadata.value); + }) + .then(() => { + getOrder(order.id); + }) + .then(orderResp => { + expect(orderResp.metadata[0].key).to.eq(metadata.key); + expect(orderResp.metadata[0].value).to.eq(metadata.value); + expect(orderResp.privateMetadata[0].key).to.eq(metadata.key); + expect(orderResp.privateMetadata[0].value).to.eq(metadata.value); + }); + }); +}); diff --git a/cypress/utils/ordersUtils.js b/cypress/utils/ordersUtils.js index bd47f6fa4..2bea53e90 100644 --- a/cypress/utils/ordersUtils.js +++ b/cypress/utils/ordersUtils.js @@ -61,7 +61,7 @@ export function createReadyToFulfillOrder({ }) { let order; return orderRequest - .createDraftOrder(customerId, shippingMethodId, channelId, address) + .createDraftOrder({ customerId, shippingMethodId, channelId, address }) .then(orderResp => { order = orderResp; assignVariantsToOrder(order, variantsList); @@ -104,7 +104,7 @@ export function createOrder({ }) { let order; return orderRequest - .createDraftOrder(customerId, shippingMethodId, channelId, address) + .createDraftOrder({ customerId, shippingMethodId, channelId, address }) .then(orderResp => { order = orderResp; assignVariantsToOrder(order, variantsList);