2023-03-12 05:42:46 +08:00
|
|
|
/* eslint-disable no-unused-expressions,@typescript-eslint/no-unused-expressions */
|
|
|
|
import {expect} from 'chai';
|
|
|
|
import {ErrorDTO} from '../../../../../src/common/entities/Error';
|
|
|
|
import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers';
|
|
|
|
import {SettingsMWs} from '../../../../../src/backend/middlewares/admin/SettingsMWs';
|
|
|
|
import {ServerUserConfig} from '../../../../../src/common/config/private/PrivateConfig';
|
|
|
|
import {Config} from '../../../../../src/common/config/private/Config';
|
|
|
|
import {UserRoles} from '../../../../../src/common/entities/UserDTO';
|
2023-07-30 20:40:54 +08:00
|
|
|
import {ConfigClassBuilder} from '../../../../../node_modules/typeconfig/node';
|
|
|
|
import {ServerEnvironment} from '../../../../../src/backend/Environment';
|
2023-07-30 22:11:46 +08:00
|
|
|
import {EmailMessagingType} from '../../../../../src/common/config/private/MessagingConfig';
|
2023-07-31 01:10:53 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2023-03-12 05:42:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
declare const describe: any;
|
|
|
|
declare const it: any;
|
|
|
|
declare const beforeEach: any;
|
|
|
|
|
|
|
|
describe('Settings middleware', () => {
|
|
|
|
|
2023-07-31 01:10:53 +08:00
|
|
|
const tempDir = path.join(__dirname, '../../../tmp');
|
|
|
|
beforeEach(async () => {
|
|
|
|
await ObjectManagers.reset();
|
|
|
|
await fs.promises.rm(tempDir, {recursive: true, force: true});
|
2023-03-12 05:42:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should save empty enforced users settings', (done: (err?: any) => void) => {
|
2023-07-30 23:52:46 +08:00
|
|
|
console.log('sarting - should save empty enforced users settings');
|
2023-07-30 20:40:54 +08:00
|
|
|
ServerEnvironment.sendMailAvailable = false;
|
2023-07-30 21:45:46 +08:00
|
|
|
Config.Environment.sendMailAvailable = false;
|
2023-07-30 23:45:11 +08:00
|
|
|
Config.Messaging.Email.type = EmailMessagingType.SMTP;
|
2023-03-12 05:42:46 +08:00
|
|
|
const req: any = {
|
|
|
|
session: {},
|
|
|
|
sessionOptions: {},
|
|
|
|
query: {},
|
|
|
|
params: {},
|
|
|
|
body: {
|
|
|
|
settingsPath: 'Users',
|
2023-07-30 20:40:54 +08:00
|
|
|
settings: ConfigClassBuilder.attachPrivateInterface(new ServerUserConfig()).toJSON()
|
2023-03-12 05:42:46 +08:00
|
|
|
}
|
|
|
|
};
|
2023-07-30 23:22:47 +08:00
|
|
|
console.log('Settings', req);
|
2023-03-12 05:42:46 +08:00
|
|
|
req.body.settings.enforcedUsers = [];
|
|
|
|
const next: any = (err: ErrorDTO) => {
|
|
|
|
try {
|
2023-07-30 20:40:54 +08:00
|
|
|
expect(err).to.be.undefined;
|
2023-03-12 05:42:46 +08:00
|
|
|
expect(Config.Users.enforcedUsers.length).to.be.equal(0);
|
|
|
|
done();
|
|
|
|
} catch (err) {
|
2023-07-30 20:49:55 +08:00
|
|
|
console.error(err);
|
2023-03-12 05:42:46 +08:00
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
SettingsMWs.updateSettings(req, null, next);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
it('should save enforced users settings', (done: (err?: any) => void) => {
|
2023-07-30 23:52:46 +08:00
|
|
|
console.log('sarting - should save enforced users settings');
|
2023-07-30 20:49:55 +08:00
|
|
|
|
|
|
|
ServerEnvironment.sendMailAvailable = false;
|
2023-07-30 23:37:54 +08:00
|
|
|
Config.Environment.sendMailAvailable = false;
|
2023-07-30 23:45:11 +08:00
|
|
|
Config.Messaging.Email.type = EmailMessagingType.SMTP;
|
2023-03-12 05:42:46 +08:00
|
|
|
const req: any = {
|
|
|
|
session: {},
|
|
|
|
sessionOptions: {},
|
|
|
|
query: {},
|
|
|
|
params: {},
|
|
|
|
body: {
|
|
|
|
settingsPath: 'Users',
|
|
|
|
settings: {
|
|
|
|
enforcedUsers: [],
|
|
|
|
authenticationRequired: false,
|
|
|
|
unAuthenticatedUserRole: UserRoles.User
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.body.settings.enforcedUsers = [{name: 'Apple', password: 'Apple pass', role: UserRoles.User}];
|
|
|
|
|
|
|
|
const next2: any = (err: ErrorDTO) => {
|
|
|
|
try {
|
|
|
|
expect(Config.Users.enforcedUsers.length).to.be.equal(1);
|
|
|
|
expect(Config.Users.enforcedUsers[0].name).to.be.equal('Apple');
|
|
|
|
expect(Config.Users.enforcedUsers.length).to.be.equal(1);
|
|
|
|
Config.original().then((cfg) => {
|
2023-07-30 23:37:54 +08:00
|
|
|
try {
|
|
|
|
expect(cfg.Users.enforcedUsers.length).to.be.equal(1);
|
|
|
|
expect(cfg.Users.enforcedUsers[0].name).to.be.equal('Apple');
|
|
|
|
done();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
done(err);
|
|
|
|
}
|
2023-03-12 05:42:46 +08:00
|
|
|
}).catch(done);
|
|
|
|
} catch (err) {
|
2023-07-30 20:49:55 +08:00
|
|
|
console.error(err);
|
2023-03-12 05:42:46 +08:00
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
SettingsMWs.updateSettings(req, null, next2);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|