2018-03-31 03:30:30 +08:00
|
|
|
import {expect} from 'chai';
|
2019-12-15 00:27:01 +08:00
|
|
|
import {SQLConnection} from '../../../../../src/backend/model/database/sql/SQLConnection';
|
|
|
|
import {SharingManager} from '../../../../../src/backend/model/database/sql/SharingManager';
|
2019-12-10 17:44:35 +08:00
|
|
|
import {SharingDTO} from '../../../../../src/common/entities/SharingDTO';
|
2019-12-15 00:27:01 +08:00
|
|
|
import {UserEntity} from '../../../../../src/backend/model/database/sql/enitites/UserEntity';
|
2019-12-10 17:44:35 +08:00
|
|
|
import {UserDTO, UserRoles} from '../../../../../src/common/entities/UserDTO';
|
2019-01-28 03:36:42 +08:00
|
|
|
import {SQLTestHelper} from '../../../SQLTestHelper';
|
2018-02-04 04:07:50 +08:00
|
|
|
|
2019-01-28 03:36:42 +08:00
|
|
|
// to help WebStorm to handle the test cases
|
|
|
|
declare let describe: any;
|
|
|
|
declare const after: any;
|
|
|
|
describe = SQLTestHelper.describe;
|
2018-02-04 04:07:50 +08:00
|
|
|
|
2019-01-28 03:36:42 +08:00
|
|
|
describe('SharingManager', (sqlHelper: SQLTestHelper) => {
|
2018-02-04 04:07:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
let creator: UserDTO = null;
|
|
|
|
|
|
|
|
const setUpSqlDB = async () => {
|
2019-01-28 03:36:42 +08:00
|
|
|
await sqlHelper.initDB();
|
2018-02-04 04:07:50 +08:00
|
|
|
|
|
|
|
const conn = await SQLConnection.getConnection();
|
|
|
|
|
|
|
|
creator = await conn.getRepository(UserEntity).save({
|
|
|
|
id: null,
|
2018-03-31 03:30:30 +08:00
|
|
|
name: 'test use',
|
|
|
|
password: '',
|
2018-02-04 04:07:50 +08:00
|
|
|
role: UserRoles.User,
|
|
|
|
permissions: null
|
|
|
|
});
|
|
|
|
|
|
|
|
await SQLConnection.close();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await setUpSqlDB();
|
|
|
|
});
|
|
|
|
|
2019-01-28 03:36:42 +08:00
|
|
|
after(async () => {
|
|
|
|
await sqlHelper.clearDB();
|
2018-02-04 04:07:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should create sharing', async () => {
|
2018-11-24 18:50:11 +08:00
|
|
|
const sm = new SharingManager();
|
2018-02-04 04:07:50 +08:00
|
|
|
|
2018-11-24 18:50:11 +08:00
|
|
|
const sharing: SharingDTO = {
|
2018-02-04 04:07:50 +08:00
|
|
|
id: null,
|
2018-03-31 03:30:30 +08:00
|
|
|
sharingKey: 'testKey',
|
|
|
|
path: '/',
|
2018-02-04 04:07:50 +08:00
|
|
|
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 () => {
|
2018-11-24 18:50:11 +08:00
|
|
|
const sm = new SharingManager();
|
2018-02-04 04:07:50 +08:00
|
|
|
|
2018-11-24 18:50:11 +08:00
|
|
|
const sharing: SharingDTO = {
|
2018-02-04 04:07:50 +08:00
|
|
|
id: null,
|
2018-03-31 03:30:30 +08:00
|
|
|
sharingKey: 'testKey',
|
|
|
|
path: '/',
|
2018-02-04 04:07:50 +08:00
|
|
|
password: null,
|
|
|
|
creator: creator,
|
|
|
|
expires: Date.now() + 1000,
|
|
|
|
includeSubfolders: true,
|
|
|
|
timeStamp: Date.now()
|
|
|
|
};
|
|
|
|
|
|
|
|
const saved = await sm.createSharing(sharing);
|
2018-03-31 03:30:30 +08:00
|
|
|
const found = await sm.findOne({sharingKey: 'testKey'});
|
2018-02-04 04:07:50 +08:00
|
|
|
|
|
|
|
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 () => {
|
2018-11-24 18:50:11 +08:00
|
|
|
const sm = new SharingManager();
|
2018-02-04 04:07:50 +08:00
|
|
|
|
2018-11-24 18:50:11 +08:00
|
|
|
const sharing: SharingDTO = {
|
2018-02-04 04:07:50 +08:00
|
|
|
id: null,
|
2018-03-31 03:30:30 +08:00
|
|
|
sharingKey: 'testKey',
|
|
|
|
path: '/',
|
2018-02-04 04:07:50 +08:00
|
|
|
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);
|
|
|
|
|
2018-11-24 18:50:11 +08:00
|
|
|
const update: SharingDTO = {
|
2018-02-04 04:07:50 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|