-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathpassport-http.spec.js
199 lines (168 loc) · 5.71 KB
/
passport-http.spec.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
'use strict'
const agent = require('../../dd-trace/test/plugins/agent')
const axios = require('axios').create({ validateStatus: null })
const dc = require('dc-polyfill')
const { storage } = require('../../datadog-core')
withVersions('passport-http', 'passport-http', version => {
describe('passport-http instrumentation', () => {
const passportVerifyChannel = dc.channel('datadog:passport:verify:finish')
let port, server, subscriberStub
before(() => {
return agent.load(['http', 'express', 'passport', 'passport-http'], { client: false })
})
before((done) => {
const express = require('../../../versions/express').get()
const passport = require('../../../versions/passport').get()
const BasicStrategy = require(`../../../versions/passport-http@${version}`).get().BasicStrategy
const app = express()
function validateUser (req, username, password, done) {
// support with or without passReqToCallback
if (typeof done !== 'function') {
done = password
password = username
username = req
}
// simulate db error
if (username === 'error') return done('error')
const users = [{
_id: 1,
username: 'test',
password: '1234',
email: 'testuser@ddog.com'
}]
const user = users.find(user => (user.username === username) && (user.password === password))
if (!user) {
return done(null, false)
} else {
return done(null, user)
}
}
passport.use('basic', new BasicStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: false
}, validateUser))
passport.use('basic-withreq', new BasicStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
}, validateUser))
app.use(passport.initialize())
app.use(express.json())
app.get('/',
passport.authenticate('basic', {
successRedirect: '/grant',
failureRedirect: '/deny',
session: false
})
)
app.get('/req',
passport.authenticate('basic-withreq', {
successRedirect: '/grant',
failureRedirect: '/deny',
session: false
})
)
app.get('/grant', (req, res) => {
res.send('Granted')
})
app.get('/deny', (req, res) => {
res.send('Denied')
})
passportVerifyChannel.subscribe((data) => subscriberStub(data))
server = app.listen(0, () => {
port = server.address().port
done()
})
})
beforeEach(() => {
subscriberStub = sinon.stub()
})
after(() => {
server.close()
return agent.close({ ritmReset: false })
})
it('should not call subscriber when an error occurs', async () => {
const res = await axios.get(`http://localhost:${port}/`, {
headers: {
// error:1234
Authorization: 'Basic ZXJyb3I6MTIzNA=='
}
})
expect(res.status).to.equal(500)
expect(subscriberStub).to.not.be.called
})
it('should call subscriber with proper arguments on success', async () => {
const res = await axios.get(`http://localhost:${port}/`, {
headers: {
// test:1234
Authorization: 'Basic dGVzdDoxMjM0'
}
})
expect(res.status).to.equal(200)
expect(res.data).to.equal('Granted')
expect(subscriberStub).to.be.calledOnceWithExactly({
framework: 'passport-basic',
login: 'test',
user: { _id: 1, username: 'test', password: '1234', email: 'testuser@ddog.com' },
success: true,
abortController: new AbortController()
})
})
it('should call subscriber with proper arguments on success with passReqToCallback set to true', async () => {
const res = await axios.get(`http://localhost:${port}/req`, {
headers: {
// test:1234
Authorization: 'Basic dGVzdDoxMjM0'
}
})
expect(res.status).to.equal(200)
expect(res.data).to.equal('Granted')
expect(subscriberStub).to.be.calledOnceWithExactly({
framework: 'passport-basic',
login: 'test',
user: { _id: 1, username: 'test', password: '1234', email: 'testuser@ddog.com' },
success: true,
abortController: new AbortController()
})
})
it('should call subscriber with proper arguments on failure', async () => {
const res = await axios.get(`http://localhost:${port}/`, {
headers: {
// test:1
Authorization: 'Basic dGVzdDox'
}
})
expect(res.status).to.equal(200)
expect(res.data).to.equal('Denied')
expect(subscriberStub).to.be.calledOnceWithExactly({
framework: 'passport-basic',
login: 'test',
user: false,
success: false,
abortController: new AbortController()
})
})
it('should block when subscriber aborts', async () => {
subscriberStub = sinon.spy(({ abortController }) => {
storage('legacy').getStore().req.res.writeHead(403).end('Blocked')
abortController.abort()
})
const res = await axios.get(`http://localhost:${port}/`, {
headers: {
// test:1234
Authorization: 'Basic dGVzdDoxMjM0'
}
})
expect(res.status).to.equal(403)
expect(res.data).to.equal('Blocked')
expect(subscriberStub).to.be.calledOnceWithExactly({
framework: 'passport-basic',
login: 'test',
user: { _id: 1, username: 'test', password: '1234', email: 'testuser@ddog.com' },
success: true,
abortController: new AbortController()
})
})
})
})