hotfix(medusa): Get cart with session (#165)

This commit is contained in:
Oliver Windall Juhl
2021-02-04 14:51:04 +01:00
committed by GitHub
parent d8e806d8ff
commit 81df78384c
7 changed files with 197 additions and 2 deletions

View File

@@ -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);
});
});
});

View File

@@ -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);
};

View File

@@ -1,3 +1,22 @@
const glob = require(`glob`);
const pkgs = glob
.sync(`${__dirname}/*/`)
.map((p) => p.replace(__dirname, `<rootDir>/integration-tests`));
module.exports = {
setupFilesAfterEnv: ["<rootDir>/setup.js"],
testEnvironment: `node`,
rootDir: `../`,
roots: pkgs,
testPathIgnorePatterns: [
`/examples/`,
`/www/`,
`/dist/`,
`/node_modules/`,
`__tests__/fixtures`,
`__testfixtures__`,
`.cache`,
],
transform: { "^.+\\.[jt]s$": `<rootDir>/jest-transformer.js` },
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
};

View File

@@ -0,0 +1,5 @@
const { dropDatabase } = require("pg-god");
afterAll(() => {
dropDatabase({ databaseName: "medusa-integration" });
});

View File

@@ -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)

View File

@@ -1,6 +1,8 @@
import { defaultFields, defaultRelations } from "./"
export default async (req, res) => {
const { id } = req.params
try {
const cartService = req.scope.resolve("cartService")

View File

@@ -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
}