I have an Express 4 application that makes use of Passport with a LocalStrategy for authentication. Authentication is functional and works in the browser using a single connect.sid
cookie as expected.
I make use of Ava for tests and would like to test authenticated endpoints. From reading documentation and other threads, I should be looking into Supertest Agents to persist the login information. I have come up with the following non-functional code.
test('booking api with auth using await', async t => {
t.plan(2)
const app = makeServer(t.context.config, t.context.connection)
const server = await request.agent(app)
const login = await server
.post('/v2/auth/login')
.send({ username: 'admin', password: 'password' })
.expect('set-cookie', /connect.sid/)
t.is(login.body.message, 'Logged in as admin') // Passes
const GET = await server
.get('/v2/Booking')
t.is(GET.status, 200) // Actual: 403
})
test('booking api with auth using promises', async t => {
t.plan(2)
const app = makeServer(t.context.config, t.context.connection)
const server = await request.agent(app)
return server
.post('/v2/auth/login')
.send({ username: 'admin', password: 'password' })
.expect('set-cookie', /connect.sid/)
.then(res => {
t.is(res.body.message, 'Logged in as admin') // Passes
return server
.get('/v2/Booking')
.then(res => {
t.is(res.status, 200) // Actual: 403
})
})
})
I have provided two samples in case it was an async-related issue. Among the other things I have tried is setting the cookie manually and using the done
callback to reuse the agent. What else am I missing?
via timur
No comments:
Post a Comment