Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authentication, types): Create authentication entities and services #5979

Merged
merged 23 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
30d2608
create authentication entities
pKorsholm Jan 2, 2024
909c788
ensure hashes are not returned
pKorsholm Jan 2, 2024
5ddae7e
re-add integration test argument
pKorsholm Jan 2, 2024
3f1e669
Merge branch 'develop' into feat/add-authentication-entities
pKorsholm Jan 2, 2024
94d805e
update filterableprops
pKorsholm Jan 2, 2024
1e423d3
update type
pKorsholm Jan 2, 2024
f6a51d4
pr feedback
pKorsholm Jan 3, 2024
71bce63
Merge branch 'develop' into feat/add-authentication-entities
pKorsholm Jan 3, 2024
c110973
update serialization
pKorsholm Jan 3, 2024
178363e
move finding existing entities and validation to service layer
pKorsholm Jan 3, 2024
431d872
Merge branch 'develop' into feat/add-authentication-entities
pKorsholm Jan 3, 2024
202fe58
update types
pKorsholm Jan 3, 2024
3b3e6ce
Merge branch 'develop' into feat/add-authentication-entities
olivermrbl Jan 4, 2024
3a13123
Merge branch 'develop' into feat/add-authentication-entities
pKorsholm Jan 4, 2024
3dee75a
Merge branch 'develop' into feat/add-authentication-entities
pKorsholm Jan 4, 2024
1b00425
update models
pKorsholm Jan 4, 2024
dde3b30
update user model
pKorsholm Jan 4, 2024
198ac80
pr feedback
pKorsholm Jan 5, 2024
4096b04
Merge branch 'develop' into feat/add-authentication-entities
pKorsholm Jan 5, 2024
ab4aec2
fix build
pKorsholm Jan 5, 2024
37bf4ae
pr feedback
pKorsholm Jan 8, 2024
3d7357b
update integration test fixtures
pKorsholm Jan 8, 2024
f6e62a9
Merge branch 'develop' into feat/add-authentication-entities
pKorsholm Jan 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { AuthProvider } from "@models"

export async function createAuthProviders(
manager: SqlEntityManager,
providerData: any[] = [
{
provider: "manual",
name: "manual",
is_active: true,
},
{
provider: "disabled",
name: "disabled",
},
{
provider: "store",
name: "store",
domain: "store",
is_active: true,
},
{
provider: "admin",
name: "admin",
domain: "admin",
is_active: true,
},
]
): Promise<AuthProvider[]> {
const authProviders: AuthProvider[] = []

for (const provider of providerData) {
const authProvider = manager.create(AuthProvider, provider)

authProviders.push(authProvider)
}

await manager.persistAndFlush(authProviders)

return authProviders
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { AuthUser } from "@models"

export async function createAuthUsers(
manager: SqlEntityManager,
userData: any[] = [
{
id: "test-id",
provider: "manual",
email: "[email protected]",
},
{
id: "test-id-1",
provider: "manual",
email: "[email protected]",
password_hash: "test",
},
{
provider: "store",
email: "[email protected]",
},
]
): Promise<AuthUser[]> {
const authUsers: AuthUser[] = []

for (const user of userData) {
const authUser = manager.create(AuthUser, user)

authUsers.push(authUser)
}

await manager.persistAndFlush(authUsers)

return authUsers
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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",
})
)
})
})
})
Loading