feat(user): Init user module (#6293)
* init user module * add migration * update module with latest utils * pr ready * make interface types interfaces
This commit is contained in:
16
packages/user/integration-tests/__fixtures__/user.ts
Normal file
16
packages/user/integration-tests/__fixtures__/user.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { User } from "@models"
|
||||
|
||||
export const createUsers = async (
|
||||
manager: SqlEntityManager,
|
||||
userData = [{ id: "1" }]
|
||||
) => {
|
||||
const users: User[] = []
|
||||
|
||||
for (const user of userData) {
|
||||
const usr = manager.create(User, user)
|
||||
users.push(usr)
|
||||
}
|
||||
|
||||
await manager.persistAndFlush(users)
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
import { IUserModuleService } from "@medusajs/types/dist/user"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createUsers } from "../../../__fixtures__/user"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("UserModuleService - User", () => {
|
||||
let service: IUserModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.USER]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
await createUsers(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
describe("listUsers", () => {
|
||||
it("should list users", async () => {
|
||||
const users = await service.list()
|
||||
const serialized = JSON.parse(JSON.stringify(users))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list users by id", async () => {
|
||||
const users = await service.list({
|
||||
id: ["1"],
|
||||
})
|
||||
|
||||
expect(users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountUsers", () => {
|
||||
it("should list and count users", async () => {
|
||||
const [users, count] = await service.listAndCount()
|
||||
const serialized = JSON.parse(JSON.stringify(users))
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount Users by id", async () => {
|
||||
const [Users, count] = await service.listAndCount({
|
||||
id: "1",
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(Users).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveUser", () => {
|
||||
const id = "1"
|
||||
|
||||
it("should return an user for the given id", async () => {
|
||||
const user = await service.retrieve(id)
|
||||
|
||||
expect(user).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an user with the given id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"User with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a userId is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("user - id must be defined")
|
||||
})
|
||||
|
||||
it("should return user based on config select param", async () => {
|
||||
const User = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(User))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteUser", () => {
|
||||
const id = "1"
|
||||
|
||||
it("should delete the Users given an id successfully", async () => {
|
||||
await service.delete([id])
|
||||
|
||||
const users = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(users).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateUser", () => {
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('User with id "does-not-exist" not found')
|
||||
})
|
||||
})
|
||||
|
||||
describe("createUser", () => {
|
||||
it("should create a User successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
id: "2",
|
||||
},
|
||||
])
|
||||
|
||||
const [User, count] = await service.listAndCount({
|
||||
id: ["2"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(User[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "2",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
6
packages/user/integration-tests/setup-env.js
Normal file
6
packages/user/integration-tests/setup-env.js
Normal file
@@ -0,0 +1,6 @@
|
||||
if (typeof process.env.DB_TEMP_NAME === "undefined") {
|
||||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
process.env.DB_TEMP_NAME = `medusa-user-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_USER_DB_SCHEMA = "public"
|
||||
3
packages/user/integration-tests/setup.js
Normal file
3
packages/user/integration-tests/setup.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import { JestUtils } from "medusa-test-utils"
|
||||
|
||||
JestUtils.afterAllHookDropDatabase()
|
||||
6
packages/user/integration-tests/utils/config.ts
Normal file
6
packages/user/integration-tests/utils/config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ModuleServiceInitializeOptions } from "@medusajs/types"
|
||||
|
||||
export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
|
||||
schema: "public",
|
||||
clientUrl: "medusa-user-test",
|
||||
}
|
||||
18
packages/user/integration-tests/utils/database.ts
Normal file
18
packages/user/integration-tests/utils/database.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import * as UserModels from "@models"
|
||||
|
||||
import { TestDatabaseUtils } from "medusa-test-utils"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = UserModels as unknown as any[]
|
||||
|
||||
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
|
||||
mikroOrmEntities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
|
||||
mikroOrmEntities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
export const DB_URL = TestDatabaseUtils.getDatabaseURL()
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
|
||||
|
||||
import { DB_URL } from "./database"
|
||||
|
||||
export function getInitModuleConfig() {
|
||||
const moduleOptions = {
|
||||
defaultAdapterOptions: {
|
||||
database: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_USER_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const injectedDependencies = {}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.USER]: {
|
||||
definition: ModulesDefinition[Modules.USER],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_USER_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
2
packages/user/integration-tests/utils/index.ts
Normal file
2
packages/user/integration-tests/utils/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./config"
|
||||
export * from "./database"
|
||||
Reference in New Issue
Block a user