-
Notifications
You must be signed in to change notification settings - Fork 36
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
2 changed files
with
55 additions
and
49 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,9 +1,12 @@ | ||
const request = require('supertest'); | ||
const express = require('express'); | ||
const User = require('../models/User'); | ||
const { sendVerificationEmail } = require('../services/emailServices'); | ||
const { signToken } = require('../utils/jwt'); | ||
const crypto = require('crypto'); | ||
const authController = require('../controllers/authController'); | ||
|
||
jest.mock('../services/emailServices'); // Mock email service | ||
jest.mock('../utils/jwt', () => ({ | ||
signToken: jest.fn().mockReturnValue('mockedToken'), | ||
})); | ||
|
@@ -12,43 +15,14 @@ const app = express(); | |
app.use(express.json()); | ||
app.post('/register', authController.register); | ||
app.post('/login', authController.login); | ||
app.get('/verify-email', authController.verifyEmail); // GET method to verify email | ||
|
||
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'); | ||
jest.clearAllMocks(); // Clear mocks before each test | ||
}); | ||
|
||
|
||
// Test failed user registration - email already exists | ||
it('should return an error when email already exists during registration', async () => { | ||
const mockUser = { | ||
|
@@ -86,6 +60,21 @@ describe('Auth Controller Tests', () => { | |
expect(response.body.message).toBe('Passwords do not match'); | ||
}); | ||
|
||
// 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 successful user login | ||
it('should login a user successfully', async () => { | ||
const mockUser = { | ||
|
@@ -94,6 +83,7 @@ describe('Auth Controller Tests', () => { | |
password: 'password123', | ||
matchPassword: jest.fn().mockResolvedValue(true), | ||
githubId: 'john-github', | ||
isVerified: true, | ||
}; | ||
|
||
User.findOne = jest.fn().mockResolvedValue(mockUser); | ||
|
@@ -105,6 +95,7 @@ describe('Auth Controller Tests', () => { | |
|
||
expect(response.status).toBe(200); | ||
expect(response.body.status).toBe('success'); | ||
expect(response.body.message).toBe('Login successful'); | ||
expect(response.body.data.user).toEqual({ | ||
id: mockUser._id, | ||
name: mockUser.name, | ||
|
@@ -120,7 +111,7 @@ describe('Auth Controller Tests', () => { | |
_id: '60d6f96a9b1f8f001c8f27c5', | ||
email: '[email protected]', | ||
password: 'password123', | ||
matchPassword: jest.fn().mockResolvedValue(false), | ||
matchPassword: jest.fn().mockResolvedValue(false), | ||
}; | ||
|
||
User.findOne = jest.fn().mockResolvedValue(mockUser); | ||
|
@@ -137,7 +128,7 @@ describe('Auth Controller Tests', () => { | |
|
||
// 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); | ||
User.findOne = jest.fn().mockResolvedValue(null); | ||
|
||
const response = await request(app).post('/login').send({ | ||
email: '[email protected]', | ||
|
@@ -149,28 +140,25 @@ describe('Auth Controller Tests', () => { | |
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', | ||
// Test failed user login - email not verified | ||
it('should return an error if email is not verified during login', async () => { | ||
const mockUser = { | ||
_id: '60d6f96a9b1f8f001c8f27c5', | ||
email: '[email protected]', | ||
password: 'password123', | ||
confirmPassword: 'password123', | ||
}); | ||
matchPassword: jest.fn().mockResolvedValue(true), | ||
isVerified: false, | ||
}; | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body.status).toBe('error'); | ||
expect(response.body.message).toBe('Invalid email format'); | ||
}); | ||
User.findOne = jest.fn().mockResolvedValue(mockUser); | ||
|
||
// 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]', | ||
email: '[email protected]', | ||
password: 'password123', | ||
}); | ||
|
||
expect(response.status).toBe(401); | ||
expect(response.body.status).toBe('error'); | ||
expect(response.body.message).toBe('User not found'); | ||
expect(response.body.message).toBe('Please verify your email to login'); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.