-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpayment-helper.test.js
More file actions
133 lines (123 loc) · 3.08 KB
/
payment-helper.test.js
File metadata and controls
133 lines (123 loc) · 3.08 KB
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
import {
buildPaymentInfo,
getPaymentContext
} from '~/src/server/plugins/engine/routes/payment-helper.js'
import { get } from '~/src/server/services/httpService.js'
jest.mock('~/src/server/services/httpService.ts')
describe('payment helper', () => {
const uuid = '5a54c2fe-da49-4202-8cd3-2121eaca03c3'
it('should throw if no session', async () => {
const mockRequest = {
yar: {
get: jest.fn().mockReturnValueOnce(undefined)
}
}
// @ts-expect-error - partial request mock
await expect(() => getPaymentContext(mockRequest, uuid)).rejects.toThrow(
'No payment session found for uuid=5a54c2fe-da49-4202-8cd3-2121eaca03c3'
)
})
it('should throw if no payment id', async () => {
const mockRequest = {
yar: {
get: jest.fn().mockReturnValueOnce({})
}
}
// @ts-expect-error - partial request mock
await expect(() => getPaymentContext(mockRequest, uuid)).rejects.toThrow(
'No paymentId in session'
)
})
it('should get context successfully', async () => {
const mockRequest = {
yar: {
get: jest.fn().mockReturnValueOnce({
paymentId: 'payment-id',
isLivePayment: false,
formId: 'formid'
})
}
}
const getPaymentStatusApiResult = {
payment_id: 'payment-id-12345',
_links: {
next_url: {
href: 'http://next-url-href/payment'
}
},
state: {
status: 'created'
}
}
jest.mocked(get).mockResolvedValueOnce({
res: /** @type {IncomingMessage} */ ({
statusCode: 200,
headers: {}
}),
payload: getPaymentStatusApiResult,
error: undefined
})
// @ts-expect-error - partial request mock
const res = await getPaymentContext(mockRequest, uuid)
expect(res).toEqual({
paymentStatus: {
paymentId: 'payment-id-12345',
_links: {
next_url: {
href: 'http://next-url-href/payment'
}
},
state: {
status: 'created'
}
},
session: {
formId: 'formid',
isLivePayment: false,
paymentId: 'payment-id'
},
sessionKey: 'payment-5a54c2fe-da49-4202-8cd3-2121eaca03c3'
})
})
it('should create logging info for a test payment', () => {
const res = buildPaymentInfo(
'action1',
'outcome1',
'reason1',
false,
'pay-123'
)
expect(res).toEqual({
event: {
category: 'payment',
action: 'action1',
outcome: 'outcome1',
reason: 'reason1',
type: 'test',
reference: 'pay-123'
}
})
})
it('should create logging info for a live payment', () => {
const res = buildPaymentInfo(
'action2',
'outcome2',
'reason2',
true,
'pay-123'
)
expect(res).toEqual({
event: {
category: 'payment',
action: 'action2',
outcome: 'outcome2',
reason: 'reason2',
type: 'live',
reference: 'pay-123'
}
})
})
})
/**
* @import { IncomingMessage } from 'node:http'
*/