-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathindex.ts
144 lines (127 loc) · 3.86 KB
/
index.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
import assert from 'assert'
import { MockASE } from '../mock-ase'
import { parseCookies } from '../utils'
import { WalletAddress, PendingGrant } from '@interledger/open-payments'
import { AdminActions, createAdminActions } from './admin'
import { OpenPaymentsActions, createOpenPaymentsActions } from './open-payments'
export interface TestActionsDeps {
sendingASE: MockASE
receivingASE: MockASE
}
export interface TestActions {
consentInteraction(
outgoingPaymentGrant: PendingGrant,
senderWalletAddress: WalletAddress
): Promise<void>
consentInteractionWithInteractRef(
outgoingPaymentGrant: PendingGrant,
senderWalletAddress: WalletAddress
): Promise<string>
admin: AdminActions
openPayments: OpenPaymentsActions
}
export function createTestActions(deps: TestActionsDeps): TestActions {
return {
consentInteraction: (outgoingPaymentGrant, senderWalletAddress) =>
consentInteraction(deps, outgoingPaymentGrant, senderWalletAddress),
consentInteractionWithInteractRef: (
outgoingPaymentGrant,
senderWalletAddress
) =>
consentInteractionWithInteractRef(
deps,
outgoingPaymentGrant,
senderWalletAddress
),
admin: createAdminActions(deps),
openPayments: createOpenPaymentsActions(deps)
}
}
async function consentInteraction(
deps: TestActionsDeps,
outgoingPaymentGrant: PendingGrant,
senderWalletAddress: WalletAddress
) {
const { idpSecret } = deps.sendingASE.config
const { interactId, nonce, cookie } = await _startAndAcceptInteraction(
deps,
outgoingPaymentGrant,
senderWalletAddress,
idpSecret
)
// Finish interacton
const finishResponse = await fetch(
`${senderWalletAddress.authServer}/interact/${interactId}/${nonce}/finish`,
{
method: 'GET',
headers: {
'x-idp-secret': idpSecret,
cookie
}
}
)
expect(finishResponse.status).toBe(202)
}
async function consentInteractionWithInteractRef(
deps: TestActionsDeps,
outgoingPaymentGrant: PendingGrant,
senderWalletAddress: WalletAddress
): Promise<string> {
const { idpSecret } = deps.sendingASE.config
const { interactId, nonce, cookie } = await _startAndAcceptInteraction(
deps,
outgoingPaymentGrant,
senderWalletAddress,
idpSecret
)
// Finish interacton
const finishResponse = await fetch(
`${senderWalletAddress.authServer}/interact/${interactId}/${nonce}/finish`,
{
method: 'GET',
headers: {
'x-idp-secret': idpSecret,
cookie
},
redirect: 'manual' // dont follow redirects
}
)
expect(finishResponse.status).toBe(302)
const redirectURI = finishResponse.headers.get('location')
assert(redirectURI)
const url = new URL(redirectURI)
const interact_ref = url.searchParams.get('interact_ref')
assert(interact_ref)
return interact_ref
}
async function _startAndAcceptInteraction(
deps: TestActionsDeps,
outgoingPaymentGrant: PendingGrant,
senderWalletAddress: WalletAddress,
idpSecret: string
): Promise<{ nonce: string; interactId: string; cookie: string }> {
const { redirect: startInteractionUrl } = outgoingPaymentGrant.interact
// Start interaction
const interactResponse = await fetch(startInteractionUrl, {
redirect: 'manual' // dont follow redirects
})
expect(interactResponse.status).toBe(302)
const cookie = parseCookies(interactResponse)
const nonce = outgoingPaymentGrant.interact.finish
const tokens = startInteractionUrl.split('/interact/')
const interactId = tokens[1] ? tokens[1].split('/')[0] : null
assert(interactId)
// Accept
const acceptResponse = await fetch(
`${deps.sendingASE.config.interactionServer}/grant/${interactId}/${nonce}/accept`,
{
method: 'POST',
headers: {
'x-idp-secret': idpSecret,
cookie
}
}
)
expect(acceptResponse.status).toBe(202)
return { nonce, interactId, cookie }
}