forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-request-test.js
More file actions
100 lines (76 loc) · 2.92 KB
/
auth-request-test.js
File metadata and controls
100 lines (76 loc) · 2.92 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
'use strict'
const chai = require('chai')
const expect = chai.expect
// const sinon = require('sinon')
chai.use(require('sinon-chai'))
chai.use(require('dirty-chai'))
chai.should()
// const HttpMocks = require('node-mocks-http')
const url = require('url')
const AuthRequest = require('../../lib/requests/auth-request')
const SolidHost = require('../../lib/models/solid-host')
const AccountManager = require('../../lib/models/account-manager')
const UserAccount = require('../../lib/models/user-account')
describe('AuthRequest', () => {
function testAuthQueryParams () {
let body = {}
body['response_type'] = 'code'
body['scope'] = 'openid'
body['client_id'] = 'client1'
body['redirect_uri'] = 'https://redirect.example.com/'
body['state'] = '1234'
body['nonce'] = '5678'
body['display'] = 'page'
return body
}
const host = SolidHost.from({ serverUri: 'https://localhost:8443' })
const accountManager = AccountManager.from({ host })
describe('extractAuthParams()', () => {
it('should initialize the auth url query object from params', () => {
let body = testAuthQueryParams()
body['other_key'] = 'whatever'
let req = { body, method: 'POST' }
let extracted = AuthRequest.extractAuthParams(req)
for (let param of AuthRequest.AUTH_QUERY_PARAMS) {
expect(extracted[param]).to.equal(body[param])
}
// make sure *only* the listed params were copied
expect(extracted['other_key']).to.not.exist()
})
it('should return empty params with no request body present', () => {
let req = { method: 'POST' }
expect(AuthRequest.extractAuthParams(req)).to.eql({})
})
})
describe('authorizeUrl()', () => {
it('should return an /authorize url', () => {
let request = new AuthRequest({ accountManager })
let authUrl = request.authorizeUrl()
expect(authUrl.startsWith('https://localhost:8443/authorize')).to.be.true()
})
it('should pass through relevant auth query params from request body', () => {
let body = testAuthQueryParams()
let req = { body, method: 'POST' }
let request = new AuthRequest({ accountManager })
request.authQueryParams = AuthRequest.extractAuthParams(req)
let authUrl = request.authorizeUrl()
let parseQueryString = true
let parsedUrl = url.parse(authUrl, parseQueryString)
for (let param in body) {
expect(body[param]).to.equal(parsedUrl.query[param])
}
})
})
describe('initUserSession()', () => {
it('should initialize the request session', () => {
let webId = 'https://alice.example.com/#me'
let alice = UserAccount.from({ username: 'alice', webId })
let session = {}
let request = new AuthRequest({ session })
request.initUserSession(alice)
expect(request.session.userId).to.equal(webId)
let subject = request.session.subject
expect(subject['_id']).to.equal(webId)
})
})
})