From 79c601279e8d16d1a5f1f98396dbd1e8970c5228 Mon Sep 17 00:00:00 2001 From: zakariaelas Date: Thu, 30 Sep 2021 14:50:38 +0100 Subject: [PATCH] tests: integration tests --- .../api/__tests__/admin/order.js | 686 +++++++++--------- integration-tests/api/__tests__/store/cart.js | 421 ++++++----- .../api/__tests__/store/shipping-options.js | 152 ++-- integration-tests/api/helpers/cart-seeder.js | 112 +-- integration-tests/api/helpers/swap-seeder.js | 116 ++- 5 files changed, 837 insertions(+), 650 deletions(-) diff --git a/integration-tests/api/__tests__/admin/order.js b/integration-tests/api/__tests__/admin/order.js index 863b151551..640221d5f2 100644 --- a/integration-tests/api/__tests__/admin/order.js +++ b/integration-tests/api/__tests__/admin/order.js @@ -1,64 +1,65 @@ -const path = require("path"); +const path = require("path") const { ReturnReason, Order, LineItem, ProductVariant, -} = require("@medusajs/medusa"); + RMAShippingOption, +} = require("@medusajs/medusa") -const setupServer = require("../../../helpers/setup-server"); -const { useApi } = require("../../../helpers/use-api"); -const { initDb, useDb } = require("../../../helpers/use-db"); +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { initDb, useDb } = require("../../../helpers/use-db") -const orderSeeder = require("../../helpers/order-seeder"); -const swapSeeder = require("../../helpers/swap-seeder"); -const adminSeeder = require("../../helpers/admin-seeder"); -const claimSeeder = require("../../helpers/claim-seeder"); +const orderSeeder = require("../../helpers/order-seeder") +const swapSeeder = require("../../helpers/swap-seeder") +const adminSeeder = require("../../helpers/admin-seeder") +const claimSeeder = require("../../helpers/claim-seeder") const { expectPostCallToReturn, expectAllPostCallsToReturn, callGet, partial, -} = require("../../helpers/call-helpers"); +} = require("../../helpers/call-helpers") -jest.setTimeout(30000); +jest.setTimeout(30000) describe("/admin/orders", () => { - let medusaProcess; - let dbConnection; + let medusaProcess + let dbConnection beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..")); - dbConnection = await initDb({ cwd }); - medusaProcess = await setupServer({ cwd }); - }); + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) afterAll(async () => { - const db = useDb(); - await db.shutdown(); + const db = useDb() + await db.shutdown() - medusaProcess.kill(); - }); + medusaProcess.kill() + }) describe("GET /admin/orders", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("gets orders", async () => { - const api = useApi(); + const api = useApi() const response = await api .get("/admin/orders", { @@ -67,25 +68,25 @@ describe("/admin/orders", () => { }, }) .catch((err) => { - console.log(err); - }); - expect(response.status).toEqual(200); - }); - }); + console.log(err) + }) + expect(response.status).toEqual(200) + }) + }) describe("GET /admin/orders", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); - await swapSeeder(dbConnection); - await claimSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) + await swapSeeder(dbConnection) + await claimSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - const manager = dbConnection.manager; + const manager = dbConnection.manager const order2 = manager.create(Order, { id: "test-order-not-payed", @@ -136,9 +137,9 @@ describe("/admin/orders", () => { }, ], items: [], - }); + }) - await manager.save(order2); + await manager.save(order2) const li2 = manager.create(LineItem, { id: "test-item", @@ -151,23 +152,23 @@ describe("/admin/orders", () => { quantity: 1, variant_id: "test-variant", order_id: "test-order-not-payed", - }); + }) - await manager.save(li2); - }); + await manager.save(li2) + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("cancels an order and increments inventory_quantity", async () => { - const api = useApi(); - const manager = dbConnection.manager; + const api = useApi() + const manager = dbConnection.manager - const initialInventoryRes = await api.get("/store/variants/test-variant"); + const initialInventoryRes = await api.get("/store/variants/test-variant") - expect(initialInventoryRes.data.variant.inventory_quantity).toEqual(1); + expect(initialInventoryRes.data.variant.inventory_quantity).toEqual(1) const response = await api .post( @@ -180,26 +181,26 @@ describe("/admin/orders", () => { } ) .catch((err) => { - console.log(err); - }); - expect(response.status).toEqual(200); + console.log(err) + }) + expect(response.status).toEqual(200) - const secondInventoryRes = await api.get("/store/variants/test-variant"); + const secondInventoryRes = await api.get("/store/variants/test-variant") - expect(secondInventoryRes.data.variant.inventory_quantity).toEqual(2); - }); + expect(secondInventoryRes.data.variant.inventory_quantity).toEqual(2) + }) it("cancels an order but does not increment inventory_quantity of unmanaged variant", async () => { - const api = useApi(); - const manager = dbConnection.manager; + const api = useApi() + const manager = dbConnection.manager await manager.query( `UPDATE "product_variant" SET manage_inventory=false WHERE id = 'test-variant'` - ); + ) - const initialInventoryRes = await api.get("/store/variants/test-variant"); + const initialInventoryRes = await api.get("/store/variants/test-variant") - expect(initialInventoryRes.data.variant.inventory_quantity).toEqual(1); + expect(initialInventoryRes.data.variant.inventory_quantity).toEqual(1) const response = await api .post( @@ -212,35 +213,35 @@ describe("/admin/orders", () => { } ) .catch((err) => { - console.log(err); - }); - expect(response.status).toEqual(200); + console.log(err) + }) + expect(response.status).toEqual(200) - const secondInventoryRes = await api.get("/store/variants/test-variant"); + const secondInventoryRes = await api.get("/store/variants/test-variant") - expect(secondInventoryRes.data.variant.inventory_quantity).toEqual(1); - }); - }); + expect(secondInventoryRes.data.variant.inventory_quantity).toEqual(1) + }) + }) describe("POST /admin/orders/:id/claims", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); - await claimSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) + await claimSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("creates a claim", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -267,30 +268,30 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) const variant = await api.get("/admin/products", { headers: { authorization: "Bearer test_token", }, - }); + }) // find test variant and verify that its inventory quantity has changed const toTest = variant.data.products[0].variants.find( (v) => v.id === "test-variant" - ); - expect(toTest.inventory_quantity).toEqual(0); + ) + expect(toTest.inventory_quantity).toEqual(0) expect(response.data.order.claims[0].shipping_address_id).toEqual( "test-shipping-address" - ); + ) expect(response.data.order.claims[0].shipping_address).toEqual( expect.objectContaining({ first_name: "lebron", country_code: "us", }) - ); + ) expect(response.data.order.claims[0].claim_items).toEqual( expect.arrayContaining([ @@ -305,7 +306,7 @@ describe("/admin/orders", () => { ]), }), ]) - ); + ) expect(response.data.order.claims[0].additional_items).toEqual( expect.arrayContaining([ @@ -314,11 +315,11 @@ describe("/admin/orders", () => { quantity: 1, }), ]) - ); - }); + ) + }) it("creates a claim with a shipping address", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -353,8 +354,8 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) expect(response.data.order.claims[0].shipping_address).toEqual( expect.objectContaining({ @@ -365,7 +366,7 @@ describe("/admin/orders", () => { postal_code: "12345", country_code: "us", }) - ); + ) expect(response.data.order.claims[0].claim_items).toEqual( expect.arrayContaining([ @@ -380,7 +381,7 @@ describe("/admin/orders", () => { ]), }), ]) - ); + ) expect(response.data.order.claims[0].additional_items).toEqual( expect.arrayContaining([ @@ -389,11 +390,11 @@ describe("/admin/orders", () => { quantity: 1, }), ]) - ); - }); + ) + }) it("creates a claim with return shipping", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -421,9 +422,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.order.claims[0].claim_items).toEqual( expect.arrayContaining([ @@ -438,7 +439,7 @@ describe("/admin/orders", () => { ]), }), ]) - ); + ) expect(response.data.order.claims[0].additional_items).toEqual( expect.arrayContaining([ @@ -447,7 +448,7 @@ describe("/admin/orders", () => { quantity: 1, }), ]) - ); + ) expect( response.data.order.claims[0].return_order.shipping_method @@ -456,11 +457,11 @@ describe("/admin/orders", () => { price: 0, shipping_option_id: "test-return-option", }) - ); - }); + ) + }) it("updates a claim", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -487,10 +488,10 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) - const cid = response.data.order.claims[0].id; + const cid = response.data.order.claims[0].id const { status, data: updateData } = await api.post( `/admin/orders/test-order/claims/${cid}`, { @@ -505,18 +506,18 @@ describe("/admin/orders", () => { authorization: "bearer test_token", }, } - ); + ) - expect(status).toEqual(200); + expect(status).toEqual(200) expect(updateData.order.claims[0].shipping_methods).toEqual([ expect.objectContaining({ id: "test-method", }), - ]); - }); + ]) + }) it("updates claim items", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -543,11 +544,11 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) - let claim = response.data.order.claims[0]; - const cid = claim.id; + let claim = response.data.order.claims[0] + const cid = claim.id const { status, data: updateData } = await api.post( `/admin/orders/test-order/claims/${cid}`, { @@ -570,14 +571,14 @@ describe("/admin/orders", () => { authorization: "bearer test_token", }, } - ); + ) - expect(status).toEqual(200); - expect(updateData.order.claims.length).toEqual(1); + expect(status).toEqual(200) + expect(updateData.order.claims.length).toEqual(1) - claim = updateData.order.claims[0]; + claim = updateData.order.claims[0] - expect(claim.claim_items.length).toEqual(1); + expect(claim.claim_items.length).toEqual(1) expect(claim.claim_items).toEqual( expect.arrayContaining([ expect.objectContaining({ @@ -599,11 +600,11 @@ describe("/admin/orders", () => { // ]), }), ]) - ); - }); + ) + }) it("updates claim items - removes image", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -630,11 +631,11 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) - let claim = response.data.order.claims[0]; - const cid = claim.id; + let claim = response.data.order.claims[0] + const cid = claim.id const { status, data: updateData } = await api.post( `/admin/orders/test-order/claims/${cid}`, { @@ -654,14 +655,14 @@ describe("/admin/orders", () => { authorization: "bearer test_token", }, } - ); + ) - expect(status).toEqual(200); - expect(updateData.order.claims.length).toEqual(1); + expect(status).toEqual(200) + expect(updateData.order.claims.length).toEqual(1) - claim = updateData.order.claims[0]; + claim = updateData.order.claims[0] - expect(claim.claim_items.length).toEqual(1); + expect(claim.claim_items.length).toEqual(1) expect(claim.claim_items).toEqual([ expect.objectContaining({ id: claim.claim_items[0].id, @@ -674,11 +675,11 @@ describe("/admin/orders", () => { // expect.objectContaining({ value: "tags" }), // ]), }), - ]); - }); + ]) + }) it("fulfills a claim", async () => { - const api = useApi(); + const api = useApi() const response = await api .post( @@ -713,10 +714,10 @@ describe("/admin/orders", () => { } ) .catch((err) => { - console.log(err); - }); + console.log(err) + }) - const cid = response.data.order.claims[0].id; + const cid = response.data.order.claims[0].id const fulRes = await api.post( `/admin/orders/test-order/claims/${cid}/fulfillments`, {}, @@ -725,18 +726,18 @@ describe("/admin/orders", () => { Authorization: "Bearer test_token", }, } - ); - expect(fulRes.status).toEqual(200); + ) + expect(fulRes.status).toEqual(200) expect(fulRes.data.order.claims).toEqual([ expect.objectContaining({ id: cid, order_id: "test-order", fulfillment_status: "fulfilled", }), - ]); + ]) - const fid = fulRes.data.order.claims[0].fulfillments[0].id; - const iid = fulRes.data.order.claims[0].additional_items[0].id; + const fid = fulRes.data.order.claims[0].fulfillments[0].id + const iid = fulRes.data.order.claims[0].additional_items[0].id expect(fulRes.data.order.claims[0].fulfillments).toEqual([ expect.objectContaining({ items: [ @@ -747,63 +748,63 @@ describe("/admin/orders", () => { }, ], }), - ]); - }); + ]) + }) it("Only allow canceling claim after canceling fulfillments", async () => { - const order_id = "order-with-claim"; + const order_id = "order-with-claim" const order = await callGet({ path: `/admin/orders/${order_id}`, get: "order", - }); + }) - const claim = order.claims.filter((s) => s.id === "claim-w-f")[0]; - const claim_id = claim.id; + const claim = order.claims.filter((s) => s.id === "claim-w-f")[0] + const claim_id = claim.id const expectCancelToReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/claims/${claim_id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 400 }); + await expectCancelToReturn({ code: 400 }) await expectAllPostCallsToReturn({ code: 200, col: claim.fulfillments, pathf: (f) => `/admin/orders/${order_id}/claims/${claim_id}/fulfillments/${f.id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 200 }); - }); + await expectCancelToReturn({ code: 200 }) + }) it("Only allow canceling claim after canceling returns", async () => { - const order_id = "order-with-claim"; + const order_id = "order-with-claim" const order = await callGet({ path: `/admin/orders/${order_id}`, get: "order", - }); + }) - const claim = order.claims.filter((c) => c.id === "claim-w-r")[0]; - const claim_id = claim.id; + const claim = order.claims.filter((c) => c.id === "claim-w-r")[0] + const claim_id = claim.id const expectCancelToReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/claims/${claim_id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 400 }); + await expectCancelToReturn({ code: 400 }) await expectPostCallToReturn({ code: 200, path: `/admin/returns/${claim.return_order.id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 200 }); - }); + await expectCancelToReturn({ code: 200 }) + }) it("fails to creates a claim due to no stock on additional items", async () => { - const api = useApi(); + const api = useApi() try { await api.post( "/admin/orders/test-order/claims", @@ -830,43 +831,43 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) } catch (e) { - expect(e.response.status).toEqual(400); + expect(e.response.status).toEqual(400) expect(e.response.data.message).toEqual( "Variant with id: test-variant does not have the required inventory" - ); + ) } - }); - }); + }) + }) describe("POST /admin/orders/:id/return", () => { - let rrId; + let rrId beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) const created = dbConnection.manager.create(ReturnReason, { value: "too_big", label: "Too Big", - }); - const result = await dbConnection.manager.save(created); + }) + const result = await dbConnection.manager.save(created) - rrId = result.id; + rrId = result.id } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("creates a return", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/return", @@ -885,10 +886,10 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) - expect(response.data.order.returns[0].refund_amount).toEqual(7200); + expect(response.data.order.returns[0].refund_amount).toEqual(7200) expect(response.data.order.returns[0].items).toEqual([ expect.objectContaining({ item_id: "test-item", @@ -896,11 +897,11 @@ describe("/admin/orders", () => { reason_id: rrId, note: "TOO SMALL", }), - ]); - }); + ]) + }) it("increases inventory_quantity when return is received", async () => { - const api = useApi(); + const api = useApi() const returned = await api.post( "/admin/orders/test-order/return", @@ -918,24 +919,22 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) //Find variant that should have its inventory_quantity updated - const toTest = returned.data.order.items.find( - (i) => i.id === "test-item" - ); + const toTest = returned.data.order.items.find((i) => i.id === "test-item") - expect(returned.status).toEqual(200); - expect(toTest.variant.inventory_quantity).toEqual(2); - }); + expect(returned.status).toEqual(200) + expect(toTest.variant.inventory_quantity).toEqual(2) + }) it("does not increases inventory_quantity when return is received when inventory is not managed", async () => { - const api = useApi(); - const manager = dbConnection.manager; + const api = useApi() + const manager = dbConnection.manager await manager.query( `UPDATE "product_variant" SET manage_inventory=false WHERE id = 'test-variant'` - ); + ) const returned = await api.post( "/admin/orders/test-order/return", @@ -953,48 +952,46 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) //Find variant that should have its inventory_quantity updated - const toTest = returned.data.order.items.find( - (i) => i.id === "test-item" - ); + const toTest = returned.data.order.items.find((i) => i.id === "test-item") - expect(returned.status).toEqual(200); - expect(toTest.variant.inventory_quantity).toEqual(1); - }); - }); + expect(returned.status).toEqual(200) + expect(toTest.variant.inventory_quantity).toEqual(1) + }) + }) describe("GET /admin/orders", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); + await adminSeeder(dbConnection) // Manually insert date for filtering - const createdAt = new Date("26 January 1997 12:00 UTC"); + const createdAt = new Date("26 January 1997 12:00 UTC") await orderSeeder(dbConnection, { created_at: createdAt.toISOString(), - }); + }) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("lists all orders", async () => { - const api = useApi(); + const api = useApi() const response = await api.get("/admin/orders?fields=id", { headers: { authorization: "Bearer test_token", }, - }); + }) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.orders).toEqual([ expect.objectContaining({ id: "test-order", @@ -1013,11 +1010,11 @@ describe("/admin/orders", () => { expect.objectContaining({ id: "test-order-w-r", }), - ]); - }); + ]) + }) it("successfully lists orders with greater than", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[gt]=01-26-1990", @@ -1026,9 +1023,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.orders).toEqual([ expect.objectContaining({ id: "test-order", @@ -1046,11 +1043,11 @@ describe("/admin/orders", () => { expect.objectContaining({ id: "test-order-w-r", }), - ]); - }); + ]) + }) it("successfully lists no orders with greater than", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[gt]=01-26-2000", @@ -1059,14 +1056,14 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); - expect(response.data.orders).toEqual([]); - }); + expect(response.status).toEqual(200) + expect(response.data.orders).toEqual([]) + }) it("successfully lists orders with less than", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[lt]=01-26-2000", @@ -1075,9 +1072,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.orders).toEqual([ expect.objectContaining({ id: "test-order", @@ -1095,11 +1092,11 @@ describe("/admin/orders", () => { expect.objectContaining({ id: "test-order-w-r", }), - ]); - }); + ]) + }) it("successfully lists no orders with less than", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[lt]=01-26-1990", @@ -1108,14 +1105,14 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); - expect(response.data.orders).toEqual([]); - }); + expect(response.status).toEqual(200) + expect(response.data.orders).toEqual([]) + }) it("successfully lists orders using unix (greater than)", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[gt]=633351600", @@ -1124,9 +1121,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.orders).toEqual([ expect.objectContaining({ id: "test-order", @@ -1144,8 +1141,8 @@ describe("/admin/orders", () => { expect.objectContaining({ id: "test-order-w-r", }), - ]); - }); + ]) + }) it.each([ [ @@ -1175,49 +1172,49 @@ describe("/admin/orders", () => { ])( "Only allows canceling order after canceling %s", async (id, o, of, pf) => { - const order_id = o; + const order_id = o const order = await callGet({ path: `/admin/orders/${order_id}`, get: "order", - }); + }) const expectCanceltoReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/cancel`, - }); + }) - await expectCanceltoReturn({ code: 400 }); + await expectCanceltoReturn({ code: 400 }) await expectAllPostCallsToReturn({ code: 200, col: of(order), pathf: pf, - }); + }) - await expectCanceltoReturn({ code: 200 }); + await expectCanceltoReturn({ code: 200 }) } - ); - }); + ) + }) describe("POST /admin/orders/:id/swaps", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); - await swapSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) + await swapSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("creates a swap", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/swaps", @@ -1235,12 +1232,51 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); - }); + ) + expect(response.status).toEqual(200) + }) + + it("creates a swap with rma shipping options", async () => { + const api = useApi() + + const response = await api.post( + "/admin/orders/test-order/swaps", + { + return_items: [ + { + item_id: "test-item", + quantity: 1, + }, + ], + additional_items: [{ variant_id: "test-variant-2", quantity: 1 }], + rma_shipping_options: [{ option_id: "test-option", price: 0 }], + }, + { + headers: { + authorization: "Bearer test_token", + }, + } + ) + + const swap = response.data.order.swaps[0] + + const manager = dbConnection.manager + const rma = await manager.findOne(RMAShippingOption, { + shipping_option_id: "test-option", + swap_id: swap.id, + }) + + expect(response.status).toEqual(200) + expect(rma).toEqual( + expect.objectContaining({ + shipping_option_id: "test-option", + price: 0, + }) + ) + }) it("creates a swap and a return", async () => { - const api = useApi(); + const api = useApi() const returnedOrderFirst = await api.post( "/admin/orders/order-with-swap/return", @@ -1258,9 +1294,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(returnedOrderFirst.status).toEqual(200); + expect(returnedOrderFirst.status).toEqual(200) const returnedOrderSecond = await api.post( "/admin/orders/order-with-swap/return", @@ -1278,19 +1314,19 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) // find item to test returned quantiy for const toTest = returnedOrderSecond.data.order.items.find( (i) => i.id === "test-item-many" - ); + ) - expect(returnedOrderSecond.status).toEqual(200); - expect(toTest.returned_quantity).toBe(3); - }); + expect(returnedOrderSecond.status).toEqual(200) + expect(toTest.returned_quantity).toBe(3) + }) it("creates a swap and receives the items", async () => { - const api = useApi(); + const api = useApi() const createdSwapOrder = await api.post( "/admin/orders/test-order/swaps", @@ -1308,11 +1344,11 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(createdSwapOrder.status).toEqual(200); + expect(createdSwapOrder.status).toEqual(200) - const swap = createdSwapOrder.data.order.swaps[0]; + const swap = createdSwapOrder.data.order.swaps[0] const receivedSwap = await api.post( `/admin/returns/${swap.return_order.id}/receive`, @@ -1329,14 +1365,14 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(receivedSwap.status).toEqual(200); - expect(receivedSwap.data.return.status).toBe("received"); - }); + expect(receivedSwap.status).toEqual(200) + expect(receivedSwap.data.return.status).toBe("received") + }) it("creates a swap on a swap", async () => { - const api = useApi(); + const api = useApi() const swapOnSwap = await api.post( "/admin/orders/order-with-swap/swaps", @@ -1354,13 +1390,13 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(swapOnSwap.status).toEqual(200); - }); + expect(swapOnSwap.status).toEqual(200) + }) it("receives a swap on swap", async () => { - const api = useApi(); + const api = useApi() const received = await api.post( `/admin/returns/return-on-swap/receive`, @@ -1377,13 +1413,13 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(received.status).toEqual(200); - }); + expect(received.status).toEqual(200) + }) it("creates a return on a swap", async () => { - const api = useApi(); + const api = useApi() const returnOnSwap = await api.post( "/admin/orders/order-with-swap/return", @@ -1400,13 +1436,13 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(returnOnSwap.status).toEqual(200); - }); + expect(returnOnSwap.status).toEqual(200) + }) it("creates a return on an order", async () => { - const api = useApi(); + const api = useApi() const returnOnOrder = await api.post( "/admin/orders/test-order/return", @@ -1423,9 +1459,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(returnOnOrder.status).toEqual(200); + expect(returnOnOrder.status).toEqual(200) const captured = await api.post( "/admin/orders/test-order/capture", @@ -1435,9 +1471,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - const returnId = returnOnOrder.data.order.returns[0].id; + const returnId = returnOnOrder.data.order.returns[0].id const received = await api.post( `/admin/returns/${returnId}/receive`, @@ -1454,63 +1490,63 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(received.status).toEqual(200); - }); + expect(received.status).toEqual(200) + }) it("Only allows canceling swap after canceling fulfillments", async () => { try { - const swap_id = "swap-w-f"; + const swap_id = "swap-w-f" const swap = await callGet({ path: `/admin/swaps/${swap_id}`, get: "swap", - }); + }) - const { order_id } = swap; + const { order_id } = swap const expectCancelToReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/swaps/${swap_id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 400 }); + await expectCancelToReturn({ code: 400 }) await expectAllPostCallsToReturn({ code: 200, col: swap.fulfillments, pathf: (f) => `/admin/orders/${order_id}/swaps/${swap_id}/fulfillments/${f.id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 200 }); + await expectCancelToReturn({ code: 200 }) } catch (e) { - console.log(e); + console.log(e) } - }); + }) it("Only allows canceling swap after canceling return", async () => { - const swap_id = "swap-w-r"; + const swap_id = "swap-w-r" const swap = await callGet({ path: `/admin/swaps/${swap_id}`, get: "swap", - }); + }) - const { order_id } = swap; + const { order_id } = swap const expectCancelToReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/swaps/${swap_id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 400 }); + await expectCancelToReturn({ code: 400 }) await expectPostCallToReturn({ code: 200, path: `/admin/returns/${swap.return_order.id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 200 }); - }); - }); -}); + await expectCancelToReturn({ code: 200 }) + }) + }) +}) diff --git a/integration-tests/api/__tests__/store/cart.js b/integration-tests/api/__tests__/store/cart.js index 8527218273..f1a01ae492 100644 --- a/integration-tests/api/__tests__/store/cart.js +++ b/integration-tests/api/__tests__/store/cart.js @@ -1,155 +1,162 @@ -const path = require("path"); -const { Region, LineItem, GiftCard } = require("@medusajs/medusa"); +const path = require("path") +const { + Region, + LineItem, + GiftCard, + RMAShippingOption, + Cart, +} = require("@medusajs/medusa") -const setupServer = require("../../../helpers/setup-server"); -const { useApi } = require("../../../helpers/use-api"); -const { initDb, useDb } = require("../../../helpers/use-db"); +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { initDb, useDb } = require("../../../helpers/use-db") -const cartSeeder = require("../../helpers/cart-seeder"); +const cartSeeder = require("../../helpers/cart-seeder") +const swapSeeder = require("../../helpers/swap-seeder") -jest.setTimeout(30000); +jest.setTimeout(30000) describe("/store/carts", () => { - let medusaProcess; - let dbConnection; + let medusaProcess + let dbConnection const doAfterEach = async () => { - const db = useDb(); - return await db.teardown(); - }; + const db = useDb() + return await db.teardown() + } beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..")); - dbConnection = await initDb({ cwd }); - medusaProcess = await setupServer({ cwd }); - }); + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) afterAll(async () => { - const db = useDb(); - await db.shutdown(); - medusaProcess.kill(); - }); + const db = useDb() + await db.shutdown() + medusaProcess.kill() + }) describe("POST /store/carts", () => { beforeEach(async () => { - const manager = dbConnection.manager; + const manager = dbConnection.manager await manager.insert(Region, { id: "region", name: "Test Region", currency_code: "usd", tax_rate: 0, - }); + }) await manager.query( `UPDATE "country" SET region_id='region' WHERE iso_2 = 'us'` - ); - }); + ) + }) afterEach(async () => { - await doAfterEach(); - }); + await doAfterEach() + }) it("creates a cart", async () => { - const api = useApi(); + const api = useApi() - const response = await api.post("/store/carts"); - expect(response.status).toEqual(200); + const response = await api.post("/store/carts") + expect(response.status).toEqual(200) - const getRes = await api.post(`/store/carts/${response.data.cart.id}`); - expect(getRes.status).toEqual(200); - }); + const getRes = await api.post(`/store/carts/${response.data.cart.id}`) + expect(getRes.status).toEqual(200) + }) it("creates a cart with country", async () => { - const api = useApi(); + const api = useApi() const response = await api.post("/store/carts", { country_code: "us", - }); - expect(response.status).toEqual(200); - expect(response.data.cart.shipping_address.country_code).toEqual("us"); + }) + expect(response.status).toEqual(200) + expect(response.data.cart.shipping_address.country_code).toEqual("us") - const getRes = await api.post(`/store/carts/${response.data.cart.id}`); - expect(getRes.status).toEqual(200); - }); + const getRes = await api.post(`/store/carts/${response.data.cart.id}`) + expect(getRes.status).toEqual(200) + }) it("creates a cart with context", async () => { - const api = useApi(); + const api = useApi() const response = await api.post("/store/carts", { context: { test_id: "test", }, - }); - expect(response.status).toEqual(200); + }) + expect(response.status).toEqual(200) - const getRes = await api.post(`/store/carts/${response.data.cart.id}`); - expect(getRes.status).toEqual(200); + const getRes = await api.post(`/store/carts/${response.data.cart.id}`) + expect(getRes.status).toEqual(200) - const cart = getRes.data.cart; + const cart = getRes.data.cart expect(cart.context).toEqual({ ip: "::ffff:127.0.0.1", user_agent: "axios/0.21.1", test_id: "test", - }); - }); - }); + }) + }) + }) describe("POST /store/carts/:id", () => { beforeEach(async () => { try { - await cartSeeder(dbConnection); + await cartSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - await doAfterEach(); - }); + await doAfterEach() + }) it("fails on apply discount if limit has been reached", async () => { - const api = useApi(); + const api = useApi() try { await api.post("/store/carts/test-cart", { discounts: [{ code: "CREATED" }], - }); + }) } catch (error) { - expect(error.response.status).toEqual(400); + expect(error.response.status).toEqual(400) expect(error.response.data.message).toEqual( "Discount has been used maximum allowed times" - ); + ) } - }); + }) it("updates cart customer id", async () => { - const api = useApi(); + const api = useApi() const response = await api.post("/store/carts/test-cart", { customer_id: "test-customer-2", - }); + }) - expect(response.status).toEqual(200); - }); + expect(response.status).toEqual(200) + }) it("updates address using string id", async () => { - const api = useApi(); + const api = useApi() const response = await api.post("/store/carts/test-cart", { billing_address: "test-general-address", shipping_address: "test-general-address", - }); + }) expect(response.data.cart.shipping_address_id).toEqual( "test-general-address" - ); + ) expect(response.data.cart.billing_address_id).toEqual( "test-general-address" - ); - expect(response.status).toEqual(200); - }); + ) + expect(response.status).toEqual(200) + }) it("updates address", async () => { - const api = useApi(); + const api = useApi() const response = await api.post("/store/carts/test-cart", { shipping_address: { @@ -160,14 +167,14 @@ describe("/store/carts", () => { country_code: "us", postal_code: "something", }, - }); + }) - expect(response.data.cart.shipping_address.first_name).toEqual("clark"); - expect(response.status).toEqual(200); - }); + expect(response.data.cart.shipping_address.first_name).toEqual("clark") + expect(response.status).toEqual(200) + }) it("adds free shipping to cart then removes it again", async () => { - const api = useApi(); + const api = useApi() let cart = await api.post( "/store/carts/test-cart", @@ -175,10 +182,10 @@ describe("/store/carts", () => { discounts: [{ code: "FREE_SHIPPING" }, { code: "CREATED" }], }, { withCredentials: true } - ); + ) - expect(cart.data.cart.shipping_total).toBe(0); - expect(cart.status).toEqual(200); + expect(cart.data.cart.shipping_total).toBe(0) + expect(cart.status).toEqual(200) cart = await api.post( "/store/carts/test-cart", @@ -186,68 +193,68 @@ describe("/store/carts", () => { discounts: [{ code: "CREATED" }], }, { withCredentials: true } - ); + ) - expect(cart.data.cart.shipping_total).toBe(1000); - expect(cart.status).toEqual(200); - }); + expect(cart.data.cart.shipping_total).toBe(1000) + expect(cart.status).toEqual(200) + }) it("complete cart with giftcard total 0", async () => { - const manager = dbConnection.manager; + const manager = dbConnection.manager await manager.insert(GiftCard, { id: "gift_test", code: "GC_TEST", value: 20000, balance: 20000, region_id: "test-region", - }); + }) - const api = useApi(); + const api = useApi() await api.post(`/store/carts/test-cart-3`, { gift_cards: [{ code: "GC_TEST" }], - }); + }) const getRes = await api .post(`/store/carts/test-cart-3/complete`) .catch((err) => { - console.log(err.response.data); - }); + console.log(err.response.data) + }) - expect(getRes.status).toEqual(200); - expect(getRes.data.type).toEqual("order"); - }); + expect(getRes.status).toEqual(200) + expect(getRes.data.type).toEqual("order") + }) it("complete cart with items inventory covered", async () => { - const api = useApi(); - const getRes = await api.post(`/store/carts/test-cart-2/complete-cart`); + const api = useApi() + const getRes = await api.post(`/store/carts/test-cart-2/complete-cart`) - expect(getRes.status).toEqual(200); + expect(getRes.status).toEqual(200) - const variantRes = await api.get("/store/variants/test-variant"); - expect(variantRes.data.variant.inventory_quantity).toEqual(0); - }); + const variantRes = await api.get("/store/variants/test-variant") + expect(variantRes.data.variant.inventory_quantity).toEqual(0) + }) it("returns early, if cart is already completed", async () => { - const manager = dbConnection.manager; - const api = useApi(); + const manager = dbConnection.manager + const api = useApi() await manager.query( `UPDATE "cart" SET completed_at=current_timestamp WHERE id = 'test-cart-2'` - ); + ) try { - await api.post(`/store/carts/test-cart-2/complete-cart`); + await api.post(`/store/carts/test-cart-2/complete-cart`) } catch (error) { expect(error.response.data).toMatchSnapshot({ code: "not_allowed", message: "Cart has already been completed", code: "cart_incompatible_state", - }); - expect(error.response.status).toEqual(409); + }) + expect(error.response.status).toEqual(409) } - }); + }) it("fails to complete cart with items inventory not/partially covered", async () => { - const manager = dbConnection.manager; + const manager = dbConnection.manager const li = manager.create(LineItem, { id: "test-item", @@ -258,37 +265,58 @@ describe("/store/carts", () => { quantity: 99, variant_id: "test-variant-2", cart_id: "test-cart-2", - }); - await manager.save(li); + }) + await manager.save(li) - const api = useApi(); + const api = useApi() try { - await api.post(`/store/carts/test-cart-2/complete-cart`); + await api.post(`/store/carts/test-cart-2/complete-cart`) } catch (e) { expect(e.response.data).toMatchSnapshot({ code: "insufficient_inventory", - }); - expect(e.response.status).toBe(409); + }) + expect(e.response.status).toBe(409) } //check to see if payment has been cancelled - const res = await api.get(`/store/carts/test-cart-2`); - expect(res.data.cart.payment.canceled_at).not.toBe(null); - }); - }); + const res = await api.get(`/store/carts/test-cart-2`) + expect(res.data.cart.payment.canceled_at).not.toBe(null) + }) + }) describe("POST /store/carts/:id/shipping-methods", () => { beforeEach(async () => { - await cartSeeder(dbConnection); - }); + await cartSeeder(dbConnection) + const manager = dbConnection.manager + + await manager.insert(Cart, { + id: "test-cart-rma", + customer_id: "some-customer", + email: "some-customer@email.com", + shipping_address: { + id: "test-shipping-address", + first_name: "lebron", + country_code: "us", + }, + region_id: "test-region", + currency_code: "usd", + type: "swap", + }) + + await manager.insert(RMAShippingOption, { + id: "test-rmaso", + shipping_option_id: "test-option", + price: 5, + }) + }) afterEach(async () => { - await doAfterEach(); - }); + await doAfterEach() + }) - it("adds a shipping method to cart", async () => { - const api = useApi(); + it("adds a normal shipping method to cart", async () => { + const api = useApi() const cartWithShippingMethod = await api.post( "/store/carts/test-cart/shipping-methods", @@ -296,16 +324,37 @@ describe("/store/carts", () => { option_id: "test-option", }, { withCredentials: true } - ); + ) expect(cartWithShippingMethod.data.cart.shipping_methods).toContainEqual( expect.objectContaining({ shipping_option_id: "test-option" }) - ); - expect(cartWithShippingMethod.status).toEqual(200); - }); + ) + expect(cartWithShippingMethod.status).toEqual(200) + }) + + it("adds a rma shipping method to cart", async () => { + const api = useApi() + + const cartWithRMAShippingMethod = await api + .post( + "/store/carts/test-cart-rma/shipping-methods", + { + option_id: "test-rmaso", + }, + { withCredentials: true } + ) + .catch((err) => err.response) + + expect( + cartWithRMAShippingMethod.data.cart.shipping_methods + ).toContainEqual( + expect.objectContaining({ shipping_option_id: "test-option", price: 5 }) + ) + expect(cartWithRMAShippingMethod.status).toEqual(200) + }) it("adds a giftcard to cart, but ensures discount only applied to discountable items", async () => { - const api = useApi(); + const api = useApi() // Add standard line item to cart await api.post( @@ -315,7 +364,7 @@ describe("/store/carts", () => { quantity: 1, }, { withCredentials: true } - ); + ) // Add gift card to cart await api.post( @@ -325,7 +374,7 @@ describe("/store/carts", () => { quantity: 1, }, { withCredentials: true } - ); + ) // Add a 10% discount to the cart const cartWithGiftcard = await api.post( @@ -334,16 +383,16 @@ describe("/store/carts", () => { discounts: [{ code: "10PERCENT" }], }, { withCredentials: true } - ); + ) // Ensure that the discount is only applied to the standard item - expect(cartWithGiftcard.data.cart.total).toBe(1900); // 1000 (giftcard) + 900 (standard item with 10% discount) - expect(cartWithGiftcard.data.cart.discount_total).toBe(100); - expect(cartWithGiftcard.status).toEqual(200); - }); + expect(cartWithGiftcard.data.cart.total).toBe(1900) // 1000 (giftcard) + 900 (standard item with 10% discount) + expect(cartWithGiftcard.data.cart.discount_total).toBe(100) + expect(cartWithGiftcard.status).toEqual(200) + }) it("adds no more than 1 shipping method per shipping profile", async () => { - const api = useApi(); + const api = useApi() const addShippingMethod = async (option_id) => { return await api.post( "/store/carts/test-cart/shipping-methods", @@ -351,17 +400,17 @@ describe("/store/carts", () => { option_id, }, { withCredentials: true } - ); - }; + ) + } - await addShippingMethod("test-option"); + await addShippingMethod("test-option") const cartWithAnotherShippingMethod = await addShippingMethod( "test-option-2" - ); + ) expect( cartWithAnotherShippingMethod.data.cart.shipping_methods.length - ).toEqual(1); + ).toEqual(1) expect( cartWithAnotherShippingMethod.data.cart.shipping_methods ).toContainEqual( @@ -369,30 +418,30 @@ describe("/store/carts", () => { shipping_option_id: "test-option-2", price: 500, }) - ); - expect(cartWithAnotherShippingMethod.status).toEqual(200); - }); - }); + ) + expect(cartWithAnotherShippingMethod.status).toEqual(200) + }) + }) describe("DELETE /store/carts/:id/discounts/:code", () => { beforeEach(async () => { try { - await cartSeeder(dbConnection); + await cartSeeder(dbConnection) await dbConnection.manager.query( `INSERT INTO "cart_discounts" (cart_id, discount_id) VALUES ('test-cart', 'free-shipping')` - ); + ) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - await doAfterEach(); - }); + await doAfterEach() + }) it("removes free shipping and updates shipping total", async () => { - const api = useApi(); + const api = useApi() const cartWithFreeShipping = await api.post( "/store/carts/test-cart", @@ -400,36 +449,36 @@ describe("/store/carts", () => { discounts: [{ code: "FREE_SHIPPING" }], }, { withCredentials: true } - ); + ) - expect(cartWithFreeShipping.data.cart.shipping_total).toBe(0); - expect(cartWithFreeShipping.status).toEqual(200); + expect(cartWithFreeShipping.data.cart.shipping_total).toBe(0) + expect(cartWithFreeShipping.status).toEqual(200) const response = await api.delete( "/store/carts/test-cart/discounts/FREE_SHIPPING" - ); + ) - expect(response.data.cart.shipping_total).toBe(1000); - expect(response.status).toEqual(200); - }); - }); + expect(response.data.cart.shipping_total).toBe(1000) + expect(response.status).toEqual(200) + }) + }) describe("get-cart with session customer", () => { beforeEach(async () => { try { - await cartSeeder(dbConnection); + await cartSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - await doAfterEach(); - }); + await doAfterEach() + }) it("updates empty cart.customer_id on cart retrieval", async () => { - const api = useApi(); + const api = useApi() let customer = await api.post( "/store/customers", @@ -440,29 +489,25 @@ describe("/store/carts", () => { last_name: "oli", }, { withCredentials: true } - ); + ) - const cookie = customer.headers["set-cookie"][0]; + const cookie = customer.headers["set-cookie"][0] - const cart = await api.post( - "/store/carts", - {}, - { withCredentials: true } - ); + const cart = await api.post("/store/carts", {}, { withCredentials: true }) const response = await api.get(`/store/carts/${cart.data.cart.id}`, { headers: { cookie, }, withCredentials: true, - }); + }) - expect(response.data.cart.customer_id).toEqual(customer.data.customer.id); - expect(response.status).toEqual(200); - }); + expect(response.data.cart.customer_id).toEqual(customer.data.customer.id) + expect(response.status).toEqual(200) + }) it("updates cart.customer_id on cart retrieval if cart.customer_id differ from session customer", async () => { - const api = useApi(); + const api = useApi() let customer = await api.post( "/store/customers", @@ -473,15 +518,15 @@ describe("/store/carts", () => { last_name: "oli", }, { withCredentials: true } - ); + ) - const cookie = customer.headers["set-cookie"][0]; + const cookie = customer.headers["set-cookie"][0] - const cart = await api.post("/store/carts"); + const cart = await api.post("/store/carts") const updatedCart = await api.post(`/store/carts/${cart.data.cart.id}`, { customer_id: "test-customer", - }); + }) const response = await api.get( `/store/carts/${updatedCart.data.cart.id}`, @@ -490,10 +535,10 @@ describe("/store/carts", () => { cookie, }, } - ); + ) - expect(response.data.cart.customer_id).toEqual(customer.data.customer.id); - expect(response.status).toEqual(200); - }); - }); -}); + expect(response.data.cart.customer_id).toEqual(customer.data.customer.id) + expect(response.status).toEqual(200) + }) + }) +}) diff --git a/integration-tests/api/__tests__/store/shipping-options.js b/integration-tests/api/__tests__/store/shipping-options.js index 2a9858facd..2c5f54050d 100644 --- a/integration-tests/api/__tests__/store/shipping-options.js +++ b/integration-tests/api/__tests__/store/shipping-options.js @@ -1,51 +1,53 @@ -const path = require("path"); -const { Region, ShippingProfile, ShippingOption } = require("@medusajs/medusa"); +const path = require("path") +const { Region, ShippingProfile, ShippingOption } = require("@medusajs/medusa") -const setupServer = require("../../../helpers/setup-server"); -const { useApi } = require("../../../helpers/use-api"); -const { initDb, useDb } = require("../../../helpers/use-db"); +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { initDb, useDb } = require("../../../helpers/use-db") +const cartSeeder = require("../../helpers/cart-seeder") +const swapSeeder = require("../../helpers/swap-seeder") -jest.setTimeout(30000); +jest.setTimeout(30000) describe("/store/shipping-options", () => { - let medusaProcess; - let dbConnection; + let medusaProcess + let dbConnection beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..")); - dbConnection = await initDb({ cwd }); - medusaProcess = await setupServer({ cwd }); - }); + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) afterAll(async () => { - const db = useDb(); - await db.shutdown(); - medusaProcess.kill(); - }); + const db = useDb() + await db.shutdown() + medusaProcess.kill() + }) describe("POST /store/shipping-options", () => { beforeEach(async () => { - const manager = dbConnection.manager; + const manager = dbConnection.manager await manager.query( `ALTER SEQUENCE order_display_id_seq RESTART WITH 111` - ); + ) await manager.insert(Region, { id: "region", name: "Test Region", currency_code: "usd", tax_rate: 0, - }); + }) await manager.insert(Region, { id: "region2", name: "Test Region 2", currency_code: "usd", tax_rate: 0, - }); + }) const defaultProfile = await manager.findOne(ShippingProfile, { type: "default", - }); + }) await manager.insert(ShippingOption, { id: "test-out", @@ -57,7 +59,7 @@ describe("/store/shipping-options", () => { price_type: "flat_rate", amount: 2000, is_return: false, - }); + }) await manager.insert(ShippingOption, { id: "test-return", @@ -69,7 +71,7 @@ describe("/store/shipping-options", () => { price_type: "flat_rate", amount: 1000, is_return: true, - }); + }) await manager.insert(ShippingOption, { id: "test-region2", @@ -81,51 +83,101 @@ describe("/store/shipping-options", () => { price_type: "flat_rate", amount: 1000, is_return: false, - }); - }); + }) + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("retrieves all shipping options", async () => { - const api = useApi(); + const api = useApi() const response = await api.get("/store/shipping-options").catch((err) => { - return err.response; - }); + return err.response + }) - expect(response.status).toEqual(200); - expect(response.data.shipping_options.length).toEqual(3); - }); + expect(response.status).toEqual(200) + expect(response.data.shipping_options.length).toEqual(3) + }) it("creates a return with shipping method", async () => { - const api = useApi(); + const api = useApi() const response = await api .get("/store/shipping-options?is_return=true") .catch((err) => { - return err.response; - }); + return err.response + }) - expect(response.status).toEqual(200); - expect(response.data.shipping_options.length).toEqual(1); - expect(response.data.shipping_options[0].id).toEqual("test-return"); - }); + expect(response.status).toEqual(200) + expect(response.data.shipping_options.length).toEqual(1) + expect(response.data.shipping_options[0].id).toEqual("test-return") + }) it("creates a return with shipping method", async () => { - const api = useApi(); + const api = useApi() const response = await api .get("/store/shipping-options?region_id=region2") .catch((err) => { - return err.response; - }); + return err.response + }) - expect(response.status).toEqual(200); - expect(response.data.shipping_options.length).toEqual(1); - expect(response.data.shipping_options[0].id).toEqual("test-region2"); - }); - }); -}); + expect(response.status).toEqual(200) + expect(response.data.shipping_options.length).toEqual(1) + expect(response.data.shipping_options[0].id).toEqual("test-region2") + }) + }) + + describe("GET /store/shipping-options/:cart_id", () => { + beforeEach(async () => { + await cartSeeder(dbConnection) + await swapSeeder(dbConnection) + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("given a default cart, when user retrieves its shipping options, then should return a list of shipping options", async () => { + const api = useApi() + + const response = await api + .get("/store/shipping-options/test-cart-2") + .catch((err) => { + return err.response + }) + + expect(response.status).toEqual(200) + expect(response.data.shipping_options).toEqual( + expect.arrayContaining([ + expect.objectContaining({ id: "test-option", amount: 1000 }), + expect.objectContaining({ id: "test-option-2", amount: 500 }), + ]) + ) + }) + + it("given a swap cart, when user retrieves its shipping options, then should return a list of RMA shipping options", async () => { + const api = useApi() + + const response = await api + .get("/store/shipping-options/test-cart-rma") + .catch((err) => { + return err.response + }) + + expect(response.status).toEqual(200) + expect(response.data.shipping_options).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + shipping_option_id: "test-option", + price: 0, + }), + ]) + ) + }) + }) +}) diff --git a/integration-tests/api/helpers/cart-seeder.js b/integration-tests/api/helpers/cart-seeder.js index 1d769fb0c0..40dbe74566 100644 --- a/integration-tests/api/helpers/cart-seeder.js +++ b/integration-tests/api/helpers/cart-seeder.js @@ -14,31 +14,31 @@ const { LineItem, Payment, PaymentSession, -} = require("@medusajs/medusa"); +} = require("@medusajs/medusa") module.exports = async (connection, data = {}) => { - const manager = connection.manager; + const manager = connection.manager const defaultProfile = await manager.findOne(ShippingProfile, { type: "default", - }); + }) const gcProfile = await manager.findOne(ShippingProfile, { type: "gift_card", - }); + }) await manager.insert(Address, { id: "test-general-address", first_name: "superman", country_code: "us", - }); + }) const r = manager.create(Region, { id: "test-region", name: "Test Region", currency_code: "usd", tax_rate: 0, - }); + }) const freeRule = manager.create(DiscountRule, { id: "free-shipping-rule", @@ -46,18 +46,18 @@ module.exports = async (connection, data = {}) => { type: "free_shipping", value: 100, allocation: "total", - }); + }) const freeDisc = manager.create(Discount, { id: "free-shipping", code: "FREE_SHIPPING", is_dynamic: false, is_disabled: false, - }); + }) - freeDisc.regions = [r]; - freeDisc.rule = freeRule; - await manager.save(freeDisc); + freeDisc.regions = [r] + freeDisc.rule = freeRule + await manager.save(freeDisc) const tenPercentRule = manager.create(DiscountRule, { id: "tenpercent-rule", @@ -65,25 +65,25 @@ module.exports = async (connection, data = {}) => { type: "percentage", value: 10, allocation: "total", - }); + }) const tenPercent = manager.create(Discount, { id: "10Percent", code: "10PERCENT", is_dynamic: false, is_disabled: false, - }); + }) - tenPercent.regions = [r]; - tenPercent.rule = tenPercentRule; - await manager.save(tenPercent); + tenPercent.regions = [r] + tenPercent.rule = tenPercentRule + await manager.save(tenPercent) const d = await manager.create(Discount, { id: "test-discount", code: "CREATED", is_dynamic: false, is_disabled: false, - }); + }) const dr = await manager.create(DiscountRule, { id: "test-discount-rule", @@ -91,31 +91,31 @@ module.exports = async (connection, data = {}) => { type: "fixed", value: 10000, allocation: "total", - }); + }) - d.rule = dr; - d.regions = [r]; + d.rule = dr + d.regions = [r] - await manager.save(d); + await manager.save(d) await manager.query( `UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'` - ); + ) await manager.insert(Customer, { id: "test-customer", email: "test@email.com", - }); + }) await manager.insert(Customer, { id: "test-customer-2", email: "test-2@email.com", - }); + }) await manager.insert(Customer, { id: "some-customer", email: "some-customer@email.com", - }); + }) await manager.insert(ShippingOption, { id: "test-option", @@ -126,7 +126,7 @@ module.exports = async (connection, data = {}) => { price_type: "flat_rate", amount: 1000, data: {}, - }); + }) await manager.insert(ShippingOption, { id: "gc-option", @@ -137,7 +137,7 @@ module.exports = async (connection, data = {}) => { price_type: "flat_rate", amount: 0, data: {}, - }); + }) await manager.insert(ShippingOption, { id: "test-option-2", @@ -148,7 +148,7 @@ module.exports = async (connection, data = {}) => { price_type: "flat_rate", amount: 500, data: {}, - }); + }) await manager.insert(Product, { id: "giftcard-product", @@ -157,7 +157,7 @@ module.exports = async (connection, data = {}) => { discountable: false, profile_id: gcProfile.id, options: [{ id: "denom", title: "Denomination" }], - }); + }) await manager.insert(ProductVariant, { id: "giftcard-denom", @@ -170,14 +170,14 @@ module.exports = async (connection, data = {}) => { value: "1000", }, ], - }); + }) await manager.insert(Product, { id: "test-product", title: "test product", profile_id: defaultProfile.id, options: [{ id: "test-option", title: "Size" }], - }); + }) await manager.insert(ProductVariant, { id: "test-variant", @@ -190,7 +190,7 @@ module.exports = async (connection, data = {}) => { value: "Size", }, ], - }); + }) await manager.insert(ProductVariant, { id: "test-variant-2", @@ -203,31 +203,31 @@ module.exports = async (connection, data = {}) => { value: "Size", }, ], - }); + }) const ma = manager.create(MoneyAmount, { variant_id: "test-variant", currency_code: "usd", amount: 1000, - }); + }) - await manager.save(ma); + await manager.save(ma) const ma2 = manager.create(MoneyAmount, { variant_id: "test-variant-2", currency_code: "usd", amount: 8000, - }); + }) - await manager.save(ma2); + await manager.save(ma2) const ma3 = manager.create(MoneyAmount, { variant_id: "giftcard-denom", currency_code: "usd", amount: 1000, - }); + }) - await manager.save(ma3); + await manager.save(ma3) const cart = manager.create(Cart, { id: "test-cart", @@ -241,9 +241,9 @@ module.exports = async (connection, data = {}) => { region_id: "test-region", currency_code: "usd", items: [], - }); + }) - await manager.save(cart); + await manager.save(cart) const cart2 = manager.create(Cart, { id: "test-cart-2", @@ -258,7 +258,7 @@ module.exports = async (connection, data = {}) => { currency_code: "usd", completed_at: null, items: [], - }); + }) const pay = manager.create(Payment, { id: "test-payment", @@ -267,13 +267,13 @@ module.exports = async (connection, data = {}) => { amount_refunded: 0, provider_id: "test-pay", data: {}, - }); + }) - await manager.save(pay); + await manager.save(pay) - cart2.payment = pay; + cart2.payment = pay - await manager.save(cart2); + await manager.save(cart2) await manager.insert(PaymentSession, { id: "test-session", @@ -282,7 +282,7 @@ module.exports = async (connection, data = {}) => { is_selected: true, data: {}, status: "authorized", - }); + }) await manager.insert(ShippingMethod, { id: "test-method", @@ -290,7 +290,7 @@ module.exports = async (connection, data = {}) => { cart_id: "test-cart", price: 1000, data: {}, - }); + }) const li = manager.create(LineItem, { id: "test-item", @@ -301,8 +301,8 @@ module.exports = async (connection, data = {}) => { quantity: 1, variant_id: "test-variant", cart_id: "test-cart-2", - }); - await manager.save(li); + }) + await manager.save(li) const cart3 = manager.create(Cart, { id: "test-cart-3", @@ -317,8 +317,8 @@ module.exports = async (connection, data = {}) => { currency_code: "usd", completed_at: null, items: [], - }); - await manager.save(cart3); + }) + await manager.save(cart3) await manager.insert(ShippingMethod, { id: "test-method-2", @@ -326,7 +326,7 @@ module.exports = async (connection, data = {}) => { cart_id: "test-cart-3", price: 0, data: {}, - }); + }) const li2 = manager.create(LineItem, { id: "test-item-2", @@ -337,6 +337,6 @@ module.exports = async (connection, data = {}) => { quantity: 1, variant_id: "test-variant", cart_id: "test-cart-3", - }); - await manager.save(li2); -}; + }) + await manager.save(li2) +} diff --git a/integration-tests/api/helpers/swap-seeder.js b/integration-tests/api/helpers/swap-seeder.js index 7fcc9e6395..c5d6dda7fc 100644 --- a/integration-tests/api/helpers/swap-seeder.js +++ b/integration-tests/api/helpers/swap-seeder.js @@ -14,10 +14,11 @@ const { Swap, Cart, Return, -} = require("@medusajs/medusa"); + RMAShippingOption, +} = require("@medusajs/medusa") module.exports = async (connection, data = {}) => { - const manager = connection.manager; + const manager = connection.manager let orderWithSwap = manager.create(Order, { id: "order-with-swap", @@ -50,9 +51,9 @@ module.exports = async (connection, data = {}) => { ], items: [], ...data, - }); + }) - orderWithSwap = await manager.save(orderWithSwap); + orderWithSwap = await manager.save(orderWithSwap) const cart = manager.create(Cart, { id: "test-cart", @@ -66,9 +67,9 @@ module.exports = async (connection, data = {}) => { swap_id: "test-swap", parent_order_id: orderWithSwap.id, }, - }); + }) - await manager.save(cart); + await manager.save(cart) const swap = manager.create(Swap, { id: "test-swap", @@ -97,9 +98,62 @@ module.exports = async (connection, data = {}) => { cart_id: "test-cart", }, ], - }); + }) - await manager.save(swap); + await manager.save(swap) + + const rmaCart = manager.create(Cart, { + id: "test-cart-rma", + customer_id: "test-customer", + email: "test-customer@email.com", + shipping_address_id: "test-shipping-address", + billing_address_id: "test-billing-address", + region_id: "test-region", + type: "swap", + metadata: { + swap_id: "test-swap", + parent_order_id: orderWithSwap.id, + }, + }) + + await manager.save(rmaCart) + + const swapWithRMAMethod = manager.create(Swap, { + id: "test-swap-rma", + order_id: "order-with-swap", + payment_status: "captured", + fulfillment_status: "fulfilled", + cart_id: "test-cart-rma", + payment: { + id: "test-payment-swap", + amount: 10000, + currency_code: "usd", + amount_refunded: 0, + provider_id: "test-pay", + data: {}, + }, + additional_items: [ + { + id: "test-item-swapped", + fulfilled_quantity: 1, + title: "Line Item", + description: "Line Item Desc", + thumbnail: "https://test.js/1234", + unit_price: 9000, + quantity: 1, + variant_id: "test-variant-2", + cart_id: "test-cart", + }, + ], + rma_shipping_options: [ + { + shipping_option_id: "test-option", + price: 0, + }, + ], + }) + + await manager.save(swapWithRMAMethod) const cartTemplate = async (cartId) => { const cart = manager.create(Cart, { @@ -112,13 +166,13 @@ module.exports = async (connection, data = {}) => { type: "swap", metadata: {}, ...data, - }); + }) - await manager.save(cart); - }; + await manager.save(cart) + } const swapTemplate = async (cartId) => { - await cartTemplate(cartId); + await cartTemplate(cartId) return { order_id: orderWithSwap.id, fulfillment_status: "fulfilled", @@ -132,8 +186,8 @@ module.exports = async (connection, data = {}) => { data: {}, }, ...data, - }; - }; + } + } const swapWithFulfillments = manager.create(Swap, { id: "swap-w-f", @@ -150,9 +204,9 @@ module.exports = async (connection, data = {}) => { }, ], ...(await swapTemplate("sc-w-f")), - }); + }) - await manager.save(swapWithFulfillments); + await manager.save(swapWithFulfillments) const swapWithReturn = manager.create(Swap, { id: "swap-w-r", @@ -162,9 +216,9 @@ module.exports = async (connection, data = {}) => { refund_amount: 0, }, ...(await swapTemplate("sc-w-r")), - }); + }) - await manager.save(swapWithReturn); + await manager.save(swapWithReturn) const li = manager.create(LineItem, { id: "return-item-1", fulfilled_quantity: 1, @@ -176,9 +230,9 @@ module.exports = async (connection, data = {}) => { variant_id: "test-variant", order_id: orderWithSwap.id, cart_id: cart.id, - }); + }) - await manager.save(li); + await manager.save(li) const li2 = manager.create(LineItem, { id: "test-item-many", @@ -190,9 +244,9 @@ module.exports = async (connection, data = {}) => { quantity: 4, variant_id: "test-variant", order_id: orderWithSwap.id, - }); + }) - await manager.save(li2); + await manager.save(li2) const swapReturn = await manager.create(Return, { swap_id: swap.id, @@ -200,16 +254,16 @@ module.exports = async (connection, data = {}) => { item_id: li.id, refund_amount: li.quantity * li.unit_price, // shipping_method_id: , - }); + }) - await manager.save(swapReturn); + await manager.save(swapReturn) const return_item1 = manager.create(LineItem, { ...li, unit_price: -1 * li.unit_price, - }); + }) - await manager.save(return_item1); + await manager.save(return_item1) await manager.insert(ShippingMethod, { id: "another-test-method", @@ -217,7 +271,7 @@ module.exports = async (connection, data = {}) => { cart_id: "test-cart", price: 1000, data: {}, - }); + }) const swapOnSwap = manager.create(Swap, { id: "swap-on-swap", @@ -255,9 +309,9 @@ module.exports = async (connection, data = {}) => { variant_id: "test-variant", }, ], - }); + }) - await manager.save(swapOnSwap); + await manager.save(swapOnSwap) await manager.insert(ShippingMethod, { id: "test-method-swap-order", @@ -265,5 +319,5 @@ module.exports = async (connection, data = {}) => { order_id: "order-with-swap", price: 1000, data: {}, - }); -}; + }) +}