1+ import { v4 } from 'uuid' ;
2+
13import { FastifyInstance } from 'fastify' ;
24
5+ import { FolderItemFactory } from '@graasp/sdk' ;
6+
37import build , { clearDatabase } from '../../../test/app' ;
48import { BaseLogger } from '../../logger' ;
59import { 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' ;
613import { ThumbnailService } from '../thumbnail/service' ;
14+ import { Item } from './entities/Item' ;
715import { ItemThumbnailService } from './plugins/thumbnail/service' ;
816import { ItemService } from './service' ;
917import { ItemTestUtils } from './test/fixtures/items' ;
@@ -20,32 +28,73 @@ const service = new ItemService(
2028
2129describe ( '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 } ) ;
0 commit comments