|
| 1 | +import { IocContract } from '@adonisjs/fold' |
| 2 | +import { initIocContainer } from '..' |
| 3 | +import { AppServices } from '../app' |
| 4 | +import { Config } from '../config/app' |
| 5 | +import { TestContainer, createTestApp } from '../tests/app' |
| 6 | +import { PaymentContext, PaymentRoutes } from './routes' |
| 7 | +import { truncateTables } from '../tests/tableManager' |
| 8 | +import { PaymentService } from './service' |
| 9 | +import { CardServiceClient, Result } from '../card-service-client/client' |
| 10 | +import { createContext } from '../tests/context' |
| 11 | +import { CardServiceClientError } from '../card-service-client/errors' |
| 12 | + |
| 13 | +describe('Payment Routes', () => { |
| 14 | + let deps: IocContract<AppServices> |
| 15 | + let appContainer: TestContainer |
| 16 | + let paymentRoutes: PaymentRoutes |
| 17 | + let paymentService: PaymentService |
| 18 | + let cardServiceClient: CardServiceClient |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + deps = initIocContainer(Config) |
| 22 | + appContainer = await createTestApp(deps) |
| 23 | + paymentService = await deps.use('paymentClient') |
| 24 | + cardServiceClient = await deps.use('cardServiceClient') |
| 25 | + paymentRoutes = await deps.use('paymentRoutes') |
| 26 | + }) |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + jest.clearAllMocks() |
| 30 | + }) |
| 31 | + |
| 32 | + afterEach(async (): Promise<void> => { |
| 33 | + await truncateTables(deps) |
| 34 | + }) |
| 35 | + |
| 36 | + afterAll(async (): Promise<void> => { |
| 37 | + await appContainer.shutdown() |
| 38 | + }) |
| 39 | + |
| 40 | + describe('payment', () => { |
| 41 | + test('returns 200 with result approved', async () => { |
| 42 | + const ctx = createPaymentContext() |
| 43 | + mockPaymentService() |
| 44 | + jest |
| 45 | + .spyOn(cardServiceClient, 'sendPayment') |
| 46 | + .mockResolvedValueOnce(Result.APPROVED) |
| 47 | + |
| 48 | + await paymentRoutes.payment(ctx) |
| 49 | + expect(ctx.response.body).toBe(Result.APPROVED) |
| 50 | + expect(ctx.status).toBe(200) |
| 51 | + }) |
| 52 | + |
| 53 | + test('returns 401 with result card_expired or invalid_signature', async () => { |
| 54 | + const ctx = createPaymentContext() |
| 55 | + mockPaymentService() |
| 56 | + jest |
| 57 | + .spyOn(cardServiceClient, 'sendPayment') |
| 58 | + .mockResolvedValueOnce(Result.CARD_EXPIRED) |
| 59 | + |
| 60 | + await paymentRoutes.payment(ctx) |
| 61 | + expect(ctx.response.body).toBe(Result.CARD_EXPIRED) |
| 62 | + expect(ctx.status).toBe(401) |
| 63 | + }) |
| 64 | + |
| 65 | + test('returns cardService error code when thrown', async () => { |
| 66 | + const ctx = createPaymentContext() |
| 67 | + mockPaymentService() |
| 68 | + jest |
| 69 | + .spyOn(cardServiceClient, 'sendPayment') |
| 70 | + .mockRejectedValue(new CardServiceClientError('Some error', 404)) |
| 71 | + await paymentRoutes.payment(ctx) |
| 72 | + expect(ctx.response.body).toBe('Some error') |
| 73 | + expect(ctx.status).toBe(404) |
| 74 | + }) |
| 75 | + |
| 76 | + test('returns 400 when there is a paymentService error', async () => { |
| 77 | + const ctx = createPaymentContext() |
| 78 | + jest |
| 79 | + .spyOn(paymentService, 'getWalletAddress') |
| 80 | + .mockRejectedValueOnce(new Error('Wallet address error')) |
| 81 | + await paymentRoutes.payment(ctx) |
| 82 | + expect(ctx.response.body).toBe('Wallet address error') |
| 83 | + expect(ctx.status).toBe(400) |
| 84 | + }) |
| 85 | + |
| 86 | + test('returns 500 when an unknown error is thrown', async () => { |
| 87 | + const ctx = createPaymentContext() |
| 88 | + jest |
| 89 | + .spyOn(paymentService, 'getWalletAddress') |
| 90 | + .mockRejectedValueOnce('Unknown error') |
| 91 | + await paymentRoutes.payment(ctx) |
| 92 | + expect(ctx.response.body).toBe('Unknown error') |
| 93 | + expect(ctx.status).toBe(500) |
| 94 | + }) |
| 95 | + |
| 96 | + function mockPaymentService() { |
| 97 | + jest.spyOn(paymentService, 'getWalletAddress').mockResolvedValueOnce({ |
| 98 | + id: 'id', |
| 99 | + assetCode: 'USD', |
| 100 | + assetScale: 1, |
| 101 | + authServer: 'authServer', |
| 102 | + resourceServer: 'resourceServer', |
| 103 | + cardService: 'cardService' |
| 104 | + }) |
| 105 | + jest |
| 106 | + .spyOn(paymentService, 'createIncomingPayment') |
| 107 | + .mockResolvedValueOnce('incoming-payment-url') |
| 108 | + } |
| 109 | + }) |
| 110 | +}) |
| 111 | + |
| 112 | +function createPaymentContext() { |
| 113 | + return createContext<PaymentContext>({ |
| 114 | + headers: { Accept: 'application/json' }, |
| 115 | + method: 'POST', |
| 116 | + url: `/payment`, |
| 117 | + body: { |
| 118 | + card: { |
| 119 | + walletAddress: 'wallet-address', |
| 120 | + trasactionCounter: 0, |
| 121 | + expiry: new Date(new Date().getDate() + 1) |
| 122 | + }, |
| 123 | + signature: 'signature', |
| 124 | + value: 100, |
| 125 | + merchantWalletAddress: 'merchant-wallet-address' |
| 126 | + } |
| 127 | + }) |
| 128 | +} |
0 commit comments