Skip to content

Commit

Permalink
test-cases-email-services
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayw1 committed Dec 17, 2024
1 parent 799de4b commit 2c3d77f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
86 changes: 37 additions & 49 deletions webiu-server/__tests__/authController.test.js
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'),
}));
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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]',
Expand All @@ -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');
});
});
18 changes: 18 additions & 0 deletions webiu-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2c3d77f

Please sign in to comment.