Skip to content

Commit

Permalink
Merge pull request #29 from fga-eps-mds/tests
Browse files Browse the repository at this point in the history
[tests]-adding_tests
  • Loading branch information
DaviMatheus authored Aug 27, 2024
2 parents 79bc9ab + 133da9c commit 8b6949c
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions test/user.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { UsersController } from 'src/users/users.controller';
import { UsersService } from 'src/users/users.service';
import { UserRole } from 'src/users/dtos/user-role.enum';
import { CreateUserDto } from 'src/users/dtos/create-user.dto';
import { UpdateRoleDto } from 'src/users/dtos/update-role.dto';
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from 'src/auth/auth.service'; // Import AuthService
import { JwtService } from '@nestjs/jwt'; // Import JwtService

describe('UsersController', () => {
let controller: UsersController;

const mockUser = {
_id: 'mockUserId',
name: 'Mock User',
email: '[email protected]',
role: UserRole.ALUNO,
};

const mockUserService = {
createUser: jest.fn().mockResolvedValue(mockUser),
verifyUser: jest.fn().mockResolvedValue(mockUser),
getSubscribedJourneys: jest.fn().mockResolvedValue([]),
getUsers: jest.fn().mockResolvedValue([mockUser]),
addJourneyToUser: jest.fn().mockResolvedValue(mockUser),
subscribeJourney: jest.fn().mockResolvedValue(mockUser),
unsubscribeJourney: jest.fn().mockResolvedValue(mockUser),
getUserById: jest.fn().mockResolvedValue(mockUser),
deleteUserById: jest.fn().mockResolvedValue(undefined),
updateUserRole: jest.fn().mockResolvedValue(mockUser),
};

const mockAuthService = {}; // Mock any methods you use from AuthService
const mockJwtService = {}; // Mock any methods you use from JwtService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers: [
{ provide: UsersService, useValue: mockUserService },
{ provide: AuthService, useValue: mockAuthService },
{ provide: JwtService, useValue: mockJwtService },
],
}).compile();

controller = module.get<UsersController>(UsersController);

});

it('should be defined', () => {
expect(controller).toBeDefined();
});

it('should create a user', async () => {
const createUserDto: CreateUserDto = {
name: 'New User',
email: '[email protected]',
password: 'password123',
username: ''
};
await expect(controller.createUser(createUserDto)).resolves.toEqual({
message: 'User created successfully. Please verify your email.',
});
});

it('should verify a user', async () => {
const token = 'validToken';
await expect(controller.verifyUser(token)).resolves.toEqual({
message: 'Account verified successfully',
});
});

it('should get subscribed journeys', async () => {
const userId = 'mockUserId';
await expect(controller.getSubscribedJourneys(userId)).resolves.toEqual([]);
});

it('should get all users', async () => {
await expect(controller.getUsers()).resolves.toEqual([mockUser]);
});

it('should add a journey to a user', async () => {
const userId = 'mockUserId';
const journeyId = 'mockJourneyId';
const body = { journeyId };
await expect(controller.addJourneyToUser(userId, body)).resolves.toEqual(
mockUser,
);
});

it('should subscribe a journey for a user', async () => {
const userId = 'mockUserId';
const journeyId = 'mockJourneyId';
await expect(
controller.subscribeJourney(userId, journeyId),
).resolves.toEqual(mockUser);
});

it('should unsubscribe a journey for a user', async () => {
const userId = 'mockUserId';
const journeyId = 'mockJourneyId';
await expect(
controller.unsubscribeJourney(userId, journeyId),
).resolves.toEqual(mockUser);
});

it('should get a user by ID', async () => {
const userId = 'mockUserId';
await expect(controller.getUserById(userId)).resolves.toEqual(mockUser);
});

it('should delete a user by ID', async () => {
const userId = 'mockUserId';
await expect(controller.deleteUserById(userId)).resolves.toBeUndefined();
});

it('should update user role', async () => {
const userId = 'mockUserId';
const updateRoleDto: UpdateRoleDto = { role: UserRole.ADMIN };
await expect(
controller.updateUserRole(userId, updateRoleDto),
).resolves.toEqual({
message: 'User role updated successfully',
user: mockUser,
});
});
});

0 comments on commit 8b6949c

Please sign in to comment.