Skip to content

Commit

Permalink
chore(backend, auth): verify try-catches in tests (#2782)
Browse files Browse the repository at this point in the history
* feat: add logging plugin

* wip: update graphql schema

* chore: update asset resolvers

* wip: update autopeering resolvers

* feat: further simplify asset resolver

* feat: update fee resolvers

* fix: autopeering tests

* feat: update incoming payments

* fix: improve error handling on incoming payment resolver

* feat: update outgoing payment resolvers

* feat: update peer resolvers

* feat: update quote resolvers

* feat: update receiver resolvers

* feat: update wallet address resolvers

* feat: update wallet address key resolvers

* feat(wip): backend graphql logs

* feat(backend): updated logs for graphql resolvers, remove code & message from resolver responses

* update frontend types

* chore: formatting

* feat: update integration tests

* fix: rebase induced test errors

* fix: frontend wallet address update

* fix: reinstate and fix removed test

* fix: change some payment gql error codes

* feat: change liquidity response to just return success

* fix: bigints, integration query

* fix: integration tests

* fix: asset test

* fix: delete unnecessary code

* fix: test for graphqlerror instead of apolloerror

* chore: formatting

* fix: inspect error object in tests

* chore(backend, auth): verify try-catches in tests

* chore(backend): fix auto-peering test

---------

Co-authored-by: Sabine Schaller <[email protected]>
Co-authored-by: Nathan Lie <[email protected]>
  • Loading branch information
3 people authored Jun 27, 2024
1 parent 48a286b commit 00adfd4
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 442 deletions.
25 changes: 9 additions & 16 deletions packages/auth/src/graphql/resolvers/grant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,9 @@ describe('Grant Resolvers', (): void => {
})

test('Returns error for unknown grant', async (): Promise<void> => {
let gqlQuery
expect.assertions(2)
try {
gqlQuery = appContainer.apolloClient
await appContainer.apolloClient
.mutate({
mutation: gql`
query GetGrant($id: ID!) {
Expand Down Expand Up @@ -515,8 +515,6 @@ describe('Grant Resolvers', (): void => {
throw new Error('Data was empty')
}
})

await gqlQuery // throw error to inspect error object
} catch (error) {
assert.ok(error instanceof ApolloError)
expect(error.message).toBe('No grant')
Expand Down Expand Up @@ -563,13 +561,13 @@ describe('Grant Resolvers', (): void => {
})

test('Returns error if grant id is not provided', async (): Promise<void> => {
let gqlQuery
expect.assertions(2)
try {
const input: RevokeGrantInput = {
grantId: ''
}

gqlQuery = appContainer.apolloClient
await appContainer.apolloClient
.mutate({
mutation: gql`
mutation revokeGrant($input: RevokeGrantInput!) {
Expand All @@ -589,8 +587,6 @@ describe('Grant Resolvers', (): void => {
throw new Error('Data was empty')
}
})

await gqlQuery // throw error to inspect error object
} catch (error) {
assert.ok(error instanceof ApolloError)
expect(error.message).toBe('Grant id is not provided')
Expand All @@ -601,13 +597,13 @@ describe('Grant Resolvers', (): void => {
})

test('Returns error if id does not exist', async (): Promise<void> => {
let gqlQuery
expect.assertions(2)
try {
const input: RevokeGrantInput = {
grantId: uuid()
}

gqlQuery = appContainer.apolloClient
await appContainer.apolloClient
.mutate({
mutation: gql`
mutation revokeGrant($input: RevokeGrantInput!) {
Expand All @@ -627,8 +623,6 @@ describe('Grant Resolvers', (): void => {
throw new Error('Data was empty')
}
})

await gqlQuery // throw error to inspect error object
} catch (error) {
assert.ok(error instanceof ApolloError)
expect(error.message).toBe('Revoke grant was not successful')
Expand All @@ -639,13 +633,14 @@ describe('Grant Resolvers', (): void => {
})

test('Returns error if grant id is in invalid format', async (): Promise<void> => {
let gqlQuery
expect.assertions(1)

try {
const input: RevokeGrantInput = {
grantId: '123'
}

gqlQuery = appContainer.apolloClient
await appContainer.apolloClient
.mutate({
mutation: gql`
mutation revokeGrant($input: RevokeGrantInput!) {
Expand All @@ -665,8 +660,6 @@ describe('Grant Resolvers', (): void => {
throw new Error('Data was empty')
}
})

await gqlQuery // throw error to inspect error object
} catch (error) {
assert.ok(error instanceof ApolloError)
expect(error.graphQLErrors[0].extensions.code).toEqual(
Expand Down
26 changes: 16 additions & 10 deletions packages/backend/src/graphql/middleware/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from '@apollo/client'
import { ApolloError, gql } from '@apollo/client'
import assert from 'assert'
import { v4 as uuid } from 'uuid'

Expand Down Expand Up @@ -193,16 +193,22 @@ describe('GraphQL Middleware', (): void => {

createAssetSpy.mockClear()

const repeatResponse = callCreateAssetMutation({
...input,
idempotencyKey: uuid()
})

await expect(repeatResponse).rejects.toThrow(
new GraphQLError(errorToMessage[AssetError.DuplicateAsset], {
extensions: {
let error
try {
await callCreateAssetMutation({
...input,
idempotencyKey: uuid()
})
} catch (err) {
error = err
}
expect(error).toBeInstanceOf(ApolloError)
expect((error as ApolloError).graphQLErrors).toContainEqual(
expect.objectContaining({
message: errorToMessage[AssetError.DuplicateAsset],
extensions: expect.objectContaining({
code: errorToCode[AssetError.DuplicateAsset]
}
})
})
)
expect(createAssetSpy).toHaveBeenCalledTimes(1)
Expand Down
10 changes: 9 additions & 1 deletion packages/backend/src/graphql/resolvers/asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ describe('Asset Resolvers', (): void => {

test('Returns error for duplicate asset', async (): Promise<void> => {
const input = randomAsset()
await expect(assetService.create(input)).resolves.toMatchObject(input)

await assetService.create(input)

expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down Expand Up @@ -173,6 +176,7 @@ describe('Asset Resolvers', (): void => {
.spyOn(assetService, 'create')
.mockRejectedValueOnce(new Error('unexpected'))

expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down Expand Up @@ -359,6 +363,7 @@ describe('Asset Resolvers', (): void => {
})

test('Returns error for unknown asset', async (): Promise<void> => {
expect.assertions(2)
try {
await appContainer.apolloClient
.query({
Expand Down Expand Up @@ -624,6 +629,7 @@ describe('Asset Resolvers', (): void => {
)

test('Returns error for unknown asset', async (): Promise<void> => {
expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down Expand Up @@ -703,6 +709,7 @@ describe('Asset Resolvers', (): void => {
})

test('Returns error for unknown asset', async (): Promise<void> => {
expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down Expand Up @@ -746,6 +753,7 @@ describe('Asset Resolvers', (): void => {
throw new Error('unexpected')
})

expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/graphql/resolvers/auto-peering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ describe('Auto Peering Resolvers', (): void => {
.spyOn(autoPeeringService, 'initiatePeeringRequest')
.mockResolvedValueOnce(testError)
const input = createOrUpdatePeerByUrlInput()
expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down Expand Up @@ -283,6 +284,7 @@ describe('Auto Peering Resolvers', (): void => {
.spyOn(autoPeeringService, 'initiatePeeringRequest')
.mockRejectedValueOnce(new Error('unexpected'))

expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/graphql/resolvers/fee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ describe('Fee Resolvers', () => {
basisPoints: 100
}
}

expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down Expand Up @@ -141,6 +143,8 @@ describe('Fee Resolvers', () => {
basisPoints: -10_000
}
}

expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down Expand Up @@ -192,6 +196,8 @@ describe('Fee Resolvers', () => {
basisPoints: -10_000
}
}

expect.assertions(2)
try {
await appContainer.apolloClient
.mutate({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ describe('Incoming Payment Resolver', (): void => {
walletAddressId: uuid()
}

expect.assertions(3)
try {
await appContainer.apolloClient
.query({
Expand Down Expand Up @@ -231,7 +232,7 @@ describe('Incoming Payment Resolver', (): void => {
})
)
}
await expect(createSpy).toHaveBeenCalledWith(input)
expect(createSpy).toHaveBeenCalledWith(input)
})

test('Internal server error', async (): Promise<void> => {
Expand All @@ -243,6 +244,7 @@ describe('Incoming Payment Resolver', (): void => {
walletAddressId: uuid()
}

expect.assertions(3)
try {
await appContainer.apolloClient
.query({
Expand Down Expand Up @@ -275,7 +277,7 @@ describe('Incoming Payment Resolver', (): void => {
})
)
}
await expect(createSpy).toHaveBeenCalledWith(input)
expect(createSpy).toHaveBeenCalledWith(input)
})
})

Expand Down Expand Up @@ -412,6 +414,7 @@ describe('Incoming Payment Resolver', (): void => {
.spyOn(incomingPaymentService, 'get')
.mockImplementation(async () => undefined)

expect.assertions(2)
try {
await appContainer.apolloClient.query({
query: gql`
Expand Down
Loading

0 comments on commit 00adfd4

Please sign in to comment.