Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
njlie committed Dec 18, 2024
1 parent 8eb71fc commit 4bd7546
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 21 deletions.
1 change: 1 addition & 0 deletions localenv/cloud-nine-wallet/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
IDP_SECRET: 2pEcn2kkCclbOHQiGNEwhJ0rucATZhrA807HTm2rNXE=
DISPLAY_NAME: Cloud Nine Wallet
DISPLAY_ICON: wallet-icon.svg
OPERATOR_TENANT_ID: 438fa74a-fa7d-4317-9ced-dde32ece1787
volumes:
- ../cloud-nine-wallet/seed.yml:/workspace/seed.yml
- ../cloud-nine-wallet/private-key.pem:/workspace/private-key.pem
Expand Down
1 change: 1 addition & 0 deletions localenv/happy-life-bank/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
IDP_SECRET: 2pEcn2kkCclbOHQiGNEwhJ0rucATZhrA807HTm2rNXE=
DISPLAY_NAME: Happy Life Bank
DISPLAY_ICON: bank-icon.svg
OPERATOR_TENANT_ID: cf5fd7d3-1eb1-4041-8e43-ba45747e9e5d
volumes:
- ../happy-life-bank/seed.yml:/workspace/seed.yml
- ../happy-life-bank/private-key.pem:/workspace/private-key.pem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const authLink = setContext((request, { headers }) => {
return {
headers: {
...headers,
signature: `t=${timestamp}, v${version}=${digest}`
signature: `t=${timestamp}, v${version}=${digest}`,
'tenant-id': process.env.OPERATOR_TENANT_ID
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class App {
/* Back-channel GNAP Routes */
// Grant Initiation
router.post<DefaultState, CreateContext>(
'/:tenantId/',
'/:tenantId',
createValidatorMiddleware<CreateContext>(openApi.authServerSpec, {
path: '/',
method: HttpMethod.POST
Expand Down
6 changes: 3 additions & 3 deletions packages/auth/src/grant/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function toOpenPaymentPendingGrant(
access_token: {
value: grant.continueToken
},
uri: `${authServerUrl}/continue/${grant.continueId}`,
uri: `${authServerUrl}/${grant.tenantId}/continue/${grant.continueId}`,
wait: waitTimeSeconds
}
}
Expand All @@ -158,7 +158,7 @@ export function toOpenPaymentsGrantContinuation(
access_token: {
value: grant.continueToken
},
uri: `${args.authServerUrl}/continue/${grant.continueId}`,
uri: `${args.authServerUrl}/${grant.tenantId}/continue/${grant.continueId}`,
wait: args.waitTimeSeconds
}
}
Expand All @@ -178,7 +178,7 @@ export function toOpenPaymentsGrant(
access_token: {
value: grant.continueToken
},
uri: `${args.authServerUrl}/continue/${grant.continueId}`
uri: `${args.authServerUrl}/${grant.tenantId}/continue/${grant.continueId}`
}
}
}
Expand Down
72 changes: 63 additions & 9 deletions packages/auth/src/grant/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ import { AccessTokenService } from '../accessToken/service'
import { generateNonce } from '../shared/utils'
import { ClientService } from '../client/service'
import { withConfigOverride } from '../tests/helpers'
import { AccessAction, AccessType } from '@interledger/open-payments'
import {
AccessAction,
AccessType,
GrantContinuation,
PendingGrant
} from '@interledger/open-payments'
import { generateBaseGrant } from '../tests/grant'
import { generateBaseInteraction } from '../tests/interaction'
import { GNAPErrorCode } from '../shared/gnapErrors'
Expand Down Expand Up @@ -73,6 +78,18 @@ const BASE_GRANT_REQUEST = {
}
}

function getGrantContinueId(continueUrl: string): string {
const continueUrlObj = new URL(continueUrl)
const pathItems = continueUrlObj.pathname.split('/')
return pathItems[pathItems.length - 1]
}

function getInteractionId(redirectUrl: string): string {
const redirectUrlObj = new URL(redirectUrl)
const pathItems = redirectUrlObj.pathname.split('/')
return pathItems[pathItems.length - 2]
}

describe('Grant Routes', (): void => {
let deps: IocContract<AppServices>
let appContainer: TestContainer
Expand Down Expand Up @@ -215,6 +232,14 @@ describe('Grant Routes', (): void => {
).resolves.toBeUndefined()
expect(ctx.response).toSatisfyApiSpec()
expect(ctx.status).toBe(200)
const createdGrant = await Grant.query().findOne({
continueId: getGrantContinueId(
(ctx.body as GrantContinuation).continue.uri
),
continueToken: (ctx.body as GrantContinuation)
.continue.access_token.value
})
assert.ok(createdGrant)
expect(ctx.body).toEqual({
access_token: {
value: expect.any(String),
Expand All @@ -224,9 +249,9 @@ describe('Grant Routes', (): void => {
},
continue: {
access_token: {
value: expect.any(String)
value: createdGrant.continueToken
},
uri: expect.any(String)
uri: `${config.authServerUrl}/${tenant.id}/continue/${createdGrant.continueId}`
}
})
}
Expand Down Expand Up @@ -270,16 +295,37 @@ describe('Grant Routes', (): void => {
await expect(grantRoutes.create(ctx)).resolves.toBeUndefined()
expect(ctx.response).toSatisfyApiSpec()
expect(ctx.status).toBe(200)
const createdGrant = await Grant.query().findOne({
continueId: getGrantContinueId(
(ctx.body as PendingGrant).continue.uri
),
continueToken: (ctx.body as PendingGrant).continue.access_token.value
})
assert.ok(createdGrant)
const createdInteraction = await Interaction.query().findOne({
nonce: (ctx.body as PendingGrant).interact.finish,
id: getInteractionId((ctx.body as PendingGrant).interact.redirect)
})
assert.ok(createdInteraction)
const expectedRedirectUrl = new URL(
config.authServerUrl +
`/interact/${createdInteraction.id}/${createdInteraction.nonce}`
)
expectedRedirectUrl.searchParams.set(
'clientName',
TEST_CLIENT_DISPLAY.name
)
expectedRedirectUrl.searchParams.set('clientUri', CLIENT)
expect(ctx.body).toEqual({
interact: {
redirect: expect.any(String),
finish: expect.any(String)
redirect: expectedRedirectUrl.toString(),
finish: createdInteraction.nonce
},
continue: {
access_token: {
value: expect.any(String)
value: createdGrant.continueToken
},
uri: expect.any(String),
uri: `${config.authServerUrl}/${tenant.id}/continue/${createdGrant.continueId}`,
wait: Config.waitTimeSeconds
}
})
Expand Down Expand Up @@ -563,6 +609,14 @@ describe('Grant Routes', (): void => {
assert.ok(accessToken)

expect(ctx.status).toBe(200)
const createdGrant = await Grant.query().findOne({
continueId: getGrantContinueId(
(ctx.body as GrantContinuation).continue.uri
),
continueToken: (ctx.body as GrantContinuation).continue.access_token
.value
})
assert.ok(createdGrant)
expect(ctx.body).toEqual({
access_token: {
value: accessToken.value,
Expand All @@ -578,9 +632,9 @@ describe('Grant Routes', (): void => {
},
continue: {
access_token: {
value: expect.any(String)
value: createdGrant.continueToken
},
uri: expect.any(String)
uri: `${config.authServerUrl}/${tenant.id}/continue/${createdGrant.continueId}`
}
})
})
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ export function initIocContainer(
})
container.singleton('remoteIncomingPaymentService', async (deps) => {
return await createRemoteIncomingPaymentService({
config: await deps.use('config'),
logger: await deps.use('logger'),
knex: await deps.use('knex'),
grantService: await deps.use('grantService'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function getIncomingPaymentPublic(
}

ctx.body = incomingPayment.toPublicOpenPaymentsType(
deps.config.authServerGrantUrl
`${deps.config.authServerGrantUrl}/${deps.config.operatorTenantId}` // TODO: update this when tenanted incoming payments added
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { BaseService } from '../../../shared/baseService'
import { Amount, serializeAmount } from '../../amount'
import { RemoteIncomingPaymentError } from './errors'
import { isGrantError } from '../../grant/errors'
import { IAppConfig } from '../../../config/app'

interface CreateRemoteIncomingPaymentArgs {
walletAddressUrl: string
Expand All @@ -35,6 +36,7 @@ export interface RemoteIncomingPaymentService {
}

interface ServiceDependencies extends BaseService {
config: IAppConfig
grantService: GrantService
openPaymentsUrl: string
openPaymentsClient: AuthenticatedClient
Expand Down Expand Up @@ -102,7 +104,7 @@ async function createIncomingPayment(
walletAddress.resourceServer ?? new URL(walletAddress.id).origin

const grantOptions = {
authServer: walletAddress.authServer,
authServer: `${walletAddress.authServer}/cf5fd7d3-1eb1-4041-8e43-ba45747e9e5d`, // TODO: update with wallet address tenant id when tenanted wallet addresses are in
accessType: AccessType.IncomingPayment,
accessActions: [AccessAction.Create, AccessAction.ReadAll]
}
Expand Down
10 changes: 5 additions & 5 deletions test/integration/lib/test-actions/open-payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ async function grantRequestIncomingPayment(
deps: OpenPaymentsActionsDeps,
receiverWalletAddress: WalletAddress
): Promise<Grant> {
const { sendingASE } = deps
const { sendingASE, receivingASE } = deps

const grant = await sendingASE.opClient.grant.request(
{
url: receiverWalletAddress.authServer
url: `${receiverWalletAddress.authServer}/${receivingASE.config.operatorTenantId}`
},
{
access_token: {
Expand Down Expand Up @@ -185,7 +185,7 @@ async function grantRequestQuote(
const { sendingASE } = deps
const grant = await sendingASE.opClient.grant.request(
{
url: senderWalletAddress.authServer
url: `${senderWalletAddress.authServer}/${sendingASE.config.operatorTenantId}`
},
{
access_token: {
Expand Down Expand Up @@ -232,10 +232,10 @@ async function grantRequestOutgoingPayment(
limits: GrantRequestPaymentLimits,
finish?: InteractFinish
): Promise<PendingGrant> {
const { receivingASE } = deps
const { receivingASE, sendingASE } = deps
const grant = await receivingASE.opClient.grant.request(
{
url: senderWalletAddress.authServer
url: `${senderWalletAddress.authServer}/${sendingASE.config.operatorTenantId}`
},
{
access_token: {
Expand Down

0 comments on commit 4bd7546

Please sign in to comment.