1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-01-02 03:37:54 +02:00

fixing sharing manager, imeplementing tests

This commit is contained in:
Patrik J. Braun 2018-02-03 15:07:50 -05:00
parent fe1b31feea
commit 7896b5ae28
3 changed files with 168 additions and 19 deletions

View File

@ -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));
}
}

View File

@ -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<SharingDTO> {
await this.removeExpiredLink();
await SharingManager.removeExpiredLink();
const connection = await SQLConnection.getConnection();
return await connection.getRepository(SharingEntity).findOne(filter);
}
async createSharing(sharing: SharingDTO): Promise<SharingDTO> {
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<SharingDTO> {
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: <any>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();
}
}

View File

@ -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);
});
});