Skip to content

Commit

Permalink
Merge pull request #9 from olaviolacerda/feature/testing-unit
Browse files Browse the repository at this point in the history
test: add controllers tests
  • Loading branch information
olaviolacerda authored Mar 2, 2024
2 parents 4e085e2 + 8556914 commit 0846ec6
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 9 deletions.
67 changes: 67 additions & 0 deletions test/e2e/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { HttpStatus, INestApplication } from '@nestjs/common';
import request from 'supertest';
import { TestingModule, Test } from '@nestjs/testing';

import { AppModule } from '../../src/core/modules/app.module';

describe('AuthController (e2e)', () => {
let app: INestApplication;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

afterAll(async () => {
await app.close();
});

it('it should login a valid user', () => {
const credentials = {
username: 'test',
password: '123',
};

return request(app.getHttpServer())
.post(`/auth/login`)
.send(credentials)
.expect(({ body }: request.Response) => {
expect(body).toBeDefined();
expect(body['accessToken']).toBeDefined();
expect(body['refreshToken']).toBeDefined();
})
.expect(HttpStatus.OK);
});

it('it should NOT login a user with invalid password', () => {
const credentials = {
username: 'test',
password: 'fakepass',
};

return request(app.getHttpServer())
.post(`/auth/login`)
.send(credentials)
.expect(HttpStatus.UNAUTHORIZED);
});

it('it should NOT login an user if it not exists', () => {
const credentials = {
username: 'idonotexist',
password: '123',
};

return request(app.getHttpServer())
.post(`/auth/login`)
.send(credentials)
.expect(({ body }: request.Response) => {
expect(body).toBeDefined();
expect(body.message).toBe('Something is wrong with your credentials');
})
.expect(HttpStatus.BAD_REQUEST);
});
});
File renamed without changes.
4 changes: 2 additions & 2 deletions test/unit/mocks/users.mock.ts → test/mocks/users.mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Role } from '../../../src/common/enums/role.enum';
import { User } from '../../../src/core/entities/user.entity';
import { Role } from '../../src/common/enums/role.enum';
import { User } from '../../src/core/entities/user.entity';

export const mockUser: User = {
id: 'id',
Expand Down
2 changes: 1 addition & 1 deletion test/unit/auth/strategies/local.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createMock } from '@golevelup/ts-jest';

import { LocalStrategy } from '../../../../src/core/auth/strategies/local.strategy';
import { AuthService } from '../../../../src/core/providers/services/auth.service';
import { mockUser } from '../../mocks/users.mock';
import { mockUser } from '../../../mocks/users.mock';

describe('LocalStrategy', () => {
let authService: AuthService;
Expand Down
4 changes: 2 additions & 2 deletions test/unit/controllers/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import * as httpMock from 'node-mocks-http';
import { AuthController } from '../../../src/api/controllers/auth.controller';
import { UsersService } from '../../../src/core/providers/services/users.service';
import { AuthService } from './../../../src/core/providers/services/auth.service';
import { UsersRepositoryFake, mockUser } from '../mocks/users.mock';
import { UsersRepositoryFake, mockUser } from '../../mocks/users.mock';
import { User } from '../../../src/core/entities/user.entity';
import { AuthHelper } from '../../../src/core/providers/helpers/auth.helper';
import { mockTokens } from '../mocks/auth.mock';
import { mockTokens } from '../../mocks/auth.mock';

describe('AuthController', () => {
let controller: AuthController;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/controllers/users.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getRepositoryToken } from '@nestjs/typeorm';

import { UsersController } from '../../../src/api/controllers/users.controller';
import { UsersService } from '../../../src/core/providers/services/users.service';
import { UsersRepositoryFake, mockUser } from '../mocks/users.mock';
import { UsersRepositoryFake, mockUser } from '../../mocks/users.mock';
import { CreateUserDto } from '../../../src/common/dtos/users/create-user.dto';
import { User } from '../../../src/core/entities/user.entity';
import { Role } from '../../../src/common/enums/role.enum';
Expand Down
4 changes: 2 additions & 2 deletions test/unit/providers/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { AuthService } from '../../../../src/core/providers/services/auth.servic
import { UsersService } from '../../../../src/core/providers/services/users.service';
import { User } from '../../../../src/core/entities/user.entity';
import { AuthHelper } from '../../../../src/core/providers/helpers/auth.helper';
import { mockJwtService, mockTokens } from '../../mocks/auth.mock';
import { UsersRepositoryFake, mockUser } from '../../mocks/users.mock';
import { mockJwtService, mockTokens } from '../../../mocks/auth.mock';
import { UsersRepositoryFake, mockUser } from '../../../mocks/users.mock';

let compareResponse = true;

Expand Down
2 changes: 1 addition & 1 deletion test/unit/providers/services/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { User } from '../../../../src/core/entities/user.entity';
import { Role } from '../../../../src/common/enums/role.enum';
import { CreateUserDto } from '../../../../src/common/dtos/users/create-user.dto';
import { UpdateUserDto } from '../../../../src/common/dtos/users/update-user.dto';
import { UsersRepositoryFake, mockUser } from '../../mocks/users.mock';
import { UsersRepositoryFake, mockUser } from '../../../mocks/users.mock';

describe('UsersService', () => {
let service: UsersService;
Expand Down

0 comments on commit 0846ec6

Please sign in to comment.