-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathpassport-local.spec.js
174 lines (143 loc) · 5.38 KB
/
passport-local.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
'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-local', 'passport-local', version => {
describe('passport-local instrumentation', () => {
const passportVerifyChannel = dc.channel('datadog:passport:verify:finish')
let port, server, subscriberStub
before(() => {
return agent.load(['http', 'express', 'passport', 'passport-local'], { client: false })
})
before((done) => {
const express = require('../../../versions/express').get()
const passport = require('../../../versions/passport').get()
const LocalStrategy = require(`../../../versions/passport-local@${version}`).get().Strategy
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('local', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: false
}, validateUser))
passport.use('local-withreq', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
}, validateUser))
app.use(passport.initialize())
app.use(express.json())
app.post('/',
passport.authenticate('local', {
successRedirect: '/grant',
failureRedirect: '/deny',
session: false
})
)
app.post('/req',
passport.authenticate('local-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.post(`http://localhost:${port}/`, { username: 'error', password: '1234' })
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.post(`http://localhost:${port}/`, { username: 'test', password: '1234' })
expect(res.status).to.equal(200)
expect(res.data).to.equal('Granted')
expect(subscriberStub).to.be.calledOnceWithExactly({
framework: 'passport-local',
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.post(`http://localhost:${port}/req`, { username: 'test', password: '1234' })
expect(res.status).to.equal(200)
expect(res.data).to.equal('Granted')
expect(subscriberStub).to.be.calledOnceWithExactly({
framework: 'passport-local',
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.post(`http://localhost:${port}/`, { username: 'test', password: '1' })
expect(res.status).to.equal(200)
expect(res.data).to.equal('Denied')
expect(subscriberStub).to.be.calledOnceWithExactly({
framework: 'passport-local',
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.post(`http://localhost:${port}/`, { username: 'test', password: '1234' })
expect(res.status).to.equal(403)
expect(res.data).to.equal('Blocked')
expect(subscriberStub).to.be.calledOnceWithExactly({
framework: 'passport-local',
login: 'test',
user: { _id: 1, username: 'test', password: '1234', email: 'testuser@ddog.com' },
success: true,
abortController: new AbortController()
})
})
})
})