feat(medusa): Validate LineItems in SalesChannel (#1871)
* wip: validate line item SC * fix: repository type, remove relation, use sc id, check if cart has associated sc * feat: setup tests and seeder, change entity retrieval in cart service method * feat: remove repo usage and method, use Adrien's method from product service to check sc association, add test cases, add seeder entities, accept flag for validating sc on the endpoint * feat: add a unit test to ensure validation method is called if flag is passed * feat: allow `validate_sales_channels` flag in other relevant endpoints * fix: typo * fix: flag rename * fix: correct FF in test suites * fix: address PR feedback * fix: change error message * feat: remove query params, guard with FF, refactor * feat: guard validation in the service * refactor: rename validation method * refactor: reorganise tests * wip: cleanup test file * wip: revert cart seeder changes use factories * fix: remove seeder, update mocks * fix: method name * fix: units, validate by default * git: resolve merge conflicts * refactor: separate line item sales chanel units Co-authored-by: fPolic <frane@medusajs.com>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
const path = require("path")
|
||||
|
||||
const { useDb } = require("../../../helpers/use-db")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { simpleCartFactory, simpleProductFactory } = require("../../factories")
|
||||
|
||||
const startServerWithEnvironment =
|
||||
require("../../../helpers/start-server-with-environment").default
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Line Item - Sales Channel", () => {
|
||||
let dbConnection
|
||||
let medusaProcess
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
const [process, connection] = await startServerWithEnvironment({
|
||||
cwd,
|
||||
env: { MEDUSA_FF_SALES_CHANNELS: true },
|
||||
verbose: false,
|
||||
})
|
||||
dbConnection = connection
|
||||
medusaProcess = process
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await simpleProductFactory(dbConnection, {
|
||||
id: "test-product-in-sales-channel",
|
||||
title: "test product belonging to a channel",
|
||||
sales_channels: [
|
||||
{
|
||||
id: "main-sales-channel",
|
||||
name: "Main sales channel",
|
||||
description: "Main sales channel",
|
||||
is_disabled: false,
|
||||
},
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant-sales-channel",
|
||||
title: "test variant in sales channel",
|
||||
product_id: "test-product-in-sales-channel",
|
||||
inventory_quantity: 1000,
|
||||
prices: [
|
||||
{
|
||||
currency: "usd",
|
||||
amount: 59,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await simpleProductFactory(dbConnection, {
|
||||
id: "test-product-no-sales-channel",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant-no-sales-channel",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await simpleCartFactory(dbConnection, {
|
||||
id: "test-cart-with-sales-channel",
|
||||
sales_channel: {
|
||||
id: "main-sales-channel",
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
describe("Adding line item with associated sales channel to a cart", () => {
|
||||
it("adding line item to a cart with associated sales channel returns 400", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-with-sales-channel/line-items",
|
||||
{
|
||||
variant_id: "test-variant-no-sales-channel", // variant's product doesn't belong to a sales channel
|
||||
quantity: 1,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => err.response)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data.type).toEqual("invalid_data")
|
||||
})
|
||||
|
||||
it("adding line item successfully if product and cart belong to the same sales channel", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/store/carts/test-cart-with-sales-channel/line-items",
|
||||
{
|
||||
variant_id: "test-variant-sales-channel",
|
||||
quantity: 1,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart).toMatchObject({
|
||||
id: "test-cart-with-sales-channel",
|
||||
items: [
|
||||
expect.objectContaining({
|
||||
cart_id: "test-cart-with-sales-channel",
|
||||
description: "test variant in sales channel",
|
||||
title: "test product belonging to a channel",
|
||||
variant_id: "test-variant-sales-channel",
|
||||
}),
|
||||
],
|
||||
sales_channel_id: "main-sales-channel",
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user