Skip to content

Commit

Permalink
Merge pull request #152 from RJ-SMTR/feature/#94-tests-phase-1
Browse files Browse the repository at this point in the history
Feature/#94 tests phase 1
  • Loading branch information
williamfl2007 committed Dec 15, 2023
2 parents e12dbe4 + dcbae4b commit e12e9c0
Show file tree
Hide file tree
Showing 39 changed files with 1,831 additions and 629 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ lerna-debug.log*
.data
/files
.env
/ormconfig.json
/ormconfig.json

# Other
local_dev/
temp/
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run lint
npm run lint;
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ O [Projeto do App CCT](https://github.com/RJ-SMTR/app-cct) consome esta API.
* [Descrição](#descrição)
* [Table of Contents](#table-of-contents)
* [Quick run](#quick-run)
* [Comfortable development](#comfortable-development)
* [Desenvolvimento confortável](#desenvolvimento-confortável)
* [Links](#links)
* [Automatic update of dependencies](#automatic-update-of-dependencies)
* [Banco de dados](#banco-de-dados)
Expand Down
4 changes: 1 addition & 3 deletions env-example
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,4 @@ TWITTER_CONSUMER_SECRET=
WORKER_HOST=redis://redis:6379/1

TEST_ADMIN_EMAIL=
TEST_ADMIN_PASSWORD=
TEST_LICENSEE_PERMIT_CODE=
TEST_LICENSEE_PASSWORD=
TEST_ADMIN_PASSWORD=
34 changes: 13 additions & 21 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"class-validator": "0.14.0",
"date-fns": "^2.30.0",
"fb": "2.0.0",
"gerador-validador-cpf": "^5.0.2",
"google-auth-library": "8.8.0",
"handlebars": "4.7.7",
"multer": "1.4.4",
Expand Down
18 changes: 0 additions & 18 deletions src/auth-licensee/auth-licensee.controller.spec.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/auth-licensee/auth-licensee.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { LoginResponseType } from 'src/utils/types/auth/login-response.type';
import { AuthLicenseeService } from './auth-licensee.service';
import { AuthLicenseeLoginDto } from './dto/auth-licensee-login.dto';
import { AuthRegisterLicenseeDto } from './dto/auth-register-licensee.dto';
import { IALConcludeRegistration } from './interfaces/al-conclude-registration.interface';
import { IALInviteProfile } from './interfaces/al-invite-profile.interface';

@ApiTags('Auth')
@Controller({
Expand Down Expand Up @@ -41,7 +43,7 @@ export class AuthLicenseeController {
async invite(
@Param('hash', MailHistoryValidationPipe)
hash: string,
): Promise<void | object> {
): Promise<IALInviteProfile> {
return await this.authLicenseeService.getInviteProfile(hash);
}

Expand All @@ -52,7 +54,7 @@ export class AuthLicenseeController {
@Param('hash', MailHistoryValidationPipe)
hash: string,
@Body() data: AuthRegisterLicenseeDto,
): Promise<void | object> {
): Promise<IALConcludeRegistration> {
return await this.authLicenseeService.concludeRegistration(data, hash);
}
}
206 changes: 202 additions & 4 deletions src/auth-licensee/auth-licensee.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,216 @@
import { Provider } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Test, TestingModule } from '@nestjs/testing';
import { CoreBankService } from 'src/core-bank/core-bank.service';
import { ForgotService } from 'src/forgot/forgot.service';
import { MailHistory } from 'src/mail-history/entities/mail-history.entity';
import { MailHistoryService } from 'src/mail-history/mail-history.service';
import { MailService } from 'src/mail/mail.service';
import { User } from 'src/users/entities/user.entity';
import { UsersService } from 'src/users/users.service';
import { AuthLicenseeService } from './auth-licensee.service';
import { SgtuService } from 'src/sgtu/sgtu.service';
import { JaeService } from 'src/jae/jae.service';
import { InviteStatus } from 'src/mail-history-statuses/entities/mail-history-status.entity';
import { InviteStatusEnum } from 'src/mail-history-statuses/mail-history-status.enum';
import { SgtuDto } from 'src/sgtu/dto/sgtu.dto';
import { JaeProfileInterface } from 'src/jae/interfaces/jae-profile.interface';
import { Role } from 'src/roles/entities/role.entity';
import { RoleEnum } from 'src/roles/roles.enum';
import { BaseValidator } from 'src/utils/validators/base-validator';

/**
* All tests below were based on the requirements on GitHub.
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements #94 - GitHub}
*/
describe('AuthLicenseeService', () => {
let service: AuthLicenseeService;
let authLicenseeService: AuthLicenseeService;
let jwtService: JwtService;
let usersService: UsersService;
let mailHistoryService: MailHistoryService;
let sgtuService: SgtuService;
let jaeService: JaeService;
let baseValidator: BaseValidator;

beforeEach(async () => {
const usersServiceMock = {
provide: UsersService,
useValue: {
create: jest.fn(),
getOne: jest.fn(),
findOne: jest.fn(),
update: jest.fn(),
softDelete: jest.fn(),
},
} as Provider;
const forgotServiceMock = {
provide: ForgotService,
useValue: {
findOne: jest.fn(),
create: jest.fn(),
},
} as Provider;
const mailServiceMock = {
provide: MailService,
useValue: {
userConcludeRegistration: jest.fn(),
forgotPassword: jest.fn(),
},
} as Provider;
const coreBankServiceMock = {
provide: CoreBankService,
useValue: {
updateDataIfNeeded: jest.fn(),
},
} as Provider;
const mailHistoryServiceMock = {
provide: MailHistoryService,
useValue: {
findOne: jest.fn(),
getOne: jest.fn(),
getRemainingQuota: jest.fn(),
update: jest.fn(),
generateHash: jest.fn(),
},
} as Provider;
const jwtServiceMock = {
provide: JwtService,
useValue: {
sign: jest.fn(),
},
} as Provider;
const sgtuServiceMock = {
provide: SgtuService,
useValue: {
getGeneratedProfile: jest.fn(),
},
} as Provider;
const jaeServiceMock = {
provide: JaeService,
useValue: {
getGeneratedProfileByUser: jest.fn(),
},
} as Provider;
const BaseValidatorMock = {
provide: BaseValidator,
useValue: {
validateOrReject: jest.fn(),
},
} as Provider;

const module: TestingModule = await Test.createTestingModule({
providers: [AuthLicenseeService],
providers: [
AuthLicenseeService,
jwtServiceMock,
usersServiceMock,
forgotServiceMock,
mailServiceMock,
coreBankServiceMock,
mailHistoryServiceMock,
sgtuServiceMock,
jaeServiceMock,
BaseValidatorMock,
],
}).compile();

service = module.get<AuthLicenseeService>(AuthLicenseeService);
authLicenseeService = module.get<AuthLicenseeService>(AuthLicenseeService);
usersService = module.get<UsersService>(UsersService);
mailHistoryService = module.get<MailHistoryService>(MailHistoryService);
sgtuService = module.get<SgtuService>(SgtuService);
jaeService = module.get<JaeService>(JaeService);
jwtService = module.get<JwtService>(JwtService);
jwtService = module.get<JwtService>(JwtService);
baseValidator = module.get<BaseValidator>(BaseValidator);
});

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

describe('getInviteProfile', () => {
it('should throw exception when mail status is not SENT', async () => {
// Arrange
const user = new User({
id: 1,
email: 'user1@example.com',
hash: 'hash_1',
});
const mailHistory = {
id: 1,
user: user,
hash: 'hash_1',
inviteStatus: new InviteStatus(InviteStatusEnum.queued),
} as MailHistory;
jest.spyOn(usersService, 'getOne').mockResolvedValue(user);
jest.spyOn(mailHistoryService, 'getOne').mockResolvedValue(mailHistory);
jest.spyOn(mailHistoryService, 'getRemainingQuota').mockResolvedValue(0);
jest.spyOn(baseValidator, 'validateOrReject').mockResolvedValue({});

// Act
const response = authLicenseeService.getInviteProfile('hash_1');
// Assert
await expect(response).rejects.toThrowError();
});
});

describe('concludeRegistration', () => {
it('should set mail status to SENT when succeeded', async () => {
// Arrange
const dateNow = new Date('2023-01-01T10:00:00');
const user = new User({
id: 1,
email: 'user1@example.com',
hash: 'hash_1',
permitCode: 'permitCode1',
role: new Role(RoleEnum.user),
});
const mailHistory = {
id: 1,
user: user,
hash: 'hash_1',
inviteStatus: new InviteStatus(InviteStatusEnum.sent),
sentAt: dateNow,
} as MailHistory;
const sgtuProfile = {
cpfCnpj: 'cpf1',
permitCode: 'permitCode1',
email: user.email,
} as SgtuDto;
const jaeProfile = {
id: 1,
passValidatorId: 'validatorId',
permitCode: 'permitCode1',
} as JaeProfileInterface;
jest.spyOn(mailHistoryService, 'findOne').mockResolvedValue(mailHistory);
jest.spyOn(usersService, 'getOne').mockResolvedValue(user);
jest.spyOn(usersService, 'update').mockResolvedValue(user);
jest
.spyOn(sgtuService, 'getGeneratedProfile')
.mockResolvedValue(sgtuProfile);
jest
.spyOn(jaeService, 'getGeneratedProfileByUser')
.mockReturnValue(jaeProfile);
jest
.spyOn(global.Date, 'now')
.mockImplementation(() => dateNow.valueOf());
jest.spyOn(jwtService, 'sign').mockReturnValue('token');
jest.spyOn(baseValidator, 'validateOrReject').mockResolvedValue({});

// Act
await authLicenseeService.concludeRegistration(
{ password: 'secret' },
'hash_1',
);
// Assert
expect(mailHistoryService.update).toBeCalledWith(
1,
{
inviteStatus: {
id: InviteStatusEnum.used,
},
},
expect.any(String),
);
});
});
});
Loading

0 comments on commit e12e9c0

Please sign in to comment.