Skip to content

Commit

Permalink
fix(auth): improve tenant tests, cleanup tenant get response,
Browse files Browse the repository at this point in the history
  • Loading branch information
BlairCurrey committed Dec 3, 2024
1 parent 7cc448d commit d2b6411
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 10 deletions.
51 changes: 50 additions & 1 deletion packages/auth/src/tenant/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
UpdateContext,
DeleteContext,
TenantRoutes,
createTenantRoutes
createTenantRoutes,
GetContext
} from './routes'
import { TenantService } from './service'
import { Tenant } from './model'
Expand Down Expand Up @@ -43,6 +44,54 @@ describe('Tenant Routes', (): void => {
await appContainer.shutdown()
})

describe('get', (): void => {
test('Gets a tenant', async (): Promise<void> => {
const tenant = await Tenant.query().insert({
id: v4(),
idpConsentUrl: 'https://example.com/consent',
idpSecret: 'secret123'
})

const ctx = createContext<GetContext>(
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
},
{
id: tenant.id
}
)

await expect(tenantRoutes.get(ctx)).resolves.toBeUndefined()
expect(ctx.status).toBe(200)
expect(ctx.body).toEqual({
id: tenant.id,
idpConsentUrl: tenant.idpConsentUrl,
idpSecret: tenant.idpSecret
})
})

test('Returns 404 when getting non-existent tenant', async (): Promise<void> => {
const ctx = createContext<GetContext>(
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
},
{
id: v4()
}
)

await expect(tenantRoutes.get(ctx)).resolves.toBeUndefined()
expect(ctx.status).toBe(404)
expect(ctx.body).toBeUndefined()
})
})

describe('create', (): void => {
test('Creates a tenant', async (): Promise<void> => {
const tenantData = {
Expand Down
17 changes: 16 additions & 1 deletion packages/auth/src/tenant/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ParsedUrlQuery } from 'querystring'
import { AppContext } from '../app'
import { TenantService } from './service'
import { BaseService } from '../shared/baseService'
import { Tenant } from './model'

type TenantRequest<BodyT = never, QueryT = ParsedUrlQuery> = Exclude<
AppContext['request'],
Expand Down Expand Up @@ -30,6 +31,12 @@ interface TenantParams {
id: string
}

interface TenantResponse {
id: string
idpConsentUrl: string
idpSecret: string
}

export type GetContext = TenantContext<never, TenantParams>
export type CreateContext = TenantContext<CreateTenantBody>
export type UpdateContext = TenantContext<UpdateTenantBody, TenantParams>
Expand Down Expand Up @@ -117,5 +124,13 @@ async function getTenant(
}

ctx.status = 200
ctx.body = tenant
ctx.body = toTenantResponse(tenant)
}

function toTenantResponse(tenant: Tenant): TenantResponse {
return {
id: tenant.id,
idpConsentUrl: tenant.idpConsentUrl,
idpSecret: tenant.idpSecret
}
}
31 changes: 23 additions & 8 deletions packages/auth/src/tenant/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ describe('Tenant Service', (): void => {
const tenantData = createTenantData()
const tenant = await tenantService.create(tenantData)

expect(tenant).toMatchObject({
expect(tenant).toEqual({
id: tenantData.id,
idpConsentUrl: tenantData.idpConsentUrl,
idpSecret: tenantData.idpSecret
idpSecret: tenantData.idpSecret,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
deletedAt: undefined
})
expect(tenant.deletedAt).toBe(undefined)
})

test('fails to create tenant with duplicate id', async (): Promise<void> => {
Expand All @@ -61,7 +63,14 @@ describe('Tenant Service', (): void => {
const created = await tenantService.create(tenantData)

const tenant = await tenantService.get(created.id)
expect(tenant).toMatchObject(tenantData)
expect(tenant).toEqual({
id: tenantData.id,
idpConsentUrl: tenantData.idpConsentUrl,
idpSecret: tenantData.idpSecret,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
deletedAt: null
})
})

test('returns undefined for non-existent tenant', async (): Promise<void> => {
Expand Down Expand Up @@ -90,9 +99,12 @@ describe('Tenant Service', (): void => {
}

const updated = await tenantService.update(created.id, updateData)
expect(updated).toMatchObject({
expect(updated).toEqual({
id: created.id,
...updateData
...updateData,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
deletedAt: null
})
})

Expand All @@ -105,10 +117,13 @@ describe('Tenant Service', (): void => {
}

const updated = await tenantService.update(created.id, updateData)
expect(updated).toMatchObject({
expect(updated).toEqual({
id: created.id,
idpConsentUrl: updateData.idpConsentUrl,
idpSecret: created.idpSecret
idpSecret: created.idpSecret,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
deletedAt: null
})
})

Expand Down

0 comments on commit d2b6411

Please sign in to comment.