1 | const test = require('ava')
|
2 | const _ = require('lodash')
|
3 | const fixtures = require('./fixtures')
|
4 | const token = require('../src/token')
|
5 |
|
6 | test('enable multiple instances', (t) => {
|
7 | const tkn1 = token.create('bearer a.b.c')
|
8 | const tkn2 = token.create('bearer c.b.a')
|
9 | t.truthy(tkn1)
|
10 | t.truthy(tkn2)
|
11 | t.is(tkn1, 'a.b.c')
|
12 | t.is(tkn2, 'c.b.a')
|
13 | })
|
14 |
|
15 | test('get bearer token – lowercase', (t) => {
|
16 | const tkn = token.create('bearer a.b.c')
|
17 | t.truthy(tkn)
|
18 | t.is(tkn, 'a.b.c')
|
19 | })
|
20 |
|
21 | test('get bearer token – uppercase', (t) => {
|
22 | const tkn = token.create('Bearer a.b.c')
|
23 | t.truthy(tkn)
|
24 | t.is(tkn, 'a.b.c')
|
25 | })
|
26 |
|
27 | test('get bearer token – capital case', (t) => {
|
28 | const tkn = token.create('BEARER a.b.c')
|
29 | t.truthy(tkn)
|
30 | t.is(tkn, 'a.b.c')
|
31 | })
|
32 |
|
33 | test('get no bearer token – wrong scheme', (t) => {
|
34 | const tkn = token.create('beareer a.b.c')
|
35 | t.falsy(tkn)
|
36 | })
|
37 |
|
38 | test('get no bearer token – multiple spaces', (t) => {
|
39 | const tkn = token.create('bearer a.b.c')
|
40 | t.falsy(tkn)
|
41 | })
|
42 |
|
43 | test('get no bearer token – too less segments', (t) => {
|
44 | const tkn = token.create('bearer a.b')
|
45 | t.falsy(tkn)
|
46 | })
|
47 |
|
48 | test('get no bearer token – spaces between', (t) => {
|
49 | const tkn = token.create('bearer a.b.c c')
|
50 | t.falsy(tkn)
|
51 | })
|
52 |
|
53 | test('get user data of token', (t) => {
|
54 | const tkn = fixtures.composeJwt('current')
|
55 | const data = token.getData(tkn, { clientId: fixtures.common.clientId })
|
56 |
|
57 | t.truthy(tkn)
|
58 | t.truthy(_.inRange(data.expiresIn, 3590000, 3600000))
|
59 | t.is(data.credentials.sub, fixtures.content.current.sub)
|
60 | t.falsy(data.credentials.name)
|
61 | t.deepEqual(data.credentials.scope.sort(), fixtures.targetScope)
|
62 | })
|
63 |
|
64 | test('get user data of token – rpt', (t) => {
|
65 | const tkn = fixtures.composeJwt('rpt')
|
66 | const data = token.getData(tkn, { clientId: fixtures.common.clientId })
|
67 |
|
68 | t.truthy(tkn)
|
69 | t.truthy(_.inRange(-1 * data.expiresIn, Date.now()))
|
70 | t.is(data.credentials.sub, fixtures.content.rpt.sub)
|
71 | t.falsy(data.credentials.name)
|
72 | t.deepEqual(data.credentials.scope.sort(), [...fixtures.targetScope, 'scope:foo.READ', 'scope:foo.WRITE'])
|
73 | })
|
74 |
|
75 | test('get user data of token – additional fields', (t) => {
|
76 | const tkn = fixtures.composeJwt('current')
|
77 | const data = token.getData(tkn, {
|
78 | clientId: fixtures.common.clientId,
|
79 | userInfo: ['name']
|
80 | })
|
81 |
|
82 | t.truthy(tkn)
|
83 | t.truthy(_.inRange(data.expiresIn, 3590000, 3600000))
|
84 | t.is(data.credentials.sub, fixtures.content.current.sub)
|
85 | t.is(data.credentials.name, fixtures.content.current.name)
|
86 | t.deepEqual(data.credentials.scope.sort(), fixtures.targetScope)
|
87 | })
|
88 |
|
89 | test('get user data of token – default expiration', (t) => {
|
90 | const tkn = fixtures.composeJwt('noExp')
|
91 | const data = token.getData(tkn, { clientId: fixtures.common.clientId })
|
92 |
|
93 | t.truthy(tkn)
|
94 | t.is(data.expiresIn, 60000)
|
95 | t.is(data.credentials.sub, fixtures.content.expired.sub)
|
96 | t.falsy(data.credentials.name)
|
97 | t.deepEqual(data.credentials.scope.sort(), fixtures.targetScope)
|
98 | })
|
99 |
|
100 | test('get user data of token – default scopes', (t) => {
|
101 | const tkn = fixtures.composeJwt('noScope')
|
102 | const data = token.getData(tkn, { clientId: fixtures.common.clientId })
|
103 |
|
104 | t.truthy(tkn)
|
105 | t.truthy(_.inRange(-1 * data.expiresIn, Date.now()))
|
106 | t.is(data.credentials.sub, fixtures.content.expired.sub)
|
107 | t.falsy(data.credentials.name)
|
108 | t.deepEqual(data.credentials.scope, [])
|
109 | })
|