Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication favorites #253

Open
wants to merge 75 commits into
base: authorization
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
904cca2
the 'guards' were added to each path correspondingly, the swagger doc…
ayrtoncravero Jan 12, 2022
dc152d2
changed the name of the relation of 'user' and its typing in the enti…
ayrtoncravero Jan 12, 2022
7c4dae4
adding permissions of 'Favorite'
ayrtoncravero Jan 12, 2022
89240f3
changes in the controller
ayrtoncravero Jan 13, 2022
b1c4060
adding methods for testing
ayrtoncravero Jan 13, 2022
6b4ced3
add to spaces for more prolixity
ayrtoncravero Jan 14, 2022
07fb28f
changes test
ayrtoncravero Jan 14, 2022
e10e9a3
changes in the controller, and test
ayrtoncravero Jan 14, 2022
3f638a6
Merge branch 'authorization' of github.com:RooftopAcademy/rooftend in…
ayrtoncravero Jan 14, 2022
302a48f
Merge branch 'authorization' of github.com:RooftopAcademy/rooftend in…
ayrtoncravero Jan 14, 2022
9fb3579
deleted import police
ayrtoncravero Jan 14, 2022
53a3335
add logic for exception forbidden
ayrtoncravero Jan 14, 2022
ef705de
adding all test
ayrtoncravero Jan 14, 2022
32ab772
add new method 'findFavorite'
ayrtoncravero Jan 14, 2022
4a6ae0a
addign all test for the service
ayrtoncravero Jan 14, 2022
7409ac1
add documentation for unautorized
ayrtoncravero Jan 14, 2022
1430bd8
add more documentation for nauthorized
ayrtoncravero Jan 14, 2022
ac32919
change decorators and change logic
ayrtoncravero Jan 14, 2022
adc36c9
change in test of create
ayrtoncravero Jan 14, 2022
4536ce0
changes in the controller
ayrtoncravero Jan 14, 2022
e367de5
changes in the test of create
ayrtoncravero Jan 14, 2022
feb8e4c
changes
ayrtoncravero Jan 14, 2022
d80b259
add auth in the routes
ayrtoncravero Jan 18, 2022
b07f7bd
changes in the test
ayrtoncravero Jan 18, 2022
edecb43
deleted 'ApiForbiddenResponse'
ayrtoncravero Jan 25, 2022
c4ca7bd
add 'ApiQuery'
ayrtoncravero Jan 25, 2022
3f98c68
adding example of 'ApiUnauthorizedResponse' and 'ApiForbiddenResponse'
ayrtoncravero Jan 25, 2022
3b590ee
code was orded, added decorators for 'ApiUnauthorizedResponse' and 'A…
ayrtoncravero Jan 25, 2022
3304869
code was orded, added decorators for 'ApiUnauthorizedResponse' and 'A…
ayrtoncravero Jan 25, 2022
4fff32d
change in the method
ayrtoncravero Jan 25, 2022
cae905c
add scheme for documentation for 'ApiUnauthorizedResponse' and 'ApiFo…
ayrtoncravero Jan 25, 2022
9220e48
change response and documentation for delete
ayrtoncravero Jan 25, 2022
b41082c
comment line with return
ayrtoncravero Jan 25, 2022
ab8e485
change test
ayrtoncravero Jan 25, 2022
8799a2d
deleted dile unnecesary
ayrtoncravero Jan 25, 2022
3f13463
change dto with date necessary
ayrtoncravero Jan 25, 2022
a4080a6
changes
ayrtoncravero Jan 25, 2022
1e2804c
deleted import unesesary
ayrtoncravero Jan 25, 2022
8b8c623
changes in the test of created
ayrtoncravero Jan 25, 2022
5bcd1ae
deleted decorator for documentation for forbidden
ayrtoncravero Jan 26, 2022
557d0f1
deleted decorator for forbidden and unautorize
ayrtoncravero Jan 26, 2022
f08eb7e
add to correct structure
ayrtoncravero Jan 26, 2022
fb08635
add import of caslModule
ayrtoncravero Jan 26, 2022
0d5a9b8
completed tests
ayrtoncravero Jan 26, 2022
7136951
changes in the service
ayrtoncravero Jan 26, 2022
4b23407
completed test
ayrtoncravero Jan 26, 2022
64824f7
change decorator and deleted decorator repeated
ayrtoncravero Jan 26, 2022
2d74628
resolved conflict
ayrtoncravero Jan 26, 2022
4c11ca1
add documentation for 'ApiNotFoundResponse' and 'ApiBearerAuth'
ayrtoncravero Jan 27, 2022
f347980
change in 'joinColums', change name of attributes
ayrtoncravero Jan 27, 2022
1ad45bf
deleted ';' and add one ';'
ayrtoncravero Jan 27, 2022
c743f04
change method 'delete' for 'softDeleted'
ayrtoncravero Jan 27, 2022
b06535d
deleted returns unnesessary
ayrtoncravero Jan 27, 2022
3ac7490
change method 'findFavorite'
ayrtoncravero Jan 27, 2022
4f5157e
deleted 'query' for token documentation, add decorators of documentat…
ayrtoncravero Jan 27, 2022
f6ed594
change test of created
ayrtoncravero Jan 27, 2022
edad85f
change in typed and documentation of typed
ayrtoncravero Jan 27, 2022
352f53b
change in the method 'findFavorite'
ayrtoncravero Jan 27, 2022
e2273be
changes in the logic, and add of comment
ayrtoncravero Jan 28, 2022
ba5ed70
completed tests and code order
ayrtoncravero Jan 28, 2022
06e481d
import 'Item' module
ayrtoncravero Jan 28, 2022
82b8131
change in the method create
ayrtoncravero Jan 28, 2022
f5f6215
deleted method unnecessary
ayrtoncravero Jan 28, 2022
2a31eb4
add mock of 'Item', deleted comment
ayrtoncravero Jan 28, 2022
1dbd541
deleted query unnesessary
ayrtoncravero Jan 28, 2022
8edf3b7
spacing in the code
ayrtoncravero Jan 28, 2022
20140de
deleted import unnecessary and run prettier
ayrtoncravero Jan 28, 2022
159e687
deleted test repeated
ayrtoncravero Jan 28, 2022
a034e3c
deleted variable not used and test unnecessary
ayrtoncravero Jan 28, 2022
c3e03df
add test for notFoundException in method delete
ayrtoncravero Jan 28, 2022
d84c9f8
add to documentation for unauthorizedResponse
ayrtoncravero Jan 29, 2022
2dec2e2
add documentation for 400s
ayrtoncravero Jan 29, 2022
9cc0a83
add documentation for UnprocessableEntity
ayrtoncravero Jan 29, 2022
ac027be
add documentation 404 in the method 'getById'
ayrtoncravero Jan 29, 2022
ab9caa3
deleted documentation '@ApiQuery' for token
ayrtoncravero Jan 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/auth/casl/casl-ability.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { Injectable } from '@nestjs/common';
import { Permission } from '../enums/permission.enum';
import { User } from '../../users/entities/user.entity';
import { FlatClass } from '../types/flat-class.type';
import { Favorite } from '../../favorites/entities/favorite.entity';
import { History } from '../../history/models/history.entity';
import { Cart } from '../../cart/entities/cart.entity';
import { CustomMessage } from '../../custom-messages/entities/custom-messages.entity';
import { Item } from '../../items/entities/items.entity';

// TODO: add classes to InferSubjects -> InferSubjects<typeof Item | typeof Review ...>
type Subjects = InferSubjects<typeof Item | typeof Cart | typeof CustomMessage | typeof History> | 'all';
type Subjects = InferSubjects<typeof Item | typeof Cart | typeof CustomMessage | typeof History | typeof Favorite> | 'all';

export type AppAbility = Ability<[Permission, Subjects]>;

Expand All @@ -38,6 +38,14 @@ export class CaslAbilityFactory {
'user.id': user.id,
});

can<FlatClass<Favorite>>([
Permission.Read,
Permission.Create,
Permission.Delete
], Favorite, {
'user.id': user.id,
});

can<FlatClass<CustomMessage>>(
[Permission.Read, Permission.Delete, Permission.Update],
CustomMessage,
Expand Down
141 changes: 111 additions & 30 deletions src/favorites/controllers/favorites.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,57 @@
import {
ForbiddenException,
NotFoundException,
} from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { plainToClass } from 'class-transformer';
import { CaslModule } from '../../auth/casl/casl.module';
import STATUS from '../../statusCodes/statusCodes';
import { User } from '../../users/entities/user.entity';
import { CreateFavoriteDto } from '../dto/create-favorite.dto';
import { Favorite } from '../entities/favorite.entity';
import { FavoritesService } from '../services/favorites.service';
import { FavoritesController } from './favorites.controller';

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

const user: User = new User();
user.id = 1;

const mockFavoriteService = {
paginate: jest.fn(),
create: jest.fn(dto => {
return {
...dto,
user_id: 24
}
paginate: jest.fn().mockResolvedValue([
{
id: 1,
user: 1,
item_id: 1,
createdAt: new Date(),
},
]),
create: jest.fn((user: User, body: CreateFavoriteDto) => {
Promise.resolve({
item_id: 1,
});
}),
delete: jest.fn()
}
delete: jest.fn(),
findFavorite: jest.fn().mockImplementation((id) =>
Promise.resolve(
plainToClass(Favorite, {
user: user,
id: id,
}),
),
),
};

const response: any = {
user: new User(),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [FavoritesController],
providers: [FavoritesService]
providers: [FavoritesService],
imports: [CaslModule],
})
.overrideProvider(FavoritesService)
.useValue(mockFavoriteService)
Expand All @@ -32,28 +64,77 @@ describe('FavoritesController', () => {
expect(controller).toBeDefined();
});

it('should get 10 favorites records.', () => {
const page = 1
const limit = 10
const token = 1
expect(controller.paginate(token, page, limit)).not.toBeUndefined()
expect(mockFavoriteService.paginate).toHaveBeenCalled()
})

it('should create a favorite.', () => {
const data = { item_id: 61 }
const token = 1
expect(controller.create(token, data)).toEqual({
"message": "Created",
"statusCode": 201,
describe('paginate', () => {
it('should get 10 favorites records.', async () => {
const page = 1;
const limit = 10;
const route = '/favorites';

const user: User = new User();
user.id = 1;

await expect(controller.paginate(response, page, limit)).resolves.toEqual(
[
{
id: 1,
user: 1,
item_id: 1,
createdAt: expect.any(Date),
},
],
);

expect(mockFavoriteService.paginate).toHaveBeenCalledWith(
{ page, limit, route },
response.user,
);
});
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

falta testear el findById

describe('create', () => {
it('should create a favorite.', async () => {
const mockUser = new User();
mockUser.id = 1;

const request: any = {
user: mockUser,
};
request.user.id = 1;

const dto: CreateFavoriteDto = {
itemId: 1,
};

expect(await controller.create(request, dto)).toEqual(STATUS.CREATED);

expect(mockFavoriteService.create).toHaveBeenCalledWith(
dto,
request.user,
);
});
expect(mockFavoriteService.create).toHaveBeenCalledWith(data, token)
});

it('should delete a favorite.', () => {
const okResponse = { "message": "Ok", "statusCode": 200}
const favoriteId = 1
expect(controller.delete(favoriteId)).toStrictEqual(okResponse)
expect(mockFavoriteService.delete).toHaveBeenCalledWith(favoriteId)
})
describe('delete', () => {
ghnoob marked this conversation as resolved.
Show resolved Hide resolved
it('should delete a favorite.', async () => {
const request: any = {
user: user,
};

expect(await controller.delete(request, 1)).toEqual(STATUS.DELETED);
});

it('should return a ForbiddenException', async () => {
await expect(controller.delete(response, 1)).rejects.toThrowError(
ForbiddenException,
);
});

it('should return a NotFoundException', async () => {
mockFavoriteService.findFavorite.mockRejectedValueOnce(new NotFoundException());

await expect(controller.delete(response, 1)).rejects.toThrowError(
NotFoundException,
);
});
});
});
Loading