chore: Use new test runner in admin payments API (#6611)
No changes to the tests. Only replacing the test runner. Awaiting #6610
This commit is contained in:
@@ -1,17 +1,5 @@
|
|||||||
const path = require("path")
|
const { medusaIntegrationTestRunner } = require("medusa-test-utils")
|
||||||
|
const { createAdminUser } = require("../../../helpers/create-admin-user")
|
||||||
const startServerWithEnvironment =
|
|
||||||
require("../../../environment-helpers/start-server-with-environment").default
|
|
||||||
const { useApi } = require("../../../environment-helpers/use-api")
|
|
||||||
const { useDb } = require("../../../environment-helpers/use-db")
|
|
||||||
const adminSeeder = require("../../../helpers/admin-seeder")
|
|
||||||
|
|
||||||
const {
|
|
||||||
simplePaymentCollectionFactory,
|
|
||||||
} = require("../../../factories/simple-payment-collection-factory")
|
|
||||||
const {
|
|
||||||
simpleCustomerFactory,
|
|
||||||
} = require("../../../factories/simple-customer-factory")
|
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
@@ -21,154 +9,145 @@ const adminHeaders = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("/admin/payment", () => {
|
let { simpleCustomerFactory, simplePaymentCollectionFactory } = {}
|
||||||
let medusaProcess
|
|
||||||
let dbConnection
|
|
||||||
|
|
||||||
let payCol = null
|
medusaIntegrationTestRunner({
|
||||||
beforeAll(async () => {
|
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
|
||||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||||
const [process, connection] = await startServerWithEnvironment({
|
beforeAll(() => {
|
||||||
cwd,
|
;({
|
||||||
|
simplePaymentCollectionFactory,
|
||||||
|
} = require("../../../factories/simple-payment-collection-factory"))
|
||||||
|
;({
|
||||||
|
simpleCustomerFactory,
|
||||||
|
} = require("../../../factories/simple-customer-factory"))
|
||||||
})
|
})
|
||||||
dbConnection = connection
|
|
||||||
medusaProcess = process
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(async () => {
|
|
||||||
const db = useDb()
|
|
||||||
await db.shutdown()
|
|
||||||
|
|
||||||
medusaProcess.kill()
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("POST /admin/payment-collections/:id", () => {
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await adminSeeder(dbConnection)
|
const container = getContainer()
|
||||||
await simpleCustomerFactory(dbConnection, {
|
await createAdminUser(dbConnection, adminHeaders, container)
|
||||||
id: "customer",
|
|
||||||
email: "test@customer.com",
|
|
||||||
})
|
|
||||||
|
|
||||||
payCol = await simplePaymentCollectionFactory(dbConnection, {
|
|
||||||
description: "paycol description",
|
|
||||||
amount: 10000,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
describe("POST /admin/payment-collections/:id", () => {
|
||||||
const db = useDb()
|
let payCol
|
||||||
return await db.teardown()
|
|
||||||
})
|
|
||||||
|
|
||||||
it("Captures an authorized payment", async () => {
|
beforeEach(async () => {
|
||||||
const api = useApi()
|
await simpleCustomerFactory(dbConnection, {
|
||||||
|
id: "customer",
|
||||||
|
email: "test@customer.com",
|
||||||
|
})
|
||||||
|
|
||||||
// create payment session
|
payCol = await simplePaymentCollectionFactory(dbConnection, {
|
||||||
const payColRes = await api.post(
|
description: "paycol description",
|
||||||
`/store/payment-collections/${payCol.id}/sessions`,
|
|
||||||
{
|
|
||||||
provider_id: "test-pay",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
await api.post(
|
|
||||||
`/store/payment-collections/${payCol.id}/sessions/batch/authorize`,
|
|
||||||
{
|
|
||||||
session_ids: payColRes.data.payment_collection.payment_sessions.map(
|
|
||||||
({ id }) => id
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const paymentCollections = await api.get(
|
|
||||||
`/admin/payment-collections/${payCol.id}`,
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(paymentCollections.data.payment_collection.payments).toHaveLength(
|
|
||||||
1
|
|
||||||
)
|
|
||||||
|
|
||||||
const payment = paymentCollections.data.payment_collection.payments[0]
|
|
||||||
|
|
||||||
expect(payment.captured_at).toBe(null)
|
|
||||||
|
|
||||||
const response = await api.post(
|
|
||||||
`/admin/payments/${payment.id}/capture`,
|
|
||||||
undefined,
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(response.data.payment).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
id: payment.id,
|
|
||||||
captured_at: expect.any(String),
|
|
||||||
amount: 10000,
|
amount: 10000,
|
||||||
})
|
})
|
||||||
)
|
})
|
||||||
expect(response.status).toEqual(200)
|
|
||||||
|
it("Captures an authorized payment", async () => {
|
||||||
|
// create payment session
|
||||||
|
const payColRes = await api.post(
|
||||||
|
`/store/payment-collections/${payCol.id}/sessions`,
|
||||||
|
{
|
||||||
|
provider_id: "test-pay",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
await api.post(
|
||||||
|
`/store/payment-collections/${payCol.id}/sessions/batch/authorize`,
|
||||||
|
{
|
||||||
|
session_ids: payColRes.data.payment_collection.payment_sessions.map(
|
||||||
|
({ id }) => id
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const paymentCollections = await api.get(
|
||||||
|
`/admin/payment-collections/${payCol.id}`,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
paymentCollections.data.payment_collection.payments
|
||||||
|
).toHaveLength(1)
|
||||||
|
|
||||||
|
const payment = paymentCollections.data.payment_collection.payments[0]
|
||||||
|
|
||||||
|
expect(payment.captured_at).toBe(null)
|
||||||
|
|
||||||
|
const response = await api.post(
|
||||||
|
`/admin/payments/${payment.id}/capture`,
|
||||||
|
undefined,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(response.data.payment).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: payment.id,
|
||||||
|
captured_at: expect.any(String),
|
||||||
|
amount: 10000,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Refunds an captured payment", async () => {
|
||||||
|
// create payment session
|
||||||
|
const payColRes = await api.post(
|
||||||
|
`/store/payment-collections/${payCol.id}/sessions`,
|
||||||
|
{
|
||||||
|
provider_id: "test-pay",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
await api.post(
|
||||||
|
`/store/payment-collections/${payCol.id}/sessions/batch/authorize`,
|
||||||
|
{
|
||||||
|
session_ids: payColRes.data.payment_collection.payment_sessions.map(
|
||||||
|
({ id }) => id
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const paymentCollections = await api.get(
|
||||||
|
`/admin/payment-collections/${payCol.id}`,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
const payment = paymentCollections.data.payment_collection.payments[0]
|
||||||
|
await api.post(
|
||||||
|
`/admin/payments/${payment.id}/capture`,
|
||||||
|
undefined,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
// refund
|
||||||
|
const response = await api.post(
|
||||||
|
`/admin/payments/${payment.id}/refund`,
|
||||||
|
{
|
||||||
|
amount: 5000,
|
||||||
|
reason: "return",
|
||||||
|
note: "Do not like it",
|
||||||
|
},
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(response.data.refund).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
payment_id: payment.id,
|
||||||
|
reason: "return",
|
||||||
|
amount: 5000,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
|
||||||
|
const savedPayment = await api.get(
|
||||||
|
`/admin/payments/${payment.id}`,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(savedPayment.data.payment).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
amount_refunded: 5000,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
},
|
||||||
it("Refunds an captured payment", async () => {
|
|
||||||
const api = useApi()
|
|
||||||
|
|
||||||
// create payment session
|
|
||||||
const payColRes = await api.post(
|
|
||||||
`/store/payment-collections/${payCol.id}/sessions`,
|
|
||||||
{
|
|
||||||
provider_id: "test-pay",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
await api.post(
|
|
||||||
`/store/payment-collections/${payCol.id}/sessions/batch/authorize`,
|
|
||||||
{
|
|
||||||
session_ids: payColRes.data.payment_collection.payment_sessions.map(
|
|
||||||
({ id }) => id
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const paymentCollections = await api.get(
|
|
||||||
`/admin/payment-collections/${payCol.id}`,
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
const payment = paymentCollections.data.payment_collection.payments[0]
|
|
||||||
await api.post(
|
|
||||||
`/admin/payments/${payment.id}/capture`,
|
|
||||||
undefined,
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
// refund
|
|
||||||
const response = await api.post(
|
|
||||||
`/admin/payments/${payment.id}/refund`,
|
|
||||||
{
|
|
||||||
amount: 5000,
|
|
||||||
reason: "return",
|
|
||||||
note: "Do not like it",
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(response.data.refund).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
payment_id: payment.id,
|
|
||||||
reason: "return",
|
|
||||||
amount: 5000,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
expect(response.status).toEqual(200)
|
|
||||||
|
|
||||||
const savedPayment = await api.get(
|
|
||||||
`/admin/payments/${payment.id}`,
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(savedPayment.data.payment).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
amount_refunded: 5000,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user