diff --git a/.gitignore b/.gitignore index 4b7dd0e..4aad388 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /node_modules /.env /package-lock.json -/coverage \ No newline at end of file +/coverage +/uploads \ No newline at end of file diff --git a/__test__/user.test.ts b/__test__/user.test.ts index 15afa2c..40201a8 100644 --- a/__test__/user.test.ts +++ b/__test__/user.test.ts @@ -16,7 +16,7 @@ const userData: any = { const dummySeller = { name: "dummy", username: "username", - email: "srukundo02@gmail.com", + email: "niyonkurubertin50@gmail.com", password: "1234567890", isMerchant: true, }; @@ -94,21 +94,6 @@ describe("Testing user Routes", () => { 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({ email: userData.email, diff --git a/src/docs/users.ts b/src/docs/users.ts index 7776e09..239eef2 100644 --- a/src/docs/users.ts +++ b/src/docs/users.ts @@ -28,9 +28,9 @@ export const updatePasswordSchema = { }, confirmPassword: { type: "string", - } - } -} + }, + }, +}; export const loginSchema = { properties: { @@ -97,12 +97,34 @@ export const createUsers = { export const loginAsUser = { tags: ["Users"], summary: "Login as user", - responses: { - 200: { - description: "OK", - } - } - }; + requestBody: { + required: true, + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/Login", + }, + }, + }, + }, + responses: { + 200: { + description: "OK", + }, + 400: { + description: "Bad request missing or extra filed", + }, + 404: { + description: "Account not found", + }, + 409: { + description: "Invalid credentials", + }, + 500: { + description: "Internal server error", + }, + }, +}; export const passwordUpdate = { tags: ["Users"], @@ -113,18 +135,17 @@ export const passwordUpdate = { content: { "application/json": { schema: { - $ref: "#/components/schemas/updatePassword" - } - } - } + $ref: "#/components/schemas/updatePassword", + }, + }, + }, }, responses: { 200: { description: "OK", }, 400: { - description: "Bad Request" - } - } -} - + description: "Bad Request", + }, + }, +}; diff --git a/src/middleware/validator.ts b/src/middlewares/validator.ts similarity index 100% rename from src/middleware/validator.ts rename to src/middlewares/validator.ts diff --git a/src/routes/userRoutes.ts b/src/routes/userRoutes.ts index 74ac281..a918258 100644 --- a/src/routes/userRoutes.ts +++ b/src/routes/userRoutes.ts @@ -1,7 +1,8 @@ import { Router } from "express"; import { fetchAllUsers, createUserController, userLogin, updatePassword } from "../controllers/userControllers"; -import { emailValidation, validateSchema } from "../middleware/validator"; +import { emailValidation, validateSchema } from "../middlewares/validator"; import signUpSchema from "../schemas/signUpSchema"; +import { logInSchema } from "../schemas/loginSchema"; import { isLoggedIn } from "../middlewares/isLoggedIn"; import { passwordUpdateSchema } from "../schemas/passwordUpdate"; import { otpVerification } from "../controllers/2faControllers"; @@ -9,7 +10,7 @@ import { otpVerification } from "../controllers/2faControllers"; const userRoutes = Router(); userRoutes.get("/", fetchAllUsers); -userRoutes.post("/login", userLogin); +userRoutes.post("/login",validateSchema(logInSchema), userLogin); userRoutes.post("/register", emailValidation, validateSchema(signUpSchema), createUserController); userRoutes.put("/passwordupdate", isLoggedIn, validateSchema(passwordUpdateSchema), updatePassword); userRoutes.get("/2fa/verify", otpVerification); diff --git a/src/schemas/loginSchema.ts b/src/schemas/loginSchema.ts new file mode 100644 index 0000000..addd0a3 --- /dev/null +++ b/src/schemas/loginSchema.ts @@ -0,0 +1,9 @@ +import Joi from "joi"; +import { loginSchema } from "../docs/users"; + +export const logInSchema = Joi.object({ + email: Joi.string().min(6).required().email(), + password: Joi.string().min(6).max(20).required(), +}).options({ allowUnknown: false }); + +export default loginSchema; diff --git a/src/utils/2fa.ts b/src/utils/2fa.ts index ee5125b..92b2566 100644 --- a/src/utils/2fa.ts +++ b/src/utils/2fa.ts @@ -1,8 +1,8 @@ import { IUser, SUBJECTS } from "../types"; import { generateMagicLinkToken, generateToken } from "../utils/jsonwebtoken"; import { env } from "../utils/env"; -const verifyOtpTemplate = require("./verifyotp"); import transporter from "./transporter"; +import { verifyOtpTemplate } from "./verifyotp"; export const sendOTP = async (user: IUser) => { try { diff --git a/src/utils/verifyotp.js b/src/utils/verifyotp.ts similarity index 95% rename from src/utils/verifyotp.js rename to src/utils/verifyotp.ts index 581c47e..5c99bec 100644 --- a/src/utils/verifyotp.js +++ b/src/utils/verifyotp.ts @@ -1,4 +1,4 @@ -const verifyOtpTemplate = (url) => { +export const verifyOtpTemplate = (url: string) => { return ` @@ -22,5 +22,3 @@ const verifyOtpTemplate = (url) => { `; }; - -module.exports = verifyOtpTemplate;