**What** - add error handling when loading project config **How** - Add error parameter to get-medusa-config result if an error was thrown (previously we returned an empty config) - Discussion: A different, but equally valid approach could be just throwing the error rather than creating an error parameter. This causes a more ugly output without warnings and changes the api a bit but it would force error handling. wdyt? **Why** - cli would fail with database error `databaseMissingDriverError` if config was invalid, ex. missing a comma ### example (missing `,` in config) **old output** ``` Successfully compiled 2 files with Babel (143ms). [medusa-config] ⚠️ redis_url not found. A fake redis instance will be used. [medusa-config] ⚠️ database_type not found. fallback to default sqlite. info: Using flag MEDUSA_FF_ORDER_EDITING from environment with value true info: Using flag MEDUSA_FF_SALES_CHANNELS from environment with value true info: Using flag MEDUSA_FF_TAX_INCLUSIVE_PRICING from environment with value true info: Using fake Redis ✔ Models initialized – 13ms ✔ Plugin models initialized – 0ms ✔ Repositories initialized – 17ms ⠋ Initializing databaseMissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-data-api", "aurora-data-api-pg", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs". ``` **new output** ``` Successfully compiled 2 files with Babel (185ms). error: Error in loading config: Unexpected identifier error: /Users/phko/projects/community/my-medusa-store/medusa-config.js:129 plugins, ^^^^^^^ SyntaxError: Unexpected identifier at compileFunction (<anonymous>) at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1033:15) at Module._compile (node:internal/modules/cjs/loader:1069:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at getConfigFile (/Users/phko/projects/community/my-medusa-store/node_modules/medusa-core-utils/dist/get-config-file.js:26:20) ```
318 lines
8.4 KiB
JavaScript
318 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) => {
|
|
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"
|
|
)
|
|
})
|
|
})
|
|
})
|
|
})
|