Skip to content

Commit

Permalink
fix(backend): graphql resolver tests (#2984)
Browse files Browse the repository at this point in the history
* feat(backend): create operator kratos identity on backend startup

* chore: formatting

* Add migration files

* Update migration files

* Fix file name for migrations

* feat(auth): create basic tenant service and model plus graphql schema

* feat(auth): add basic tenant schema and appropriate resources like model and service

* feat(backend): add apollo client do dependencies

* feat(auth): delete tenant

* chore(auth): format

* feat(backend): create tenant service implementation

* feat(packages): make multi tenant work wip

* feat(backend): update resolvers with tenant id and finish the tenant creation

* feat(backend): small changes to schema + mapping of tenant to graphql + bruno

* fix: outgoing/quote tests

* fix: combined payments tests

* fix(backend): graphql resolver tests

* fix: mock auth admin gql call

---------

Co-authored-by: bsanduc <[email protected]>
Co-authored-by: golobitch <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent fd281f0 commit d0867fa
Show file tree
Hide file tree
Showing 19 changed files with 382 additions and 77 deletions.
12 changes: 12 additions & 0 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ export function initIocContainer(
}
)

container.singleton(
'tenantService',
async (deps: IocContract<AppServices>) => {
const [logger, knex] = await Promise.all([
deps.use('logger'),
deps.use('knex')
])

return createTenantService({ logger, knex })
}
)

container.singleton(
'accessService',
async (deps: IocContract<AppServices>) => {
Expand Down
14 changes: 14 additions & 0 deletions packages/backend/src/graphql/middleware/index.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<AppServices>
Expand All @@ -29,6 +32,17 @@ describe('GraphQL Middleware', (): void => {
assetService = await deps.use('assetService')
})

beforeEach(async (): Promise<void> => {
await createTenant(deps, {
email: Config.kratosAdminEmail,
idpSecret: 'testsecret',
idpConsentEndpoint: faker.internet.url(),
endpoints: [
{ type: EndpointType.WebhookBaseUrl, value: faker.internet.url() }
]
})
})

afterEach(async (): Promise<void> => {
await truncateTables(appContainer.knex)
})
Expand Down
14 changes: 14 additions & 0 deletions packages/backend/src/graphql/resolvers/asset.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<AppServices>
Expand All @@ -48,6 +51,17 @@ describe('Asset Resolvers', (): void => {
feeService = await deps.use('feeService')
})

beforeEach(async (): Promise<void> => {
await createTenant(deps, {
email: Config.kratosAdminEmail,
idpSecret: 'testsecret',
idpConsentEndpoint: faker.internet.url(),
endpoints: [
{ type: EndpointType.WebhookBaseUrl, value: faker.internet.url() }
]
})
})

afterEach(async (): Promise<void> => {
await truncateTables(appContainer.knex)
})
Expand Down
14 changes: 13 additions & 1 deletion packages/backend/src/graphql/resolvers/auto-peering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppServices>
Expand Down Expand Up @@ -90,6 +93,15 @@ describe('Auto Peering Resolvers', (): void => {

beforeEach(async (): Promise<void> => {
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<void> => {
Expand Down
67 changes: 54 additions & 13 deletions packages/backend/src/graphql/resolvers/combined_payments.test.ts
Original file line number Diff line number Diff line change
@@ -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 '../..'
Expand All @@ -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<AppServices>
let appContainer: TestContainer
let asset: Asset
let tenantId: string

beforeAll(async (): Promise<void> => {
deps = initIocContainer(Config)
Expand All @@ -31,6 +37,21 @@ describe('Payment', (): void => {

beforeEach(async (): Promise<void> => {
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<void> => {
Expand All @@ -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<void> => {
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()}`,
Expand All @@ -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
})

Expand Down Expand Up @@ -145,9 +176,13 @@ describe('Payment', (): void => {
})

test('Can filter payments by type and wallet address', async (): Promise<void> => {
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()}`,
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions packages/backend/src/graphql/resolvers/fee.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<AppServices>
Expand All @@ -28,6 +31,15 @@ describe('Fee Resolvers', () => {

beforeEach(async (): Promise<void> => {
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 () => {
Expand Down
Loading

0 comments on commit d0867fa

Please sign in to comment.