Skip to content

Commit

Permalink
feat(auth): fix tests broken by multi-tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
njlie committed Oct 10, 2024
1 parent 7bcb13a commit a77ea0b
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 71 deletions.
11 changes: 9 additions & 2 deletions packages/auth/src/access/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IncomingPaymentRequest, OutgoingPaymentRequest } from './types'
import { generateNonce, generateToken } from '../shared/utils'
import { AccessType, AccessAction } from '@interledger/open-payments'
import { Access } from './model'
import { Tenant } from '../tenants/model'

describe('Access Service', (): void => {
let deps: IocContract<AppServices>
Expand All @@ -22,7 +23,8 @@ describe('Access Service', (): void => {
let trx: Knex.Transaction
let grant: Grant

const generateBaseGrant = () => ({
const generateBaseGrant = (tenantId: string) => ({
tenantId,
state: GrantState.Pending,
startMethod: [StartMethod.Redirect],
continueToken: generateToken(),
Expand All @@ -34,7 +36,12 @@ describe('Access Service', (): void => {
})

beforeEach(async (): Promise<void> => {
grant = await Grant.query(trx).insertAndFetch(generateBaseGrant())
const tenant = await Tenant.query(trx).insertAndFetch({
id: v4(),
idpConsentEndpoint: faker.internet.url(),
idpSecret: 'test-secret'
})
grant = await Grant.query(trx).insertAndFetch(generateBaseGrant(tenant.id))
})

beforeAll(async (): Promise<void> => {
Expand Down
11 changes: 11 additions & 0 deletions packages/auth/src/access/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { createTestApp, TestContainer } from '../tests/app'
import { truncateTables } from '../tests/tableManager'
import { generateToken, generateNonce } from '../shared/utils'
import { compareRequestAndGrantAccessItems } from './utils'
import { Tenant } from '../tenants/model'

describe('Access utilities', (): void => {
let deps: IocContract<AppServices>
Expand All @@ -25,6 +26,7 @@ describe('Access utilities', (): void => {
let identifier: string
let grant: Grant
let grantAccessItem: Access
let tenantId: string

const receiver: string =
'https://wallet.com/alice/incoming-payments/12341234-1234-1234-1234-123412341234'
Expand All @@ -35,8 +37,16 @@ describe('Access utilities', (): void => {
})

beforeEach(async (): Promise<void> => {
tenantId = (
await Tenant.query(trx).insertAndFetch({
id: v4(),
idpConsentEndpoint: faker.internet.url(),
idpSecret: 'test-secret'
})
).id
identifier = `https://example.com/${v4()}`
grant = await Grant.query(trx).insertAndFetch({
tenantId,
state: GrantState.Processing,
startMethod: [StartMethod.Redirect],
continueToken: generateToken(),
Expand Down Expand Up @@ -174,6 +184,7 @@ describe('Access utilities', (): void => {
}

const grant = await Grant.query(trx).insertAndFetch({
tenantId,
state: GrantState.Processing,
startMethod: [StartMethod.Redirect],
continueToken: generateToken(),
Expand Down
27 changes: 24 additions & 3 deletions packages/auth/src/accessToken/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { GrantService } from '../grant/service'
import { AccessTokenService } from './service'
import { GNAPErrorCode } from '../shared/gnapErrors'
import { Tenant } from '../tenants/model'

describe('Access Token Routes', (): void => {
let deps: IocContract<AppServices>
Expand All @@ -32,6 +33,7 @@ describe('Access Token Routes', (): void => {
let accessTokenRoutes: AccessTokenRoutes
let accessTokenService: AccessTokenService
let grantService: GrantService
let tenantId: string

beforeAll(async (): Promise<void> => {
deps = initIocContainer(Config)
Expand All @@ -43,6 +45,16 @@ describe('Access Token Routes', (): void => {
jestOpenAPI(openApi.authServerSpec)
})

beforeEach(async (): Promise<void> => {
tenantId = (
await Tenant.query(trx).insertAndFetch({
id: v4(),
idpConsentEndpoint: faker.internet.url(),
idpSecret: 'test-secret'
})
).id
})

afterEach(async (): Promise<void> => {
jest.useRealTimers()
await truncateTables(appContainer.knex)
Expand Down Expand Up @@ -96,7 +108,10 @@ describe('Access Token Routes', (): void => {
const method = 'POST'

beforeEach(async (): Promise<void> => {
grant = await Grant.query(trx).insertAndFetch(BASE_GRANT)
grant = await Grant.query(trx).insertAndFetch({
tenantId,
...BASE_GRANT
})
access = await Access.query(trx).insertAndFetch({
grantId: grant.id,
...BASE_ACCESS
Expand Down Expand Up @@ -367,7 +382,10 @@ describe('Access Token Routes', (): void => {
let token: AccessToken

beforeEach(async (): Promise<void> => {
grant = await Grant.query(trx).insertAndFetch(BASE_GRANT)
grant = await Grant.query(trx).insertAndFetch({
tenantId,
...BASE_GRANT
})
token = await AccessToken.query(trx).insertAndFetch({
grantId: grant.id,
...BASE_TOKEN
Expand Down Expand Up @@ -406,7 +424,10 @@ describe('Access Token Routes', (): void => {
let token: AccessToken

beforeEach(async (): Promise<void> => {
grant = await Grant.query(trx).insertAndFetch(BASE_GRANT)
grant = await Grant.query(trx).insertAndFetch({
tenantId,
...BASE_GRANT
})
access = await Access.query(trx).insertAndFetch({
grantId: grant.id,
...BASE_ACCESS
Expand Down
19 changes: 17 additions & 2 deletions packages/auth/src/accessToken/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,32 @@ import {
AccessItem
} from '@interledger/open-payments'
import { generateBaseGrant } from '../tests/grant'
import { Tenant } from '../tenants/model'
import { faker } from '@faker-js/faker'

describe('Access Token Service', (): void => {
let deps: IocContract<AppServices>
let appContainer: TestContainer
let trx: Knex.Transaction
let accessTokenService: AccessTokenService
let tenantId: string

beforeAll(async (): Promise<void> => {
deps = initIocContainer(Config)
appContainer = await createTestApp(deps)
accessTokenService = await deps.use('accessTokenService')
})

beforeEach(async (): Promise<void> => {
tenantId = (
await Tenant.query(trx).insertAndFetch({
id: v4(),
idpConsentEndpoint: faker.internet.url(),
idpSecret: 'test-secret'
})
).id
})

afterEach(async (): Promise<void> => {
jest.useRealTimers()
await truncateTables(appContainer.knex)
Expand Down Expand Up @@ -64,7 +77,7 @@ describe('Access Token Service', (): void => {
let grant: Grant
beforeEach(async (): Promise<void> => {
grant = await Grant.query(trx).insertAndFetch(
generateBaseGrant({ state: GrantState.Approved })
generateBaseGrant({ tenantId, state: GrantState.Approved })
)
grant.access = [
await Access.query(trx).insertAndFetch({
Expand Down Expand Up @@ -187,7 +200,7 @@ describe('Access Token Service', (): void => {

test('Introspection only returns requested access', async (): Promise<void> => {
const grantWithTwoAccesses = await Grant.query(trx).insertAndFetch(
generateBaseGrant({ state: GrantState.Approved })
generateBaseGrant({ tenantId, state: GrantState.Approved })
)
grantWithTwoAccesses.access = [
await Access.query(trx).insertAndFetch({
Expand Down Expand Up @@ -252,6 +265,7 @@ describe('Access Token Service', (): void => {
beforeEach(async (): Promise<void> => {
grant = await Grant.query(trx).insertAndFetch(
generateBaseGrant({
tenantId,
state: GrantState.Finalized,
finalizationReason: GrantFinalization.Issued
})
Expand Down Expand Up @@ -354,6 +368,7 @@ describe('Access Token Service', (): void => {
beforeEach(async (): Promise<void> => {
grant = await Grant.query(trx).insertAndFetch(
generateBaseGrant({
tenantId,
state: GrantState.Finalized,
finalizationReason: GrantFinalization.Issued
})
Expand Down
31 changes: 25 additions & 6 deletions packages/auth/src/grant/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { AccessAction, AccessType } from '@interledger/open-payments'
import { generateBaseGrant } from '../tests/grant'
import { generateBaseInteraction } from '../tests/interaction'
import { GNAPErrorCode } from '../shared/gnapErrors'
import { Tenant } from '../tenants/model'

export const TEST_CLIENT_DISPLAY = {
name: 'Test Client',
Expand Down Expand Up @@ -80,10 +81,18 @@ describe('Grant Routes', (): void => {
let clientService: ClientService
let interactionService: InteractionService

let tenantId: string
let grant: Grant

beforeEach(async (): Promise<void> => {
grant = await Grant.query().insert(generateBaseGrant())
tenantId = (
await Tenant.query().insertAndFetch({
id: v4(),
idpConsentEndpoint: faker.internet.url(),
idpSecret: 'test-secret'
})
).id
grant = await Grant.query().insert(generateBaseGrant({ tenantId }))

await Access.query().insert({
...BASE_GRANT_ACCESS,
Expand Down Expand Up @@ -173,7 +182,7 @@ describe('Grant Routes', (): void => {
url,
method
},
{}
{ tenantId }
)
const body = {
access_token: {
Expand Down Expand Up @@ -252,7 +261,7 @@ describe('Grant Routes', (): void => {
url,
method
},
{}
{ tenantId }
)

ctx.request.body = BASE_GRANT_REQUEST
Expand Down Expand Up @@ -291,7 +300,7 @@ describe('Grant Routes', (): void => {
url,
method
},
{}
{ tenantId }
)
const body = {
access_token: {
Expand Down Expand Up @@ -327,7 +336,7 @@ describe('Grant Routes', (): void => {
url,
method
},
{}
{ tenantId }
)

ctx.request.body = { ...BASE_GRANT_REQUEST, interact: undefined }
Expand Down Expand Up @@ -443,6 +452,7 @@ describe('Grant Routes', (): void => {
beforeEach(async (): Promise<void> => {
grant = await Grant.query().insert(
generateBaseGrant({
tenantId,
state: GrantState.Approved
})
)
Expand Down Expand Up @@ -545,6 +555,7 @@ describe('Grant Routes', (): void => {
test('Cannot issue access token if grant has not been granted', async (): Promise<void> => {
const grant = await Grant.query().insert(
generateBaseGrant({
tenantId,
state: GrantState.Pending
})
)
Expand Down Expand Up @@ -589,6 +600,7 @@ describe('Grant Routes', (): void => {
test('Cannot issue access token if grant has been revoked', async (): Promise<void> => {
const grant = await Grant.query().insert(
generateBaseGrant({
tenantId,
state: GrantState.Finalized,
finalizationReason: GrantFinalization.Revoked
})
Expand Down Expand Up @@ -699,7 +711,9 @@ describe('Grant Routes', (): void => {
})

test('Honors wait value when continuing too early', async (): Promise<void> => {
const grantWithWait = await Grant.query().insert(generateBaseGrant())
const grantWithWait = await Grant.query().insert(
generateBaseGrant({ tenantId })
)

await Access.query().insert({
...BASE_GRANT_ACCESS,
Expand Down Expand Up @@ -746,6 +760,7 @@ describe('Grant Routes', (): void => {
async ({ state }): Promise<void> => {
const polledGrant = await Grant.query().insert(
generateBaseGrant({
tenantId,
state,
noFinishMethod: true
})
Expand Down Expand Up @@ -842,6 +857,7 @@ describe('Grant Routes', (): void => {
test('Cannot poll a finalized grant', async (): Promise<void> => {
const finalizedPolledGrant = await Grant.query().insert(
generateBaseGrant({
tenantId,
state: GrantState.Finalized,
noFinishMethod: true
})
Expand Down Expand Up @@ -903,6 +919,7 @@ describe('Grant Routes', (): void => {
test('Cannot poll a grant faster than its wait method', async (): Promise<void> => {
const polledGrant = await Grant.query().insert(
generateBaseGrant({
tenantId,
noFinishMethod: true
})
)
Expand Down Expand Up @@ -963,6 +980,7 @@ describe('Grant Routes', (): void => {
test('Can revoke an existing grant', async (): Promise<void> => {
const grant = await Grant.query().insert(
generateBaseGrant({
tenantId,
state: GrantState.Finalized,
finalizationReason: GrantFinalization.Issued
})
Expand All @@ -987,6 +1005,7 @@ describe('Grant Routes', (): void => {
test('Cannot revoke an already revoked grant', async (): Promise<void> => {
const grant = await Grant.query().insert(
generateBaseGrant({
tenantId,
state: GrantState.Finalized,
finalizationReason: GrantFinalization.Revoked
})
Expand Down
Loading

0 comments on commit a77ea0b

Please sign in to comment.