chore: V2 core loader + modules integration-tests (#6544)

This commit is contained in:
Oli Juhl
2024-02-29 16:46:30 +01:00
committed by GitHub
parent dc025302a1
commit 296d7faad4
100 changed files with 318 additions and 78 deletions
@@ -0,0 +1,74 @@
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
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 }
describe("POST /store/customers/me/addresses", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should create a customer address", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
const api = useApi() as any
const response = await api.post(
`/store/customers/me/addresses`,
{
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
},
{ headers: { authorization: `Bearer ${jwt}` } }
)
expect(response.status).toEqual(200)
expect(response.data.address).toEqual(
expect.objectContaining({
id: expect.any(String),
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
customer_id: customer.id,
})
)
const customerWithAddresses = await customerModuleService.retrieve(
customer.id,
{ relations: ["addresses"] }
)
expect(customerWithAddresses.addresses?.length).toEqual(1)
})
})
@@ -0,0 +1,82 @@
import { IAuthModuleService, ICustomerModuleService } from "@medusajs/types"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import jwt from "jsonwebtoken"
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 }
describe("POST /store/customers", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
await adminSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should create a customer", async () => {
const authService: IAuthModuleService = appContainer.resolve(
ModuleRegistrationName.AUTH
)
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
const authUser = await authService.create({
entity_id: "store_user",
provider: "emailpass",
scope: "store",
})
const token = jwt.sign(authUser, jwt_secret)
const api = useApi() as any
const response = await api.post(
`/store/customers`,
{
first_name: "John",
last_name: "Doe",
email: "john@me.com",
},
{ headers: { authorization: `Bearer ${token}` } }
)
expect(response.status).toEqual(200)
expect(response.data.customer).toEqual(
expect.objectContaining({
id: expect.any(String),
first_name: "John",
last_name: "Doe",
email: "john@me.com",
})
)
})
})
@@ -0,0 +1,90 @@
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
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"
const env = { MEDUSA_FF_MEDUSA_V2: true }
jest.setTimeout(50000)
describe("DELETE /store/customers/me/addresses/:address_id", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should delete a customer address", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
const address = await customerModuleService.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
})
const api = useApi() as any
const response = await api.delete(
`/store/customers/me/addresses/${address.id}`,
{ headers: { authorization: `Bearer ${jwt}` } }
)
expect(response.status).toEqual(200)
const updatedCustomer = await customerModuleService.retrieve(customer.id, {
relations: ["addresses"],
})
expect(updatedCustomer.addresses?.length).toEqual(0)
})
it("should fail to delete another customer's address", async () => {
const { jwt } = await createAuthenticatedCustomer(appContainer)
const otherCustomer = await customerModuleService.create({
first_name: "Jane",
last_name: "Doe",
})
const address = await customerModuleService.addAddresses({
customer_id: otherCustomer.id,
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
})
const api = useApi() as any
const response = await api
.delete(`/store/customers/me/addresses/${address.id}`, {
headers: { authorization: `Bearer ${jwt}` },
})
.catch((e) => e.response)
expect(response.status).toEqual(404)
})
})
@@ -0,0 +1,60 @@
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
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 }
describe("GET /store/customers", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should retrieve auth user's customer", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
const api = useApi() as any
const response = await api.get(`/store/customers/me`, {
headers: { authorization: `Bearer ${jwt}` },
})
expect(response.status).toEqual(200)
expect(response.data.customer).toEqual(
expect.objectContaining({
id: customer.id,
first_name: "John",
last_name: "Doe",
email: "john@me.com",
})
)
})
})
@@ -0,0 +1,109 @@
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
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"
const env = { MEDUSA_FF_MEDUSA_V2: true }
jest.setTimeout(100000)
describe("GET /store/customers/me/addresses", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService
beforeAll(async () => {
try {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
} catch (error) {
console.error(error)
}
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should get all customer addresses and its count", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
await customerModuleService.addAddresses([
{
first_name: "Test",
last_name: "Test",
address_1: "Test street 1",
customer_id: customer.id,
},
{
first_name: "Test",
last_name: "Test",
address_1: "Test street 2",
customer_id: customer.id,
},
{
first_name: "Test",
last_name: "Test",
address_1: "Test street 3",
customer_id: customer.id,
},
])
await customerModuleService.create({
first_name: "Test Test",
last_name: "Test Test",
addresses: [
{
first_name: "Test TEST",
last_name: "Test TEST",
address_1: "NOT street 1",
},
],
})
const api = useApi() as any
const response = await api.get(`/store/customers/me/addresses`, {
headers: { authorization: `Bearer ${jwt}` },
})
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(3)
expect(response.data.addresses).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
customer_id: customer.id,
address_1: "Test street 1",
}),
expect.objectContaining({
id: expect.any(String),
customer_id: customer.id,
address_1: "Test street 2",
}),
expect.objectContaining({
id: expect.any(String),
customer_id: customer.id,
address_1: "Test street 3",
}),
])
)
})
})
@@ -0,0 +1,97 @@
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
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 { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
describe("POST /store/customers/:id/addresses/:address_id", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should update a customer address", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
const address = await customerModuleService.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
})
const api = useApi() as any
const response = await api.post(
`/store/customers/me/addresses/${address.id}`,
{
first_name: "Jane",
},
{ headers: { authorization: `Bearer ${jwt}` } }
)
expect(response.status).toEqual(200)
expect(response.data.address).toEqual(
expect.objectContaining({
id: address.id,
first_name: "Jane",
last_name: "Doe",
})
)
})
it("should fail to update another customer's address", async () => {
const { jwt } = await createAuthenticatedCustomer(appContainer)
const otherCustomer = await customerModuleService.create({
first_name: "Jane",
last_name: "Doe",
})
const address = await customerModuleService.addAddresses({
customer_id: otherCustomer.id,
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
})
const api = useApi() as any
const response = await api
.post(
`/store/customers/me/addresses/${address.id}`,
{ first_name: "Jane" },
{ headers: { authorization: `Bearer ${jwt}` } }
)
.catch((e) => e.response)
expect(response.status).toEqual(404)
})
})