-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): testing login middleware
- Loading branch information
1 parent
7f16cff
commit 37464d6
Showing
2 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { startServer, stopServer } = require('../../lib/server.js'); | ||
const { request } = require('../scripts/helpers'); | ||
const generator = require('../scripts/generator'); | ||
|
||
describe('Logged in middleware', () => { | ||
beforeAll(async () => { | ||
await startServer(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopServer(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await generator.clearAll(); | ||
}); | ||
|
||
test('should fail if the token is not provided', async () => { | ||
const res = await request({ | ||
uri: '/members/me', | ||
method: 'GET' | ||
}); | ||
|
||
expect(res.statusCode).toEqual(401); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).not.toHaveProperty('data'); | ||
expect(res.body).toHaveProperty('message'); | ||
}); | ||
|
||
test('should fail if the token is invalid', async () => { | ||
const res = await request({ | ||
uri: '/members/me', | ||
method: 'GET', | ||
headers: { 'X-Auth-Token': 'blablabla' }, | ||
}); | ||
|
||
expect(res.statusCode).toEqual(401); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).not.toHaveProperty('data'); | ||
expect(res.body).toHaveProperty('message'); | ||
}); | ||
|
||
test('should fail if the token expires', async () => { | ||
const user = await generator.createUser({ password: 'test', mail_confirmed_at: null }); | ||
const token = await generator.createAccessToken({ expires_at: new Date() }, user); | ||
const res = await request({ | ||
uri: '/members/me', | ||
method: 'GET', | ||
headers: { 'X-Auth-Token': token.value }, | ||
}); | ||
|
||
expect(res.statusCode).toEqual(401); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).not.toHaveProperty('data'); | ||
expect(res.body).toHaveProperty('message'); | ||
}); | ||
|
||
test('should succeed if everything is okay', async () => { | ||
const user = await generator.createUser({ password: 'test', mail_confirmed_at: new Date() }); | ||
const token = await generator.createAccessToken({}, user); | ||
|
||
const res = await request({ | ||
uri: '/members/me', | ||
method: 'GET', | ||
headers: { 'X-Auth-Token': token.value } | ||
}); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.success).toEqual(true); | ||
expect(res.body).toHaveProperty('data'); | ||
expect(res.body).not.toHaveProperty('errors'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters