Skip to content

Commit acd5e91

Browse files
committed
fix remaining tests
1 parent 47dc761 commit acd5e91

File tree

7 files changed

+67
-43
lines changed

7 files changed

+67
-43
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"scripts": {
3434
"start": "NODE_ENV=test npm test -- -w",
3535
"lint": "standard",
36-
"test:cmd": "node --harmony node_modules/.bin/ava test/**/utils.spec.js --timeout=30s",
36+
"test:cmd": "node --harmony node_modules/.bin/ava test/**/*.spec.js --timeout=30s",
3737
"test": "NODE_ENV=test nyc --check-coverage --lines 80 npm run test:cmd",
3838
"coverage": "nyc report --reporter=lcov",
3939
"ci.coverage": "nyc report --reporter=text-lcov | coveralls"

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const pkg = require('../package.json')
1212
*/
1313
const internals = {
1414
manager: undefined,
15-
userInfoFields: []
15+
userInfoFields: undefined
1616
}
1717

1818
/**

src/token.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ function token (field) {
7272
* Get necessary user information out of token content.
7373
*
7474
* @param {Object} content The token its content
75-
* @param {Array.<?string>} fields The necessary fields
75+
* @param {Array.<?string>} [fields] The necessary fields
7676
* @returns {Object} The collection of requested user info
7777
*/
78-
function getUserInfo (content, fields) {
79-
return _.pick(content, _.uniq(['sub', ...fields]))
78+
function getUserInfo (content, fields = []) {
79+
return _.pick(content, ['sub', ...fields])
8080
}
8181

8282
/**

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const scheme = joi.object({
1515
cache: joi.alternatives().try(joi.object({
1616
segment: joi.string().default('keycloakJwt')
1717
}), joi.boolean().invalid(true)).default(false),
18-
userInfo: joi.array().items(joi.string()).default([])
18+
userInfo: joi.array().items(joi.string())
1919
}).unknown(true).required()
2020

2121
/**

test/_fixtures.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,50 @@ const config = {
1414
secret: 'barfoo'
1515
}
1616

17+
const content = {
18+
userData: {
19+
'exp': 5,
20+
'iat': 1,
21+
'sub': '1234567890',
22+
'name': 'John Doe',
23+
'email': '[email protected]',
24+
'admin': true,
25+
'realm_access': {
26+
'roles': [
27+
'admin'
28+
]
29+
},
30+
'resource_access': {
31+
'account': {
32+
'roles': [
33+
'manage-account',
34+
'manage-account-links',
35+
'view-profile'
36+
]
37+
},
38+
'same': {
39+
'roles': [
40+
'editor'
41+
]
42+
},
43+
'other-app': {
44+
'roles': [
45+
'other-app:creator'
46+
]
47+
}
48+
}
49+
}
50+
}
51+
1752
/**
1853
* @type Object
1954
* @public
2055
*
2156
* Various JSON Web Tokens
2257
*/
2358
const jwt = {
24-
content: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ',
25-
userData: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjUsImlhdCI6MSwic3ViIjoiMTIzNDU2Nzg5MCIsIm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlLCJyZWFsbV9hY2Nlc3MiOnsicm9sZXMiOlsiYWRtaW4iXX0sInJlc291cmNlX2FjY2VzcyI6eyJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX0sInNhbWUiOnsicm9sZXMiOlsiZWRpdG9yIl19LCJvdGhlci1hcHAiOnsicm9sZXMiOlsib3RoZXItYXBwOmNyZWF0b3IiXX19fQ._yxUAslOcgCp2Fd2xyO0q3iB24brG8PqqXQ-TCblQ1w',
26-
userDataExp: 'eyJhbG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nsicm9sZXMiOlsiYWRtaW4iXX0sInJlc291cmNlX2FjY2VzcyI6eyJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX0sInNhbWUiOnsicm9sZXMiOlsiZWRpdG9yIl19LCJvdGhlci1hcHAiOnsicm9sZXMiOlsib3RoZXItYXBwOmNyZWF0b3IiXX19fQ.BcTtSEpyiUVBVkUOwVDM0_T9UIy-vk2aaUAR8XM6Hd0'
2761
}
2862

2963
/**
@@ -92,6 +126,7 @@ module.exports = {
92126
realmUrl,
93127
clientId,
94128
config,
129+
content,
95130
jwt,
96131
validation,
97132
userInfo

test/index.spec.js

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ test.cb.serial('throw error if plugin gets registered twice', (t) => {
2121

2222
test.cb.serial('authentication does succeed', (t) => {
2323
prototypes.stub('validateAccessToken', fixtures.validation)
24-
prototypes.stub('userInfo', fixtures.userInfo)
2524

2625
getServer(undefined, (server) => {
2726
server.inject({
2827
method: 'GET',
2928
url: '/',
3029
headers: {
31-
authorization: `bearer ${fixtures.jwt.content}`
30+
authorization: `bearer ${fixtures.jwt.userData}`
3231
}
3332
}, (res) => {
3433
t.truthy(res)
@@ -40,13 +39,12 @@ test.cb.serial('authentication does succeed', (t) => {
4039

4140
test.cb.serial('authentication does succeed – cached', (t) => {
4241
prototypes.stub('validateAccessToken', fixtures.validation)
43-
prototypes.stub('userInfo', fixtures.userInfo)
4442

4543
const mockReq = {
4644
method: 'GET',
4745
url: '/',
4846
headers: {
49-
authorization: `bearer ${fixtures.jwt.content}`
47+
authorization: `bearer ${fixtures.jwt.userData}`
5048
}
5149
}
5250

@@ -66,7 +64,6 @@ test.cb.serial('authentication does succeed – cached', (t) => {
6664

6765
test.cb.serial('authentication does success – valid roles', (t) => {
6866
prototypes.stub('validateAccessToken', fixtures.validation)
69-
prototypes.stub('userInfo', fixtures.userInfo)
7067

7168
getServer(undefined, (server) => {
7269
server.inject({
@@ -85,7 +82,6 @@ test.cb.serial('authentication does success – valid roles', (t) => {
8582

8683
test.cb.serial('authentication does fail – invalid roles', (t) => {
8784
prototypes.stub('validateAccessToken', fixtures.validation)
88-
prototypes.stub('userInfo', fixtures.userInfo)
8985

9086
getServer(undefined, (server) => {
9187
server.inject({
@@ -110,7 +106,7 @@ test.cb.serial('authentication does fail – invalid token', (t) => {
110106
method: 'GET',
111107
url: '/',
112108
headers: {
113-
authorization: `bearer ${fixtures.jwt.content}`
109+
authorization: `bearer ${fixtures.jwt.userData}`
114110
}
115111
}, (res) => {
116112
t.truthy(res)
@@ -140,10 +136,9 @@ test.cb.serial('authentication does fail – invalid header', (t) => {
140136

141137
test.cb.serial('server method validates token', (t) => {
142138
prototypes.stub('validateAccessToken', fixtures.validation)
143-
prototypes.stub('userInfo', fixtures.userInfo)
144139

145140
getServer(undefined, (server) => {
146-
server.kjwt.validate(`bearer ${fixtures.jwt.content}`, (err, res) => {
141+
server.kjwt.validate(`bearer ${fixtures.jwt.userData}`, (err, res) => {
147142
t.falsy(err)
148143
t.truthy(res)
149144
t.truthy(res.credentials)
@@ -152,27 +147,11 @@ test.cb.serial('server method validates token', (t) => {
152147
})
153148
})
154149

155-
test.cb.serial('server method invalidates token – userinfo error', (t) => {
156-
prototypes.stub('validateAccessToken', fixtures.validation)
157-
prototypes.stub('userInfo', new Error('an error'), 'reject')
158-
159-
getServer(undefined, (server) => {
160-
server.kjwt.validate(`bearer ${fixtures.jwt.content}`, (err, res) => {
161-
t.falsy(res)
162-
t.truthy(err)
163-
t.truthy(err.isBoom)
164-
t.is(err.output.statusCode, 401)
165-
t.is(err.output.headers['WWW-Authenticate'], 'Bearer error="Error: an error"')
166-
t.end()
167-
})
168-
})
169-
})
170-
171150
test.cb.serial('server method invalidates token – validation error', (t) => {
172151
prototypes.stub('validateAccessToken', new Error('an error'), 'reject')
173152

174153
getServer(undefined, (server) => {
175-
server.kjwt.validate(`bearer ${fixtures.jwt.content}`, (err, res) => {
154+
server.kjwt.validate(`bearer ${fixtures.jwt.userData}`, (err, res) => {
176155
t.falsy(res)
177156
t.truthy(err)
178157
t.truthy(err.isBoom)
@@ -187,7 +166,7 @@ test.cb.serial('server method invalidates token – invalid', (t) => {
187166
prototypes.stub('validateAccessToken', false)
188167

189168
getServer(undefined, (server) => {
190-
server.kjwt.validate(`bearer ${fixtures.jwt.content}`, (err, res) => {
169+
server.kjwt.validate(`bearer ${fixtures.jwt.userData}`, (err, res) => {
191170
t.falsy(res)
192171
t.truthy(err)
193172
t.truthy(err.isBoom)
@@ -200,7 +179,7 @@ test.cb.serial('server method invalidates token – invalid', (t) => {
200179

201180
test.cb.serial('server method invalidates token – wrong format', (t) => {
202181
getServer(undefined, (server) => {
203-
server.kjwt.validate(fixtures.jwt.content, (err, res) => {
182+
server.kjwt.validate(fixtures.jwt.userData, (err, res) => {
204183
t.falsy(res)
205184
t.truthy(err)
206185
t.truthy(err.isBoom)

test/token.spec.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@ test('get no bearer token – spaces between', (t) => {
5050
})
5151

5252
test('get decoded content part of token', (t) => {
53-
const jwt = `bearer ${fixtures.jwt.content}`
53+
const jwt = `bearer ${fixtures.jwt.userData}`
5454
const tkn = token(jwt)
5555

56-
t.deepEqual(tkn.getContent(), {
57-
'sub': '1234567890',
58-
'name': 'John Doe',
59-
'admin': true
60-
})
56+
t.deepEqual(tkn.getContent(), fixtures.content.userData)
6157
})
6258

6359
test('get user data of token', (t) => {
@@ -67,6 +63,20 @@ test('get user data of token', (t) => {
6763

6864
t.truthy(data)
6965
t.is(data.expiresIn, 4000)
66+
t.is(data.sub, fixtures.content.userData.sub)
67+
t.falsy(data.name)
68+
t.deepEqual(data.scope.sort(), ['editor', 'other-app:creator', 'realm:admin'])
69+
})
70+
71+
test('get user data of token', (t) => {
72+
const jwt = `bearer ${fixtures.jwt.userData}`
73+
const tkn = token(jwt)
74+
const data = tkn.getData(['name'])
75+
76+
t.truthy(data)
77+
t.is(data.expiresIn, 4000)
78+
t.is(data.sub, fixtures.content.userData.sub)
79+
t.is(data.name, fixtures.content.userData.name)
7080
t.deepEqual(data.scope.sort(), ['editor', 'other-app:creator', 'realm:admin'])
7181
})
7282

0 commit comments

Comments
 (0)