-
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.
- Loading branch information
1 parent
29fea0a
commit cdb900c
Showing
3 changed files
with
128 additions
and
0 deletions.
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
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
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,100 @@ | ||
const { startServer, stopServer } = require('../../lib/server.js'); | ||
const { MailChange, User } = require('../../models'); | ||
|
||
const { request } = require('../scripts/helpers'); | ||
const generator = require('../scripts/generator'); | ||
|
||
describe('Mail change confirm', () => { | ||
beforeAll(async () => { | ||
await startServer(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopServer(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await generator.clearAll(); | ||
}); | ||
|
||
test('should return 404 if the mail change is not found', async () => { | ||
const res = await request({ | ||
uri: '/confirm-email-change', | ||
method: 'POST', | ||
body: { token: '1' } | ||
}); | ||
|
||
expect(res.statusCode).toEqual(404); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).not.toHaveProperty('data'); | ||
expect(res.body).toHaveProperty('message'); | ||
}); | ||
|
||
test('should return 404 if no token is provided', async () => { | ||
const res = await request({ | ||
uri: '/confirm-email-change', | ||
method: 'POST', | ||
body: {} | ||
}); | ||
|
||
expect(res.statusCode).toEqual(404); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).not.toHaveProperty('data'); | ||
expect(res.body).toHaveProperty('message'); | ||
}); | ||
|
||
test('should fail if token is expired', async () => { | ||
const user = await generator.createUser(); | ||
const mailChange = await generator.createMailChange({ expires_at: new Date() }, user); | ||
|
||
const res = await request({ | ||
uri: '/confirm-email-change', | ||
method: 'POST', | ||
body: { token: mailChange.value } | ||
}); | ||
|
||
expect(res.statusCode).toEqual(404); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).toHaveProperty('message'); | ||
expect(res.body).not.toHaveProperty('data'); | ||
}); | ||
|
||
|
||
test('should work if the token is found', async () => { | ||
const user = await generator.createUser(); | ||
const mailChange = await generator.createMailChange({}, user); | ||
|
||
const res = await request({ | ||
uri: '/confirm-email-change', | ||
method: 'POST', | ||
body: { token: mailChange.value } | ||
}); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.success).toEqual(true); | ||
expect(res.body).not.toHaveProperty('errors'); | ||
expect(res.body).toHaveProperty('message'); | ||
|
||
const mailChanges = await MailChange.count({ where: { user_id: user.id } }); | ||
expect(mailChanges).toEqual(0); | ||
|
||
const userFromDb = await User.scope('withPassword').findByPk(user.id); | ||
expect(userFromDb.email).toEqual(mailChange.new_email); | ||
}); | ||
|
||
test('should work if the token is found with email with spaces/tabs', async () => { | ||
const user = await generator.createUser(); | ||
const mailChange = await generator.createMailChange({}, user); | ||
|
||
const res = await request({ | ||
uri: '/confirm-email-change', | ||
method: 'POST', | ||
body: { token: '\t\t\t \t' + mailChange.value + '\t \t' } | ||
}); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.success).toEqual(true); | ||
expect(res.body).not.toHaveProperty('errors'); | ||
expect(res.body).toHaveProperty('message'); | ||
}); | ||
}); |