Skip to content

Commit

Permalink
#14 - Added changes according to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders164a committed Oct 6, 2023
1 parent 05d1b68 commit 77f71e1
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 8 deletions.
45 changes: 41 additions & 4 deletions src/modules/item/__test__/item.read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -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({
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -101,14 +131,21 @@ 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',
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',
Expand Down
35 changes: 32 additions & 3 deletions src/modules/item/__test__/item.root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ 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;

beforeAll(async () => {
userService = new UserService();
folderService = new FolderService();
authService = new AuthService();
blobService = new BlobService();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
82 changes: 81 additions & 1 deletion src/modules/item/__test__/item.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 77f71e1

Please sign in to comment.