diff --git a/integration-tests/api/__tests__/store/cart.js b/integration-tests/api/__tests__/store/cart.js index 9b647436fd..e924f26e42 100644 --- a/integration-tests/api/__tests__/store/cart.js +++ b/integration-tests/api/__tests__/store/cart.js @@ -6,6 +6,8 @@ const setupServer = require("../../../helpers/setup-server"); const { useApi } = require("../../../helpers/use-api"); const { initDb } = require("../../../helpers/use-db"); +const cartSeeder = require("../../helpers/cart-seeder"); + jest.setTimeout(30000); describe("/store/carts", () => { @@ -71,4 +73,124 @@ describe("/store/carts", () => { expect(getRes.status).toEqual(200); }); }); + + describe("POST /store/carts/:id", () => { + beforeEach(async () => { + try { + await cartSeeder(dbConnection); + } catch (err) { + console.log(err); + throw err; + } + }); + + afterEach(async () => { + const manager = dbConnection.manager; + await manager.query(`DELETE FROM "cart"`); + await manager.query(`DELETE FROM "customer"`); + await manager.query( + `UPDATE "country" SET region_id=NULL WHERE iso_2 = 'us'` + ); + await manager.query(`DELETE FROM "region"`); + }); + + it("updates cart customer id", async () => { + const api = useApi(); + + const response = await api.post("/store/carts/test-cart", { + customer_id: "test-customer-2", + }); + + expect(response.status).toEqual(200); + }); + }); + + describe("get-cart with session customer", () => { + beforeEach(async () => { + try { + await cartSeeder(dbConnection); + } catch (err) { + console.log(err); + throw err; + } + }); + + afterEach(async () => { + const manager = dbConnection.manager; + await manager.query(`DELETE FROM "cart"`); + await manager.query(`DELETE FROM "customer"`); + await manager.query( + `UPDATE "country" SET region_id=NULL WHERE iso_2 = 'us'` + ); + await manager.query(`DELETE FROM "region"`); + }); + + it("updates empty cart.customer_id on cart retrieval", async () => { + const api = useApi(); + + let customer = await api.post( + "/store/customers", + { + email: "oli@test.dk", + password: "olitest", + first_name: "oli", + last_name: "oli", + }, + { withCredentials: true } + ); + + const cookie = customer.headers["set-cookie"][0]; + + 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); + }); + + it("updates cart.customer_id on cart retrieval if cart.customer_id differ from session customer", async () => { + const api = useApi(); + + let customer = await api.post( + "/store/customers", + { + email: "oli@test.dk", + password: "olitest", + first_name: "oli", + last_name: "oli", + }, + { withCredentials: true } + ); + + const cookie = customer.headers["set-cookie"][0]; + + 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}`, + { + headers: { + cookie, + }, + } + ); + + expect(response.data.cart.customer_id).toEqual(customer.data.customer.id); + expect(response.status).toEqual(200); + }); + }); }); diff --git a/integration-tests/api/helpers/cart-seeder.js b/integration-tests/api/helpers/cart-seeder.js new file mode 100644 index 0000000000..ae8a00304d --- /dev/null +++ b/integration-tests/api/helpers/cart-seeder.js @@ -0,0 +1,47 @@ +const { Customer, Region, Cart } = require("@medusajs/medusa"); + +module.exports = async (connection, data = {}) => { + const manager = connection.manager; + + await manager.insert(Region, { + id: "test-region", + name: "Test Region", + currency_code: "usd", + tax_rate: 0, + }); + + 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", + }); + + const cart = manager.create(Cart, { + id: "test-cart", + 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", + items: [], + }); + + await manager.save(cart); +}; diff --git a/integration-tests/api/jest.config.js b/integration-tests/api/jest.config.js index 8b73ce7d31..bf175fbbeb 100644 --- a/integration-tests/api/jest.config.js +++ b/integration-tests/api/jest.config.js @@ -1,3 +1,22 @@ +const glob = require(`glob`); + +const pkgs = glob + .sync(`${__dirname}/*/`) + .map((p) => p.replace(__dirname, `/integration-tests`)); + module.exports = { - setupFilesAfterEnv: ["/setup.js"], + testEnvironment: `node`, + rootDir: `../`, + roots: pkgs, + testPathIgnorePatterns: [ + `/examples/`, + `/www/`, + `/dist/`, + `/node_modules/`, + `__tests__/fixtures`, + `__testfixtures__`, + `.cache`, + ], + transform: { "^.+\\.[jt]s$": `/jest-transformer.js` }, + setupFilesAfterEnv: ["/integration-tests/setup.js"], }; diff --git a/integration-tests/api/setup.js b/integration-tests/api/setup.js new file mode 100644 index 0000000000..861746aaaa --- /dev/null +++ b/integration-tests/api/setup.js @@ -0,0 +1,5 @@ +const { dropDatabase } = require("pg-god"); + +afterAll(() => { + dropDatabase({ databaseName: "medusa-integration" }); +}); diff --git a/packages/medusa/src/api/routes/admin/customers/update-customer.js b/packages/medusa/src/api/routes/admin/customers/update-customer.js index 4d63e92400..93e18ce901 100644 --- a/packages/medusa/src/api/routes/admin/customers/update-customer.js +++ b/packages/medusa/src/api/routes/admin/customers/update-customer.js @@ -16,7 +16,6 @@ export default async (req, res) => { } try { - const orderService = req.scope.resolve("orderService") const customerService = req.scope.resolve("customerService") await customerService.update(id, value) diff --git a/packages/medusa/src/api/routes/store/carts/get-cart.js b/packages/medusa/src/api/routes/store/carts/get-cart.js index 986abc6198..6fa02a1a7f 100644 --- a/packages/medusa/src/api/routes/store/carts/get-cart.js +++ b/packages/medusa/src/api/routes/store/carts/get-cart.js @@ -1,6 +1,8 @@ import { defaultFields, defaultRelations } from "./" + export default async (req, res) => { const { id } = req.params + try { const cartService = req.scope.resolve("cartService") diff --git a/packages/medusa/src/services/cart.js b/packages/medusa/src/services/cart.js index 887a17e701..cd9bf09ced 100644 --- a/packages/medusa/src/services/cart.js +++ b/packages/medusa/src/services/cart.js @@ -666,6 +666,7 @@ class CartService extends BaseService { .withTransaction(this.transactionManager_) .retrieve(customerId) + cart.customer = customer cart.customer_id = customer.id cart.email = customer.email }