-
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 #19 from atlp-rwanda/ft-user-login-#187419169
#187419169 user can login via email and password
- Loading branch information
Showing
13 changed files
with
177 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
PORT = 3000 | ||
DB_CONNECTION = "" --> TODO: put your own connection string here | ||
TEST_DB = ""-->TODO: put your own testing database connection string here | ||
JWT_SECRET = ""-->TODO: put your own jsonwebtoken scret here |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -5,20 +5,74 @@ import User from "../src/sequelize/models/users"; | |
import * as userServices from "../src/services/user.service"; | ||
import sequelize, { connect } from "../src/config/dbConnection"; | ||
|
||
const userData:any = { | ||
name: 'yvanna', | ||
username: 'testuser', | ||
email: '[email protected]', | ||
password:'test1234', | ||
}; | ||
|
||
const loginData:any = { | ||
email:'[email protected]', | ||
password:"test1234" | ||
} | ||
describe("Testing user Routes", () => { | ||
beforeAll(async () => { | ||
try { | ||
await connect(); | ||
await User.destroy({truncate:true}) | ||
} catch (error) { | ||
sequelize.close(); | ||
} | ||
}, 20000); | ||
|
||
afterAll(async () => { | ||
await User.destroy({ truncate: true }); | ||
await sequelize.close(); | ||
}); | ||
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); | ||
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); | ||
expect(response.status).toBe(409); }, 20000); | ||
|
||
test('should return 500 when registering with an invalid credential', async () => { | ||
const userData = { | ||
email: '[email protected]', name: "", username: 'existinguser', }; | ||
const response = await request(app) | ||
.post('/api/v1/users/register') | ||
.send(userData); | ||
|
||
expect(response.status).toBe(500); }, 20000); }); | ||
|
||
test("should return all users in db --> given '/api/v1/users'", async () => { | ||
const spy = jest.spyOn(User, "findAll"); | ||
const spy2 = jest.spyOn(userServices, "getAllUsers"); | ||
const response = await request(app).get("/api/v1/users"); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(spy2).toHaveBeenCalled(); | ||
}, 20000); | ||
}); | ||
test("Should return status 401 to indicate Unauthorized user",async() =>{ | ||
const loggedInUser ={ | ||
email:userData.email, | ||
password:"test", | ||
}; | ||
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) | ||
expect(response.body.status).toBe(401); | ||
spyonOne.mockRestore(); | ||
}); | ||
}) |
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
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,4 @@ | ||
import bcrypt from 'bcrypt' | ||
export const comparePasswords = async(plainPassword: string, hashedPassword: string): Promise<boolean> => { | ||
return await bcrypt.compare(plainPassword, hashedPassword); | ||
} |
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,13 +1,15 @@ | ||
import { Router } from "express"; | ||
import { | ||
fetchAllUsers, | ||
createUserController } | ||
createUserController, | ||
userLogin } | ||
from "../controllers/userControllers"; | ||
|
||
const userRoutes = Router(); | ||
|
||
userRoutes.get("/", fetchAllUsers); | ||
userRoutes.post("/", createUserController) | ||
userRoutes.get("/", fetchAllUsers); | ||
userRoutes.post('/login',userLogin); | ||
userRoutes.post("/register", createUserController) | ||
|
||
|
||
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
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,11 @@ | ||
import { IUser } from "../types"; | ||
import { env } from "../utils/env"; | ||
import { sign,verify } from "jsonwebtoken"; | ||
|
||
export const generateToken = async(user:IUser) =>{ | ||
const accessToken = sign({email:user.email,password:user.password}, | ||
`${env.jwt_secret}`,{expiresIn:'72h'} | ||
); | ||
return accessToken; | ||
} | ||
|
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