From 77f71e1d8b2b8e454960ce5736c067f8148912af Mon Sep 17 00:00:00 2001 From: Anders Rasmussen Date: Fri, 6 Oct 2023 07:57:44 +0200 Subject: [PATCH] #14 - Added changes according to feedback --- src/modules/item/__test__/item.read.test.ts | 45 +++++++++- src/modules/item/__test__/item.root.test.ts | 35 +++++++- .../item/__test__/item.service.test.ts | 82 ++++++++++++++++++- 3 files changed, 154 insertions(+), 8 deletions(-) diff --git a/src/modules/item/__test__/item.read.test.ts b/src/modules/item/__test__/item.read.test.ts index b9fe015..76409e1 100644 --- a/src/modules/item/__test__/item.read.test.ts +++ b/src/modules/item/__test__/item.read.test.ts @@ -2,12 +2,13 @@ import { User } from '@prisma/client'; import UserService from '../../auth/user.service'; import FolderService from '../folder/folder.service'; import AuthService from '../../auth/auth.service'; +import BlobService from '../blob/blob.service'; -// The tests can only be run with folder - Since the blob service has istanbul ignore next describe('GET /api/item/:parentId', () => { let userService: UserService; let folderService: FolderService; let authService: AuthService; + let blobService: BlobService; let user: User; let otherUser: User; @@ -16,6 +17,7 @@ describe('GET /api/item/:parentId', () => { userService = new UserService(); folderService = new FolderService(); authService = new AuthService(); + blobService = new BlobService(); user = await userService.createUser({ name: 'Joe Biden the 1st', @@ -29,7 +31,7 @@ describe('GET /api/item/:parentId', () => { }); }); - it('All items should from the folder, with the given parentId', async () => { + it('Should return status 200 and all items from parentId', async () => { const { accessToken } = await authService.createTokens(user.id); const parentFolder = await folderService.createFolder({ @@ -38,6 +40,15 @@ describe('GET /api/item/:parentId', () => { ownerId: user.id, parentId: null, }); + + await blobService.createBlob({ + mimeType: 'text/plain', + name: 'test1.txt', + ownerId: user.id, + parentId: parentFolder.id, + blobUrl: 'https://example.com/test1.txt', + }); + await folderService.createFolder({ name: 'Folder2', color: '#987654', @@ -55,6 +66,17 @@ describe('GET /api/item/:parentId', () => { expect(response.statusCode).toBe(200); expect(response.json()).toEqual([ + { + id: expect.any(Number), + name: 'test1.txt', + blobUrl: 'https://example.com/test1.txt', + parentId: parentFolder.id, + ownerId: user.id, + mimeType: 'text/plain', + createdAt: expect.any(String), + deletedAt: null, + updatedAt: expect.any(String), + }, { id: expect.any(Number), name: 'Folder2', @@ -69,13 +91,21 @@ describe('GET /api/item/:parentId', () => { ]); }); - it('Getting items should return error, if auth is not set', async () => { + it('Should return status 401, when unauthorized', async () => { const parentFolder = await folderService.createFolder({ name: 'Folder1', color: '#123456', ownerId: user.id, parentId: null, }); + + await blobService.createBlob({ + mimeType: 'text/plain', + name: 'test1.txt', + ownerId: user.id, + parentId: parentFolder.id, + blobUrl: 'https://example.com/test1.txt', + }); await folderService.createFolder({ name: 'Folder2', color: '#987654', @@ -101,7 +131,7 @@ describe('GET /api/item/:parentId', () => { }); }); - it('Getting items should return error, if you do not have access to the parent folder', async () => { + it('Should return status 401, when unauthorized to the parent folder', async () => { const { accessToken } = await authService.createTokens(otherUser.id); const parentFolder = await folderService.createFolder({ name: 'Folder1', @@ -109,6 +139,13 @@ describe('GET /api/item/:parentId', () => { ownerId: user.id, parentId: null, }); + await blobService.createBlob({ + mimeType: 'text/plain', + name: 'test1.txt', + ownerId: user.id, + parentId: parentFolder.id, + blobUrl: 'https://example.com/test1.txt', + }); await folderService.createFolder({ name: 'Folder2', color: '#987654', diff --git a/src/modules/item/__test__/item.root.test.ts b/src/modules/item/__test__/item.root.test.ts index b887f4a..bc67b5a 100644 --- a/src/modules/item/__test__/item.root.test.ts +++ b/src/modules/item/__test__/item.root.test.ts @@ -2,12 +2,13 @@ import { User } from '@prisma/client'; import UserService from '../../auth/user.service'; import FolderService from '../folder/folder.service'; import AuthService from '../../auth/auth.service'; +import BlobService from '../blob/blob.service'; -// The tests can only be run with folder - Since the blob service has istanbul ignore next describe('GET /api/item', () => { let userService: UserService; let folderService: FolderService; let authService: AuthService; + let blobService: BlobService; let user: User; @@ -15,6 +16,7 @@ describe('GET /api/item', () => { userService = new UserService(); folderService = new FolderService(); authService = new AuthService(); + blobService = new BlobService(); user = await userService.createUser({ name: 'Joe Biden the 1st', @@ -23,9 +25,17 @@ describe('GET /api/item', () => { }); }); - it('All items should be returned from the users root "folder"', async () => { + it('Should return status 200 and all items from root folder', async () => { const { accessToken } = await authService.createTokens(user.id); + await blobService.createBlob({ + mimeType: 'text/plain', + name: 'test1.txt', + ownerId: user.id, + parentId: null, + blobUrl: 'https://example.com/test1.txt', + }); + await folderService.createFolder({ name: 'Folder1', color: '#123456', @@ -49,6 +59,17 @@ describe('GET /api/item', () => { expect(response.statusCode).toBe(200); expect(response.json()).toEqual([ + { + id: expect.any(Number), + name: 'test1.txt', + blobUrl: 'https://example.com/test1.txt', + parentId: null, + ownerId: user.id, + mimeType: 'text/plain', + createdAt: expect.any(String), + deletedAt: null, + updatedAt: expect.any(String), + }, { id: expect.any(Number), name: 'Folder1', @@ -74,7 +95,15 @@ describe('GET /api/item', () => { ]); }); - it('Getting root items should return error, if auth is not set', async () => { + it('Should return status 401, when unauthorized', async () => { + await blobService.createBlob({ + mimeType: 'text/plain', + name: 'test1.txt', + ownerId: user.id, + parentId: null, + blobUrl: 'https://example.com/test1.txt', + }); + await folderService.createFolder({ name: 'Folder1', color: '#123456', diff --git a/src/modules/item/__test__/item.service.test.ts b/src/modules/item/__test__/item.service.test.ts index ff3df5f..9580f97 100644 --- a/src/modules/item/__test__/item.service.test.ts +++ b/src/modules/item/__test__/item.service.test.ts @@ -3,13 +3,14 @@ import UserService from '../../auth/user.service'; import ItemService from '../item.service'; import FolderService from '../folder/folder.service'; import SharingService from '../sharing/sharing.service'; +import BlobService from '../blob/blob.service'; -// The tests can only be run with folder - Since the blob service has istanbul ignore next describe('ItemService', () => { let itemService: ItemService; let userService: UserService; let folderService: FolderService; let sharingService: SharingService; + let blobService: BlobService; let user: User; let otherUser: User; @@ -19,6 +20,7 @@ describe('ItemService', () => { userService = new UserService(); folderService = new FolderService(); sharingService = new SharingService(); + blobService = new BlobService(); user = await userService.createUser({ name: 'Joe Biden the 1st', @@ -34,12 +36,21 @@ describe('ItemService', () => { describe('getByOwnerIdAndParentId()', () => { it('should return all items in users root folder', async () => { + await blobService.createBlob({ + mimeType: 'text/plain', + name: 'test1.txt', + ownerId: user.id, + parentId: null, + blobUrl: 'https://example.com/test1.txt', + }); + await folderService.createFolder({ name: 'Folder1', color: '#123456', ownerId: user.id, parentId: null, }); + await folderService.createFolder({ name: 'Folder2', color: '#987654', @@ -50,6 +61,18 @@ describe('ItemService', () => { const items = await itemService.getByOwnerIdAndParentId(user.id, null); expect(items).toEqual([ + { + id: expect.any(Number), + name: 'test1.txt', + blobUrl: 'https://example.com/test1.txt', + parentId: null, + ownerId: user.id, + mimeType: 'text/plain', + itemId: expect.any(Number), + createdAt: expect.any(Date), + deletedAt: null, + updatedAt: expect.any(Date), + }, { id: expect.any(Number), name: 'Folder1', @@ -97,6 +120,22 @@ describe('ItemService', () => { userId: otherUser.id, }); + const blob1 = await blobService.createBlob({ + mimeType: 'text/plain', + name: 'test1.txt', + ownerId: user.id, + parentId: folder.id, + blobUrl: 'https://example.com/test1.txt', + }); + + await blobService.createBlob({ + mimeType: 'text/plain', + name: 'test2.txt', + ownerId: user.id, + parentId: folder.id, + blobUrl: 'https://example.com/test2.txt', + }); + const folder1 = await folderService.createFolder({ name: 'Folder1', color: '#123456', @@ -116,6 +155,11 @@ describe('ItemService', () => { parentId: folder.id, }); + await sharingService.createSharing({ + itemId: blob1.id, + userId: otherUser.id, + }); + await sharingService.createSharing({ itemId: folder1.id, userId: otherUser.id, @@ -136,6 +180,30 @@ describe('ItemService', () => { ); const expectedOwner = [ + { + id: expect.any(Number), + name: 'test1.txt', + blobUrl: 'https://example.com/test1.txt', + parentId: folder.id, + ownerId: user.id, + mimeType: 'text/plain', + itemId: expect.any(Number), + createdAt: expect.any(Date), + deletedAt: null, + updatedAt: expect.any(Date), + }, + { + id: expect.any(Number), + name: 'test2.txt', + blobUrl: 'https://example.com/test2.txt', + parentId: folder.id, + ownerId: user.id, + mimeType: 'text/plain', + itemId: expect.any(Number), + createdAt: expect.any(Date), + deletedAt: null, + updatedAt: expect.any(Date), + }, { id: expect.any(Number), name: 'Folder1', @@ -174,6 +242,18 @@ describe('ItemService', () => { }, ]; const expectedSharredUser = [ + { + id: expect.any(Number), + name: 'test1.txt', + blobUrl: 'https://example.com/test1.txt', + parentId: folder.id, + ownerId: user.id, + mimeType: 'text/plain', + itemId: expect.any(Number), + createdAt: expect.any(Date), + deletedAt: null, + updatedAt: expect.any(Date), + }, { id: expect.any(Number), name: 'Folder1',