-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(authentication, types): Create authentication entities and servi…
…ces (#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 <[email protected]>
- Loading branch information
1 parent
6b0b3fe
commit 479a8b8
Showing
36 changed files
with
2,394 additions
and
32 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
packages/authentication/integration-tests/__fixtures__/auth-provider/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/authentication/integration-tests/__fixtures__/auth-user/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { SqlEntityManager } from "@mikro-orm/postgresql" | ||
import { AuthUser } from "@models" | ||
|
||
export async function createAuthUsers( | ||
manager: SqlEntityManager, | ||
userData: any[] = [ | ||
{ | ||
id: "test-id", | ||
provider: "manual", | ||
}, | ||
{ | ||
id: "test-id-1", | ||
provider: "manual", | ||
}, | ||
{ | ||
provider: "store", | ||
}, | ||
] | ||
): 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 | ||
} |
5 changes: 0 additions & 5 deletions
5
packages/authentication/integration-tests/__tests__/index.spec.ts
This file was deleted.
Oops, something went wrong.
270 changes: 270 additions & 0 deletions
270
packages/authentication/integration-tests/__tests__/services/auth-provider/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.