diff --git a/packages/backend/src/graphql/middleware/index.test.ts b/packages/backend/src/graphql/middleware/index.test.ts index 1c816dd1cd..1792694109 100644 --- a/packages/backend/src/graphql/middleware/index.test.ts +++ b/packages/backend/src/graphql/middleware/index.test.ts @@ -1,6 +1,7 @@ import { ApolloError, gql } from '@apollo/client' import assert from 'assert' import { v4 as uuid } from 'uuid' +import { faker } from '@faker-js/faker' import { createTestApp, TestContainer } from '../../tests/app' import { IocContract } from '@adonisjs/fold' @@ -17,6 +18,8 @@ import { } from '../generated/graphql' import { GraphQLError } from 'graphql' import { AssetError, errorToMessage, errorToCode } from '../../asset/errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('GraphQL Middleware', (): void => { let deps: IocContract @@ -29,6 +32,17 @@ describe('GraphQL Middleware', (): void => { assetService = await deps.use('assetService') }) + beforeEach(async (): Promise => { + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + }) + afterEach(async (): Promise => { await truncateTables(appContainer.knex) }) diff --git a/packages/backend/src/graphql/resolvers/asset.test.ts b/packages/backend/src/graphql/resolvers/asset.test.ts index d174e32a2b..0ae070e6a9 100644 --- a/packages/backend/src/graphql/resolvers/asset.test.ts +++ b/packages/backend/src/graphql/resolvers/asset.test.ts @@ -1,6 +1,7 @@ import { ApolloError, gql } from '@apollo/client' import assert from 'assert' import { v4 as uuid } from 'uuid' +import { faker } from '@faker-js/faker' import { getPageTests } from './page.test' import { createTestApp, TestContainer } from '../../tests/app' @@ -32,6 +33,8 @@ import { isFeeError } from '../../fee/errors' import { createFee } from '../../tests/fee' import { createAsset } from '../../tests/asset' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('Asset Resolvers', (): void => { let deps: IocContract @@ -48,6 +51,17 @@ describe('Asset Resolvers', (): void => { feeService = await deps.use('feeService') }) + beforeEach(async (): Promise => { + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + }) + afterEach(async (): Promise => { await truncateTables(appContainer.knex) }) diff --git a/packages/backend/src/graphql/resolvers/auto-peering.test.ts b/packages/backend/src/graphql/resolvers/auto-peering.test.ts index 49715fdfe0..78fca107fc 100644 --- a/packages/backend/src/graphql/resolvers/auto-peering.test.ts +++ b/packages/backend/src/graphql/resolvers/auto-peering.test.ts @@ -18,8 +18,11 @@ import { createAsset } from '../../tests/asset' import { CreateOrUpdatePeerByUrlInput } from '../generated/graphql' import { AutoPeeringService } from '../../payment-method/ilp/auto-peering/service' import { v4 as uuid } from 'uuid' -import nock from 'nock' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' + +const nock = (global as unknown as { nock: typeof import('nock') }).nock describe('Auto Peering Resolvers', (): void => { let deps: IocContract @@ -90,6 +93,15 @@ describe('Auto Peering Resolvers', (): void => { beforeEach(async (): Promise => { asset = await createAsset(deps) + const config = await deps.use('config') + await createTenant(deps, { + email: config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) }) afterEach(async (): Promise => { diff --git a/packages/backend/src/graphql/resolvers/combined_payments.test.ts b/packages/backend/src/graphql/resolvers/combined_payments.test.ts index 50af7a5d4c..610847fb69 100644 --- a/packages/backend/src/graphql/resolvers/combined_payments.test.ts +++ b/packages/backend/src/graphql/resolvers/combined_payments.test.ts @@ -1,5 +1,6 @@ import { IocContract } from '@adonisjs/fold' import { gql } from '@apollo/client' +import { faker } from '@faker-js/faker' import { AppServices } from '../../app' import { createTestApp, TestContainer } from '../../tests/app' import { initIocContainer } from '../..' @@ -18,11 +19,16 @@ import { } from '../../tests/combinedPayment' import { PaymentType } from '../../open_payments/payment/combined/model' import { getPageTests } from './page.test' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' + +const nock = (global as unknown as { nock: typeof import('nock') }).nock describe('Payment', (): void => { let deps: IocContract let appContainer: TestContainer let asset: Asset + let tenantId: string beforeAll(async (): Promise => { deps = initIocContainer(Config) @@ -31,6 +37,21 @@ describe('Payment', (): void => { beforeEach(async (): Promise => { asset = await createAsset(deps) + nock(Config.kratosAdminUrl) + .get('/identities') + .query({ credentials_identifier: Config.kratosAdminEmail }) + .reply(200, [{ id: uuid(), metadata_public: {} }]) + .persist() + tenantId = ( + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + ).id }) afterEach(async (): Promise => { @@ -44,18 +65,23 @@ describe('Payment', (): void => { getPageTests({ getClient: () => appContainer.apolloClient, - createModel: () => createCombinedPayment(deps), + createModel: () => createCombinedPayment(deps, tenantId), pagedQuery: 'payments' }) test('Can get payments', async (): Promise => { - const { id: outWalletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: outWalletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const client = 'client-test' const outgoingPayment = await createOutgoingPayment(deps, { walletAddressId: outWalletAddressId, + tenantId, client: client, method: 'ilp', receiver: `${Config.openPaymentsUrl}/${uuid()}`, @@ -67,11 +93,16 @@ describe('Payment', (): void => { validDestination: false }) - const { id: inWalletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: inWalletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const incomingPayment = await createIncomingPayment(deps, { walletAddressId: inWalletAddressId, + tenantId, client: client }) @@ -145,9 +176,13 @@ describe('Payment', (): void => { }) test('Can filter payments by type and wallet address', async (): Promise => { - const { id: outWalletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: outWalletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const baseOutgoingPayment = { receiver: `${Config.openPaymentsUrl}/${uuid()}`, @@ -162,16 +197,22 @@ describe('Payment', (): void => { const client = 'client-test-type-wallet-address' const outgoingPayment = await createOutgoingPayment(deps, { walletAddressId: outWalletAddressId, + tenantId, client: client, method: 'ilp', ...baseOutgoingPayment }) - const { id: outWalletAddressId2 } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: outWalletAddressId2 } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) await createOutgoingPayment(deps, { walletAddressId: outWalletAddressId2, + tenantId, client: client, method: 'ilp', ...baseOutgoingPayment diff --git a/packages/backend/src/graphql/resolvers/fee.test.ts b/packages/backend/src/graphql/resolvers/fee.test.ts index ee7306f2ef..b6a4e2194e 100644 --- a/packages/backend/src/graphql/resolvers/fee.test.ts +++ b/packages/backend/src/graphql/resolvers/fee.test.ts @@ -1,4 +1,5 @@ import { IocContract } from '@adonisjs/fold' +import { faker } from '@faker-js/faker' import { AppServices } from '../../app' import { initIocContainer } from '../..' import { Config } from '../../config/app' @@ -13,6 +14,8 @@ import { FeeService } from '../../fee/service' import { v4 } from 'uuid' import { FeeError, errorToMessage, errorToCode } from '../../fee/errors' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('Fee Resolvers', () => { let deps: IocContract @@ -28,6 +31,15 @@ describe('Fee Resolvers', () => { beforeEach(async (): Promise => { asset = await createAsset(deps) + const config = await deps.use('config') + await createTenant(deps, { + email: config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) }) afterEach(async () => { diff --git a/packages/backend/src/graphql/resolvers/incoming_payment.test.ts b/packages/backend/src/graphql/resolvers/incoming_payment.test.ts index 61a185980c..5c19b38993 100644 --- a/packages/backend/src/graphql/resolvers/incoming_payment.test.ts +++ b/packages/backend/src/graphql/resolvers/incoming_payment.test.ts @@ -1,4 +1,5 @@ import { ApolloError, gql } from '@apollo/client' +import { faker } from '@faker-js/faker' import { getPageTests } from './page.test' import { createTestApp, TestContainer } from '../../tests/app' import { IocContract } from '@adonisjs/fold' @@ -29,6 +30,8 @@ import { } from '../../open_payments/payment/incoming/errors' import { Amount, serializeAmount } from '../../open_payments/amount' import { GraphQLErrorCode } from '../errors' +import { EndpointType } from '../../tenant/endpoints/model' +import { createTenant } from '../../tests/tenant' describe('Incoming Payment Resolver', (): void => { let deps: IocContract @@ -38,6 +41,7 @@ describe('Incoming Payment Resolver', (): void => { let incomingPaymentService: IncomingPaymentService let accountingService: AccountingService let asset: Asset + let tenantId: string beforeAll(async (): Promise => { deps = await initIocContainer(Config) @@ -47,6 +51,19 @@ describe('Incoming Payment Resolver', (): void => { asset = await createAsset(deps) }) + beforeEach(async (): Promise => { + tenantId = ( + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + ).id + }) + afterAll(async (): Promise => { await truncateTables(appContainer.knex) await appContainer.apolloClient.stop() @@ -55,8 +72,9 @@ describe('Incoming Payment Resolver', (): void => { describe('Wallet address incoming payments', (): void => { beforeEach(async (): Promise => { - walletAddressId = (await createWalletAddress(deps, { assetId: asset.id })) - .id + walletAddressId = ( + await createWalletAddress(deps, tenantId, { assetId: asset.id }) + ).id }) getPageTests({ @@ -64,6 +82,7 @@ describe('Incoming Payment Resolver', (): void => { createModel: () => createIncomingPayment(deps, { walletAddressId, + tenantId, client, incomingAmount: { value: BigInt(123), @@ -105,11 +124,16 @@ describe('Incoming Payment Resolver', (): void => { 'Successfully creates an incoming payment with $desc', async ({ metadata, expiresAt, withAmount }): Promise => { const incomingAmount = withAmount ? amount : undefined - const { id: walletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: walletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const payment = await createIncomingPayment(deps, { walletAddressId, + tenantId, client, metadata, expiresAt, @@ -122,6 +146,7 @@ describe('Incoming Payment Resolver', (): void => { const input = { walletAddressId, + tenantId, incomingAmount, expiresAt, metadata @@ -196,7 +221,8 @@ describe('Incoming Payment Resolver', (): void => { .mockResolvedValueOnce(IncomingPaymentError.UnknownWalletAddress) const input = { - walletAddressId: uuid() + walletAddressId: uuid(), + tenantId } expect.assertions(3) @@ -241,7 +267,8 @@ describe('Incoming Payment Resolver', (): void => { .mockRejectedValueOnce(new Error('unexpected')) const input = { - walletAddressId: uuid() + walletAddressId: uuid(), + tenantId } expect.assertions(3) @@ -290,6 +317,7 @@ describe('Incoming Payment Resolver', (): void => { }): Promise => { return await createIncomingPayment(deps, { ...options, + tenantId, incomingAmount: { value: BigInt(56), assetCode: asset.code, @@ -304,9 +332,13 @@ describe('Incoming Payment Resolver', (): void => { externalRef: '202201' } beforeEach(async (): Promise => { - const { id: walletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: walletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) payment = await createPayment({ walletAddressId, metadata }) }) diff --git a/packages/backend/src/graphql/resolvers/liquidity.test.ts b/packages/backend/src/graphql/resolvers/liquidity.test.ts index e0968b27e1..8774208f86 100644 --- a/packages/backend/src/graphql/resolvers/liquidity.test.ts +++ b/packages/backend/src/graphql/resolvers/liquidity.test.ts @@ -2,6 +2,7 @@ import assert from 'assert' import { ApolloError, gql } from '@apollo/client' import { Knex } from 'knex' import { v4 as uuid } from 'uuid' +import { faker } from '@faker-js/faker' import { DepositEventType } from './liquidity' import { createTestApp, TestContainer } from '../../tests/app' @@ -44,12 +45,15 @@ import { WalletAddressWithdrawalMutationResponse } from '../generated/graphql' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('Liquidity Resolvers', (): void => { let deps: IocContract let appContainer: TestContainer let accountingService: AccountingService let knex: Knex + let tenantId: string const timeoutTwoPhase = 10 beforeAll(async (): Promise => { @@ -59,6 +63,19 @@ describe('Liquidity Resolvers', (): void => { accountingService = await deps.use('accountingService') }) + beforeEach(async (): Promise => { + tenantId = ( + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + ).id + }) + afterAll(async (): Promise => { await truncateTables(knex) await appContainer.apolloClient.stop() @@ -1014,7 +1031,7 @@ describe('Liquidity Resolvers', (): void => { const amount = BigInt(100) beforeEach(async (): Promise => { - walletAddress = await createWalletAddress(deps, { + walletAddress = await createWalletAddress(deps, tenantId, { createLiquidityAccount: true }) @@ -1747,10 +1764,11 @@ describe('Liquidity Resolvers', (): void => { let payment: OutgoingPayment beforeEach(async (): Promise => { - walletAddress = await createWalletAddress(deps) + walletAddress = await createWalletAddress(deps, tenantId) const walletAddressId = walletAddress.id incomingPayment = await createIncomingPayment(deps, { walletAddressId, + tenantId, incomingAmount: { value: BigInt(56), assetCode: walletAddress.asset.code, @@ -1760,6 +1778,7 @@ describe('Liquidity Resolvers', (): void => { }) payment = await createOutgoingPayment(deps, { walletAddressId, + tenantId, method: 'ilp', receiver: `${Config.openPaymentsUrl}/incoming-payments/${uuid()}`, debitAmount: { @@ -2157,10 +2176,11 @@ describe('Liquidity Resolvers', (): void => { let outgoingPayment: OutgoingPayment beforeEach(async (): Promise => { - walletAddress = await createWalletAddress(deps) + walletAddress = await createWalletAddress(deps, tenantId) const walletAddressId = walletAddress.id incomingPayment = await createIncomingPayment(deps, { walletAddressId, + tenantId, incomingAmount: { value: BigInt(56), assetCode: walletAddress.asset.code, @@ -2170,6 +2190,7 @@ describe('Liquidity Resolvers', (): void => { }) outgoingPayment = await createOutgoingPayment(deps, { walletAddressId, + tenantId, method: 'ilp', receiver: `${ Config.openPaymentsUrl diff --git a/packages/backend/src/graphql/resolvers/outgoing_payment.test.ts b/packages/backend/src/graphql/resolvers/outgoing_payment.test.ts index dd41c4eb89..94fb6e33f2 100644 --- a/packages/backend/src/graphql/resolvers/outgoing_payment.test.ts +++ b/packages/backend/src/graphql/resolvers/outgoing_payment.test.ts @@ -37,6 +37,8 @@ import { } from '../generated/graphql' import { faker } from '@faker-js/faker' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('OutgoingPayment Resolvers', (): void => { let deps: IocContract @@ -44,6 +46,7 @@ describe('OutgoingPayment Resolvers', (): void => { let accountingService: AccountingService let outgoingPaymentService: OutgoingPaymentService let asset: Asset + let tenantId: string beforeAll(async (): Promise => { deps = await initIocContainer(Config) @@ -54,6 +57,16 @@ describe('OutgoingPayment Resolvers', (): void => { beforeEach(async (): Promise => { asset = await createAsset(deps) + tenantId = ( + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + ).id }) afterEach(async (): Promise => { @@ -75,6 +88,7 @@ describe('OutgoingPayment Resolvers', (): void => { ): Promise => { const obj: CreateTestQuoteAndOutgoingPaymentOptions = { ...options, + tenantId, method: 'ilp', receiver: `${Config.openPaymentsUrl}/${uuid()}`, debitAmount: { @@ -98,7 +112,7 @@ describe('OutgoingPayment Resolvers', (): void => { beforeEach(async (): Promise => { walletAddressId = ( - await createWalletAddress(deps, { + await createWalletAddress(deps, tenantId, { assetId: asset.id }) ).id @@ -134,16 +148,24 @@ describe('OutgoingPayment Resolvers', (): void => { ` beforeEach(async (): Promise => { - const firstReceiverWalletAddress = await createWalletAddress(deps, { - assetId: asset.id - }) - const secondWalletAddress = await createWalletAddress(deps, { + const firstReceiverWalletAddress = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) + const secondWalletAddress = await createWalletAddress(deps, tenantId, { assetId: asset.id }) - const secondReceiverWalletAddress = await createWalletAddress(deps, { - assetId: asset.id - }) + const secondReceiverWalletAddress = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const baseOutgoingPayment = { debitAmount: { @@ -156,22 +178,26 @@ describe('OutgoingPayment Resolvers', (): void => { } const incomingPayment = await createIncomingPayment(deps, { - walletAddressId: firstReceiverWalletAddress.id + walletAddressId: firstReceiverWalletAddress.id, + tenantId }) receiver = incomingPayment.getUrl(firstReceiverWalletAddress) firstOutgoingPayment = await createOutgoingPayment(deps, { walletAddressId, + tenantId, receiver, method: 'ilp', ...baseOutgoingPayment }) const secondIncomingPayment = await createIncomingPayment(deps, { - walletAddressId: secondReceiverWalletAddress.id + walletAddressId: secondReceiverWalletAddress.id, + tenantId }) const secondReceiver = secondIncomingPayment.getUrl(secondWalletAddress) secondOutgoingPayment = await createOutgoingPayment(deps, { walletAddressId: secondWalletAddress.id, + tenantId, receiver: secondReceiver, method: 'ilp', ...baseOutgoingPayment @@ -325,9 +351,13 @@ describe('OutgoingPayment Resolvers', (): void => { it('should return grantId', async (): Promise => { const grantId = uuid() - const { id: walletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: walletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const payment = await createPayment({ walletAddressId }, grantId) @@ -364,9 +394,13 @@ describe('OutgoingPayment Resolvers', (): void => { } beforeEach(async (): Promise => { - const { id: walletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: walletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) payment = await createPayment({ walletAddressId, metadata }) }) @@ -552,9 +586,13 @@ describe('OutgoingPayment Resolvers', (): void => { } test('success (metadata)', async (): Promise => { - const { id: walletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: walletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const payment = await createPayment({ walletAddressId, metadata }) const createSpy = jest @@ -563,6 +601,7 @@ describe('OutgoingPayment Resolvers', (): void => { const input = { walletAddressId: payment.walletAddressId, + tenantId, quoteId: payment.quote.id } @@ -598,6 +637,7 @@ describe('OutgoingPayment Resolvers', (): void => { const input = { walletAddressId: uuid(), + tenantId: uuid(), quoteId: uuid() } @@ -644,6 +684,7 @@ describe('OutgoingPayment Resolvers', (): void => { const input = { walletAddressId: uuid(), + tenantId: uuid(), quoteId: uuid() } @@ -688,7 +729,7 @@ describe('OutgoingPayment Resolvers', (): void => { const mockIncomingPaymentUrl = `https://${faker.internet.domainName()}/incoming-payments/${uuid()}` test('create', async (): Promise => { - const walletAddress = await createWalletAddress(deps, { + const walletAddress = await createWalletAddress(deps, tenantId, { assetId: asset.id }) const payment = await createPayment({ walletAddressId: walletAddress.id }) @@ -839,7 +880,7 @@ describe('OutgoingPayment Resolvers', (): void => { describe('Mutation.cancelOutgoingPayment', (): void => { let payment: OutgoingPaymentModel beforeEach(async () => { - const walletAddress = await createWalletAddress(deps, { + const walletAddress = await createWalletAddress(deps, tenantId, { assetId: asset.id }) @@ -955,7 +996,7 @@ describe('OutgoingPayment Resolvers', (): void => { beforeEach(async (): Promise => { walletAddressId = ( - await createWalletAddress(deps, { + await createWalletAddress(deps, tenantId, { assetId: asset.id }) ).id diff --git a/packages/backend/src/graphql/resolvers/peer.test.ts b/packages/backend/src/graphql/resolvers/peer.test.ts index 8fc24012d2..7b8043281b 100644 --- a/packages/backend/src/graphql/resolvers/peer.test.ts +++ b/packages/backend/src/graphql/resolvers/peer.test.ts @@ -30,6 +30,8 @@ import { } from '../generated/graphql' import { AccountingService } from '../../accounting/service' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('Peer Resolvers', (): void => { let deps: IocContract @@ -65,6 +67,14 @@ describe('Peer Resolvers', (): void => { beforeEach(async (): Promise => { asset = await createAsset(deps) + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) }) afterEach(async (): Promise => { diff --git a/packages/backend/src/graphql/resolvers/quote.test.ts b/packages/backend/src/graphql/resolvers/quote.test.ts index dcb703edc6..ef2ef4dbf1 100644 --- a/packages/backend/src/graphql/resolvers/quote.test.ts +++ b/packages/backend/src/graphql/resolvers/quote.test.ts @@ -1,5 +1,6 @@ import { gql, ApolloError } from '@apollo/client' import { v4 as uuid } from 'uuid' +import { faker } from '@faker-js/faker' import { getPageTests } from './page.test' import { createTestApp, TestContainer } from '../../tests/app' @@ -22,12 +23,15 @@ import { Quote as QuoteModel } from '../../open_payments/quote/model' import { Amount } from '../../open_payments/amount' import { CreateQuoteInput, Quote, QuoteResponse } from '../generated/graphql' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('Quote Resolvers', (): void => { let deps: IocContract let appContainer: TestContainer let quoteService: QuoteService let asset: Asset + let tenantId: string const receivingWalletAddress = 'http://wallet2.example/bob' const receiver = `${receivingWalletAddress}/incoming-payments/${uuid()}` @@ -40,6 +44,16 @@ describe('Quote Resolvers', (): void => { beforeEach(async (): Promise => { asset = await createAsset(deps) + tenantId = ( + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + ).id }) afterEach(async (): Promise => { @@ -57,6 +71,7 @@ describe('Quote Resolvers', (): void => { ): Promise => { return await createQuote(deps, { walletAddressId, + tenantId, receiver, debitAmount: { value: BigInt(56), @@ -70,9 +85,13 @@ describe('Quote Resolvers', (): void => { describe('Query.quote', (): void => { test('success', async (): Promise => { - const { id: walletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: walletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const quote = await createWalletAddressQuote(walletAddressId) const query = await appContainer.apolloClient @@ -176,6 +195,7 @@ describe('Quote Resolvers', (): void => { } input = { walletAddressId: uuid(), + tenantId: uuid(), receiver, debitAmount } @@ -188,11 +208,16 @@ describe('Quote Resolvers', (): void => { ${false} | ${undefined} | ${'incoming payment'} `('$type', async ({ withAmount, receiveAmount }): Promise => { const amount = withAmount ? debitAmount : undefined - const { id: walletAddressId } = await createWalletAddress(deps, { - assetId: asset.id - }) + const { id: walletAddressId } = await createWalletAddress( + deps, + tenantId, + { + assetId: asset.id + } + ) const input = { walletAddressId, + tenantId, debitAmount: amount, receiveAmount, receiver @@ -299,7 +324,7 @@ describe('Quote Resolvers', (): void => { beforeEach(async (): Promise => { walletAddressId = ( - await createWalletAddress(deps, { + await createWalletAddress(deps, tenantId, { assetId: asset.id }) ).id diff --git a/packages/backend/src/graphql/resolvers/quote.ts b/packages/backend/src/graphql/resolvers/quote.ts index 16bd2863e1..fcd20fb237 100644 --- a/packages/backend/src/graphql/resolvers/quote.ts +++ b/packages/backend/src/graphql/resolvers/quote.ts @@ -42,6 +42,7 @@ export const createQuote: MutationResolvers['createQuote'] = const quoteService = await ctx.container.use('quoteService') const options: CreateQuoteOptions = { walletAddressId: args.input.walletAddressId, + tenantId: args.input.tenantId, receiver: args.input.receiver, method: 'ilp' } diff --git a/packages/backend/src/graphql/resolvers/walletAddressKey.test.ts b/packages/backend/src/graphql/resolvers/walletAddressKey.test.ts index 660ffbd5ba..0d1b9e2e9a 100644 --- a/packages/backend/src/graphql/resolvers/walletAddressKey.test.ts +++ b/packages/backend/src/graphql/resolvers/walletAddressKey.test.ts @@ -2,6 +2,7 @@ import assert from 'assert' import { ApolloError, gql } from '@apollo/client' import { generateJwk } from '@interledger/http-signature-utils' import { v4 as uuid } from 'uuid' +import { faker } from '@faker-js/faker' import { createTestApp, TestContainer } from '../../tests/app' import { IocContract } from '@adonisjs/fold' @@ -20,6 +21,8 @@ import { createWalletAddress } from '../../tests/walletAddress' import { getPageTests } from './page.test' import { createWalletAddressKey } from '../../tests/walletAddressKey' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' const TEST_KEY = generateJwk({ keyId: uuid() }) @@ -27,6 +30,7 @@ describe('Wallet Address Key Resolvers', (): void => { let deps: IocContract let appContainer: TestContainer let walletAddressKeyService: WalletAddressKeyService + let tenantId: string beforeAll(async (): Promise => { deps = initIocContainer(Config) @@ -34,6 +38,19 @@ describe('Wallet Address Key Resolvers', (): void => { walletAddressKeyService = await deps.use('walletAddressKeyService') }) + beforeEach(async (): Promise => { + tenantId = ( + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + ).id + }) + afterEach(async (): Promise => { await truncateTables(appContainer.knex) }) @@ -45,7 +62,7 @@ describe('Wallet Address Key Resolvers', (): void => { describe('Create Wallet Address Keys', (): void => { test('Can create wallet address key', async (): Promise => { - const walletAddress = await createWalletAddress(deps) + const walletAddress = await createWalletAddress(deps, tenantId) const input: CreateWalletAddressKeyInput = { walletAddressId: walletAddress.id, @@ -106,7 +123,7 @@ describe('Wallet Address Key Resolvers', (): void => { throw new Error('unexpected') }) - const walletAddress = await createWalletAddress(deps) + const walletAddress = await createWalletAddress(deps, tenantId) const input = { walletAddressId: walletAddress.id, @@ -164,7 +181,7 @@ describe('Wallet Address Key Resolvers', (): void => { describe('Revoke key', (): void => { test('Can revoke a key', async (): Promise => { - const walletAddress = await createWalletAddress(deps) + const walletAddress = await createWalletAddress(deps, tenantId) const key = await walletAddressKeyService.create({ walletAddressId: walletAddress.id, @@ -267,7 +284,7 @@ describe('Wallet Address Key Resolvers', (): void => { describe('List Wallet Address Keys', (): void => { let walletAddressId: string beforeEach(async (): Promise => { - walletAddressId = (await createWalletAddress(deps)).id + walletAddressId = (await createWalletAddress(deps, tenantId)).id }) getPageTests({ getClient: () => appContainer.apolloClient, diff --git a/packages/backend/src/graphql/resolvers/wallet_address.test.ts b/packages/backend/src/graphql/resolvers/wallet_address.test.ts index ff51dd0d1f..69858884a0 100644 --- a/packages/backend/src/graphql/resolvers/wallet_address.test.ts +++ b/packages/backend/src/graphql/resolvers/wallet_address.test.ts @@ -2,6 +2,7 @@ import assert from 'assert' import { gql, ApolloError } from '@apollo/client' import { Knex } from 'knex' import { v4 as uuid } from 'uuid' +import { faker } from '@faker-js/faker' import { createTestApp, TestContainer } from '../../tests/app' import { IocContract } from '@adonisjs/fold' @@ -35,12 +36,15 @@ import { import { getPageTests } from './page.test' import { WalletAddressAdditionalProperty } from '../../open_payments/wallet_address/additional_property/model' import { GraphQLErrorCode } from '../errors' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('Wallet Address Resolvers', (): void => { let deps: IocContract let appContainer: TestContainer let knex: Knex let walletAddressService: WalletAddressService + let tenantId: string beforeAll(async (): Promise => { deps = await initIocContainer(Config) @@ -49,6 +53,19 @@ describe('Wallet Address Resolvers', (): void => { walletAddressService = await deps.use('walletAddressService') }) + beforeEach(async (): Promise => { + tenantId = ( + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + ).id + }) + afterEach(async (): Promise => { await truncateTables(knex) }) @@ -66,6 +83,7 @@ describe('Wallet Address Resolvers', (): void => { asset = await createAsset(deps) input = { assetId: asset.id, + tenantId, url: 'https://alice.me/.well-known/pay' } }) @@ -309,7 +327,7 @@ describe('Wallet Address Resolvers', (): void => { let walletAddress: WalletAddressModel beforeEach(async (): Promise => { - walletAddress = await createWalletAddress(deps) + walletAddress = await createWalletAddress(deps, tenantId) }) test('Can update a wallet address', async (): Promise => { @@ -436,7 +454,7 @@ describe('Wallet Address Resolvers', (): void => { } ] } - walletAddress = await createWalletAddress(deps, createOptions) + walletAddress = await createWalletAddress(deps, tenantId, createOptions) const updateOptions = { id: walletAddress.id, @@ -497,7 +515,7 @@ describe('Wallet Address Resolvers', (): void => { } ] } - walletAddress = await createWalletAddress(deps, createOptions) + walletAddress = await createWalletAddress(deps, tenantId, createOptions) const updateOptions = { id: walletAddress.id, @@ -651,7 +669,7 @@ describe('Wallet Address Resolvers', (): void => { walletProp02.visibleInOpenPayments = false const additionalProperties = [walletProp01, walletProp02] - const walletAddress = await createWalletAddress(deps, { + const walletAddress = await createWalletAddress(deps, tenantId, { publicName, createLiquidityAccount: true, additionalProperties @@ -756,14 +774,14 @@ describe('Wallet Address Resolvers', (): void => { getPageTests({ getClient: () => appContainer.apolloClient, - createModel: () => createWalletAddress(deps), + createModel: () => createWalletAddress(deps, tenantId), pagedQuery: 'walletAddresses' }) test('Can get page of wallet addresses', async (): Promise => { const walletAddresses: WalletAddressModel[] = [] for (let i = 0; i < 2; i++) { - walletAddresses.push(await createWalletAddress(deps)) + walletAddresses.push(await createWalletAddress(deps, tenantId)) } walletAddresses.reverse() // Calling the default getPage will result in descending order const query = await appContainer.apolloClient @@ -826,7 +844,7 @@ describe('Wallet Address Resolvers', (): void => { const walletAddresses: WalletAddressModel[] = [] const withdrawalAmount = BigInt(10) for (let i = 0; i < 3; i++) { - const walletAddress = await createWalletAddress(deps, { + const walletAddress = await createWalletAddress(deps, tenantId, { createLiquidityAccount: true }) if (i) { diff --git a/packages/backend/src/graphql/resolvers/webhooks.test.ts b/packages/backend/src/graphql/resolvers/webhooks.test.ts index b5ecda5ccb..e81b3b3acf 100644 --- a/packages/backend/src/graphql/resolvers/webhooks.test.ts +++ b/packages/backend/src/graphql/resolvers/webhooks.test.ts @@ -1,4 +1,5 @@ import { gql } from '@apollo/client' +import { faker } from '@faker-js/faker' import { getPageTests } from './page.test' import { createTestApp, TestContainer } from '../../tests/app' import { IocContract } from '@adonisjs/fold' @@ -9,6 +10,8 @@ import { truncateTables } from '../../tests/tableManager' import { WebhookEventsConnection } from '../generated/graphql' import { createWebhookEvent, webhookEventTypes } from '../../tests/webhook' import { WebhookEvent } from '../../webhook/model' +import { createTenant } from '../../tests/tenant' +import { EndpointType } from '../../tenant/endpoints/model' describe('Webhook Events Query', (): void => { let deps: IocContract @@ -19,6 +22,17 @@ describe('Webhook Events Query', (): void => { appContainer = await createTestApp(deps) }) + beforeEach(async (): Promise => { + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + }) + afterEach(async (): Promise => { await truncateTables(appContainer.knex) }) diff --git a/packages/backend/src/tenant/service.test.ts b/packages/backend/src/tenant/service.test.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/packages/backend/src/tenant/service.ts b/packages/backend/src/tenant/service.ts index 5ffd3c6c98..5f3bb00215 100644 --- a/packages/backend/src/tenant/service.ts +++ b/packages/backend/src/tenant/service.ts @@ -111,7 +111,7 @@ async function createTenant( * 3.2 if error, rollback DB trx and return error */ let tenant: Tenant - const trx = await Tenant.startTransaction() + const trx = await deps.knex.transaction() const { axios } = deps try { // TODO: move into kratos service @@ -123,10 +123,6 @@ async function createTenant( const operatorRole = getIdentityResponse.data[0]?.metadata_public.operator const isExistingIdentity = getIdentityResponse.data.length > 0 && getIdentityResponse.data[0].id - deps.logger.info( - { res: getIdentityResponse.data, operatorRole, isExistingIdentity }, - 'got response' - ) if (!isExistingIdentity) { // Identity does not exist const createIdentityResponse = await axios.post( diff --git a/packages/backend/src/tests/app.ts b/packages/backend/src/tests/app.ts index 54af9d4171..5366a6d0f7 100644 --- a/packages/backend/src/tests/app.ts +++ b/packages/backend/src/tests/app.ts @@ -90,6 +90,7 @@ export const createTestApp = async ( const authLink = setContext((_, { headers }) => { return { headers: { + 'x-operator-secret': config.operatorApiSecret, ...headers } } diff --git a/packages/backend/src/webhook/service.test.ts b/packages/backend/src/webhook/service.test.ts index bb33fdec72..2666df0670 100644 --- a/packages/backend/src/webhook/service.test.ts +++ b/packages/backend/src/webhook/service.test.ts @@ -3,6 +3,7 @@ import { Definition, ReplyHeaderValue, Scope } from 'nock' import { URL } from 'url' import { Knex } from 'knex' import { v4 as uuid } from 'uuid' +import { faker } from '@faker-js/faker' import { WebhookEvent } from './model' import { @@ -31,6 +32,8 @@ import { WalletAddressEventType } from '../open_payments/wallet_address/model' import { createOutgoingPayment } from '../tests/outgoingPayment' +import { createTenant } from '../tests/tenant' +import { EndpointType } from '../tenant/endpoints/model' const nock = (global as unknown as { nock: typeof import('nock') }).nock @@ -42,6 +45,7 @@ describe('Webhook Service', (): void => { let knex: Knex let webhookUrl: URL let event: WebhookEvent + let tenantId: string const WEBHOOK_SECRET = 'test secret' async function makeWithdrawalEvent(event: WebhookEvent): Promise { @@ -72,6 +76,18 @@ describe('Webhook Service', (): void => { webhookUrl = new URL(Config.webhookUrl) }) + beforeEach(async (): Promise => { + tenantId = ( + await createTenant(deps, { + email: Config.kratosAdminEmail, + idpSecret: 'testsecret', + idpConsentEndpoint: faker.internet.url(), + endpoints: [ + { type: EndpointType.WebhookBaseUrl, value: faker.internet.url() } + ] + }) + ).id + }) afterEach(async (): Promise => { await truncateTables(knex) }) @@ -116,17 +132,19 @@ describe('Webhook Service', (): void => { let events: WebhookEvent[] = [] beforeEach(async (): Promise => { - walletAddressIn = await createWalletAddress(deps) - walletAddressOut = await createWalletAddress(deps) + walletAddressIn = await createWalletAddress(deps, tenantId) + walletAddressOut = await createWalletAddress(deps, tenantId) incomingPaymentIds = [ ( await createIncomingPayment(deps, { - walletAddressId: walletAddressIn.id + walletAddressId: walletAddressIn.id, + tenantId }) ).id, ( await createIncomingPayment(deps, { - walletAddressId: walletAddressIn.id + walletAddressId: walletAddressIn.id, + tenantId }) ).id ] @@ -135,6 +153,7 @@ describe('Webhook Service', (): void => { await createOutgoingPayment(deps, { method: 'ilp', walletAddressId: walletAddressOut.id, + tenantId, receiver: '', validDestination: false }) @@ -143,6 +162,7 @@ describe('Webhook Service', (): void => { await createOutgoingPayment(deps, { method: 'ilp', walletAddressId: walletAddressOut.id, + tenantId, receiver: '', validDestination: false })