fix: Ensure uniqueness for products, variants, collections and discounts (#382)
This commit is contained in:
@@ -1,46 +1,46 @@
|
||||
const path = require("path");
|
||||
const { Region, DiscountRule, Discount } = require("@medusajs/medusa");
|
||||
const path = require("path")
|
||||
const { Region, DiscountRule, Discount } = require("@medusajs/medusa")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server");
|
||||
const { useApi } = require("../../../helpers/use-api");
|
||||
const { initDb, useDb } = require("../../../helpers/use-db");
|
||||
const adminSeeder = require("../../helpers/admin-seeder");
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
|
||||
jest.setTimeout(30000);
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("/admin/discounts", () => {
|
||||
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 /admin/discounts", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection);
|
||||
await adminSeeder(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 discount and updates it", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
@@ -62,16 +62,16 @@ describe("/admin/discounts", () => {
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.discount).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "HELLOWORLD",
|
||||
usage_limit: 10,
|
||||
})
|
||||
);
|
||||
)
|
||||
|
||||
const updated = await api
|
||||
.post(
|
||||
@@ -86,51 +86,51 @@ describe("/admin/discounts", () => {
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(updated.status).toEqual(200);
|
||||
expect(updated.status).toEqual(200)
|
||||
expect(updated.data.discount).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "HELLOWORLD",
|
||||
usage_limit: 20,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("testing for soft-deletion + uniqueness on discount codes", () => {
|
||||
let manager;
|
||||
let manager
|
||||
beforeEach(async () => {
|
||||
manager = dbConnection.manager;
|
||||
manager = dbConnection.manager
|
||||
try {
|
||||
await adminSeeder(dbConnection);
|
||||
await adminSeeder(dbConnection)
|
||||
await manager.insert(DiscountRule, {
|
||||
id: "test-discount-rule",
|
||||
description: "Test discount rule",
|
||||
type: "percentage",
|
||||
value: 10,
|
||||
allocation: "total",
|
||||
});
|
||||
})
|
||||
await manager.insert(Discount, {
|
||||
id: "test-discount",
|
||||
code: "TESTING",
|
||||
rule_id: "test-discount-rule",
|
||||
is_dynamic: false,
|
||||
is_disabled: false,
|
||||
});
|
||||
})
|
||||
} catch (err) {
|
||||
throw err;
|
||||
throw err
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb();
|
||||
await db.teardown();
|
||||
});
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("successfully creates discount with soft-deleted discount code", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
// First we soft-delete the discount
|
||||
await api
|
||||
@@ -140,8 +140,8 @@ describe("/admin/discounts", () => {
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
// Lets try to create a discount with same code as deleted one
|
||||
const response = await api
|
||||
@@ -164,51 +164,81 @@ describe("/admin/discounts", () => {
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.discount).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "TESTING",
|
||||
usage_limit: 10,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
)
|
||||
})
|
||||
|
||||
it("should fails when creating a discount with already existing code", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// Lets try to create a discount with same code as deleted one
|
||||
try {
|
||||
await api.post(
|
||||
"/admin/discounts",
|
||||
{
|
||||
code: "TESTING",
|
||||
rule: {
|
||||
description: "test",
|
||||
type: "percentage",
|
||||
value: 10,
|
||||
allocation: "total",
|
||||
},
|
||||
usage_limit: 10,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
expect(error.response.data.message).toMatch(
|
||||
/duplicate key value violates unique constraint/i
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/discounts/:discount_id/dynamic-codes", () => {
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager;
|
||||
const manager = dbConnection.manager
|
||||
try {
|
||||
await adminSeeder(dbConnection);
|
||||
await adminSeeder(dbConnection)
|
||||
await manager.insert(DiscountRule, {
|
||||
id: "test-discount-rule",
|
||||
description: "Dynamic rule",
|
||||
type: "percentage",
|
||||
value: 10,
|
||||
allocation: "total",
|
||||
});
|
||||
})
|
||||
await manager.insert(Discount, {
|
||||
id: "test-discount",
|
||||
code: "DYNAMIC",
|
||||
is_dynamic: true,
|
||||
is_disabled: false,
|
||||
rule_id: "test-discount-rule",
|
||||
});
|
||||
})
|
||||
} 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 dynamic discount", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
@@ -223,10 +253,10 @@ describe("/admin/discounts", () => {
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
const productSeeder = require("../../helpers/product-seeder")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
jest.setTimeout(50000)
|
||||
|
||||
describe("/admin/products", () => {
|
||||
let medusaProcess
|
||||
@@ -263,7 +263,7 @@ describe("/admin/products", () => {
|
||||
const api = useApi()
|
||||
|
||||
const payload = {
|
||||
title: "Test product",
|
||||
title: "Test",
|
||||
description: "test-product-description",
|
||||
type: { value: "test-type" },
|
||||
images: ["test-image.png", "test-image-2.png"],
|
||||
@@ -293,10 +293,10 @@ describe("/admin/products", () => {
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
title: "Test product",
|
||||
title: "Test",
|
||||
discountable: true,
|
||||
is_giftcard: false,
|
||||
handle: "test-product",
|
||||
handle: "test",
|
||||
images: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
url: "test-image.png",
|
||||
@@ -646,7 +646,7 @@ describe("/admin/products", () => {
|
||||
)
|
||||
})
|
||||
|
||||
it("successfully creates product with soft-deleted product handle", async () => {
|
||||
it("successfully creates product with soft-deleted product handle and deletes it again", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// First we soft-delete the product
|
||||
@@ -691,6 +691,56 @@ describe("/admin/products", () => {
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.product.handle).toEqual("test-product")
|
||||
|
||||
// Delete product again to ensure uniqueness is enforced in all cases
|
||||
const response2 = await api
|
||||
.delete("/admin/products/test-product", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response2.status).toEqual(200)
|
||||
expect(response2.data.id).toEqual("test-product")
|
||||
})
|
||||
|
||||
it("should fail when creating a product with a handle that already exists", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// Lets try to create a product with same handle as deleted one
|
||||
const payload = {
|
||||
title: "Test product",
|
||||
handle: "test-product",
|
||||
description: "test-product-description",
|
||||
type: { value: "test-type" },
|
||||
images: ["test-image.png", "test-image-2.png"],
|
||||
collection_id: "test-collection",
|
||||
tags: [{ value: "123" }, { value: "456" }],
|
||||
options: [{ title: "size" }, { title: "color" }],
|
||||
variants: [
|
||||
{
|
||||
title: "Test variant",
|
||||
inventory_quantity: 10,
|
||||
prices: [{ currency_code: "usd", amount: 100 }],
|
||||
options: [{ value: "large" }, { value: "green" }],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
try {
|
||||
await api.post("/admin/products", payload, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
expect(error.response.data.message).toMatch(
|
||||
/duplicate key value violates unique constraint/i
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it("successfully deletes product collection", async () => {
|
||||
@@ -743,6 +793,28 @@ describe("/admin/products", () => {
|
||||
expect(res.data.collection.handle).toEqual("test-collection")
|
||||
})
|
||||
|
||||
it("should fail when creating a collection with a handle that already exists", async () => {
|
||||
const api = useApi()
|
||||
|
||||
// Lets try to create a collection with same handle as deleted one
|
||||
const payload = {
|
||||
title: "Another test collection",
|
||||
handle: "test-collection",
|
||||
}
|
||||
|
||||
try {
|
||||
await api.post("/admin/collections", payload, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
expect(error.response.data.message).toMatch(
|
||||
/duplicate key value violates unique constraint/i
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it("successfully creates soft-deleted product variant", async () => {
|
||||
const api = useApi()
|
||||
|
||||
@@ -769,7 +841,6 @@ describe("/admin/products", () => {
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.variant_id).toEqual("test-variant")
|
||||
|
||||
// Lets try to create a product collection with same handle as deleted one
|
||||
const payload = {
|
||||
title: "Second variant",
|
||||
sku: "test-sku",
|
||||
|
||||
Reference in New Issue
Block a user