Skip to content

Commit

Permalink
User Test
Browse files Browse the repository at this point in the history
  • Loading branch information
PluckySquirrel committed Nov 15, 2024
1 parent 220d208 commit a154645
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 16 deletions.
10 changes: 10 additions & 0 deletions env/test.env.example
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=''
7 changes: 0 additions & 7 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@types/find": "^0.2.4",
"@types/fs-extra": "^11.0.4",
"@types/i18next": "^13.0.0",
"@types/jasmine": "^5.1.4",
"@types/jest": "^29.5.14",
"@types/jsonfile": "^6.1.4",
"@types/jsonwebtoken": "^9.0.7",
Expand Down
1 change: 0 additions & 1 deletion src/service/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Section } from "../entity/Section";
import { Lesson } from "../entity/Lesson";
import { Payment } from "../entity/Payment";
import { Review } from "../entity/Review";
import { Category } from "./../entity/Category";
import { In } from "typeorm";
import { User } from "../entity/User";
import { CourseStatus } from "../enum/course.enum"
Expand Down
4 changes: 2 additions & 2 deletions src/service/enrollment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { User } from '../entity/User';
import { Course } from '../entity/Course';
import { Payment } from '../entity/Payment';
import { Lesson } from '../entity/Lesson';
import { Section } from '@src/entity/Section';
import { Section } from '../entity/Section';
import { getEnrollmentLesson, upsertEnrollmentLesson } from './enrollmentlesson.service';
import { Enrollmentlesson } from '@src/entity/EnrollmentLesson';
import { Enrollmentlesson } from '../entity/EnrollmentLesson';
import { In } from "typeorm";


Expand Down
4 changes: 2 additions & 2 deletions src/service/enrollmentlesson.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Enrollmentlesson } from "@src/entity/EnrollmentLesson";
import { AppDataSource } from "@src/repos/db";
import { Enrollmentlesson } from "../entity/EnrollmentLesson";
import { AppDataSource } from "../repos/db";

const enrollmentlessonRepository =
AppDataSource.getRepository(Enrollmentlesson);
Expand Down
2 changes: 1 addition & 1 deletion src/service/lession.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AppDataSource } from "../repos/db";
import { Lesson } from "../entity/Lesson";
import { Section } from "@src/entity/Section";
import { Section } from "../entity/Section";
import { In } from 'typeorm';

const lessonRepository = AppDataSource.getRepository(Lesson)
Expand Down
1 change: 0 additions & 1 deletion src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AppDataSource } from '../repos/db';
import { User } from '../entity/User';
import { Course } from '../entity/Course';
import { Payment } from '../entity/Payment';
import { ALL } from 'dns';
import { In } from 'typeorm';

const courseRepository = AppDataSource.getRepository(Course);
Expand Down
2 changes: 1 addition & 1 deletion src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Jwt from "jsonwebtoken";


import { calculateTotalTimeAndLessons } from "../service/lession.service";
import { getSectionsWithLessons, getUserPurchasedCourses } from "../service/course.service";
import { getSectionsWithLessons } from "../service/course.service";

const userRepository = AppDataSource.getRepository(User);
const courseRepository = AppDataSource.getRepository(Course);
Expand Down
155 changes: 155 additions & 0 deletions src/test/user.spec.ts
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);
});
});

0 comments on commit a154645

Please sign in to comment.