Skip to content

Commit

Permalink
more test for gatehub module service
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianboros committed Oct 2, 2024
1 parent 3fe347a commit d85d925
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
91 changes: 87 additions & 4 deletions packages/wallet/backend/tests/gatehub/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { createApp, TestApp } from '@/tests/app'
import { Knex } from 'knex'
import { truncateTables } from '@shared/backend/tests'
import type { AuthService } from '@/auth/service'
import { mockGateHubClient } from '../mocks'
import { faker } from '@faker-js/faker'
import { AwilixContainer } from 'awilix'
import { GateHubService } from '@/gatehub/service'
import { loginUser } from '../utils'
import { User } from '@/user/model'
import { IWebhookDate } from '@/gatehub/types'

import { GateHubClient } from '@/gatehub/client'

describe('GateHub Service', (): void => {
let bindings: AwilixContainer<Cradle>
Expand All @@ -21,6 +20,17 @@ describe('GateHub Service', (): void => {
let gateHubService: GateHubService
let user: User

const mockGateHubClient = {
getIframeUrl: jest.fn(),
handleWebhook: jest.fn(),
addUserToGateway: jest.fn(),
createManagedUser: jest.fn(),
createWallet: jest.fn(),
connectUserToGateway: jest.fn(),
getWalletBalance: jest.fn(),
getUserState: jest.fn()
}

beforeAll(async (): Promise<void> => {
bindings = await createContainer(env)
appContainer = await createApp(bindings)
Expand All @@ -39,7 +49,11 @@ describe('GateHub Service', (): void => {
await knex.destroy()
})
beforeEach(async (): Promise<void> => {
Reflect.set(gateHubService, 'gateHubClient', mockGateHubClient)
Reflect.set(
gateHubService,
'gateHubClient',
mockGateHubClient as unknown as GateHubClient
)

const extraUserArgs = {
isEmailVerified: true,
Expand All @@ -56,8 +70,11 @@ describe('GateHub Service', (): void => {

describe('Get Iframe Url', () => {
it('should return Iframe Url ', async () => {
const mockedIframeUrl = 'URL'
mockGateHubClient.getIframeUrl.mockResolvedValue(mockedIframeUrl)

const result = await gateHubService.getIframeUrl('withdrawal', user.id)
expect(result).toMatch('URL')
expect(result).toMatch(mockedIframeUrl)
})

it('should return NotFound if no user found', async () => {
Expand Down Expand Up @@ -120,4 +137,70 @@ describe('GateHub Service', (): void => {
)
})
})

describe('add User To Gateway', () => {
it('should set user as KYC verified', async () => {
const mockedConnectUserToGatewayResponse = true
mockGateHubClient.connectUserToGateway.mockResolvedValue(
mockedConnectUserToGatewayResponse
)
const mockedGetUserStateResponse = {
profile: {
last_name: user.lastName,
first_name: user.firstName,
address_country_code: false,
address_street1: false,
address_street2: false,
address_city: false
}
}
mockGateHubClient.getUserState.mockResolvedValue(
mockedGetUserStateResponse
)
const result = await gateHubService.addUserToGateway(user.id)

expect(result).toBe(mockedConnectUserToGatewayResponse)

const userData = await User.query().findById(user.id)
expect(userData?.kycVerified).toBe(true)
})
it('should not set user as KYC verified as gateHubService.connectUserToGateway will return false', async () => {
const mockedConnectUserToGatewayResponse = false
mockGateHubClient.connectUserToGateway.mockResolvedValue(
mockedConnectUserToGatewayResponse
)
const mockedGetUserStateResponse = {
profile: {
last_name: user.lastName,
first_name: user.firstName,
address_country_code: false,
address_street1: false,
address_street2: false,
address_city: false
}
}
mockGateHubClient.getUserState.mockResolvedValue(
mockedGetUserStateResponse
)
const result = await gateHubService.addUserToGateway(user.id)

expect(result).toBe(mockedConnectUserToGatewayResponse)

const userData = await User.query().findById(user.id)
expect(userData?.kycVerified).toBe(false)
})
it('should return Not Found if user not found', async () => {
await expect(
gateHubService.addUserToGateway(faker.string.uuid())
).rejects.toThrowError(/Not Found/)
})
it('should return Not Found if user has no gateHubUserId field set', async () => {
await User.query().findById(user.id).patch({
gateHubUserId: ''
})
await expect(
gateHubService.addUserToGateway(user.id)
).rejects.toThrowError(/Not Found/)
})
})
})
3 changes: 1 addition & 2 deletions packages/wallet/backend/tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,5 @@ export const mockGateHubClient = {
address: faker.string.uuid()
}),
connectUserToGateway: () => {},
getWalletBalance: () => [],
getIframeUrl: () => 'URL'
getWalletBalance: () => []
}

0 comments on commit d85d925

Please sign in to comment.