-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.test.js
83 lines (73 loc) · 2.14 KB
/
auth.test.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
const request = require('supertest-as-promised')
const {expect} = require('chai')
const db = require('APP/db')
const User = require('APP/db/models/user')
const app = require('./start')
const alice = {
username: '[email protected]',
password: '12345'
}
describe('/api/auth', () => {
before('create a user', () =>
db.didSync
.then(() =>
User.create(
{email: alice.username,
password: alice.password
})
)
)
describe('POST /local/login (username, password)', () => {
it('succeeds with a valid username and password', () =>
request(app)
.post('/api/auth/local/login')
.send(alice)
.expect(302)
.expect('Set-Cookie', /session=.*/)
.expect('Location', '/')
)
it('fails with an invalid username and password', () =>
request(app)
.post('/api/auth/local/login')
.send({username: alice.username, password: 'wrong'})
.expect(401)
)
})
describe('GET /whoami', () => {
describe('when logged in,', () => {
const agent = request.agent(app)
before('log in', () => agent
.post('/api/auth/local/login')
.send(alice))
it('responds with the currently logged in user', () =>
agent.get('/api/auth/whoami')
.set('Accept', 'application/json')
.expect(200)
.then(res => expect(res.body).to.contain({
email: alice.username
}))
)
})
it('when not logged in, responds with an empty object', () =>
request(app).get('/api/auth/whoami')
.expect(200)
.then(res => expect(res.body).to.eql({}))
)
})
describe('POST /logout when logged in', () => {
const agent = request.agent(app)
before('log in', () => agent
.post('/api/auth/local/login')
.send(alice))
it('logs you out and redirects to whoami', () => agent
.post('/api/auth/logout')
.expect(302)
.expect('Location', '/api/auth/whoami')
.then(() =>
agent.get('/api/auth/whoami')
.expect(200)
.then(rsp => expect(rsp.body).eql({}))
)
)
})
})