chore: V2 core loader + modules integration-tests (#6544)
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("POST /admin/campaigns", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should throw an error if required params are not passed", async () => {
|
||||
const api = useApi() as any
|
||||
const { response } = await api
|
||||
.post(`/admin/campaigns`, {}, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data.message).toEqual(
|
||||
"name must be a string, name should not be empty"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a campaign successfully", async () => {
|
||||
const createdPromotion = await promotionModuleService.create({
|
||||
code: "TEST",
|
||||
type: "standard",
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.post(
|
||||
`/admin/campaigns`,
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024").toISOString(),
|
||||
ends_at: new Date("01/01/2029").toISOString(),
|
||||
promotions: [{ id: createdPromotion.id }],
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: expect.any(String),
|
||||
ends_at: expect.any(String),
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
}),
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: createdPromotion.id,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create 3 campaigns in parallel and have the context passed as argument when calling createCampaigns with different transactionId", async () => {
|
||||
const parallelPromotion = await promotionModuleService.create({
|
||||
code: "PARALLEL",
|
||||
type: "standard",
|
||||
})
|
||||
|
||||
const spyCreateCampaigns = jest.spyOn(
|
||||
promotionModuleService.constructor.prototype,
|
||||
"createCampaigns"
|
||||
)
|
||||
|
||||
const api = useApi() as any
|
||||
|
||||
const a = async () => {
|
||||
return await api.post(
|
||||
`/admin/campaigns`,
|
||||
{
|
||||
name: "camp_1",
|
||||
campaign_identifier: "camp_1",
|
||||
starts_at: new Date("01/01/2024").toISOString(),
|
||||
ends_at: new Date("01/02/2024").toISOString(),
|
||||
promotions: [{ id: parallelPromotion.id }],
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
}
|
||||
|
||||
const b = async () => {
|
||||
return await api.post(
|
||||
`/admin/campaigns`,
|
||||
{
|
||||
name: "camp_2",
|
||||
campaign_identifier: "camp_2",
|
||||
starts_at: new Date("01/02/2024").toISOString(),
|
||||
ends_at: new Date("01/03/2029").toISOString(),
|
||||
promotions: [{ id: parallelPromotion.id }],
|
||||
budget: {
|
||||
limit: 500,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
}
|
||||
|
||||
const c = async () => {
|
||||
return await api.post(
|
||||
`/admin/campaigns`,
|
||||
{
|
||||
name: "camp_3",
|
||||
campaign_identifier: "camp_3",
|
||||
starts_at: new Date("01/03/2024").toISOString(),
|
||||
ends_at: new Date("01/04/2029").toISOString(),
|
||||
promotions: [{ id: parallelPromotion.id }],
|
||||
budget: {
|
||||
limit: 250,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
...adminHeaders.headers,
|
||||
"x-request-id": "my-custom-request-id",
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
await Promise.all([a(), b(), c()])
|
||||
|
||||
expect(spyCreateCampaigns).toHaveBeenCalledTimes(3)
|
||||
expect(spyCreateCampaigns.mock.calls[0][1].__type).toBe("MedusaContext")
|
||||
|
||||
const distinctTransactionId = [
|
||||
...new Set(
|
||||
spyCreateCampaigns.mock.calls.map((call) => call[1].transactionId)
|
||||
),
|
||||
]
|
||||
expect(distinctTransactionId).toHaveLength(3)
|
||||
|
||||
const distinctRequestId = [
|
||||
...new Set(
|
||||
spyCreateCampaigns.mock.calls.map((call) => call[1].requestId)
|
||||
),
|
||||
]
|
||||
|
||||
expect(distinctRequestId).toHaveLength(3)
|
||||
expect(distinctRequestId).toContain("my-custom-request-id")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,344 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("POST /admin/promotions", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should throw an error if required params are not passed", async () => {
|
||||
const api = useApi() as any
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
type: PromotionType.STANDARD,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data.message).toEqual(
|
||||
"code must be a string, code should not be empty, application_method should not be empty"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a standard promotion successfully", async () => {
|
||||
const api = useApi() as any
|
||||
const response = await api.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
is_automatic: true,
|
||||
campaign: {
|
||||
name: "test",
|
||||
campaign_identifier: "test-1",
|
||||
budget: {
|
||||
type: "usage",
|
||||
limit: 100,
|
||||
},
|
||||
},
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: "100",
|
||||
max_quantity: 100,
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
type: "standard",
|
||||
is_automatic: true,
|
||||
campaign: expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test-1",
|
||||
budget: expect.objectContaining({
|
||||
type: "usage",
|
||||
limit: 100,
|
||||
}),
|
||||
}),
|
||||
application_method: expect.objectContaining({
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
type: "fixed",
|
||||
target_type: "items",
|
||||
allocation: "each",
|
||||
target_rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error if buy_rules params are not passed", async () => {
|
||||
const api = useApi() as any
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.BUYGET,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: "100",
|
||||
max_quantity: 100,
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data.message).toEqual(
|
||||
"Buy rules are required for buyget promotion type"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error if buy_rules params are not passed", async () => {
|
||||
const api = useApi() as any
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.BUYGET,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: "100",
|
||||
max_quantity: 100,
|
||||
buy_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data.message).toEqual(
|
||||
"Target rules are required for buyget promotion type"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a buyget promotion successfully", async () => {
|
||||
const api = useApi() as any
|
||||
const response = await api.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.BUYGET,
|
||||
is_automatic: true,
|
||||
campaign: {
|
||||
name: "test",
|
||||
campaign_identifier: "test-1",
|
||||
budget: {
|
||||
type: "usage",
|
||||
limit: 100,
|
||||
},
|
||||
},
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: "100",
|
||||
max_quantity: 100,
|
||||
apply_to_quantity: 1,
|
||||
buy_rules_min_quantity: 1,
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
buy_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
type: "buyget",
|
||||
is_automatic: true,
|
||||
campaign: expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test-1",
|
||||
budget: expect.objectContaining({
|
||||
type: "usage",
|
||||
limit: 100,
|
||||
}),
|
||||
}),
|
||||
application_method: expect.objectContaining({
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
type: "fixed",
|
||||
target_type: "items",
|
||||
allocation: "each",
|
||||
apply_to_quantity: 1,
|
||||
buy_rules_min_quantity: 1,
|
||||
target_rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
buy_rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,73 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("DELETE /admin/campaigns/:id", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should delete campaign successfully", async () => {
|
||||
const [createdCampaign] = await promotionModuleService.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
const api = useApi() as any
|
||||
const deleteRes = await api.delete(
|
||||
`/admin/campaigns/${createdCampaign.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(deleteRes.status).toEqual(200)
|
||||
|
||||
const campaigns = await promotionModuleService.listCampaigns({
|
||||
id: [createdCampaign.id],
|
||||
})
|
||||
|
||||
expect(campaigns.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,74 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("DELETE /admin/promotions/:id", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should delete promotion successfully", async () => {
|
||||
const createdPromotion = await promotionModuleService.create({
|
||||
code: "TEST",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: "100",
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const deleteRes = await api.delete(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(deleteRes.status).toEqual(200)
|
||||
|
||||
const promotions = await promotionModuleService.list({
|
||||
id: [createdPromotion.id],
|
||||
})
|
||||
|
||||
expect(promotions.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,186 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { CampaignBudgetType } from "@medusajs/utils"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
export const campaignsData = [
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: new Date("01/01/2023"),
|
||||
ends_at: new Date("01/01/2024"),
|
||||
budget: {
|
||||
type: CampaignBudgetType.SPEND,
|
||||
limit: 1000,
|
||||
used: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "campaign-id-2",
|
||||
name: "campaign 2",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: new Date("01/01/2023"),
|
||||
ends_at: new Date("01/01/2024"),
|
||||
budget: {
|
||||
type: CampaignBudgetType.USAGE,
|
||||
limit: 1000,
|
||||
used: 0,
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("GET /admin/campaigns", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
await promotionModuleService.createCampaigns(campaignsData)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should get all campaigns and its count", async () => {
|
||||
const api = useApi() as any
|
||||
const response = await api.get(`/admin/campaigns`, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(2)
|
||||
expect(response.data.campaigns).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(String),
|
||||
ends_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
type: "spend",
|
||||
limit: 1000,
|
||||
used: 0,
|
||||
raw_limit: {
|
||||
precision: 20,
|
||||
value: "1000",
|
||||
},
|
||||
raw_used: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "campaign 2",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: expect.any(String),
|
||||
ends_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
type: "usage",
|
||||
limit: 1000,
|
||||
used: 0,
|
||||
raw_limit: {
|
||||
precision: 20,
|
||||
value: "1000",
|
||||
},
|
||||
raw_used: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("should get all campaigns and its count filtered", async () => {
|
||||
const api = useApi() as any
|
||||
const response = await api.get(
|
||||
`/admin/campaigns?fields=name,created_at,budget.id`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(2)
|
||||
expect(response.data.campaigns).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
id: expect.any(String),
|
||||
name: "campaign 1",
|
||||
created_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: expect.any(String),
|
||||
name: "campaign 2",
|
||||
created_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
},
|
||||
},
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,125 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("GET /admin/promotions", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should get all promotions and its count", async () => {
|
||||
await promotionModuleService.create([
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: "100",
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.get(`/admin/promotions`, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.promotions).toEqual([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
campaign: null,
|
||||
is_automatic: false,
|
||||
type: "standard",
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
application_method: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: 100,
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
allocation: null,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
}),
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should get all promotions and its count filtered", async () => {
|
||||
const [createdPromotion] = await promotionModuleService.create([
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: "100",
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.get(
|
||||
`/admin/promotions?fields=code,created_at,application_method.id`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.promotions).toEqual([
|
||||
{
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
created_at: expect.any(String),
|
||||
application_method: {
|
||||
id: expect.any(String),
|
||||
promotion: expect.any(Object),
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,141 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { CampaignBudgetType } from "@medusajs/utils"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
export const campaignData = {
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: new Date("01/01/2023"),
|
||||
ends_at: new Date("01/01/2024"),
|
||||
budget: {
|
||||
type: CampaignBudgetType.SPEND,
|
||||
limit: 1000,
|
||||
used: 0,
|
||||
},
|
||||
}
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("GET /admin/campaigns", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
let campaigns
|
||||
|
||||
beforeEach(async () => {})
|
||||
|
||||
it("should throw an error if id does not exist", async () => {
|
||||
const api = useApi() as any
|
||||
const { response } = await api
|
||||
.get(`/admin/campaigns/does-not-exist`, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
"Campaign with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should get the requested campaign", async () => {
|
||||
const createdCampaign = await promotionModuleService.createCampaigns(
|
||||
campaignData
|
||||
)
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.get(
|
||||
`/admin/campaigns/${createdCampaign.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual({
|
||||
id: expect.any(String),
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(String),
|
||||
ends_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
type: "spend",
|
||||
limit: 1000,
|
||||
raw_limit: {
|
||||
precision: 20,
|
||||
value: "1000",
|
||||
},
|
||||
raw_used: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
used: 0,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
})
|
||||
})
|
||||
|
||||
it("should get the requested campaign with filtered fields and relations", async () => {
|
||||
const createdCampaign = await promotionModuleService.createCampaigns(
|
||||
campaignData
|
||||
)
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.get(
|
||||
`/admin/campaigns/${createdCampaign.id}?fields=name&expand=`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual({
|
||||
id: expect.any(String),
|
||||
name: "campaign 1",
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,129 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("GET /admin/promotions", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should throw an error if id does not exist", async () => {
|
||||
const api = useApi() as any
|
||||
const { response } = await api
|
||||
.get(`/admin/promotions/does-not-exist`, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
"Promotion with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should get the requested promotion", async () => {
|
||||
const createdPromotion = await promotionModuleService.create({
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: "100",
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.get(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
campaign: null,
|
||||
is_automatic: false,
|
||||
type: "standard",
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
application_method: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
promotion: expect.any(Object),
|
||||
value: 100,
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
max_quantity: 0,
|
||||
allocation: null,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should get the requested promotion with filtered fields and relations", async () => {
|
||||
const createdPromotion = await promotionModuleService.create({
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: "100",
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.get(
|
||||
`/admin/promotions/${createdPromotion.id}?fields=id,code&expand=`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,118 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("POST /admin/campaigns/:id", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should throw an error if id does not exist", async () => {
|
||||
const api = useApi() as any
|
||||
const { response } = await api
|
||||
.post(`/admin/campaigns/does-not-exist`, {}, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
`Campaign with id "does-not-exist" not found`
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a campaign successfully", async () => {
|
||||
const createdPromotion = await promotionModuleService.create({
|
||||
code: "TEST",
|
||||
type: "standard",
|
||||
})
|
||||
|
||||
const createdPromotion2 = await promotionModuleService.create({
|
||||
code: "TEST_2",
|
||||
type: "standard",
|
||||
})
|
||||
|
||||
const createdCampaign = await promotionModuleService.createCampaigns({
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024").toISOString(),
|
||||
ends_at: new Date("01/01/2029").toISOString(),
|
||||
promotions: [{ id: createdPromotion.id }],
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.post(
|
||||
`/admin/campaigns/${createdCampaign.id}`,
|
||||
{
|
||||
name: "test-2",
|
||||
campaign_identifier: "test-2",
|
||||
budget: {
|
||||
limit: 2000,
|
||||
},
|
||||
promotions: [{ id: createdPromotion2.id }],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "test-2",
|
||||
campaign_identifier: "test-2",
|
||||
budget: expect.objectContaining({
|
||||
limit: 2000,
|
||||
type: "usage",
|
||||
used: 10,
|
||||
}),
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: createdPromotion2.id,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,192 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
import { createAdminUser } from "../../../helpers/create-admin-user"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("POST /admin/promotions/:id", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should throw an error if id does not exist", async () => {
|
||||
const api = useApi() as any
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist`,
|
||||
{ type: PromotionType.STANDARD },
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
`Promotion with id "does-not-exist" not found`
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when both campaign and campaign_id params are passed", async () => {
|
||||
const createdPromotion = await promotionModuleService.create({
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: "100",
|
||||
max_quantity: 100,
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
{
|
||||
campaign: {
|
||||
name: "test campaign",
|
||||
},
|
||||
campaign_id: "test",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data.message).toContain(
|
||||
`Failed XOR relation between "campaign_id" and "campaign"`
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a promotion successfully", async () => {
|
||||
const createdPromotion = await promotionModuleService.create({
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: "100",
|
||||
max_quantity: 100,
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
{
|
||||
code: "TEST_TWO",
|
||||
application_method: {
|
||||
value: "200",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST_TWO",
|
||||
application_method: expect.objectContaining({
|
||||
value: 200,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a buyget promotion successfully", async () => {
|
||||
const createdPromotion = await promotionModuleService.create({
|
||||
code: "PROMOTION_TEST",
|
||||
type: PromotionType.BUYGET,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "items",
|
||||
allocation: "across",
|
||||
value: "100",
|
||||
apply_to_quantity: 1,
|
||||
buy_rules_min_quantity: 1,
|
||||
buy_rules: [
|
||||
{
|
||||
attribute: "product_collection.id",
|
||||
operator: "eq",
|
||||
values: ["pcol_towel"],
|
||||
},
|
||||
],
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "product.id",
|
||||
operator: "eq",
|
||||
values: "prod_mat",
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
{
|
||||
code: "TEST_TWO",
|
||||
application_method: {
|
||||
value: "200",
|
||||
buy_rules_min_quantity: 6,
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST_TWO",
|
||||
application_method: expect.objectContaining({
|
||||
value: 200,
|
||||
buy_rules_min_quantity: 6,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user