forked from sergeiliski/bankid-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
165 lines (140 loc) · 4.51 KB
/
test.js
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
const BankID = require('./index')
const chai = require("chai")
const chaiAsPromised = require("chai-as-promised")
chai.use(chaiAsPromised)
const assert = chai.assert
const config = require('./config.js')
async function assertThrowsAsync(fn, message) {
let f = () => {};
try {
await fn();
} catch(e) {
f = () => { throw e };
} finally {
assert.throws(f, message);
}
}
describe('Initiate client', function() {
it('Throws exception if no options', function() {
return assert.throws(function() {
return new BankID()
}, 'Must include options')
})
it('Throws exception if no pfx or passphrase in options', function() {
return assert.throws(function() {
return new BankID({})
}, 'Certificate and passphrase are required')
})
it('Sets url correctly', function() {
const conf = { ...config }
conf.baseUrl = conf.baseUrl.slice(0, -1)
const bankid = new BankID(conf)
assert.equal(bankid.baseUrl, config.baseUrl)
})
})
describe('Authenticate', function() {
let bankid
beforeEach(function(done) {
bankid = new BankID(config)
done()
})
it('Auth rejects if no mandatory options', async function() {
await assertThrowsAsync(async () => {
return await bankid.auth({})
}, 'User ip address is required')
})
it('Auth returns object with orderRef and autoStartToken without personal number', async function() {
const response = await bankid.auth({
endUserIp: '194.168.2.25'
})
assert.equal(response.status, 200)
assert.exists(response.data.orderRef)
assert.exists(response.data.autoStartToken)
const res = await bankid.cancel(response.data.orderRef)
assert.equal(res.status, 200)
})
it('Auth returns object with orderRef and autoStartToken', async function() {
const response = await bankid.auth({
endUserIp: '194.168.2.25',
personalNumber: '190000000000'
})
assert.equal(response.status, 200)
assert.exists(response.data.orderRef)
assert.exists(response.data.autoStartToken)
const res = await bankid.cancel(response.data.orderRef)
assert.equal(res.status, 200)
})
})
describe('Collect', function() {
let bankid
beforeEach(function(done) {
bankid = new BankID(config)
done()
})
it('Collect rejects if no orderRef', async function() {
await assertThrowsAsync(async () => {
return await bankid.collect()
}, 'Order reference value is required')
})
it('Collect returns status', async function() {
const response = await bankid.auth({
endUserIp: '194.168.2.25',
personalNumber: '190000000000'
})
const res = await bankid.collect(response.data.orderRef)
assert.equal(res.status, 200)
assert.exists(res.data.orderRef)
assert.exists(res.data.status)
assert.exists(res.data.hintCode)
await bankid.cancel(response.data.orderRef)
})
})
describe('Sign', function() {
let bankid
beforeEach(function(done) {
bankid = new BankID(config)
done()
})
it('Sign rejects if no mandatory options', async function() {
await assertThrowsAsync(async () => {
return await bankid.sign({})
}, 'User ip and visible data are required')
})
it('Sign returns object with orderRef and autoStartToken', async function() {
const response = await bankid.sign({
endUserIp: '194.168.2.25',
personalNumber: '190000000000',
userVisibleData: Buffer.from('xxxxxxxxxxx').toString('base64')
})
assert.equal(response.status, 200)
assert.exists(response.data.orderRef)
assert.exists(response.data.autoStartToken)
const res = await bankid.cancel(response.data.orderRef)
assert.equal(res.status, 200)
})
it('Sign with userNonVisibleData returns object with orderRef and autoStartToken', async function() {
const response = await bankid.sign({
endUserIp: '194.168.2.25',
personalNumber: '190000000000',
userVisibleData: Buffer.from('xxxxxxxxxxx').toString('base64'),
userNonVisibleData: Buffer.from('xxxxxxxxxxx').toString('base64')
})
assert.equal(response.status, 200)
assert.exists(response.data.orderRef)
assert.exists(response.data.autoStartToken)
const res = await bankid.cancel(response.data.orderRef)
assert.equal(res.status, 200)
})
})
describe('Cancel', function() {
let bankid
beforeEach(function(done) {
bankid = new BankID(config)
done()
})
it('Cancel rejects if no orderRef', async function() {
await assertThrowsAsync(async () => {
return await bankid.cancel()
}, 'Order reference value is required')
})
})