forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-handlers-test.js
More file actions
103 lines (81 loc) · 2.6 KB
/
auth-handlers-test.js
File metadata and controls
103 lines (81 loc) · 2.6 KB
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
'use strict'
const chai = require('chai')
const sinon = require('sinon')
const { expect } = chai
chai.use(require('sinon-chai'))
chai.use(require('dirty-chai'))
chai.should()
const Auth = require('../../lib/api/authn')
describe('OIDC Handler', () => {
describe('setAuthenticateHeader()', () => {
let res, req
beforeEach(() => {
req = {
app: {
locals: { host: { serverUri: 'https://example.com' } }
},
get: sinon.stub()
}
res = { set: sinon.stub() }
})
it('should set the WWW-Authenticate header with error params', () => {
let error = {
error: 'invalid_token',
error_description: 'Invalid token',
error_uri: 'https://example.com/errors/token'
}
Auth.oidc.setAuthenticateHeader(req, res, error)
expect(res.set).to.be.calledWith(
'WWW-Authenticate',
'Bearer realm="https://example.com", scope="openid webid", error="invalid_token", error_description="Invalid token", error_uri="https://example.com/errors/token"'
)
})
it('should set WWW-Authenticate with no error_description if none given', () => {
let error = {}
Auth.oidc.setAuthenticateHeader(req, res, error)
expect(res.set).to.be.calledWith(
'WWW-Authenticate',
'Bearer realm="https://example.com", scope="openid webid"'
)
})
})
describe('isEmptyToken()', () => {
let req
beforeEach(() => {
req = { get: sinon.stub() }
})
it('should be true for empty access token', () => {
req.get.withArgs('Authorization').returns('Bearer ')
expect(Auth.oidc.isEmptyToken(req)).to.be.true()
req.get.withArgs('Authorization').returns('Bearer')
expect(Auth.oidc.isEmptyToken(req)).to.be.true()
})
it('should be false when access token is present', () => {
req.get.withArgs('Authorization').returns('Bearer token123')
expect(Auth.oidc.isEmptyToken(req)).to.be.false()
})
it('should be false when no authorization header is present', () => {
expect(Auth.oidc.isEmptyToken(req)).to.be.false()
})
})
})
describe('WebID-TLS Handler', () => {
describe('setAuthenticateHeader()', () => {
let res, req
beforeEach(() => {
req = {
app: {
locals: { host: { serverUri: 'https://example.com' } }
}
}
res = { set: sinon.stub() }
})
it('should set the WWW-Authenticate header', () => {
Auth.tls.setAuthenticateHeader(req, res)
expect(res.set).to.be.calledWith(
'WWW-Authenticate',
'WebID-TLS realm="https://example.com"'
)
})
})
})