-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
7 changed files
with
205 additions
and
14 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,38 @@ | ||
import decodejwt from '../../helpers/tokens/decode.token'; | ||
import status from '../../helpers/constants/status.codes'; | ||
import models from '../models/index'; | ||
import error from '../../helpers/error.sender'; | ||
import errorMessages from '../../helpers/constants/error.messages'; | ||
/** | ||
* @class | ||
*/ | ||
export default class { | ||
/** | ||
* @description verification link controller | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {*} void | ||
*/ | ||
static async verifyEmail(req, res) { | ||
const { token } = req.params; | ||
const result = decodejwt(token); | ||
if (result !== undefined) { | ||
const action = await models.User.update({ verified: true }, { | ||
where: { | ||
email: result.user.email, | ||
} | ||
}); | ||
const ZERO = 0; | ||
if (!action.includes(ZERO)) { | ||
res.status(status.OK).json({ | ||
status: 200, | ||
message: 'Your email is successfully verified' | ||
}); | ||
} else { | ||
error(status.NOT_FOUND, res, 'user', errorMessages.noUser); | ||
} | ||
} else { | ||
error(status.BAD_REQUEST, res, 'link', errorMessages.emailLinkInvalid); | ||
} | ||
} | ||
} |
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
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,90 @@ | ||
import chai from 'chai'; | ||
import chaihttp from 'chai-http'; | ||
import createToken from '../src/helpers/tokens/generate.token'; | ||
import env from '../src/configs/environments'; | ||
import server from '../src/index'; | ||
import status from '../src/helpers/constants/status.codes'; | ||
import errorMessages from '../src/helpers/constants/error.messages'; | ||
|
||
|
||
chai.use(chaihttp); | ||
chai.should(); | ||
|
||
|
||
describe('GET /verifyEmail', () => { | ||
it('Should return a success message if the link is valid', (done) => { | ||
const user = { | ||
username: 'BurindiAlain', | ||
email: 'alain@gmail.com', | ||
password: 'password23423', | ||
confirmPassword: 'password23423' | ||
}; | ||
|
||
const { username, email } = user; | ||
|
||
const verifytoken = createToken({ username, email }, env.secret); | ||
chai | ||
.request(server) | ||
.get(`/api/users/verifyEmail/${verifytoken}`) | ||
.end(async (err, res) => { | ||
await res.should.have.status(status.OK); | ||
await res.body.message.should.be.eq('Your email is successfully verified'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should return a message if a user doesn\'t exist', (done) => { | ||
const user = { | ||
username: 'BurindiAlain', | ||
email: 'alain@gmail.com', | ||
password: 'password23423', | ||
confirmPassword: 'password23423' | ||
}; | ||
chai | ||
.request(server) | ||
.post('/api/users/signup') | ||
.send(user) | ||
.end(); | ||
|
||
const { username } = user; | ||
|
||
const verifytoken = createToken({ username, email: 'dummy@email.com' }, env.secret); | ||
|
||
chai | ||
.request(server) | ||
.get(`/api/users/verifyEmail/${verifytoken}`) | ||
.end((err, res) => { | ||
res.should.have.status(status.NOT_FOUND); | ||
res.body.errors.user.should.be.eq(errorMessages.noUser); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should respond with a message if the token is invalid', (done) => { | ||
const user = { | ||
username: 'BurindiAlain', | ||
email: 'alain@gmail.com', | ||
password: 'password23423', | ||
confirmPassword: 'password23423' | ||
}; | ||
|
||
chai | ||
.request(server) | ||
.post('/api/users/signup') | ||
.send(user) | ||
.end(); | ||
|
||
const { username } = user; | ||
|
||
const verifytoken = createToken({ username }, 'env.secret'); | ||
|
||
chai | ||
.request(server) | ||
.get(`/api/users/verifyEmail/${verifytoken}`) | ||
.end((err, res) => { | ||
res.should.have.status(status.BAD_REQUEST); | ||
res.body.errors.link.should.be.eq(errorMessages.emailLinkInvalid); | ||
done(); | ||
}); | ||
}); | ||
}); |