Skip to content

Commit

Permalink
Merge pull request #20 from HF6-PROJECT/ara-14
Browse files Browse the repository at this point in the history
feat: Added item endpoint, to get item with extra data (Folder, Blob,…
  • Loading branch information
Anders164a authored Oct 6, 2023
2 parents 5347359 + cae191f commit 07ebc51
Show file tree
Hide file tree
Showing 16 changed files with 910 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/locales/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"required": "id er påkrævet",
"type": "id skal være et tal"
},
"parentId": {
"required": "Parent id er påkrævet",
"type": "Parent id skal være et tal"
},
"blob": {
"notFound": "Blob ikke fundet"
},
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"required": "id is required",
"type": "id must be a number"
},
"parentId": {
"required": "Parent id is required",
"type": "Parent id must be a number"
},
"blob": {
"notFound": "Blob not found"
},
Expand Down
8 changes: 5 additions & 3 deletions src/modules/auth/auth.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ export default async (fastify: FastifyInstance) => {
'/user',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Auth'],
response: {
200: { $ref: 'userResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand Down
173 changes: 173 additions & 0 deletions src/modules/item/__test__/item.read.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
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';

describe('GET /api/item/:parentId', () => {
let userService: UserService;
let folderService: FolderService;
let authService: AuthService;
let blobService: BlobService;

let user: User;
let otherUser: User;

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

user = await userService.createUser({
name: 'Joe Biden the 1st',
email: '[email protected]',
password: '1234',
});
otherUser = await userService.createUser({
name: 'Joe Biden the 2nd',
email: '[email protected]',
password: '4321',
});
});

it('Should return status 200 and all items from parentId', async () => {
const { accessToken } = await authService.createTokens(user.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',
ownerId: user.id,
parentId: parentFolder.id,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + parentFolder.id,
headers: {
authorization: 'Bearer ' + accessToken,
},
});

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',
color: '#987654',
parentId: parentFolder.id,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.folder',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
},
]);
});

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',
ownerId: user.id,
parentId: parentFolder.id,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + parentFolder.id,
headers: {
authorization: 'WrongAuth!',
},
});

expect(response.statusCode).toBe(401);
expect(response.json()).toEqual({
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});

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',
ownerId: user.id,
parentId: parentFolder.id,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + parentFolder.id,
headers: {
authorization: 'Bearer ' + accessToken,
},
});

expect(response.statusCode).toBe(401);
expect(response.json()).toEqual({
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
});
137 changes: 137 additions & 0 deletions src/modules/item/__test__/item.root.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
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';

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',
email: '[email protected]',
password: '1234',
});
});

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',
ownerId: user.id,
parentId: null,
});
await folderService.createFolder({
name: 'Folder2',
color: '#987654',
ownerId: user.id,
parentId: null,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item',
headers: {
authorization: 'Bearer ' + accessToken,
},
});

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',
color: '#123456',
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.folder',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
},
{
id: expect.any(Number),
name: 'Folder2',
color: '#987654',
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.folder',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
},
]);
});

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',
ownerId: user.id,
parentId: null,
});
await folderService.createFolder({
name: 'Folder2',
color: '#987654',
ownerId: user.id,
parentId: null,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item',
headers: {
authorization: 'WrongAuth!',
},
});

expect(response.statusCode).toBe(401);
expect(response.json()).toEqual({
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
});
Loading

0 comments on commit 07ebc51

Please sign in to comment.