Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Unitaire #14

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions backend-nest/src/api/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,53 @@ import { AuthService } from './auth.service';
import { UsersService } from '../users/users.service';
import { JwtService } from '@nestjs/jwt';
import { UnauthorizedException } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { CreateUserDto } from '../users/dto/create-user.dto';
import { user_role } from '@prisma/client';
import * as bcrypt from 'bcrypt';

describe('AuthService', () => {
let service: AuthService;
let usersService: UsersService;
let jwtService: JwtService;
let prisma: PrismaClient;
let testUser: CreateUserDto;
let testUserId: number;

beforeEach(async () => {
prisma = new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL_TEST,
},
},
});

testUser = {
email: `test${Date.now()}@example.com`,
password: await bcrypt.hash('bobpassword', await bcrypt.genSalt()),
firstname: 'firstname',
lastname: 'lastname',
role: user_role.STUDENT,
classGroupId: 55,
};

const createdUser = await prisma.user.create({
data: testUser,
});
testUserId = createdUser.id;

testUser.password = 'bobpassword';

const module: TestingModule = await Test.createTestingModule({
providers: [
AuthService,
{
provide: UsersService,
useValue: {
findOneByEmail: jest.fn().mockImplementation((email) => {
return email === '[email protected]'
? { id: 19, email: '[email protected]', password: 'password' }
return email === testUser.email
? { id: testUserId, password: testUser.password, ...testUser }
: null;
}),
},
Expand All @@ -39,14 +70,11 @@ describe('AuthService', () => {
jwtService = module.get<JwtService>(JwtService);
});

it('should be defined', () => {
expect(service).toBeDefined();
afterEach(async () => {
await prisma.$disconnect();
});

describe('signIn', () => {
it('should return access token', async () => {
const result = await service.signIn('[email protected]', 'password');
expect(result).toEqual({ access_token: 'test_token' });
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
47 changes: 27 additions & 20 deletions backend-nest/src/api/classgroups/classgroups.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ClassgroupsService } from './classgroups.service';
import { PrismaService } from '../../prisma/prisma.service';
import { NotFoundException } from '@nestjs/common';

describe('ClassgroupsService', () => {
let service: ClassgroupsService;
Expand All @@ -22,7 +23,13 @@ describe('ClassgroupsService', () => {
findMany: jest.fn().mockResolvedValue([]),
findUnique: jest.fn().mockResolvedValue({}),
findFirst: jest.fn().mockResolvedValue({}),
delete: jest.fn().mockResolvedValue({}),
delete: jest.fn().mockImplementation((input) => {
return {
id: input.where.id,
file: 'fileTest.xml',
name: 'fileTest',
};
}),
update: jest.fn().mockResolvedValue({}),
},
},
Expand Down Expand Up @@ -98,25 +105,25 @@ describe('ClassgroupsService', () => {
});
});

// describe('delete', () => {
// it('delete an existing classgroup', async () => {
// const newClassgroup = {
// file: 'fileTest.xml',
// name: 'fileTest',
// };
// const createdClassgroup = await service.create(newClassgroup);
// const deletedClassgroup = await service.delete(createdClassgroup.id);
//
// expect(deletedClassgroup).toEqual(createdClassgroup);
//
// try {
// await service.findOne(createdClassgroup.id);
// } catch (e) {
// expect(e).toBeInstanceOf(NotFoundException);
// expect(e.message).toEqual('Classgroup not found');
// }
// });
// });
describe('delete', () => {
it('delete an existing classgroup', async () => {
const newClassgroup = {
file: 'fileTest.xml',
name: 'fileTest',
};
const createdClassgroup = await service.create(newClassgroup);
const deletedClassgroup = await service.delete(createdClassgroup.id);

expect(deletedClassgroup).toEqual(createdClassgroup);

try {
await service.findOne(createdClassgroup.id);
} catch (e) {
expect(e).toBeInstanceOf(NotFoundException);
expect(e.message).toEqual('Classgroup not found');
}
});
});

describe('upsert', () => {
it('update an existing classgroup or create a new one if it does not exist', async () => {
Expand Down
6 changes: 5 additions & 1 deletion backend-nest/src/api/classgroups/classgroups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export class ClassgroupsService {
}

async delete(id: number) {
if (!id) {
const classgroup = await this.prisma.classgroup.findUnique({
where: { id: id },
});

if (!classgroup) {
throw new Error('The class does not exist');
}
return this.prisma.classgroup.delete({
Expand Down
7 changes: 7 additions & 0 deletions backend-nest/src/api/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { PrismaService } from '../../prisma/prisma.service';
import { UpdateUserDto } from './dto/update-user.dto';
import { JwtService } from '@nestjs/jwt';

describe('UsersController', () => {
let controller: UsersController;
Expand All @@ -26,6 +27,12 @@ describe('UsersController', () => {
},
},
PrismaService,
{
provide: JwtService,
useValue: {
sign: jest.fn().mockResolvedValue('mocked-token'),
},
},
],
}).compile();

Expand Down
Loading
Loading