-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from GoLembrar/test-user-controller
Test user controller adn Auth Controller
- Loading branch information
Showing
2 changed files
with
146 additions
and
2 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,20 +1,63 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { CredentialsDto } from './dto/credentials.dto'; | ||
import { vi } from 'vitest' | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
let authService: AuthService; | ||
|
||
const userToken = { | ||
token: 'token' | ||
} | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
providers: [AuthService], | ||
providers: [PrismaService, JwtService, { | ||
provide: AuthService, | ||
useValue: { | ||
login: async () => userToken, | ||
} | ||
}], | ||
}).compile(); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
authService = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
expect(authService).toBeDefined() | ||
}); | ||
|
||
describe('login', () => { | ||
it('should return a token when user is authenticated', async () => { | ||
const userLogin: CredentialsDto = { | ||
email: 'email', | ||
password: 'password' | ||
} | ||
|
||
expect(await controller.login(userLogin)).toEqual(userToken) | ||
}) | ||
|
||
it('should throw an error when user is not authenticated', async () => { | ||
vi.spyOn(authService, 'login').mockImplementation(() => Promise.reject(new Error('Invalid credentials'))); | ||
|
||
const userLogin: CredentialsDto = { | ||
email: 'email1', | ||
password: 'password2' | ||
} | ||
|
||
try { | ||
await controller.login(userLogin) | ||
} catch (error) { | ||
expect(error.message).toBe('Invalid credentials') | ||
} | ||
|
||
}) | ||
}) | ||
}); |
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 |
---|---|---|
|
@@ -2,20 +2,121 @@ import { Test, TestingModule } from '@nestjs/testing'; | |
import { UserController } from './user.controller'; | ||
import { UserService } from './user.service'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { Users } from '@prisma/client'; | ||
import { vi } from 'vitest' | ||
import { CreateUserDto } from './dto/create-user.dto'; | ||
import { UpdateUserDto } from './dto/update-user.dto'; | ||
|
||
describe('UserController', () => { | ||
let controller: UserController; | ||
let userService: UserService; | ||
|
||
const mockUserService: Partial<Users | null>[] = [ | ||
{ | ||
id: 1, | ||
email: '[email protected]', | ||
password: '123', | ||
phone: '999999999', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
{ | ||
id: 2, | ||
email: '[email protected]', | ||
password: '123', | ||
phone: '999999999', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
] | ||
|
||
const {password, ...withoutPass} = mockUserService[0] | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [UserController], | ||
providers: [UserService, PrismaService], | ||
providers: [PrismaService, JwtService, { | ||
provide: UserService, | ||
useValue: { | ||
findOne: async () => withoutPass, | ||
create: async () => mockUserService[1], | ||
update: async () => mockUserService[0], | ||
remove: async () => { }, | ||
}, | ||
}], | ||
}).compile(); | ||
|
||
controller = module.get<UserController>(UserController); | ||
userService = module.get<UserService>(UserService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
expect(userService).toBeDefined() | ||
}); | ||
|
||
describe('findOne', () => { | ||
it('should return a user in current section', async () => { | ||
const userExpected = withoutPass; | ||
|
||
const user = await controller.findOne({ user: { id: userExpected.id } }); | ||
expect(user).toEqual(userExpected); | ||
expect(user).not.toHaveProperty('password'); | ||
}); | ||
|
||
it('should return a 404 error', async () => { | ||
vi.spyOn(UserService.prototype, 'findOne').mockImplementation(() => Promise.reject(new Error('User not found'))); | ||
|
||
// Sim, isso funciona testando a exceção, mas não testando se a exceção foi lançada | ||
try { | ||
await controller.findOne({ user: { id: 3 } }); | ||
} catch (error) { | ||
expect(error.message).toBe('User not found'); | ||
} | ||
|
||
}) | ||
}) | ||
|
||
describe('create', () => { | ||
it('should create a new user', async () => { | ||
const userExpected = mockUserService[1]; | ||
|
||
const userToCreate: CreateUserDto = { | ||
email: '[email protected]', | ||
password: '123', | ||
phone: '999999999', | ||
} | ||
const user = await controller.create(userToCreate); | ||
|
||
|
||
expect(user).toEqual(userExpected); | ||
}); | ||
}) | ||
|
||
describe('update', () => { | ||
it('should update a user', async () => { | ||
const userExpected = mockUserService[0]; | ||
|
||
const userToUpdate: UpdateUserDto = { | ||
email: '[email protected]', | ||
phone: '999999998', | ||
password: '1234', | ||
} | ||
|
||
|
||
const user = await controller.update({ user: { id: userExpected.id } }, userToUpdate); | ||
expect(user).toEqual(userExpected); | ||
|
||
}); | ||
}) | ||
|
||
describe('remove', () => { | ||
it('should remove a user', async () => { | ||
const idExpected = mockUserService[0]; | ||
|
||
const user = await controller.remove({ user: { id: idExpected } }); | ||
expect(user).toBeUndefined() | ||
}); | ||
}) | ||
}); |