-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from atlp-rwanda/ft-2fa-authentication-#187419173
#23187419173 - ft:sets up Two Factor Authentication for Sellers
- Loading branch information
Showing
18 changed files
with
308 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import app from "../src/utils/server"; | |
import User from "../src/sequelize/models/users"; | ||
import * as userServices from "../src/services/user.service"; | ||
import sequelize, { connect } from "../src/config/dbConnection"; | ||
import * as twoFAService from "../src/utils/2fa"; | ||
|
||
const userData: any = { | ||
name: "yvanna", | ||
|
@@ -12,6 +13,13 @@ const userData: any = { | |
password: "test1234", | ||
}; | ||
|
||
const dummySeller = { | ||
name: "dummy", | ||
username: "username", | ||
email: "[email protected]", | ||
password: "1234567890", | ||
isMerchant: true, | ||
}; | ||
const userTestData = { | ||
newPassword: "Test@123", | ||
confirmPassword: "Test@123", | ||
|
@@ -26,30 +34,28 @@ describe("Testing user Routes", () => { | |
beforeAll(async () => { | ||
try { | ||
await connect(); | ||
await User.destroy({ truncate: true }); | ||
const dummy = await request(app).post("/api/v1/users/register").send(dummySeller); | ||
} catch (error) { | ||
throw error; | ||
sequelize.close(); | ||
} | ||
}, 40000); | ||
|
||
afterAll(async () => { | ||
await User.destroy({ truncate: true }); | ||
await sequelize.close(); | ||
}); | ||
}, 20000); | ||
|
||
let token: any; | ||
describe("Testing user authentication", () => { | ||
test("should return 201 and create a new user when registering successfully", async () => { | ||
const response = await request(app) | ||
.post("/api/v1/users/register") | ||
.send(userData); | ||
const response = await request(app).post("/api/v1/users/register").send(userData); | ||
expect(response.status).toBe(201); | ||
}, 20000); | ||
|
||
test("should return 409 when registering with an existing email", async () => { | ||
User.create(userData); | ||
const response = await request(app) | ||
.post("/api/v1/users/register") | ||
.send(userData); | ||
const response = await request(app).post("/api/v1/users/register").send(userData); | ||
expect(response.status).toBe(409); | ||
}, 20000); | ||
|
||
|
@@ -59,9 +65,7 @@ describe("Testing user Routes", () => { | |
name: "", | ||
username: "existinguser", | ||
}; | ||
const response = await request(app) | ||
.post("/api/v1/users/register") | ||
.send(userData); | ||
const response = await request(app).post("/api/v1/users/register").send(userData); | ||
|
||
expect(response.status).toBe(400); | ||
}, 20000); | ||
|
@@ -74,22 +78,36 @@ describe("Testing user Routes", () => { | |
expect(spy).toHaveBeenCalled(); | ||
expect(spy2).toHaveBeenCalled(); | ||
}, 20000); | ||
|
||
test("Should return status 401 to indicate Unauthorized user", async () => { | ||
const loggedInUser = { | ||
email: userData.email, | ||
password: "test", | ||
password: "test123456", | ||
}; | ||
const spyonOne = jest.spyOn(User, "findOne").mockResolvedValueOnce({ | ||
//@ts-ignore | ||
email: userData.email, | ||
password: loginData.password, | ||
}); | ||
const response = await request(app) | ||
.post("/api/v1/users/login") | ||
.send(loggedInUser); | ||
const response = await request(app).post("/api/v1/users/login").send(loggedInUser); | ||
expect(response.body.status).toBe(401); | ||
spyonOne.mockRestore(); | ||
}); | ||
}, 20000); | ||
|
||
test("Should return send magic link if seller try to login", async () => { | ||
const spy = jest.spyOn(twoFAService, "sendOTP"); | ||
const user = { | ||
email: dummySeller.email, | ||
password: dummySeller.password, | ||
}; | ||
|
||
const response = await request(app).post("/api/v1/users/login").send({ | ||
email: dummySeller.email, | ||
password: dummySeller.password, | ||
}); | ||
|
||
expect(response.body.message).toBe("Verification link has been sent to your email. Please verify it to continue"); | ||
}, 20000); | ||
|
||
test("should log a user in to retrieve a token", async () => { | ||
const response = await request(app).post("/api/v1/users/login").send({ | ||
|
@@ -114,13 +132,11 @@ describe("Testing user Routes", () => { | |
}); | ||
|
||
test("should return 401 when updating password without authorization", async () => { | ||
const response = await request(app) | ||
.put("/api/v1/users/passwordupdate") | ||
.send({ | ||
oldPassword: userData.password, | ||
newPassword: userTestData.newPassword, | ||
confirmPassword: userTestData.confirmPassword, | ||
}); | ||
const response = await request(app).put("/api/v1/users/passwordupdate").send({ | ||
oldPassword: userData.password, | ||
newPassword: userTestData.newPassword, | ||
confirmPassword: userTestData.confirmPassword, | ||
}); | ||
expect(response.status).toBe(401); | ||
}); | ||
|
||
|
@@ -154,7 +170,7 @@ describe("Testing user Routes", () => { | |
.send({ | ||
oldPassword: userTestData.wrongPassword, | ||
newPassword: userTestData.newPassword, | ||
confirmPassword:userTestData.wrongPassword, | ||
confirmPassword: userTestData.wrongPassword, | ||
}) | ||
.set("Authorization", "Bearer " + token); | ||
expect(response.status).toBe(400); | ||
|
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,32 @@ | ||
import { Request, Response } from "express"; | ||
import { IUser, STATUS } from "../types"; | ||
import { generateToken, verifyMagicLinkToken } from "../utils/jsonwebtoken"; | ||
import User from "../sequelize/models/users"; | ||
|
||
export const otpVerification = async (req: Request, res: Response) => { | ||
const token = req.query.token as string; | ||
try { | ||
const decoded = await verifyMagicLinkToken(token); | ||
//@ts-ignore | ||
const userEmail = decoded.email; | ||
//@ts-ignore | ||
const user: IUser = await User.findOne({ where: { email: userEmail } }); | ||
|
||
if (!user) { | ||
return res.status(401).json({ | ||
message: "Token expired", | ||
}); | ||
} | ||
|
||
const accessToken = await generateToken(user); | ||
|
||
return res.status(200).json({ | ||
message: "Succesfuly loged in", | ||
token: accessToken, | ||
}); | ||
} catch (error: any) { | ||
return res.status(500).json({ | ||
message: error.message, | ||
}); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,28 +1,17 @@ | ||
import { Router } from "express"; | ||
import { | ||
fetchAllUsers, | ||
createUserController, | ||
userLogin, | ||
updatePassword} | ||
from "../controllers/userControllers"; | ||
import { | ||
emailValidation, | ||
validateSchema, | ||
} from "../middleware/validator"; | ||
import { fetchAllUsers, createUserController, userLogin, updatePassword } from "../controllers/userControllers"; | ||
import { emailValidation, validateSchema } from "../middleware/validator"; | ||
import signUpSchema from "../schemas/signUpSchema"; | ||
import { isLoggedIn } from "../middlewares/isLoggedIn"; | ||
import { passwordUpdateSchema } from "../schemas/passwordUpdate"; | ||
import { otpVerification } from "../controllers/2faControllers"; | ||
|
||
const userRoutes = Router(); | ||
|
||
userRoutes.get("/", fetchAllUsers); | ||
userRoutes.post('/login',userLogin); | ||
userRoutes.post("/register", | ||
emailValidation, | ||
validateSchema(signUpSchema), | ||
createUserController | ||
) | ||
userRoutes.put("/passwordupdate", isLoggedIn, validateSchema(passwordUpdateSchema), updatePassword) | ||
|
||
userRoutes.post("/login", userLogin); | ||
userRoutes.post("/register", emailValidation, validateSchema(signUpSchema), createUserController); | ||
userRoutes.put("/passwordupdate", isLoggedIn, validateSchema(passwordUpdateSchema), updatePassword); | ||
userRoutes.get("/2fa/verify", otpVerification); | ||
|
||
export default userRoutes; |
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
23 changes: 23 additions & 0 deletions
23
src/sequelize/migrations/20240422124749-add-isMerchnat-and-twoFAenabled-field.js
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,23 @@ | ||
"use strict"; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.addColumn("users", "isMerchant", { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: false, // Default value for isMerchant field | ||
}); | ||
|
||
await queryInterface.addColumn("users", "twoFAEnabled", { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: false, // Default value for twoFAEnabled field | ||
}); | ||
}, | ||
|
||
async down(queryInterface, Sequelize) { | ||
await queryInterface.removeColumn("users", "isMerchant"); | ||
await queryInterface.removeColumn("users", "twoFAEnabled"); | ||
}, | ||
}; |
Oops, something went wrong.