Skip to content

Commit

Permalink
New tests and various bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannC committed Nov 21, 2019
1 parent 4ffeebf commit e3963f5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
1 change: 0 additions & 1 deletion lib/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ exports.resetPasswordForm = function (req, res, next) {
if (req.user) {
notifications.push({ type: 'error', message: 'Oups, you are already logged in!' })
res.json({ notifications: notifications });
next();
return;
}
const host = config.host ? config.host : req.headers.host;
Expand Down
2 changes: 0 additions & 2 deletions lib/templates/forms/ResetPassword.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description"
content=" Discover NUSID, the generic user space to import in all your Node.JS apps. It includes everything from the templates to the database management." />
<title>Reset your password</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
Expand Down
2 changes: 0 additions & 2 deletions lib/templates/pages/Notification.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description"
content=" Discover NUSID, the generic user space to import in all your Node.JS apps. It includes everything from the templates to the database management." />
<title>Reset your password</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graphql-authentification-service",
"version": "0.0.2",
"description": "A REST back-end to handle login, registration, access control and password recovery with JsonWebToken.",
"description": "A GraphQL API to handle login, registration, access control and password recovery with JsonWebToken.",
"main": "app.js",
"dependencies": {
"agenda": "^2.1.0",
Expand Down
33 changes: 24 additions & 9 deletions test/integration/RecoverPassword.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ beforeAll((done) => {
userDB: "RecoverTest"
},
onReady: async () => {
try{
try {
request = appTester.getRequestSender();
await appTester.register(user);
await appTester.register(user2);
Expand Down Expand Up @@ -117,10 +117,10 @@ test("Change password with recorevy token", async (done) => {
let res = await request.getGraphQL(recoveryEmailQuery);
expect(res.data.sendPasswordRecorevyEmail.notifications[0].message.includes("If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes")).toBeTruthy();
const UserModel = require('../../lib/model/UserModel');
const userRetrieved = await UserModel.getUser({username: user.username}, {verified: true});
const userRetrieved = await UserModel.getUser({ username: user.username }, { verified: true });
expect(typeof userRetrieved.passwordRecoveryToken === "string").toBeTruthy();
expect(userRetrieved.passwordRecoveryToken.length > 10).toBeTruthy();
let newPassword = "newPassword";
let newPassword = "newPassword";
const updatePasswordQuery = {
query: `mutation{resetMyPassword(password:"${newPassword}" passwordRecoveryToken:"${userRetrieved.passwordRecoveryToken}"){
notifications{
Expand Down Expand Up @@ -157,7 +157,7 @@ test("Wrong token", async (done) => {
let res = await request.getGraphQL(recoveryEmailQuery);
expect(res.data.sendPasswordRecorevyEmail.notifications[0].message.includes("If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes")).toBeTruthy();

let newPassword = "newPassword";
let newPassword = "newPassword";
const updatePasswordQuery = {
query: `mutation{resetMyPassword(password:"${newPassword}" passwordRecoveryToken:"WRONGTOKEN"){
notifications{
Expand Down Expand Up @@ -190,7 +190,7 @@ test("Password too short", async (done) => {
let res = await request.getGraphQL(recoveryEmailQuery);
expect(res.data.sendPasswordRecorevyEmail.notifications[0].message.includes("If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes")).toBeTruthy();
const UserModel = require('../../lib/model/UserModel');
const userRetrieved = await UserModel.getUser({username: user3.username});
const userRetrieved = await UserModel.getUser({ username: user3.username });
expect(typeof userRetrieved.passwordRecoveryToken === "string").toBeTruthy();
expect(userRetrieved.passwordRecoveryToken.length > 10).toBeTruthy();
const updatePasswordQuery = {
Expand Down Expand Up @@ -222,14 +222,14 @@ test("Token too old", async (done) => {
let res = await request.getGraphQL(recoveryEmailQuery);
expect(res.data.sendPasswordRecorevyEmail.notifications[0].message.includes("If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes")).toBeTruthy();
const UserModel = require('../../lib/model/UserModel');
const userRetrieved = await UserModel.getUser({username: user4.username});
const userRetrieved = await UserModel.getUser({ username: user4.username });
let oldDate = new Date()
oldDate.setHours(oldDate.getHours()-2);
await UserModel.updateUser({email: user4.email}, {"passwordRecoveryRequestDate":oldDate});
oldDate.setHours(oldDate.getHours() - 2);
await UserModel.updateUser({ email: user4.email }, { "passwordRecoveryRequestDate": oldDate });

expect(typeof userRetrieved.passwordRecoveryToken === "string").toBeTruthy();
expect(userRetrieved.passwordRecoveryToken.length > 10).toBeTruthy();
let newPassword = "newPassword";
let newPassword = "newPassword";
const updatePasswordQuery = {
query: `mutation{resetMyPassword(password:"${newPassword}" passwordRecoveryToken:"${userRetrieved.passwordRecoveryToken}"){
notifications{
Expand All @@ -244,6 +244,21 @@ test("Token too old", async (done) => {
done();
});

test("Acces reset password form", async (done) => {
res = await request.get("/form/reset/password?token=" + "ATOKEN");
expect(res.statusCode).toBe(200);
expect(res.text.includes("Reset your password")).toBeTruthy();
done();
});

test("Can't access reset password form when logged in", async (done) => {
res = await request.get("/form/reset/password?token=" + "ATOKEN")
.set("Authorization", "Bearer " + token).send();
expect(res.statusCode).toBe(200);
expect(res.text.includes("Oups, you are already logged in!")).toBeTruthy();
done();
});

afterAll(async (done) => {
await appTester.close(done);
}, 40000);

0 comments on commit e3963f5

Please sign in to comment.