-
Notifications
You must be signed in to change notification settings - Fork 102
feat(auth): tenant service #3144
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
Merged
BlairCurrey
merged 7 commits into
2893/multi-tenancy-v1
from
bc/3122/auth-tenant-service
Dec 3, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
343762c
feat(auth): tenant service
BlairCurrey e524421
chore(auth): format
BlairCurrey f807bfe
Merge branch '2893/multi-tenancy-v1' into bc/3122/auth-tenant-service
BlairCurrey 292d6e3
fix(auth): jest test warning about migration
BlairCurrey bcb4ab2
fix(auth): remove temporary code
BlairCurrey 28616c3
feat(auth): soft delete tenants
BlairCurrey 4b6ce80
fix(auth): return erroneously removed tests
BlairCurrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,129 @@ | ||
| import { faker } from '@faker-js/faker' | ||
| import { createTestApp, TestContainer } from '../tests/app' | ||
| import { truncateTables } from '../tests/tableManager' | ||
| import { Config } from '../config/app' | ||
| import { IocContract } from '@adonisjs/fold' | ||
| import { initIocContainer } from '../' | ||
| import { AppServices } from '../app' | ||
| import { TenantService } from './service' | ||
|
|
||
| describe('Tenant Service', (): void => { | ||
| let deps: IocContract<AppServices> | ||
| let appContainer: TestContainer | ||
| let tenantService: TenantService | ||
|
|
||
| beforeAll(async (): Promise<void> => { | ||
| deps = initIocContainer(Config) | ||
| appContainer = await createTestApp(deps) | ||
|
|
||
| tenantService = await deps.use('tenantService') | ||
| }) | ||
|
|
||
| afterEach(async (): Promise<void> => { | ||
| await truncateTables(appContainer.knex) | ||
| }) | ||
|
|
||
| afterAll(async (): Promise<void> => { | ||
| await appContainer.shutdown() | ||
| }) | ||
|
|
||
| const createTenantData = () => ({ | ||
| id: faker.string.uuid(), | ||
| idpConsentUrl: faker.internet.url(), | ||
| idpSecret: faker.string.alphanumeric(32) | ||
| }) | ||
|
|
||
| describe('create', (): void => { | ||
| test('creates a tenant', async (): Promise<void> => { | ||
| const tenantData = createTenantData() | ||
| const tenant = await tenantService.create(tenantData) | ||
|
|
||
| expect(tenant).toMatchObject({ | ||
| id: tenantData.id, | ||
| idpConsentUrl: tenantData.idpConsentUrl, | ||
| idpSecret: tenantData.idpSecret | ||
| }) | ||
| }) | ||
|
|
||
| test('fails to create tenant with duplicate id', async (): Promise<void> => { | ||
| const tenantData = createTenantData() | ||
| await tenantService.create(tenantData) | ||
|
|
||
| await expect(tenantService.create(tenantData)).rejects.toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe('get', (): void => { | ||
| test('retrieves an existing tenant', async (): Promise<void> => { | ||
| const tenantData = createTenantData() | ||
| const created = await tenantService.create(tenantData) | ||
|
|
||
| const tenant = await tenantService.get(created.id) | ||
| expect(tenant).toMatchObject(tenantData) | ||
| }) | ||
|
|
||
| test('returns undefined for non-existent tenant', async (): Promise<void> => { | ||
| const tenant = await tenantService.get(faker.string.uuid()) | ||
| expect(tenant).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('update', (): void => { | ||
| test('updates an existing tenant', async (): Promise<void> => { | ||
| const tenantData = createTenantData() | ||
| const created = await tenantService.create(tenantData) | ||
|
|
||
| const updateData = { | ||
| idpConsentUrl: faker.internet.url(), | ||
| idpSecret: faker.string.alphanumeric(32) | ||
| } | ||
|
|
||
| const updated = await tenantService.update(created.id, updateData) | ||
| expect(updated).toMatchObject({ | ||
| id: created.id, | ||
| ...updateData | ||
| }) | ||
| }) | ||
|
|
||
| test('returns undefined for non-existent tenant', async (): Promise<void> => { | ||
| const updated = await tenantService.update(faker.string.uuid(), { | ||
| idpConsentUrl: faker.internet.url() | ||
| }) | ||
| expect(updated).toBeUndefined() | ||
| }) | ||
|
|
||
| test('can update partial fields', async (): Promise<void> => { | ||
| const tenantData = createTenantData() | ||
| const created = await tenantService.create(tenantData) | ||
|
|
||
| const updateData = { | ||
| idpConsentUrl: faker.internet.url() | ||
| } | ||
|
|
||
| const updated = await tenantService.update(created.id, updateData) | ||
| expect(updated).toMatchObject({ | ||
| id: created.id, | ||
| idpConsentUrl: updateData.idpConsentUrl, | ||
| idpSecret: created.idpSecret | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('delete', (): void => { | ||
| test('deletes an existing tenant', async (): Promise<void> => { | ||
| const tenantData = createTenantData() | ||
| const created = await tenantService.create(tenantData) | ||
|
|
||
| const result = await tenantService.delete(created.id) | ||
| expect(result).toBe(true) | ||
|
|
||
| const tenant = await tenantService.get(created.id) | ||
| expect(tenant).toBeUndefined() | ||
| }) | ||
|
|
||
| test('returns false for non-existent tenant', async (): Promise<void> => { | ||
| const result = await tenantService.delete(faker.string.uuid()) | ||
| expect(result).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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,74 @@ | ||
| import { BaseService } from '../shared/baseService' | ||
| import { TransactionOrKnex } from 'objection' | ||
| import { Tenant } from './model' | ||
|
|
||
| export interface CreateOptions { | ||
| id: string | ||
| idpConsentUrl: string | ||
| idpSecret: string | ||
| } | ||
|
|
||
| export interface TenantService { | ||
| create(input: CreateOptions): Promise<Tenant> | ||
| get(id: string): Promise<Tenant | undefined> | ||
| update( | ||
| id: string, | ||
| input: Partial<Omit<CreateOptions, 'id'>> | ||
| ): Promise<Tenant | undefined> | ||
| delete(id: string): Promise<boolean> | ||
| } | ||
|
|
||
| interface ServiceDependencies extends BaseService { | ||
| knex: TransactionOrKnex | ||
| } | ||
|
|
||
| export async function createTenantService({ | ||
| logger, | ||
| knex | ||
| }: ServiceDependencies): Promise<TenantService> { | ||
| const log = logger.child({ | ||
| service: 'TenantService' | ||
| }) | ||
| const deps: ServiceDependencies = { | ||
| logger: log, | ||
| knex | ||
| } | ||
|
|
||
| return { | ||
| create: (input: CreateOptions) => createTenant(deps, input), | ||
| get: (id: string) => getTenant(deps, id), | ||
| update: (id: string, input: Partial<Omit<CreateOptions, 'id'>>) => | ||
| updateTenant(deps, id, input), | ||
| delete: (id: string) => deleteTenant(deps, id) | ||
| } | ||
| } | ||
|
|
||
| async function createTenant( | ||
| deps: ServiceDependencies, | ||
| input: CreateOptions | ||
| ): Promise<Tenant> { | ||
| return await Tenant.query(deps.knex).insert(input) | ||
| } | ||
|
|
||
| async function getTenant( | ||
| deps: ServiceDependencies, | ||
| id: string | ||
| ): Promise<Tenant | undefined> { | ||
| return await Tenant.query(deps.knex).findById(id) | ||
| } | ||
|
|
||
| async function updateTenant( | ||
| deps: ServiceDependencies, | ||
| id: string, | ||
| input: Partial<Omit<CreateOptions, 'id'>> | ||
| ): Promise<Tenant | undefined> { | ||
| return await Tenant.query(deps.knex).patchAndFetchById(id, input) | ||
| } | ||
|
|
||
| async function deleteTenant( | ||
| deps: ServiceDependencies, | ||
| id: string | ||
| ): Promise<boolean> { | ||
| const deleted = await Tenant.query(deps.knex).deleteById(id) | ||
| return deleted > 0 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets do a soft delete here, we can already leverage the
deletedAtThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated to soft delete