feat(authentication, types): Create authentication entities and services (#5979)
* create authentication entities * ensure hashes are not returned * re-add integration test argument * update filterableprops * update type * pr feedback * update serialization * move finding existing entities and validation to service layer * update types * update models * update user model * pr feedback * fix build * pr feedback * update integration test fixtures --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oli Juhl
parent
6b0b3fed7a
commit
479a8b82a9
@@ -1,5 +0,0 @@
|
||||
describe("Noop test", () => {
|
||||
it("noop check", async () => {
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
})
|
||||
+270
@@ -0,0 +1,270 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { AuthProviderRepository } from "@repositories"
|
||||
import { AuthProviderService } from "@services"
|
||||
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthProvider Service", () => {
|
||||
let service: AuthProviderService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
repositoryManager = await MikroOrmWrapper.forkManager()
|
||||
testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
const authProviderRepository = new AuthProviderRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
|
||||
service = new AuthProviderService({
|
||||
authProviderRepository,
|
||||
})
|
||||
|
||||
await createAuthProviders(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("should list AuthProviders", async () => {
|
||||
const authProviders = await service.list()
|
||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "disabled",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "admin",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authProviders by provider id", async () => {
|
||||
const authProviders = await service.list({
|
||||
provider: ["manual"],
|
||||
})
|
||||
|
||||
expect(authProviders).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list active authProviders", async () => {
|
||||
const authProviders = await service.list({
|
||||
is_active: true,
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "admin",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should list AuthProviders", async () => {
|
||||
const [authProviders, count] = await service.listAndCount()
|
||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "disabled",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "admin",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount authProviders by provider", async () => {
|
||||
const [authProviders, count] = await service.listAndCount({
|
||||
provider: ["manual"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(authProviders).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount active authProviders", async () => {
|
||||
const [authProviders, count] = await service.listAndCount({
|
||||
is_active: true,
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "admin",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const provider = "manual"
|
||||
|
||||
it("should return an authProvider for the given provider", async () => {
|
||||
const authProvider = await service.retrieve(provider)
|
||||
|
||||
expect(authProvider).toEqual(
|
||||
expect.objectContaining({
|
||||
provider,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an authProvider with the given provider does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthProvider with provider: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a provider is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('"authProviderProvider" must be defined')
|
||||
})
|
||||
|
||||
it("should return authProvider based on config select param", async () => {
|
||||
const authProvider = await service.retrieve(provider, {
|
||||
select: ["provider"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authProvider))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
provider,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
const provider = "manual"
|
||||
|
||||
it("should delete the authProviders given a provider successfully", async () => {
|
||||
await service.delete([provider])
|
||||
|
||||
const authProviders = await service.list({
|
||||
provider: [provider],
|
||||
})
|
||||
|
||||
expect(authProviders).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
const provider = "manual"
|
||||
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
provider: "does-not-exist",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'AuthProvider with provider "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authProvider", async () => {
|
||||
await service.update([
|
||||
{
|
||||
provider: "manual",
|
||||
name: "test",
|
||||
},
|
||||
])
|
||||
|
||||
const [provider] = await service.list({ provider: ["manual"] })
|
||||
expect(provider).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a authProvider successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
provider: "test",
|
||||
name: "test provider",
|
||||
},
|
||||
])
|
||||
|
||||
const [authProvider] = await service.list({
|
||||
provider: ["test"],
|
||||
})
|
||||
|
||||
expect(authProvider).toEqual(
|
||||
expect.objectContaining({
|
||||
provider: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,245 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { AuthUserRepository } from "@repositories"
|
||||
import { AuthUserService } from "@services"
|
||||
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthUser Service", () => {
|
||||
let service: AuthUserService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
repositoryManager = await MikroOrmWrapper.forkManager()
|
||||
testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
const authUserRepository = new AuthUserRepository({
|
||||
manager: repositoryManager,
|
||||
})
|
||||
|
||||
service = new AuthUserService({
|
||||
authUserRepository,
|
||||
})
|
||||
|
||||
await createAuthProviders(testManager)
|
||||
await createAuthUsers(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.list()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.list({
|
||||
id: ["test-id"],
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by provider_id", async () => {
|
||||
const authUsers = await service.list({
|
||||
provider_id: "manual",
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const [authUsers, count] = await service.listAndCount()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount authUsers by provider_id", async () => {
|
||||
const [authUsers, count] = await service.listAndCount({
|
||||
provider_id: "manual",
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authUser for the given id", async () => {
|
||||
const authUser = await service.retrieve(id)
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return authUser based on config select param", async () => {
|
||||
const authUser = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUser))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser 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(
|
||||
"AuthUser with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('"authUserId" must be defined')
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
it("should delete the authUsers given an id successfully", async () => {
|
||||
const id = "test-id"
|
||||
|
||||
await service.delete([id])
|
||||
|
||||
const authUsers = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(authUsers).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
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(
|
||||
'AuthUser with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authUser", async () => {
|
||||
const id = "test-id"
|
||||
|
||||
await service.update([
|
||||
{
|
||||
id,
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({ id: [id] })
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a authUser successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
id: "test",
|
||||
provider_id: "manual",
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({
|
||||
id: ["test"],
|
||||
})
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
+272
@@ -0,0 +1,272 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
||||
import { IAuthenticationModuleService } from "@medusajs/types"
|
||||
import { initialize } from "../../../../src"
|
||||
import { DB_URL } from "@medusajs/pricing/integration-tests/utils"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
let service: IAuthenticationModuleService
|
||||
let testManager: SqlEntityManager
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
service = await initialize({
|
||||
database: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_PRICING_DB_SCHEMA,
|
||||
},
|
||||
})
|
||||
|
||||
await createAuthProviders(testManager)
|
||||
await createAuthUsers(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listAuthProviders", () => {
|
||||
it("should list AuthProviders", async () => {
|
||||
const authProviders = await service.listAuthProviders()
|
||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "disabled",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "admin",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authProviders by id", async () => {
|
||||
const authProviders = await service.listAuthProviders({
|
||||
provider: ["manual"],
|
||||
})
|
||||
|
||||
expect(authProviders).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list active authProviders", async () => {
|
||||
const authProviders = await service.listAuthProviders({
|
||||
is_active: true,
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "admin",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountAuthProviders", () => {
|
||||
it("should list and count AuthProviders", async () => {
|
||||
const [authProviders, count] = await service.listAndCountAuthProviders()
|
||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
||||
|
||||
expect(count).toEqual(4)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "disabled",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "admin",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list and count authProviders by provider", async () => {
|
||||
const [authProviders, count] = await service.listAndCountAuthProviders({
|
||||
provider: ["manual"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(authProviders).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list and count active authProviders", async () => {
|
||||
const [authProviders, count] = await service.listAndCountAuthProviders({
|
||||
is_active: true,
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "admin",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveAuthProvider", () => {
|
||||
const provider = "manual"
|
||||
|
||||
it("should return an authProvider for the given provider", async () => {
|
||||
const authProvider = await service.retrieveAuthProvider(provider)
|
||||
|
||||
expect(authProvider).toEqual(
|
||||
expect.objectContaining({
|
||||
provider,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return authProvider based on config select param", async () => {
|
||||
const authProvider = await service.retrieveAuthProvider(provider, {
|
||||
select: ["provider"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authProvider))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
provider,
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw an error when an authProvider with the given provider does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveAuthProvider("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthProvider with provider: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a provider is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveAuthProvider(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('"authProviderProvider" must be defined')
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteAuthProvider", () => {
|
||||
const provider = "manual"
|
||||
|
||||
it("should delete the authProviders given a provider successfully", async () => {
|
||||
await service.deleteAuthProvider([provider])
|
||||
|
||||
const authProviders = await service.listAuthProviders({
|
||||
provider: [provider],
|
||||
})
|
||||
|
||||
expect(authProviders).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateAuthProvider", () => {
|
||||
const provider = "manual"
|
||||
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.updateAuthProvider([
|
||||
{
|
||||
provider: "does-not-exist",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'AuthProvider with provider "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authProvider", async () => {
|
||||
await service.updateAuthProvider([
|
||||
{
|
||||
provider: "manual",
|
||||
name: "test",
|
||||
},
|
||||
])
|
||||
|
||||
const [provider] = await service.listAuthProviders({
|
||||
provider: ["manual"],
|
||||
})
|
||||
expect(provider).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createAuthProvider", () => {
|
||||
it("should create a authProvider successfully", async () => {
|
||||
await service.createAuthProvider([
|
||||
{
|
||||
provider: "test",
|
||||
name: "test provider",
|
||||
},
|
||||
])
|
||||
|
||||
const [authProvider] = await service.listAuthProviders({
|
||||
provider: ["test"],
|
||||
})
|
||||
|
||||
expect(authProvider).toEqual(
|
||||
expect.objectContaining({
|
||||
provider: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { DB_URL } from "@medusajs/pricing/integration-tests/utils"
|
||||
import { IAuthenticationModuleService } from "@medusajs/types"
|
||||
import { initialize } from "../../../../src"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthenticationModuleService - AuthUser", () => {
|
||||
let service: IAuthenticationModuleService
|
||||
let testManager: SqlEntityManager
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
service = await initialize({
|
||||
database: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_PRICING_DB_SCHEMA,
|
||||
},
|
||||
})
|
||||
|
||||
await createAuthProviders(testManager)
|
||||
await createAuthUsers(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listAuthUsers", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.listAuthUsers()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.listAuthUsers({
|
||||
id: ["test-id"],
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by provider_id", async () => {
|
||||
const authUsers = await service.listAuthUsers({
|
||||
provider_id: "manual",
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountAuthUsers", () => {
|
||||
it("should list and count authUsers", async () => {
|
||||
const [authUsers, count] = await service.listAndCountAuthUsers()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount authUsers by provider_id", async () => {
|
||||
const [authUsers, count] = await service.listAndCountAuthUsers({
|
||||
provider_id: "manual",
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authUser for the given id", async () => {
|
||||
const authUser = await service.retrieveAuthUser(id)
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser with the given id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveAuthUser("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthUser with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should not return an authUser with password hash", async () => {
|
||||
const authUser = await service.retrieveAuthUser("test-id-1")
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
})
|
||||
)
|
||||
expect(authUser["password_hash"]).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveAuthUser(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('"authUserId" must be defined')
|
||||
})
|
||||
|
||||
it("should return authUser based on config select param", async () => {
|
||||
const authUser = await service.retrieveAuthUser(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUser))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should delete the authUsers given an id successfully", async () => {
|
||||
await service.deleteAuthUser([id])
|
||||
|
||||
const authUsers = await service.listAuthUsers({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(authUsers).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.updateAuthUser([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'AuthUser with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authUser", async () => {
|
||||
await service.updateAuthUser([
|
||||
{
|
||||
id,
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.listAuthUsers({ id: [id] })
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createAuthUser", () => {
|
||||
it("should create a authUser successfully", async () => {
|
||||
await service.createAuthUser([
|
||||
{
|
||||
id: "test",
|
||||
provider_id: "manual",
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser, count] = await service.listAndCountAuthUsers({
|
||||
id: ["test"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(authUser[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user