Skip to content

Commit

Permalink
test: auth, cornjobs, e2e
Browse files Browse the repository at this point in the history
- fix: finter users by role
  • Loading branch information
yxuo committed Dec 8, 2023
1 parent f1bb6eb commit d189bfa
Show file tree
Hide file tree
Showing 20 changed files with 638 additions and 368 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/
52 changes: 50 additions & 2 deletions package-lock.json

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

2 changes: 2 additions & 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 Expand Up @@ -86,6 +87,7 @@
"@types/node": "18.16.16",
"@types/passport-anonymous": "1.0.3",
"@types/passport-jwt": "3.0.8",
"@types/selenium-webdriver": "^4.1.21",
"@types/supertest": "2.0.12",
"@types/twitter": "1.7.1",
"@typescript-eslint/eslint-plugin": "5.59.9",
Expand Down
2 changes: 1 addition & 1 deletion src/auth-licensee/auth-licensee.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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 - GitHub}
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements #94 - GitHub}
*/
describe('AuthLicenseeService', () => {
let authLicenseeService: AuthLicenseeService;
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ process.env.TZ = 'UTC';

/**
* All tests below were based on the requirements on GitHub.
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements - GitHub}
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements #94 - GitHub}
*/
describe('AuthService', () => {
let authService: AuthService;
Expand Down
14 changes: 3 additions & 11 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,7 @@ export class AuthService {
return returnMessage;
}

let hash = crypto
.createHash('sha256')
.update(randomStringGenerator())
.digest('hex');
while (await this.mailHistoryService.findOne({ hash })) {
hash = crypto
.createHash('sha256')
.update(randomStringGenerator())
.digest('hex');
}
const hash = await this.forgotService.generateHash();

await this.forgotService.create({
hash,
Expand Down Expand Up @@ -447,7 +438,8 @@ export class AuthService {
{
error: HttpErrorMessages.UNAUTHORIZED,
details: {
hash: `notFound`,
error: 'hash not found',
hash,
},
},
HttpStatus.UNAUTHORIZED,
Expand Down
2 changes: 1 addition & 1 deletion src/cron-jobs/cron-jobs.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { DeepPartial } from 'typeorm';

/**
* All tests below were based on the requirements on GitHub.
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements - GitHub}
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements #94 - GitHub}
*/
describe('CronJobsService', () => {
let cronJobsService: CronJobsService;
Expand Down
8 changes: 8 additions & 0 deletions src/database/seeds/user/user-seed-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ export class UserSeedDataService {
role: { id: RoleEnum.admin } as Role,
status: { id: StatusEnum.active } as Status,
},
{
fullName: 'Administrador Teste',
email: 'admin.test@example.com',
password: 'secret',
permitCode: '',
role: { id: RoleEnum.admin } as Role,
status: { id: StatusEnum.active } as Status,
},
]
: []),
];
Expand Down
16 changes: 16 additions & 0 deletions src/forgot/forgot.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as crypto from 'crypto';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { FindOptions } from 'src/utils/types/find-options.type';
Expand Down Expand Up @@ -31,4 +33,18 @@ export class ForgotService {
async softDelete(id: number): Promise<void> {
await this.forgotRepository.softDelete(id);
}

async generateHash(): Promise<string> {
let hash = crypto
.createHash('sha256')
.update(randomStringGenerator())
.digest('hex');
while (await this.findOne({ where: { hash } })) {
hash = crypto
.createHash('sha256')
.update(randomStringGenerator())
.digest('hex');
}
return hash;
}
}
2 changes: 1 addition & 1 deletion src/mail-history/mail-history.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('InviteService', () => {
});

/**
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements - GitHub}
* @see {@link https://github.com/RJ-SMTR/api-cct/issues/94#issuecomment-1815016208 Requirements #94 - GitHub}
*/
describe('getUpdatedMailCounts', () => {
it('should return quota as max value after midnight', async () => {
Expand Down
23 changes: 9 additions & 14 deletions src/mail/mail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,25 @@ export class MailService {
async sendForgotPassword(
mailData: MailData<{ hash: string }>,
): Promise<MailSentInfo> {
const resetPasswordTitle = 'Refedinir senha';
const mailTitle = 'Redefinir senha';

try {
const frontendDomain = this.configService.get('app.frontendDomain', {
infer: true,
});
const response = await this.safeSendMail({
to: mailData.to,
subject: resetPasswordTitle,
subject: mailTitle,
text: `${this.configService.get('app.frontendDomain', {
infer: true,
})}reset-password/${mailData.data.hash} ${resetPasswordTitle}`,
})}reset-password/${mailData.data.hash} ${mailTitle}`,
template: 'reset-password',
context: {
title: resetPasswordTitle,
title: mailTitle,
url: `${this.configService.get('app.frontendDomain', {
infer: true,
})}reset-password/${mailData.data.hash}`,
actionTitle: resetPasswordTitle,
actionTitle: mailTitle,
logoSrc: `${frontendDomain}/assets/icons/logoPrefeitura.png`,
logoAlt: 'Prefeitura do Rio',
bodyText: 'Redefina sua senha clicando no botão abaixo!',
Expand All @@ -168,7 +168,7 @@ export class MailService {
statusCount: IMailHistoryStatusCount;
}>,
): Promise<MailSentInfo> {
const resetPasswordTitle = 'Relatório diário';
const mailTitle = 'Relatório diário';
const from = this.configService.get('mail.senderNotification', {
infer: true,
});
Expand All @@ -192,16 +192,11 @@ export class MailService {
const response = await this.safeSendMail({
from,
to: mailData.to,
subject: resetPasswordTitle,
text: `${this.configService.get('app.frontendDomain', {
infer: true,
})}reset-password/${'mailData.data.hash'} ${resetPasswordTitle}`,
subject: mailTitle,
text: mailTitle,
template: 'report',
context: {
title: resetPasswordTitle,
url: `${this.configService.get('app.frontendDomain', {
infer: true,
})}reset-password/${'mailData.data.hash'}`,
title: mailTitle,
headerTitle: appName,
mailQueued: mailData.data.statusCount.queued,
mailSent: mailData.data.statusCount.sent,
Expand Down
27 changes: 15 additions & 12 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class UsersService {
paginationOptions: IPaginationOptions,
fields?: IFindUserPaginated,
): Promise<User[]> {
console.log('findManyWithPagination');
const isSgtuBlocked = fields?.isSgtuBlocked || fields?._anyField?.value;

let inviteStatus: any = null;
Expand All @@ -91,6 +92,15 @@ export class UsersService {
),
};
}

const andWhere = {
...(fields?.role
? {
role: { id: fields.role.id },
}
: {}),
} as FindOptionsWhere<User>;

const where = [
...(fields?.name || fields?._anyField?.value
? [
Expand Down Expand Up @@ -138,20 +148,13 @@ export class UsersService {
},
]
: []),
...(fields?.role
? [
{
role: { id: fields.role.id },
},
]
: []),
] as FindOptionsWhere<User>[];

let users = await this.usersRepository.find({
...(fields ? { where: where } : {}),
skip: (paginationOptions.page - 1) * paginationOptions.limit,
take: paginationOptions.limit,
});
let users = await this.usersRepository
.createQueryBuilder()
.where(where)
.andWhere(andWhere)
.getMany();

let invites: NullableType<MailHistory[]> = null;
if (inviteStatus) {
Expand Down
Loading

0 comments on commit d189bfa

Please sign in to comment.