-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathservice.test.ts
174 lines (159 loc) · 5.46 KB
/
service.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import assert from 'assert'
import nock, { Definition } from 'nock'
import { URL } from 'url'
import { Knex } from 'knex'
import { v4 as uuid } from 'uuid'
import { WebhookEvent } from './model'
import {
WebhookService,
generateWebhookSignature,
RETRY_BACKOFF_MS
} from './service'
import { AccountingService } from '../accounting/service'
import { createTestApp, TestContainer } from '../tests/app'
import { AccountFactory } from '../tests/accountFactory'
import { createAsset } from '../tests/asset'
import { truncateTables } from '../tests/tableManager'
import { Config } from '../config/app'
import { IocContract } from '@adonisjs/fold'
import { initIocContainer } from '../'
import { AppServices } from '../app'
describe('Webhook Service', (): void => {
let deps: IocContract<AppServices>
let appContainer: TestContainer
let webhookService: WebhookService
let accountingService: AccountingService
let knex: Knex
let webhookUrl: URL
let event: WebhookEvent
const WEBHOOK_SECRET = 'test secret'
async function makeWithdrawalEvent(event: WebhookEvent): Promise<void> {
const accountFactory = new AccountFactory(accountingService)
const asset = await createAsset(deps)
const amount = BigInt(10)
const account = await accountFactory.build({
asset,
balance: amount
})
await event.$query(knex).patch({
withdrawal: {
accountId: account.id,
assetId: account.asset.id,
amount
}
})
}
beforeAll(async (): Promise<void> => {
Config.signatureSecret = WEBHOOK_SECRET
deps = await initIocContainer(Config)
appContainer = await createTestApp(deps)
knex = appContainer.knex
webhookService = await deps.use('webhookService')
accountingService = await deps.use('accountingService')
webhookUrl = new URL(Config.webhookUrl)
})
beforeEach(async (): Promise<void> => {
event = await WebhookEvent.query(knex).insertAndFetch({
id: uuid(),
type: 'account.test_event',
data: {
account: {
id: uuid()
}
}
})
})
afterEach(async (): Promise<void> => {
await truncateTables(knex)
})
afterAll(async (): Promise<void> => {
await appContainer.shutdown()
})
describe('Get Webhook Event', (): void => {
test('A webhook event can be fetched', async (): Promise<void> => {
await expect(webhookService.getEvent(event.id)).resolves.toEqual(event)
})
test('A withdrawal webhook event can be fetched', async (): Promise<void> => {
await makeWithdrawalEvent(event)
assert.ok(event.withdrawal)
await expect(webhookService.getEvent(event.id)).resolves.toEqual(event)
})
test('Cannot fetch a bogus webhook event', async (): Promise<void> => {
await expect(webhookService.getEvent(uuid())).resolves.toBeUndefined()
})
})
describe.skip('processNext', (): void => {
function mockWebhookServer(status = 200): nock.Scope {
return nock(webhookUrl.origin)
.post(webhookUrl.pathname, function (this: Definition, body) {
assert.ok(this.headers)
const signature = this.headers['rafiki-signature']
expect(
generateWebhookSignature(
body,
WEBHOOK_SECRET,
Config.signatureVersion
)
).toEqual(signature)
expect(body).toMatchObject({
id: event.id,
type: event.type,
data: event.data
})
return true
})
.reply(status)
}
test('Does not process events not scheduled to be sent', async (): Promise<void> => {
await event.$query(knex).patch({
processAt: new Date(Date.now() + 30_000)
})
await expect(webhookService.getEvent(event.id)).resolves.toEqual(event)
await expect(webhookService.processNext()).resolves.toBeUndefined()
})
test('Sends webhook event', async (): Promise<void> => {
const scope = mockWebhookServer()
await expect(webhookService.processNext()).resolves.toEqual(event.id)
scope.done()
await expect(webhookService.getEvent(event.id)).resolves.toMatchObject({
attempts: 1,
statusCode: 200,
processAt: null
})
})
test.each([[201], [400], [504]])(
'Schedules retry if request fails (%i)',
async (status): Promise<void> => {
const scope = mockWebhookServer(status)
await expect(webhookService.processNext()).resolves.toEqual(event.id)
scope.done()
const updatedEvent = await webhookService.getEvent(event.id)
assert.ok(updatedEvent?.processAt)
expect(updatedEvent).toMatchObject({
attempts: 1,
statusCode: status
})
expect(updatedEvent.processAt.getTime()).toBeGreaterThanOrEqual(
event.createdAt.getTime() + RETRY_BACKOFF_MS
)
}
)
test('Schedules retry if request times out', async (): Promise<void> => {
const scope = nock(webhookUrl.origin)
.post(webhookUrl.pathname)
.delayConnection(Config.webhookTimeout + 1)
.reply(200)
await expect(webhookService.processNext()).resolves.toEqual(event.id)
scope.done()
const updatedEvent = await webhookService.getEvent(event.id)
assert.ok(updatedEvent?.processAt)
expect(updatedEvent).toMatchObject({
attempts: 1,
statusCode: null
})
expect(updatedEvent.processAt.getTime()).toBeGreaterThanOrEqual(
event.createdAt.getTime() + RETRY_BACKOFF_MS
)
})
})
})