-
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.
- Loading branch information
1 parent
220d208
commit a154645
Showing
10 changed files
with
171 additions
and
16 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 +1,11 @@ | ||
DB_TYPE='' | ||
DB_HOST='' | ||
DB_PORT='' | ||
DB_USERNAME='' | ||
DB_PASSWORD='' | ||
DB_DATABASE='' | ||
|
||
JWT_SECRET='' | ||
|
||
PORT='' | ||
SALT='' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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,155 @@ | ||
import { userRegister } from '../service/user.service'; | ||
import { AppDataSource } from '../repos/db'; | ||
import { User } from '../entity/User'; | ||
import bcrypt from 'bcrypt'; | ||
import { config } from "dotenv"; | ||
config(); | ||
|
||
|
||
jest.mock('../repos/db', () => ({ | ||
AppDataSource: { | ||
getRepository: jest.fn(), | ||
} | ||
})); | ||
jest.mock('bcrypt'); | ||
|
||
describe('userRegister', () => { | ||
const mockUserRepository = { | ||
findOne: jest.fn(), | ||
create: jest.fn(), | ||
save: jest.fn(), | ||
}; | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(AppDataSource.getRepository as jest.Mock).mockReturnValue(mockUserRepository); | ||
(bcrypt.hash as jest.Mock).mockResolvedValue('hashed_password'); | ||
}); | ||
|
||
it('should register a user successfully', async () => { | ||
mockUserRepository.findOne.mockResolvedValue(null); | ||
mockUserRepository.create.mockImplementation((user) => user as User); | ||
mockUserRepository.save.mockResolvedValue({ | ||
id: 1, | ||
name: 'testuser', | ||
email: '[email protected]', | ||
password: 'hashed_password', | ||
role: 'student', | ||
phone_number: '1234567890', | ||
avatar: 'avatar_url', | ||
date_of_birth: new Date(), | ||
gender: 'male', | ||
address: 'test address', | ||
identity_card: '123456789', | ||
additional_info: 'Some info', | ||
department: null, | ||
years_of_experience: null, | ||
}); | ||
|
||
const user = await userRegister( | ||
'testuser', | ||
'[email protected]', | ||
'password', | ||
'student', | ||
'1234567890', | ||
'avatar_url', | ||
new Date(), | ||
'male', | ||
'test address', | ||
'123456789', | ||
'Some info' | ||
); | ||
|
||
expect(bcrypt.hash).toHaveBeenCalledWith('password', Number(process.env.SALT)); | ||
expect(mockUserRepository.create).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
name: 'testuser', | ||
email: '[email protected]', | ||
password: 'hashed_password', | ||
role: 'student', | ||
phone_number: '1234567890', | ||
avatar: 'avatar_url', | ||
date_of_birth: expect.any(Date), | ||
gender: 'male', | ||
address: 'test address', | ||
identity_card: '123456789', | ||
additional_info: 'Some info', | ||
department: null, | ||
years_of_experience: null, | ||
}) | ||
); | ||
expect(mockUserRepository.save).toHaveBeenCalled(); | ||
expect(user).toHaveProperty('id'); | ||
}); | ||
|
||
it('should throw an error if a user with the same email or name already exists', async () => { | ||
mockUserRepository.findOne.mockResolvedValue({ id: 1 } as User); | ||
|
||
await expect( | ||
userRegister( | ||
'testuser', | ||
'[email protected]', | ||
'password', | ||
'student', | ||
'1234567890', | ||
'avatar_url', | ||
new Date(), | ||
'male', | ||
'test address', | ||
'123456789', | ||
'Some info' | ||
) | ||
).rejects.toThrow('User already exists with this email or username'); | ||
}); | ||
|
||
it('should set department and years_of_experience for professors', async () => { | ||
mockUserRepository.findOne.mockResolvedValue(null); | ||
mockUserRepository.create.mockImplementation((user) => user as User); | ||
mockUserRepository.save.mockResolvedValue({ | ||
id: 1, | ||
name: 'professoruser', | ||
email: '[email protected]', | ||
password: 'hashed_password', | ||
role: 'professor', | ||
phone_number: '0987654321', | ||
avatar: 'avatar_url', | ||
date_of_birth: new Date(), | ||
gender: 'female', | ||
address: 'test address', | ||
identity_card: '987654321', | ||
additional_info: 'Professor info', | ||
department: 'Science', | ||
years_of_experience: 10, | ||
}); | ||
|
||
const user = await userRegister( | ||
'professoruser', | ||
'[email protected]', | ||
'password', | ||
'professor', | ||
'0987654321', | ||
'avatar_url', | ||
new Date(), | ||
'female', | ||
'test address', | ||
'987654321', | ||
'Professor info', | ||
'Science', | ||
10 | ||
); | ||
|
||
expect(bcrypt.hash).toHaveBeenCalledWith('password', Number(process.env.SALT)); | ||
expect(mockUserRepository.create).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
name: 'professoruser', | ||
email: '[email protected]', | ||
password: 'hashed_password', | ||
role: 'professor', | ||
department: 'Science', | ||
years_of_experience: 10, | ||
}) | ||
); | ||
expect(mockUserRepository.save).toHaveBeenCalled(); | ||
expect(user.department).toBe('Science'); | ||
expect(user.years_of_experience).toBe(10); | ||
}); | ||
}); |