Skip to content

Commit

Permalink
feat: validation to receive a valid card number[13-19]
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleymichael committed Sep 4, 2023
1 parent 056bf39 commit 9cb1527
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/cards/cards.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
ApiBody,
ApiOperation,
ApiBearerAuth,
ApiHeader,
ApiUnauthorizedResponse,
ApiConflictResponse,
ApiCreatedResponse,
Expand Down
4 changes: 3 additions & 1 deletion src/cards/dto/cards.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export class CardsDTO {
example: '1111222233334444',
description: 'Card number',
})
@Length(16, 16)
@Length(13, 19, {
message: 'O número do cartão deve ter entre 13 e 19 dígitos',
})
@Matches(/^[0-9]*$/, {
message: 'O número do cartão deve conter apenas dígitos numéricos.',
})
Expand Down
2 changes: 1 addition & 1 deletion test/factories/cards.body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class BodyCard {
isDebit?: boolean,
) {
this._title = title || faker.finance.accountName();
this._number = number || faker.finance.creditCardNumber().toString();
this._number = number || faker.finance.creditCardNumber().replace(/-/g, '');
this._name = name || faker.person.fullName();
this._cvv = cvv || faker.finance.creditCardCVV();
this._exp = exp || faker.date.month();
Expand Down
6 changes: 6 additions & 0 deletions test/integration/cards.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ describe('cardsController (e2e)', () => {
.withEmail(bodyLogin.email)
.withPassword(bodyLogin.password)
.persist();

const { body } = await login(app, bodyLogin);
const bodyCard1 = new BodyCard().generate();
const bodyCard2 = new BodyCard().generate();
await request(app.getHttpServer())
.post('/cards')
.send(bodyCard1)
.set('Authorization', `Bearer ${body.token}`);

return await request(app.getHttpServer())
.post('/cards')
.send({ ...bodyCard2, title: bodyCard1.title })
Expand All @@ -80,13 +82,15 @@ describe('cardsController (e2e)', () => {
.withEmail(bodyLogin.email)
.withPassword(bodyLogin.password)
.persist();

const { body } = await login(app, bodyLogin);
const bodyCard1 = new BodyCard().generate();
const bodyCard2 = new BodyCard().generate();
await request(app.getHttpServer())
.post('/cards')
.send(bodyCard1)
.set('Authorization', `Bearer ${body.token}`);

return await request(app.getHttpServer())
.post('/cards')
.send({ ...bodyCard2, number: bodyCard1.number })
Expand All @@ -100,8 +104,10 @@ describe('cardsController (e2e)', () => {
.withEmail(bodyLogin.email)
.withPassword(bodyLogin.password)
.persist();

const { body } = await login(app, bodyLogin);
const bodyCard = new BodyCard().generate();

await request(app.getHttpServer())
.post('/cards')
.send(bodyCard)
Expand Down

0 comments on commit 9cb1527

Please sign in to comment.