Skip to content

Commit

Permalink
#31 - Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders164a committed Oct 17, 2023
1 parent 23b5c62 commit bf902cf
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 76 deletions.
210 changes: 210 additions & 0 deletions src/modules/item/__test__/item.read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ describe('GET /api/item/:parentId', () => {
deletedAt: null,
updatedAt: expect.any(String),
isStarred: false,
linkedItemId: blob.id,
},
]);
});
Expand Down Expand Up @@ -228,3 +229,212 @@ describe('GET /api/item/:parentId', () => {
});
});
});

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

let user: User;
let otherUser: User;

beforeAll(async () => {
userService = UserServiceFactory.make();
folderService = FolderServiceFactory.make();
authService = AuthServiceFactory.make();
blobService = BlobServiceFactory.make();
docsService = DocsServiceFactory.make();

user = await userService.createUser({
name: 'Joe Biden the 4th',
email: '[email protected]',
password: '1234',
});
otherUser = await userService.createUser({
name: 'Joe Biden the 3rd',
email: '[email protected]',
password: '4321',
});
});

it('Should return status 200 and item from id', async () => {
const { accessToken } = await authService.createTokens(user.id);

const folder = await folderService.createFolder({
name: 'Folder1',
color: '#123456',
ownerId: user.id,
parentId: null,
});

const blob = await blobService.createBlob({
mimeType: 'text/plain',
name: 'test1.txt',
ownerId: user.id,
parentId: null,
blobUrl: 'https://example.com/test1.txt',
});

const docs = await docsService.createDocs({
name: 'Docser',
ownerId: user.id,
parentId: null,
text: 'Docs text here!',
});

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

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

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

expect(responseFolder.statusCode).toBe(200);
expect(responseFolder.json()).toEqual({
id: expect.any(Number),
name: 'Folder1',
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.folder',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
ItemBlob: null,
ItemFolder: {
id: expect.any(Number),
color: '#123456',
},
ItemDocs: null,
});

expect(responseBlob.statusCode).toBe(200);
expect(responseBlob.json()).toEqual({
id: expect.any(Number),
name: 'test1.txt',
parentId: null,
ownerId: user.id,
mimeType: 'text/plain',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
ItemBlob: {
id: expect.any(Number),
blobUrl: 'https://example.com/test1.txt',
},
ItemFolder: null,
ItemDocs: null,
});

expect(responseDocs.statusCode).toBe(200);
expect(responseDocs.json()).toEqual({
id: expect.any(Number),
name: 'Docser',
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.docs',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
ItemBlob: null,
ItemFolder: null,
ItemDocs: {
id: expect.any(Number),
text: 'Docs text here!',
},
});
});

it('Should return status 400, when item not found', async () => {
const { accessToken } = await authService.createTokens(user.id);

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

expect(response.statusCode).toBe(400);
expect(response.json()).toEqual({
error: 'BadRequestError',
errors: {
_: ['Item not found'],
},
statusCode: 400,
});
});

it('Should return status 401, when unauthorized', async () => {
const folder = await folderService.createFolder({
name: 'Folder1',
color: '#123456',
ownerId: user.id,
parentId: null,
});

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

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

it('Should return status 401, when no access to file', async () => {
const { accessToken } = await authService.createTokens(user.id);

const folder = await folderService.createFolder({
name: 'Folder1',
color: '#123456',
ownerId: otherUser.id,
parentId: null,
});

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

expect(response.statusCode).toBe(401);
expect(response.json()).toEqual({
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
});
1 change: 1 addition & 0 deletions src/modules/item/__test__/item.root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ describe('GET /api/item', () => {
deletedAt: null,
updatedAt: expect.any(String),
isStarred: false,
linkedItemId: folder.id,
},
]);
});
Expand Down
1 change: 1 addition & 0 deletions src/modules/item/__test__/item.shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ describe('GET /api/item/shared', () => {
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
linkedItemId: blob.id,
},
{
id: expect.any(Number),
Expand Down
11 changes: 0 additions & 11 deletions src/modules/item/item.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ export default class ItemController {
}
}

public async browseHandler(request: FastifyRequest, reply: FastifyReply) {
try {
const items = await this.itemService.getByOwnerId(request.user.sub);

return reply.code(200).send(items);
} catch (e) {
/* istanbul ignore next */
return reply.badRequest();
}
}

public async itemRootHandler(request: FastifyRequest, reply: FastifyReply) {
try {
const items = await this.itemService.getByOwnerIdAndParentId(request.user.sub, null);
Expand Down
19 changes: 0 additions & 19 deletions src/modules/item/item.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,6 @@ export default async (fastify: FastifyInstance) => {
itemController.readHandler.bind(itemController),
);

fastify.get(
'/folders',
{
schema: {
tags: ['Item'],
response: {
200: { $ref: 'itemsResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
itemController.browseHandler.bind(itemController),
);

fastify.get(
'/:parentId',
{
Expand Down
40 changes: 21 additions & 19 deletions src/modules/item/item.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const itemFolderDocsBlobResponseSchema = {
type: 'string',
},
ownerId: {
type: 'string',
type: 'number',
},
parentId: {
type: ['number', 'null'],
Expand All @@ -258,26 +258,23 @@ const itemFolderDocsBlobResponseSchema = {
},
ItemBlob: {
type: ['object', 'null'],
properties: {
id: {
type: 'number',
},
blobUrl: {
type: 'string',
},
},
properties: {
id: {
type: 'number',
},
blobUrl: {
type: 'string',
},
},
},
ItemDocs: {
type: ['object', 'null'],
items: {
type: 'object',
properties: {
id: {
type: 'number',
},
text: {
type: 'string',
},
properties: {
id: {
type: 'number',
},
text: {
type: 'string',
},
},
},
Expand All @@ -297,4 +294,9 @@ export type itemSharingsInput = FromSchema<typeof itemSharingsSchema>;
export type itemReadInput = FromSchema<typeof itemReadSchema>;
export type ReadInput = FromSchema<typeof readItemsSchema>;

export const itemSchemas = [readItemsSchema, itemsResponseSchema, itemSharingsResponseSchema, itemFolderDocsBlobResponseSchema];
export const itemSchemas = [
readItemsSchema,
itemsResponseSchema,
itemSharingsResponseSchema,
itemFolderDocsBlobResponseSchema,
];
27 changes: 0 additions & 27 deletions src/modules/item/item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,6 @@ export default class ItemService {
return this.formatItems(items);
}

public async getByOwnerId(
ownerId: number,
): Promise<ItemWithProperties[]> {
const items = await prisma.item.findMany({
where: {
ownerId: ownerId,
},
include: {
ItemBlob: true,
ItemFolder: true,
ItemDocs: true,
ItemShortcut: true,
ItemStarred: {
where: {
userId: ownerId,
},
},
},
});

return this.formatItems(items);
}

public async getAllOwnedAndSharredItemsByParentIdAndUserIdRecursively(
userId: number,
parentId: number | null,
Expand Down Expand Up @@ -226,10 +203,6 @@ export default class ItemService {
},
});

if (!item) {
throw new Error('item.notFound');
}

return item;
}

Expand Down

0 comments on commit bf902cf

Please sign in to comment.