-
Notifications
You must be signed in to change notification settings - Fork 37
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
32 changed files
with
2,607 additions
and
519 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
PORT=5100 | ||
MONGODB_URI=mongodb://localhost:27017/webiu | ||
JWT_SECRET=your_jwt_secret | ||
GITHUB_ACCESS_TOKEN=your_github_access_token_add_here | ||
|
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,176 @@ | ||
const request = require('supertest'); | ||
const express = require('express'); | ||
const User = require('../models/User'); | ||
const { signToken } = require('../utils/jwt'); | ||
const authController = require('../controllers/authController'); | ||
|
||
jest.mock('../utils/jwt', () => ({ | ||
signToken: jest.fn().mockReturnValue('mockedToken'), | ||
})); | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
app.post('/register', authController.register); | ||
app.post('/login', authController.login); | ||
|
||
describe('Auth Controller Tests', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
// Test successful user registration | ||
it('should register a user successfully', async () => { | ||
const mockUser = { | ||
_id: '60d6f96a9b1f8f001c8f27c5', | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: 'password123', | ||
}; | ||
|
||
User.findOne = jest.fn().mockResolvedValue(null); | ||
User.prototype.save = jest.fn().mockResolvedValue(mockUser); | ||
|
||
const response = await request(app).post('/register').send({ | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: 'password123', | ||
confirmPassword: 'password123', | ||
}); | ||
|
||
response.body.data.user.id = mockUser._id; | ||
|
||
expect(response.status).toBe(201); | ||
expect(response.body.status).toBe('success'); | ||
expect(response.body.data.user).toEqual({ | ||
id: mockUser._id, | ||
name: mockUser.name, | ||
email: mockUser.email, | ||
}); | ||
expect(response.body.data.token).toBe('mockedToken'); | ||
}); | ||
|
||
// Test failed user registration - email already exists | ||
it('should return an error when email already exists during registration', async () => { | ||
const mockUser = { | ||
_id: '60d6f96a9b1f8f001c8f27c5', | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: 'password123', | ||
}; | ||
|
||
User.findOne = jest.fn().mockResolvedValue(mockUser); | ||
|
||
const response = await request(app).post('/register').send({ | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: 'password123', | ||
confirmPassword: 'password123', | ||
}); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body.status).toBe('error'); | ||
expect(response.body.message).toBe('User already exists'); | ||
}); | ||
|
||
// Test failed user registration - password mismatch | ||
it('should return an error when passwords do not match', async () => { | ||
const response = await request(app).post('/register').send({ | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: 'password123', | ||
confirmPassword: 'password456', | ||
}); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body.status).toBe('error'); | ||
expect(response.body.message).toBe('Passwords do not match'); | ||
}); | ||
|
||
// Test successful user login | ||
it('should login a user successfully', async () => { | ||
const mockUser = { | ||
_id: '60d6f96a9b1f8f001c8f27c5', | ||
email: '[email protected]', | ||
password: 'password123', | ||
matchPassword: jest.fn().mockResolvedValue(true), | ||
githubId: 'john-github', | ||
}; | ||
|
||
User.findOne = jest.fn().mockResolvedValue(mockUser); | ||
|
||
const response = await request(app).post('/login').send({ | ||
email: '[email protected]', | ||
password: 'password123', | ||
}); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body.status).toBe('success'); | ||
expect(response.body.data.user).toEqual({ | ||
id: mockUser._id, | ||
name: mockUser.name, | ||
email: mockUser.email, | ||
githubId: mockUser.githubId, | ||
}); | ||
expect(response.body.data.token).toBe('mockedToken'); | ||
}); | ||
|
||
// Test failed user login - incorrect password | ||
it('should return an error for incorrect password during login', async () => { | ||
const mockUser = { | ||
_id: '60d6f96a9b1f8f001c8f27c5', | ||
email: '[email protected]', | ||
password: 'password123', | ||
matchPassword: jest.fn().mockResolvedValue(false), | ||
}; | ||
|
||
User.findOne = jest.fn().mockResolvedValue(mockUser); | ||
|
||
const response = await request(app).post('/login').send({ | ||
email: '[email protected]', | ||
password: 'wrongPassword', | ||
}); | ||
|
||
expect(response.status).toBe(401); | ||
expect(response.body.status).toBe('error'); | ||
expect(response.body.message).toBe('Invalid email or password'); | ||
}); | ||
|
||
// Test failed user login - user not found | ||
it('should return an error if user is not found during login', async () => { | ||
User.findOne = jest.fn().mockResolvedValue(null); | ||
|
||
const response = await request(app).post('/login').send({ | ||
email: '[email protected]', | ||
password: 'password123', | ||
}); | ||
|
||
expect(response.status).toBe(401); | ||
expect(response.body.status).toBe('error'); | ||
expect(response.body.message).toBe('User not found'); | ||
}); | ||
|
||
// Test failed user registration - invalid email format | ||
it('should return an error for invalid email format during registration', async () => { | ||
const response = await request(app).post('/register').send({ | ||
name: 'John Doe', | ||
email: 'invalid-email', | ||
password: 'password123', | ||
confirmPassword: 'password123', | ||
}); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body.status).toBe('error'); | ||
expect(response.body.message).toBe('Invalid email format'); | ||
}); | ||
|
||
// Test failed user login - missing fields | ||
it('should return an error if required fields are missing during login', async () => { | ||
const response = await request(app).post('/login').send({ | ||
email: '[email protected]', | ||
}); | ||
|
||
expect(response.status).toBe(401); | ||
expect(response.body.status).toBe('error'); | ||
expect(response.body.message).toBe('User not found'); | ||
}); | ||
}); |
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,16 @@ | ||
const mongoose = require('mongoose'); | ||
require('dotenv').config(); | ||
const colors = require('colors'); | ||
|
||
|
||
const connectDB = async () => { | ||
try { | ||
await mongoose.connect(process.env.MONGODB_URI); | ||
console.log('MongoDB connection established successfully.'.green.bold.underline); | ||
} catch (error) { | ||
console.error('Error: MongoDB connection failed. Please check the database server and configuration.'.red.bold.underline); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
module.exports = connectDB; |
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,103 @@ | ||
// controllers/authController.js | ||
const User = require('../models/User'); | ||
const { signToken } = require('../utils/jwt'); | ||
|
||
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/; | ||
|
||
|
||
const register = async (req, res) => { | ||
const { name, email, password, confirmPassword, githubId } = req.body; | ||
|
||
if (!emailRegex.test(email)) { | ||
return res.status(400).json({ | ||
status: 'error', | ||
message: 'Invalid email format', | ||
}); | ||
} | ||
|
||
if (password !== confirmPassword) { | ||
return res.status(400).json({ | ||
status: 'error', | ||
message: 'Passwords do not match', | ||
}); | ||
} | ||
|
||
try { | ||
const existingUser = await User.findOne({ email }); | ||
if (existingUser) { | ||
return res.status(400).json({ | ||
status: 'error', | ||
message: 'User already exists', | ||
}); | ||
} | ||
|
||
const user = new User({ name, email, password, githubId }); | ||
await user.save(); | ||
|
||
const token = signToken(user); | ||
|
||
res.status(201).json({ | ||
status: 'success', | ||
message: 'User registered successfully', | ||
data: { | ||
user: { | ||
id: user._id, | ||
name: user.name, | ||
email: user.email, | ||
}, | ||
token, | ||
}, | ||
}); | ||
} catch (error) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
|
||
const login = async (req, res) => { | ||
const { email, password } = req.body; | ||
|
||
try { | ||
const user = await User.findOne({ email }); | ||
|
||
if(!user){ | ||
return res.status(401).json({ | ||
status: 'error', | ||
message: 'User not found', | ||
}); | ||
} | ||
if (!user || !(await user.matchPassword(password))) { | ||
return res.status(401).json({ | ||
status: 'error', | ||
message: 'Invalid email or password', | ||
}); | ||
} | ||
|
||
|
||
const token = signToken(user); | ||
|
||
res.status(200).json({ | ||
status: 'success', | ||
message: 'Login successful', | ||
data: { | ||
user: { | ||
id: user._id, | ||
name: user.name, | ||
email: user.email, | ||
githubId:user.githubId | ||
}, | ||
token, | ||
}, | ||
}); | ||
} catch (error) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = { register, login }; |
Oops, something went wrong.