-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor error helper class to use CustomError class
The error helper class has been refactored to use the CustomError class instead of the generic Error class.
- Loading branch information
OomsOoms
committed
Aug 2, 2024
1 parent
2ee8b4d
commit a4c2b3f
Showing
5 changed files
with
59 additions
and
98 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,32 +1,32 @@ | ||
class Error { | ||
class CustomError extends Error { | ||
constructor(status, message) { | ||
super(message); | ||
this.status = status; | ||
this.message = message; | ||
} | ||
|
||
static invalidRequest(message = 'Invalid request') { | ||
return new Error(400, message); | ||
return new CustomError(400, message); | ||
} | ||
|
||
static invalidCredentials(message = 'Invalid credentials') { | ||
return new Error(401, message); | ||
return new CustomError(401, message); | ||
} | ||
|
||
static accountNotActive(message = 'Account not active') { | ||
return new Error(403, message); | ||
return new CustomError(403, message); | ||
} | ||
|
||
static userNotFound(message = 'User not found') { | ||
return new Error(404, message); | ||
return new CustomError(404, message); | ||
} | ||
|
||
static mongoConflictError(message = 'MongoDB conflict error') { | ||
return new Error(409, message); | ||
return new CustomError(409, message); | ||
} | ||
|
||
static serverError(message = 'Internal server error') { | ||
return new Error(500, message); | ||
return new CustomError(500, message); | ||
} | ||
} | ||
|
||
module.exports = Error; | ||
module.exports = CustomError; |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
const { User } = require('../../src/api/models'); | ||
const { userService } = require('../../src/api/services'); | ||
const { getUserByUsername } = require('../../src/api/services/user.service'); | ||
|
||
jest.mock('../../src/api/models/user.model.js', () => ({ | ||
jest.mock('../../src/api/models/user.model', () => ({ | ||
findOne: jest.fn(), | ||
})); | ||
|
||
|
@@ -14,18 +14,14 @@ describe('userService.getUserByUsername', () => { | |
const mockUser = { username: 'testUser', email: '[email protected]' }; | ||
User.findOne.mockResolvedValue(mockUser); | ||
|
||
const result = await userService.getUserByUsername('testUser'); | ||
const result = await getUserByUsername('testUser'); | ||
expect(result).toEqual(mockUser); | ||
expect(User.findOne).toHaveBeenCalledWith({ username: 'testUser' }); | ||
}); | ||
|
||
it('should throw an error if the user does not exist', async () => { | ||
User.findOne.mockResolvedValue(null); | ||
|
||
try { | ||
await userService.getUserByUsername('testUser'); | ||
} catch (error) { | ||
expect(error.message).toBe("User with username 'testUser' not found"); | ||
} | ||
await expect(getUserByUsername('testUser')).rejects.toThrow("User with username 'testUser' 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,43 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const { User } = require('../../src/api/models'); | ||
const { userService } = require('../../src/api/services'); | ||
const { requestVerification } = require('../../src/api/services/user.service'); | ||
const sendEmail = require('../../src/api/helpers/sendEmail'); | ||
|
||
jest.mock('jsonwebtoken', () => ({ | ||
sign: jest.fn().mockImplementation(() => 'mockedToken'), | ||
})); | ||
|
||
jest.mock('../../src/api/models/user.model.js', () => ({ | ||
findByIdAndUpdate: jest.fn(), | ||
findById: jest.fn(), | ||
jest.mock('../../src/api/models/user.model', () => ({ | ||
findOne: jest.fn(), | ||
})); | ||
|
||
const sendEmail = require('../../src/api/helpers/sendEmail'); | ||
|
||
jest.mock('../../src/api/helpers/sendEmail', () => jest.fn()); | ||
|
||
describe('userService.requestVerification', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
// Reset the mock implementation to return a controlled value | ||
jwt.sign.mockImplementation(() => 'mockedToken'); | ||
}); | ||
|
||
it('should throw an error if user not found or already verified', async () => { | ||
User.findOne.mockResolvedValueOnce(null); | ||
try { | ||
await userService.requestVerification('[email protected]'); | ||
} catch (error) { | ||
expect(error.message).toBe('User not found'); | ||
} | ||
}); | ||
|
||
it('should throw an error if user is already verified', async () => { | ||
const user = { _id: '123', email: '[email protected]', active: true }; | ||
User.findOne.mockResolvedValueOnce(user); | ||
try { | ||
await userService.requestVerification('[email protected]') | ||
} catch (error) { | ||
expect(error.message).toBe('User already verified'); | ||
} | ||
}); | ||
|
||
it('should send an email with verification link if user exists and not verified', async () => { | ||
const user = { _id: '123', email: '[email protected]', active: false }; | ||
User.findOne.mockResolvedValueOnce(user); | ||
const mockedUser = { _id: '123', email: '[email protected]', active: false }; | ||
User.findOne.mockResolvedValueOnce(mockedUser); | ||
const verificationLink = 'http://localhost:4000/api/users/verify?token=mockedToken'; | ||
|
||
await userService.requestVerification('[email protected]'); | ||
await requestVerification('[email protected]'); | ||
|
||
expect(User.findOne).toHaveBeenCalledWith({ email: '[email protected]' }); | ||
expect(sendEmail).toHaveBeenCalledWith('[email protected]', 'Verify your email', verificationLink); | ||
}); | ||
|
||
it('should throw an error if user not found', async () => { | ||
User.findOne.mockResolvedValueOnce(null); | ||
|
||
await expect(requestVerification('[email protected]')).rejects.toThrow('User not found'); | ||
}); | ||
|
||
it('should throw an error if user is already verified', async () => { | ||
const mockedUser = { _id: '123', email: '[email protected]', active: true }; | ||
User.findOne.mockResolvedValueOnce(mockedUser); | ||
|
||
await expect(requestVerification('[email protected]')).rejects.toThrow('User already verified'); | ||
}); | ||
}); |
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