Skip to content

Commit 412e292

Browse files
authored
refactor: remove usage of throwOnForbiddenPermission for item service get (#1721)
* refactor: remove usage of throwOnForbidden for item service get * refactor: fix tests * refactor: fix tests
1 parent 09c0d64 commit 412e292

4 files changed

Lines changed: 66 additions & 47 deletions

File tree

src/services/item/service.test.ts

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
import { v4 } from 'uuid';
2+
13
import { FastifyInstance } from 'fastify';
24

5+
import { FolderItemFactory } from '@graasp/sdk';
6+
37
import build, { clearDatabase } from '../../../test/app';
48
import { BaseLogger } from '../../logger';
59
import { buildRepositories } from '../../utils/repositories';
10+
import * as authorization from '../authorization';
11+
import { Actor } from '../member/entities/member';
12+
import { saveMember } from '../member/test/fixtures/members';
613
import { ThumbnailService } from '../thumbnail/service';
14+
import { Item } from './entities/Item';
715
import { ItemThumbnailService } from './plugins/thumbnail/service';
816
import { ItemService } from './service';
917
import { ItemTestUtils } from './test/fixtures/items';
@@ -20,32 +28,73 @@ const service = new ItemService(
2028

2129
describe('Item Service', () => {
2230
let app: FastifyInstance;
23-
let actor;
2431

25-
beforeEach(async () => {
26-
({ app, actor } = await build());
32+
beforeAll(async () => {
33+
({ app } = await build({ member: null }));
2734
});
2835

29-
afterEach(async () => {
30-
jest.clearAllMocks();
36+
afterAll(async () => {
3137
await clearDatabase(app.db);
32-
actor = null;
3338
app.close();
3439
});
40+
afterEach(async () => {
41+
jest.clearAllMocks();
42+
jest.resetAllMocks();
43+
});
44+
45+
describe('get', () => {
46+
it('return item if exists and pass validation', async () => {
47+
const actor = { id: v4() } as Actor;
48+
const item = FolderItemFactory() as unknown as Item;
49+
const repositories = buildRepositories();
50+
jest.spyOn(repositories.itemRepository, 'getOneOrThrow').mockResolvedValue(item);
51+
jest
52+
.spyOn(authorization, 'validatePermission')
53+
.mockResolvedValue({ itemMembership: null, visibilities: [] });
54+
55+
const result = await service.get(actor, repositories, item.id);
56+
expect(result).toEqual(item);
57+
});
58+
it('throw if item does not exists', async () => {
59+
const actor = { id: v4() } as Actor;
60+
const item = FolderItemFactory() as unknown as Item;
61+
const repositories = buildRepositories();
62+
jest.spyOn(repositories.itemRepository, 'getOneOrThrow').mockRejectedValue(new Error());
63+
64+
await expect(() => service.get(actor, repositories, item.id)).rejects.toThrow();
65+
});
66+
it('throw if validation does not pass', async () => {
67+
const actor = { id: v4() } as Actor;
68+
const item = FolderItemFactory() as unknown as Item;
69+
const repositories = buildRepositories();
70+
jest.spyOn(repositories.itemRepository, 'getOneOrThrow').mockResolvedValue(item);
71+
jest.spyOn(authorization, 'validatePermission').mockRejectedValue(new Error());
72+
73+
await expect(() => service.get(actor, repositories, item.id)).rejects.toThrow();
74+
});
75+
});
3576
describe('Copy', () => {
3677
it('Should copy thumbnails on item copy if original has thumbnails', async () => {
78+
const actor = await saveMember();
3779
const { item } = await testUtils.saveItemAndMembership({
3880
member: actor,
3981
item: { settings: { hasThumbnail: true } },
4082
});
83+
jest
84+
.spyOn(authorization, 'validatePermission')
85+
.mockResolvedValue({ itemMembership: null, visibilities: [] });
4186
await service.copy(actor, buildRepositories(), item.id);
4287
expect(mockedThumbnailService.copyFolder).toHaveBeenCalled();
4388
});
4489
it('Should not copy thumbnails on item copy if original has no thumbnails', async () => {
90+
const actor = await saveMember();
4591
const { item } = await testUtils.saveItemAndMembership({
4692
member: actor,
4793
item: { settings: { hasThumbnail: false } },
4894
});
95+
jest
96+
.spyOn(authorization, 'validatePermission')
97+
.mockResolvedValue({ itemMembership: null, visibilities: [] });
4998
await service.copy(actor, buildRepositories(), item.id);
5099
expect(mockedThumbnailService.copyFolder).not.toHaveBeenCalled();
51100
});

src/services/item/service.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,16 @@ export class ItemService {
215215
repositories: Repositories,
216216
id: string,
217217
permission: PermissionLevel = PermissionLevel.Read,
218-
throwOnForbiddenPermission: boolean = true,
219218
) {
220219
const item = await repositories.itemRepository.getOneOrThrow(id);
221220

222-
if (throwOnForbiddenPermission) {
223-
const { itemMembership, visibilities } = await validatePermission(
224-
repositories,
225-
permission,
226-
actor,
227-
item,
228-
);
229-
return { item, itemMembership, visibilities };
230-
}
231-
232-
return { item, itemMembership: null, visibilities: [] };
221+
const { itemMembership, visibilities } = await validatePermission(
222+
repositories,
223+
permission,
224+
actor,
225+
item,
226+
);
227+
return { item, itemMembership, visibilities };
233228
}
234229

235230
/**
@@ -245,15 +240,8 @@ export class ItemService {
245240
repositories: Repositories,
246241
id: string,
247242
permission: PermissionLevel = PermissionLevel.Read,
248-
throwOnForbiddenPermission?: boolean,
249243
) {
250-
const { item } = await this._get(
251-
actor,
252-
repositories,
253-
id,
254-
permission,
255-
throwOnForbiddenPermission,
256-
);
244+
const { item } = await this._get(actor, repositories, id, permission);
257245

258246
return item;
259247
}

src/services/itemLogin/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
4343
const repositories = buildRepositories(manager);
4444

4545
// Get item to have the path
46-
const item = await itemService.get(
47-
user?.account,
48-
repositories,
49-
itemId,
50-
PermissionLevel.Read,
51-
false,
52-
);
46+
const item = await repositories.itemRepository.getOneOrThrow(itemId);
5347

5448
// If item is not visible, throw NOT_FOUND
5549
const isVisible = await isItemVisible(

src/services/itemMembership/plugins/MembershipRequest/index.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
8888
return reply.send({ status: MembershipRequestStatus.Approved });
8989
}
9090

91-
const item = await itemService.get(
92-
member,
93-
repositories,
94-
itemId,
95-
PermissionLevel.Read,
96-
false,
97-
);
91+
const item = await repositories.itemRepository.getOneOrThrow(itemId);
9892
if (item) {
9993
return reply.send({ status: MembershipRequestStatus.NotSubmittedOrDeleted });
10094
}
@@ -127,13 +121,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
127121
throw new MembershipRequestAlreadyExists();
128122
}
129123

130-
const item = await itemService.get(
131-
member,
132-
repositories,
133-
itemId,
134-
PermissionLevel.Read,
135-
false,
136-
);
124+
const item = await repositories.itemRepository.getOneOrThrow(itemId);
137125

138126
const itemLoginSchema = await itemLoginService.getByItemPath(repositories, item.path);
139127
if (itemLoginSchema) {

0 commit comments

Comments
 (0)