diff --git a/backend/middlewares/SharingMWs.ts b/backend/middlewares/SharingMWs.ts index b4f4070..aea1723 100644 --- a/backend/middlewares/SharingMWs.ts +++ b/backend/middlewares/SharingMWs.ts @@ -90,12 +90,10 @@ export class SharingMWs { }; try { - req.resultPipe = await ObjectManagerRepository.getInstance().SharingManager.updateSharing(sharing); return next(); - } catch (err) { - return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, "Error during creating sharing link", err)); + return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, "Error during updating sharing link", err)); } } diff --git a/backend/model/sql/SharingManager.ts b/backend/model/sql/SharingManager.ts index 8758fe5..dc3a6cc 100644 --- a/backend/model/sql/SharingManager.ts +++ b/backend/model/sql/SharingManager.ts @@ -7,29 +7,37 @@ import {PasswordHelper} from "../PasswordHelper"; export class SharingManager implements ISharingManager { + private static async removeExpiredLink() { + const connection = await SQLConnection.getConnection(); + return connection + .getRepository(SharingEntity) + .createQueryBuilder("share") + .where("expires < :now", {now: Date.now()}) + .delete() + .execute(); + } + async findOne(filter: any): Promise { - await this.removeExpiredLink(); + await SharingManager.removeExpiredLink(); const connection = await SQLConnection.getConnection(); return await connection.getRepository(SharingEntity).findOne(filter); } async createSharing(sharing: SharingDTO): Promise { - await this.removeExpiredLink(); + await SharingManager.removeExpiredLink(); const connection = await SQLConnection.getConnection(); if (sharing.password) { sharing.password = PasswordHelper.cryptPassword(sharing.password); } return await connection.getRepository(SharingEntity).save(sharing); - - } async updateSharing(inSharing: SharingDTO): Promise { const connection = await SQLConnection.getConnection(); - let sharing = await connection.getRepository(SharingEntity).findOne({ + const sharing = await connection.getRepository(SharingEntity).findOne({ id: inSharing.id, - creator: inSharing.creator, + creator: inSharing.creator.id, path: inSharing.path }); @@ -44,15 +52,5 @@ export class SharingManager implements ISharingManager { return await connection.getRepository(SharingEntity).save(sharing); } - private async removeExpiredLink() { - const connection = await SQLConnection.getConnection(); - return connection - .getRepository(SharingEntity) - .createQueryBuilder("share") - .where("expires < :now", {now: Date.now()}) - .delete() - .execute(); - } - } diff --git a/test/backend/unit/model/sql/SharingManager.ts b/test/backend/unit/model/sql/SharingManager.ts new file mode 100644 index 0000000..802d8c0 --- /dev/null +++ b/test/backend/unit/model/sql/SharingManager.ts @@ -0,0 +1,153 @@ +import {expect} from "chai"; +import * as fs from "fs"; +import * as path from "path"; +import {Config} from "../../../../../common/config/private/Config"; +import {DatabaseType} from "../../../../../common/config/private/IPrivateConfig"; +import {SQLConnection} from "../../../../../backend/model/sql/SQLConnection"; +import {SharingManager} from "../../../../../backend/model/sql/SharingManager"; +import {SharingDTO} from "../../../../../common/entities/SharingDTO"; +import {UserEntity} from "../../../../../backend/model/sql/enitites/UserEntity"; +import {UserDTO, UserRoles} from "../../../../../common/entities/UserDTO"; + +describe('SharingManager', () => { + + + const tempDir = path.join(__dirname, "../../tmp"); + const dbPath = path.join(tempDir, "test.db"); + + let creator: UserDTO = null; + + const setUpSqlDB = async () => { + if (fs.existsSync(dbPath)) { + fs.unlinkSync(dbPath); + } + if (!fs.existsSync(tempDir)) { + fs.mkdirSync(tempDir); + } + + Config.Server.database.type = DatabaseType.sqlite; + Config.Server.database.sqlite.storage = dbPath; + + const conn = await SQLConnection.getConnection(); + + creator = await conn.getRepository(UserEntity).save({ + id: null, + name: "test use", + password: "", + role: UserRoles.User, + permissions: null + }); + + await SQLConnection.close(); + }; + + const teardownUpSqlDB = async () => { + await SQLConnection.close(); + if (fs.existsSync(dbPath)) { + fs.unlinkSync(dbPath); + } + if (fs.existsSync(tempDir)) { + fs.rmdirSync(tempDir); + } + }; + + beforeEach(async () => { + await setUpSqlDB(); + }); + + afterEach(async () => { + await teardownUpSqlDB(); + }); + + + it('should create sharing', async () => { + let sm = new SharingManager(); + + let sharing: SharingDTO = { + id: null, + sharingKey: "testKey", + path: "/", + password: null, + creator: creator, + expires: Date.now() + 1000, + includeSubfolders: true, + timeStamp: Date.now() + }; + + const saved = await sm.createSharing(sharing); + expect(saved.id).to.not.equals(null); + expect(saved.creator.id).to.equals(creator.id); + expect(saved.sharingKey).to.equals(sharing.sharingKey); + expect(saved.timeStamp).to.equals(sharing.timeStamp); + expect(saved.password).to.equals(sharing.password); + expect(saved.expires).to.equals(sharing.expires); + expect(saved.includeSubfolders).to.equals(sharing.includeSubfolders); + }); + + + it('should find sharing', async () => { + let sm = new SharingManager(); + + let sharing: SharingDTO = { + id: null, + sharingKey: "testKey", + path: "/", + password: null, + creator: creator, + expires: Date.now() + 1000, + includeSubfolders: true, + timeStamp: Date.now() + }; + + const saved = await sm.createSharing(sharing); + const found = await sm.findOne({sharingKey: "testKey"}); + + expect(found.id).to.not.equals(null); + expect(found.sharingKey).to.equals(sharing.sharingKey); + expect(found.timeStamp).to.equals(sharing.timeStamp); + expect(found.password).to.equals(sharing.password); + expect(found.expires).to.equals(sharing.expires); + }); + + + it('should update sharing', async () => { + let sm = new SharingManager(); + + let sharing: SharingDTO = { + id: null, + sharingKey: "testKey", + path: "/", + password: null, + creator: creator, + expires: Date.now() + 1000, + includeSubfolders: true, + timeStamp: Date.now() + }; + + const saved = await sm.createSharing(sharing); + expect(saved.password).to.equals(sharing.password); + expect(saved.expires).to.equals(sharing.expires); + expect(saved.includeSubfolders).to.equals(sharing.includeSubfolders); + + let update: SharingDTO = { + id: saved.id, + sharingKey: saved.sharingKey, + path: saved.path, + password: null, + creator: creator, + expires: Date.now() + 2000, + includeSubfolders: false, + timeStamp: Date.now() + }; + const updated = await sm.updateSharing(update); + + console.log(updated); + expect(updated.id).to.equals(saved.id); + expect(updated.sharingKey).to.equals(sharing.sharingKey); + expect(updated.timeStamp).to.equals(sharing.timeStamp); + expect(updated.password).to.equals(update.password); + expect(updated.expires).to.equals(update.expires); + expect(updated.includeSubfolders).to.equals(update.includeSubfolders); + }); + +});