Files
medusa-store/integration-tests/api/__tests__/batch-jobs/api.js
Kasper Fabricius Kristensen 4de4f20b46 feat(medusa): add analytics config (#2442)
**What**
- Adds new entity AnalyticsConfig
- Adds new service AnalyticsConfigService
- Adds new repository AnalyticsConfigRepository
- Adds new endpoints to get, create, update, and delete analytics configs

**Why**
As we begin gathering usage insights to help us improve Medusa, we want to give each individual users the ability to control what data they share with us, or not share any data with us at all. The AnalyticsConfig holds information that is used to check if the user wishes for their data to be anonymized or if they have opted out of sharing usage data.

The entire feature can be disabled on a store level by setting the feature flag `MEDUSA_FF_ANALYTICS=false` in their environment variables, the feature is enabled by default. 

**Testing**
Adds integration test for each of the new endpoints

Resolves CORE-656, CORE-655, CORE-654

Also resolves CORE-574
2022-10-21 13:04:46 +00:00

319 lines
8.4 KiB
JavaScript

const path = require("path")
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 userSeeder = require("../../helpers/user-seeder")
const { simpleBatchJobFactory } = require("../../factories")
jest.setTimeout(50000)
const adminReqConfig = {
headers: {
Authorization: "Bearer test_token",
},
}
const setupJobDb = async (dbConnection) => {
await adminSeeder(dbConnection)
await userSeeder(dbConnection)
await simpleBatchJobFactory(dbConnection, {
id: "job_1",
type: "product-export",
created_by: "admin_user",
})
await simpleBatchJobFactory(dbConnection, {
id: "job_2",
type: "product-export",
created_by: "admin_user",
})
await simpleBatchJobFactory(dbConnection, {
id: "job_3",
type: "product-export",
created_by: "admin_user",
})
await simpleBatchJobFactory(dbConnection, {
id: "job_4",
type: "product-export",
status: "awaiting_confirmation",
created_by: "member-user",
})
await simpleBatchJobFactory(dbConnection, {
id: "job_5",
type: "product-export",
status: "completed",
completed_at: "2022-06-27T22:00:00.000Z",
created_by: "admin_user",
})
}
describe("/admin/batch-jobs", () => {
let medusaProcess
let dbConnection
beforeAll(async () => {
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()
})
describe("GET /admin/batch-jobs", () => {
beforeEach(async () => {
await setupJobDb(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("lists batch jobs created by the user", async () => {
const api = useApi()
const response = await api.get("/admin/batch-jobs", adminReqConfig)
expect(response.status).toEqual(200)
expect(response.data.batch_jobs.length).toEqual(4)
expect(response.data).toMatchSnapshot({
batch_jobs: [
{
id: "job_5",
created_at: expect.any(String),
updated_at: expect.any(String),
created_by: "admin_user",
},
{
id: "job_3",
created_at: expect.any(String),
updated_at: expect.any(String),
created_by: "admin_user",
},
{
id: "job_2",
created_at: expect.any(String),
updated_at: expect.any(String),
created_by: "admin_user",
},
{
id: "job_1",
created_at: expect.any(String),
updated_at: expect.any(String),
created_by: "admin_user",
},
],
})
})
it("lists batch jobs created by the user and where completed_at is null ", async () => {
const api = useApi()
const response = await api.get(
"/admin/batch-jobs?completed_at=null",
adminReqConfig
)
expect(response.status).toEqual(200)
expect(response.data.batch_jobs.length).toEqual(3)
expect(response.data).toMatchSnapshot({
batch_jobs: [
{
id: "job_3",
created_at: expect.any(String),
updated_at: expect.any(String),
created_by: "admin_user",
},
{
id: "job_2",
created_at: expect.any(String),
updated_at: expect.any(String),
created_by: "admin_user",
},
{
id: "job_1",
created_at: expect.any(String),
updated_at: expect.any(String),
created_by: "admin_user",
},
],
})
})
})
describe("GET /admin/batch-jobs/:id", () => {
beforeEach(async () => {
await setupJobDb(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("return batch job created by the user", async () => {
const api = useApi()
const response = await api.get("/admin/batch-jobs/job_1", adminReqConfig)
expect(response.status).toEqual(200)
expect(response.data.batch_job).toEqual(
expect.objectContaining({
created_at: expect.any(String),
updated_at: expect.any(String),
created_by: "admin_user",
})
)
})
it("should fail on batch job created by other user", async () => {
const api = useApi()
await api.get("/admin/batch-jobs/job_4", adminReqConfig).catch((err) => {
expect(err.response.status).toEqual(400)
expect(err.response.data.type).toEqual("not_allowed")
expect(err.response.data.message).toEqual(
"Cannot access a batch job that does not belong to the logged in user"
)
})
})
})
describe("POST /admin/batch-jobs/", () => {
beforeEach(async () => {
await setupJobDb(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("Creates a batch job", async () => {
const api = useApi()
const response = await api.post(
"/admin/batch-jobs",
{
type: "product-export",
context: {},
},
adminReqConfig
)
expect(response.status).toEqual(201)
expect(response.data.batch_job).toMatchSnapshot({
created_by: "admin_user",
status: "created",
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
})
})
})
describe("POST /admin/batch-jobs/:id/confirm", () => {
beforeEach(async () => {
await setupJobDb(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("Fails to confirm a batch job created by a different user", async () => {
const api = useApi()
const jobId = "job_4"
await api
.post(`/admin/batch-jobs/${jobId}/confirm`, {}, adminReqConfig)
.catch((err) => {
expect(err.response.status).toEqual(400)
expect(err.response.data.type).toEqual("not_allowed")
expect(err.response.data.message).toEqual(
"Cannot access a batch job that does not belong to the logged in user"
)
})
})
})
describe("POST /admin/batch-jobs/:id/cancel", () => {
beforeEach(async () => {
await setupJobDb(dbConnection)
await simpleBatchJobFactory(dbConnection, {
id: "job_complete",
type: "product-export",
created_by: "admin_user",
completed_at: new Date(),
})
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("Cancels batch job created by the user", async () => {
const api = useApi()
const jobId = "job_1"
const response = await api.post(
`/admin/batch-jobs/${jobId}/cancel`,
{},
adminReqConfig
)
expect(response.status).toEqual(200)
expect(response.data.batch_job).toMatchSnapshot({
created_at: expect.any(String),
updated_at: expect.any(String),
canceled_at: expect.any(String),
status: "canceled",
})
})
it("Fails to cancel a batch job created by a different user", async () => {
expect.assertions(3)
const api = useApi()
const jobId = "job_4"
await api
.post(`/admin/batch-jobs/${jobId}/cancel`, {}, adminReqConfig)
.catch((err) => {
expect(err.response.status).toEqual(400)
expect(err.response.data.type).toEqual("not_allowed")
expect(err.response.data.message).toEqual(
"Cannot access a batch job that does not belong to the logged in user"
)
})
})
it("Fails to cancel a batch job that is already complete", async () => {
expect.assertions(3)
const api = useApi()
const jobId = "job_complete"
await api
.post(`/admin/batch-jobs/${jobId}/cancel`, {}, adminReqConfig)
.catch((err) => {
console.log(err)
expect(err.response.status).toEqual(400)
expect(err.response.data.type).toEqual("not_allowed")
expect(err.response.data.message).toEqual(
"Cannot cancel completed batch job"
)
})
})
})
})