diff --git a/tdrive/backend/node/src/core/platform/framework/api/crud-service.ts b/tdrive/backend/node/src/core/platform/framework/api/crud-service.ts index 373e4943..de716d12 100644 --- a/tdrive/backend/node/src/core/platform/framework/api/crud-service.ts +++ b/tdrive/backend/node/src/core/platform/framework/api/crud-service.ts @@ -135,6 +135,10 @@ export class CrudException extends Error { return new CrudException(details, 400); } + static unauthorized(details: string): CrudException { + return new CrudException(details, 401); + } + static notFound(details: string): CrudException { return new CrudException(details, 404); } diff --git a/tdrive/backend/node/src/core/platform/services/auth/web/jwt.ts b/tdrive/backend/node/src/core/platform/services/auth/web/jwt.ts index eb1ca418..641b99c0 100644 --- a/tdrive/backend/node/src/core/platform/services/auth/web/jwt.ts +++ b/tdrive/backend/node/src/core/platform/services/auth/web/jwt.ts @@ -6,6 +6,7 @@ import config from "../../../../config"; import { JwtType } from "../../types"; import { executionStorage } from "../../../framework/execution-storage"; import gr from "../../../../../services/global-resolver"; +import { CrudException } from "../../../framework/api/crud-service"; const jwtPlugin: FastifyPluginCallback = async (fastify, _opts, next) => { fastify.register(cookie); @@ -50,7 +51,7 @@ const jwtPlugin: FastifyPluginCallback = async (fastify, _opts, next) => { try { await authenticate(request); } catch (err) { - throw fastify.httpErrors.unauthorized(`Bad credentials ${JSON.stringify(err)}`); + throw CrudException.unauthorized(`Bad credentials: ${err.message}`); } }); diff --git a/tdrive/backend/node/src/services/documents/utils.ts b/tdrive/backend/node/src/services/documents/utils.ts index fd0de488..c62338ef 100644 --- a/tdrive/backend/node/src/services/documents/utils.ts +++ b/tdrive/backend/node/src/services/documents/utils.ts @@ -311,10 +311,11 @@ export const addDriveItemToArchive = async ( context: CompanyExecutionContext, prefix?: string, ): Promise => { - const item = entity || (await repository.findOne({ id, company_id: context.company.id })); + const itemPK = { id, company_id: context.company.id }; + const item = entity || (await repository.findOne(itemPK)); if (!item) { - throw Error("item not found"); + throw Error(`Item '${JSON.stringify(itemPK)}' not found`); } if (!item.is_directory) { diff --git a/tdrive/backend/node/src/services/documents/web/controllers/documents.ts b/tdrive/backend/node/src/services/documents/web/controllers/documents.ts index aeb9378d..10ec7f49 100644 --- a/tdrive/backend/node/src/services/documents/web/controllers/documents.ts +++ b/tdrive/backend/node/src/services/documents/web/controllers/documents.ts @@ -427,6 +427,8 @@ export class DocumentsController { }); archive.pipe(reply.raw); + + return reply; } catch (error) { logger.error({ error: `${error}` }, "failed to send zip file"); throw new CrudException("Failed to create zip file", 500); diff --git a/tdrive/backend/node/test/e2e/common/user-api.ts b/tdrive/backend/node/test/e2e/common/user-api.ts index 370d4f7b..88c32c7a 100644 --- a/tdrive/backend/node/test/e2e/common/user-api.ts +++ b/tdrive/backend/node/test/e2e/common/user-api.ts @@ -400,7 +400,7 @@ export default class UserApi { }); }; - async getFolder(id: string | "root" | "trash" | "shared_with_me") { + async zipDocument(id: string | "root" | "trash" | "shared_with_me") { return await this.platform.app.inject({ method: "GET", url: `${UserApi.DOC_URL}/companies/${this.platform.workspace.company_id}/item/download/zip?items=${id}`, @@ -410,7 +410,6 @@ export default class UserApi { }); }; - async getDocumentOKCheck(id: string | "root" | "trash" | "shared_with_me") { const response = await this.getDocument(id); expect(response.statusCode).toBe(200); @@ -418,13 +417,6 @@ export default class UserApi { expect(doc.item?.id).toBe(id); }; - async getFolderOKCheck(id: string | "root" | "trash" | "shared_with_me") { - const response = await this.getFolder(id); - expect(response.statusCode).toBe(200); - const doc = deserialize(DriveItemDetailsMockClass, response.body); - expect(doc.item?.id).toBe(id); - }; - async sharedWithMeDocuments( payload: Record ) { diff --git a/tdrive/backend/node/test/e2e/documents/documents.spec.ts b/tdrive/backend/node/test/e2e/documents/documents.spec.ts index 98788bbc..c3aec8ad 100644 --- a/tdrive/backend/node/test/e2e/documents/documents.spec.ts +++ b/tdrive/backend/node/test/e2e/documents/documents.spec.ts @@ -106,6 +106,22 @@ describe("the Drive feature", () => { expect(updateItemResult.name).toEqual("somethingelse"); }); + it("Download folder as a zip should work fine", async () => { + //given + const folder = await currentUser.createDirectory("user_" + currentUser.user.id) + await currentUser.uploadRandomFileAndCreateDocument(folder.id); + + //when + const zipResponse = await currentUser.zipDocument(folder.id); + + //then + expect(zipResponse).toBeTruthy(); + expect(zipResponse.statusCode).toBe(200); + + //and data is in place + expect(zipResponse.body.length).toBeGreaterThanOrEqual(100); + }); + it("did move an item to trash", async () => { const createItemResult = await currentUser.createDefaultDocument(); diff --git a/tdrive/backend/node/test/e2e/documents/public-links.spec.ts b/tdrive/backend/node/test/e2e/documents/public-links.spec.ts index 4ab025ed..9be613fd 100644 --- a/tdrive/backend/node/test/e2e/documents/public-links.spec.ts +++ b/tdrive/backend/node/test/e2e/documents/public-links.spec.ts @@ -286,7 +286,7 @@ describe("the public links feature", () => { anotherUser.jwt = token.value; await anotherUser.getDocumentOKCheck(doc.id); - expect((await anotherUser.getFolder(doc.id)).statusCode).toBe(200); + expect((await anotherUser.zipDocument(doc.id)).statusCode).toBe(200); }); }); });