-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathauthorisationTest.js
246 lines (226 loc) · 7.67 KB
/
authorisationTest.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
'use strict'
/* eslint-env mocha */
/* eslint no-unused-expressions:0 */
import rewire from 'rewire'
import {ObjectId} from 'mongodb'
import {ChannelModel} from '../../src/model/channels'
const authorisation = rewire('../../src/middleware/authorisation')
describe('Authorisation middleware', () => {
describe('.authorise(ctx, done)', () => {
it('should allow a request if the client is authorised to use the channel by role', done => {
// Setup a channel for the mock endpoint
const channel = new ChannelModel({
name: 'Authorisation mock channel 1',
urlPattern: 'test/authorisation',
allow: ['PoC', 'Test1', 'Test2'],
routes: [
{
name: 'test route',
host: 'localhost',
port: 9876,
primary: true
}
],
updatedBy: {
id: new ObjectId(),
name: 'Test'
}
})
// Setup test data, will need authentication mechanisms to set ctx.authenticated
const ctx = {}
ctx.authenticated = {
clientID: 'Musha_OpenMRS',
domain: 'poc1.jembi.org',
name: 'OpenMRS Musha instance',
roles: ['OpenMRS_PoC', 'PoC'],
passwordHash: '',
cert: ''
}
ctx.matchingChannel = channel
ctx.request = {}
ctx.request.url = 'test/authorisation'
ctx.request.path = 'test/authorisation'
ctx.response = {}
return authorisation.authorise(ctx, () => {
ctx.authorisedChannel.should.exist
return done()
})
})
it('should deny a request if the client is NOT authorised to use the channel by role', done => {
// Setup a channel for the mock endpoint
const channel = new ChannelModel({
name: 'Authorisation mock channel 2',
urlPattern: 'test/authorisation',
allow: ['Something-else'],
routes: [
{
name: 'test route',
host: 'localhost',
port: 9876,
primary: true
}
],
updatedBy: {
id: new ObjectId(),
name: 'Test'
}
})
// Setup test data, will need authentication mechanisms to set ctx.authenticated
const ctx = {}
ctx.authenticated = {
clientID: 'Musha_OpenMRS',
domain: 'poc1.jembi.org',
name: 'OpenMRS Musha instance',
roles: ['OpenMRS_PoC', 'PoC'],
passwordHash: '',
cert: ''
}
ctx.matchingChannel = channel
ctx.request = {}
ctx.request.url = 'test/authorisation'
ctx.request.path = 'test/authorisation'
ctx.response = {}
ctx.set = function () {}
return authorisation.authorise(ctx, () => {
;(ctx.authorisedChannel === undefined).should.be.true
ctx.response.status.should.be.exactly(401)
return done()
})
})
return it('should allow a request if the client is authorised to use the channel by clientID', done => {
// Setup a channel for the mock endpoint
const channel = new ChannelModel({
name: 'Authorisation mock channel 3',
urlPattern: 'test/authorisation',
allow: ['Test1', 'Musha_OpenMRS', 'Test2'],
routes: [
{
name: 'test route',
host: 'localhost',
port: 9876,
primary: true
}
],
updatedBy: {
id: new ObjectId(),
name: 'Test'
}
})
// Setup test data, will need authentication mechanisms to set ctx.authenticated
const ctx = {}
ctx.authenticated = {
clientID: 'Musha_OpenMRS',
domain: 'poc1.jembi.org',
name: 'OpenMRS Musha instance',
roles: ['OpenMRS_PoC', 'PoC'],
passwordHash: '',
cert: ''
}
ctx.matchingChannel = channel
ctx.request = {}
ctx.request.url = 'test/authorisation'
ctx.request.path = 'test/authorisation'
ctx.response = {}
return authorisation.authorise(ctx, () => {
ctx.authorisedChannel.should.exist
return done()
})
})
})
describe('.genAuthAudit', () =>
it('should generate an audit with the remoteAddress included', () => {
const audit = authorisation.genAuthAudit('1.2.3.4')
audit.should.be.ok()
return audit.should.match(/ParticipantObjectID="1\.2\.3\.4"/)
}))
describe('.authoriseClient', () => {
it('should return true for a valid client, authorised client by role', () => {
const ctx = {
authenticated: {
roles: ['admin', 'test']
}
}
const channel = {allow: ['something', 'admin']}
const authoriseClient = authorisation.__get__('authoriseClient')
const actual = authoriseClient(channel, ctx)
return actual.should.be.true()
})
it('should return false for a invalid client, authorised client by role', () => {
const ctx = {
authenticated: {
roles: ['admin', 'test']
}
}
const channel = {allow: ['another', 'notme']}
const authoriseClient = authorisation.__get__('authoriseClient')
const actual = authoriseClient(channel, ctx)
return actual.should.be.false()
})
it('should return true for a valid client, authorised client by role', () => {
const ctx = {
authenticated: {
roles: ['test1', 'test2'],
clientID: 'client1'
}
}
const channel = {allow: ['something', 'admin', 'client1']}
const authoriseClient = authorisation.__get__('authoriseClient')
const actual = authoriseClient(channel, ctx)
return actual.should.be.true()
})
it('should return false for a invalid client, authorised client by role', () => {
const ctx = {
authenticated: {
roles: ['test1', 'test2'],
clientID: 'client2'
}
}
const channel = {allow: ['something', 'admin', 'client1']}
const authoriseClient = authorisation.__get__('authoriseClient')
const actual = authoriseClient(channel, ctx)
return actual.should.be.false()
})
it('should return false for if there is no authenticated client', () => {
const ctx = {}
const channel = {allow: ['something', 'admin', 'client1']}
const authoriseClient = authorisation.__get__('authoriseClient')
const actual = authoriseClient(channel, ctx)
return actual.should.be.false()
})
return it('should return false if allows is null', () => {
const ctx = {
authenticated: {
roles: ['test1', 'test2'],
clientID: 'client2'
}
}
const channel = {allow: null}
const authoriseClient = authorisation.__get__('authoriseClient')
const actual = authoriseClient(channel, ctx)
return actual.should.be.false()
})
})
describe('authoriseIP', () => {
it('should return true if the client IP is in the whitelist', () => {
const ctx = {ip: '192.168.0.11'}
const channel = {whitelist: ['192.168.0.11']}
const authoriseIP = authorisation.__get__('authoriseIP')
const actual = authoriseIP(channel, ctx)
return actual.should.be.true()
})
it('should return false if the client IP isnt in the whitelist', () => {
const ctx = {ip: '192.168.0.11'}
const channel = {whitelist: ['192.168.0.15']}
const authoriseIP = authorisation.__get__('authoriseIP')
const actual = authoriseIP(channel, ctx)
return actual.should.be.false()
})
it('should return false if there are no whitelist entires', () => {
const ctx = {ip: '192.168.0.11'}
const channel = {whitelist: null}
const authoriseIP = authorisation.__get__('authoriseIP')
const actual = authoriseIP(channel, ctx)
return actual.should.be.false()
})
})
})