Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gmp store logging #1684

Merged
merged 5 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [8.0.2] - unreleased

### Added
- Added new setting to enable and disable debug logging for the redux store [#1684](https://github.com/greenbone/gsa/pull/1684)
- Added text notification when old password is entered but new password isn't [#1636](https://github.com/greenbone/gsa/pull/1636)
- Added a loading timer for empty trash button [#1604](https://github.com/greenbone/gsa/pull/1604)
- Added details to alert details page [#1591](https://github.com/greenbone/gsa/pull/1591)
Expand Down
123 changes: 73 additions & 50 deletions gsa/src/gmp/__tests__/gmpsettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import GmpSettings, {
const createStorage = state => {
const store = {
...state,
setItem: jest.fn((name, value) => (store[name] = value)),
setItem: jest.fn((name, value) => (store[name] = '' + value)),
removeItem: jest.fn(name => delete store[name]),
};
return store;
Expand All @@ -36,21 +36,23 @@ describe('GmpSettings tests', () => {
test('should init with defaults', () => {
const storage = createStorage();
const settings = new GmpSettings(storage);

expect(settings.disableLoginForm).toEqual(false);
expect(settings.enableStoreDebugLog).toBeUndefined();
expect(settings.guestUsername).toBeUndefined();
expect(settings.guestPassword).toBeUndefined();
expect(settings.loglevel).toEqual(DEFAULT_LOG_LEVEL);
expect(settings.reloadinterval).toEqual(DEFAULT_RELOAD_INTERVAL);
expect(settings.locale).toBeUndefined();
expect(settings.manualUrl).toEqual(DEFAULT_MANUAL_URL);
expect(settings.manualLanguageMapping).toBeUndefined();
expect(settings.protocol).toEqual('http:');
expect(settings.protocoldocurl).toEqual(DEFAULT_PROTOCOLDOC_URL);
expect(settings.reloadinterval).toEqual(DEFAULT_RELOAD_INTERVAL);
expect(settings.server).toEqual('localhost');
expect(settings.token).toBeUndefined();
expect(settings.timeout).toBeUndefined();
expect(settings.timezone).toBeUndefined();
expect(settings.username).toBeUndefined();
expect(settings.guestUsername).toBeUndefined();
expect(settings.guestPassword).toBeUndefined();
expect(settings.disableLoginForm).toEqual(false);
expect(settings.vendorVersion).toBeUndefined();
expect(settings.vendorLabel).toBeUndefined();

Expand All @@ -61,7 +63,10 @@ describe('GmpSettings tests', () => {
test('should init with passed options', () => {
const storage = createStorage();
const settings = new GmpSettings(storage, {
reloadinterval: 10,
disableLoginForm: true,
enableStoreDebugLog: true,
guestUsername: 'guest',
guestPassword: 'pass',
locale: 'en',
loglevel: 'error',
manualUrl: 'http://manual',
Expand All @@ -70,48 +75,52 @@ describe('GmpSettings tests', () => {
},
protocol: 'http',
protocoldocurl: 'http://protocol',
reloadinterval: 10,
server: 'localhost',
token: 'atoken',
timeout: 30000,
timezone: 'cet',
username: 'foo',
guestUsername: 'guest',
guestPassword: 'pass',
disableLoginForm: true,
vendorVersion: 'foo',
vendorLabel: 'foo.bar',
});

expect(settings.reloadinterval).toEqual(10);
expect(settings.disableLoginForm).toEqual(true);
expect(settings.enableStoreDebugLog).toEqual(true);
expect(settings.guestUsername).toEqual('guest');
expect(settings.guestPassword).toEqual('pass');
expect(settings.locale).toBeUndefined();
expect(settings.loglevel).toEqual('error');
expect(settings.manualUrl).toEqual('http://manual');
expect(settings.manualLanguageMapping).toEqual({foo: 'bar'});
expect(settings.protocol).toEqual('http');
expect(settings.protocoldocurl).toEqual('http://protocol');
expect(settings.reloadinterval).toEqual(10);
expect(settings.server).toEqual('localhost');
expect(settings.token).toBeUndefined();
expect(settings.timeout).toEqual(30000);
expect(settings.timezone).toBeUndefined();
expect(settings.username).toBeUndefined();
expect(settings.loglevel).toEqual('error');

expect(settings.guestUsername).toEqual('guest');
expect(settings.guestPassword).toEqual('pass');
expect(settings.disableLoginForm).toEqual(true);
expect(settings.vendorVersion).toEqual('foo');
expect(settings.vendorLabel).toEqual('foo.bar');

expect(storage.setItem).toHaveBeenCalledTimes(1);
expect(storage.setItem).toHaveBeenCalledWith('loglevel', 'error');
expect(storage.setItem).toHaveBeenCalledTimes(2);
expect(storage.setItem).toHaveBeenNthCalledWith(
1,
'enableStoreDebugLog',
'1',
);
expect(storage.setItem).toHaveBeenNthCalledWith(2, 'loglevel', 'error');
});

test('should init from store', () => {
const storage = createStorage({
enableStoreDebugLog: '0',
locale: 'en',
token: 'atoken',
loglevel: 'error',
timezone: 'cet',
token: 'atoken',
username: 'foo',
loglevel: 'error',
});

const settings = new GmpSettings(storage, {
Expand All @@ -121,100 +130,117 @@ describe('GmpSettings tests', () => {
protocol: 'http',
});

expect(settings.reloadinterval).toEqual(DEFAULT_RELOAD_INTERVAL);
expect(settings.enableStoreDebugLog).toEqual(false);
expect(settings.locale).toEqual('en');
expect(settings.loglevel).toEqual('error');
expect(settings.manualUrl).toEqual(DEFAULT_MANUAL_URL);
expect(settings.manualLanguageMapping).toBeUndefined();
expect(settings.protocol).toEqual('http');
expect(settings.protocoldocurl).toEqual(DEFAULT_PROTOCOLDOC_URL);
expect(settings.reloadinterval).toEqual(DEFAULT_RELOAD_INTERVAL);
expect(settings.server).toEqual('foo');
expect(settings.token).toEqual('atoken');
expect(settings.timeout).toBeUndefined();
expect(settings.timezone).toEqual('cet');
expect(settings.username).toEqual('foo');
expect(settings.loglevel).toEqual('error');

expect(storage.setItem).toHaveBeenCalledTimes(1);
expect(storage.setItem).toHaveBeenCalledWith('loglevel', 'error');
});

test('should ensure options override settings from storage', () => {
const storage = createStorage({
reloadinterval: 20,
enableStoreDebugLog: '0',
locale: 'de',
loglevel: 'error',
manualUrl: 'http://ipsum',
manualLanguageMapping: {lorem: 'ipsum'},
protocol: 'https',
protocoldocurl: 'http://lorem',
reloadinterval: 20,
server: 'foo.bar',
token: 'btoken',
timeout: 10000,
timezone: 'cest',
username: 'bar',
loglevel: 'error',
vendorVersion: 'foo',
vendorLabel: 'foo.bar',
});

const settings = new GmpSettings(storage, {
reloadinterval: 10,
enableStoreDebugLog: true,
locale: 'en',
loglevel: 'debug',
manualUrl: 'http://manual',
manualLanguageMapping: {foo: 'bar'},
protocol: 'http',
protocoldocurl: 'http://protocol',
reloadinterval: 10,
server: 'localhost',
token: 'atoken',
timeout: 30000,
timezone: 'cet',
username: 'foo',
loglevel: 'debug',
vendorVersion: 'bar',
vendorLabel: 'bar.foo',
});

expect(settings.reloadinterval).toEqual(10);
expect(settings.enableStoreDebugLog).toEqual(true);
expect(settings.locale).toEqual('de');
expect(settings.loglevel).toEqual('debug');
expect(settings.manualUrl).toEqual('http://manual');
expect(settings.manualLanguageMapping).toEqual({foo: 'bar'});
expect(settings.protocol).toEqual('http');
expect(settings.protocoldocurl).toEqual('http://protocol');
expect(settings.reloadinterval).toEqual(10);
expect(settings.server).toEqual('localhost');
expect(settings.token).toEqual('btoken');
expect(settings.timeout).toEqual(30000);
expect(settings.timezone).toEqual('cest');
expect(settings.username).toEqual('bar');
expect(settings.loglevel).toEqual('debug');
expect(settings.vendorVersion).toEqual('bar');
expect(settings.vendorLabel).toEqual('bar.foo');

expect(storage.setItem).toHaveBeenCalledTimes(1);
expect(storage.setItem).toHaveBeenCalledWith('loglevel', 'debug');
expect(storage.setItem).toHaveBeenCalledTimes(2);
expect(storage.setItem).toHaveBeenNthCalledWith(
1,
'enableStoreDebugLog',
'1',
);
expect(storage.setItem).toHaveBeenNthCalledWith(2, 'loglevel', 'debug');
});

test('should delete settings from storage', () => {
const storage = createStorage({
enableStoreDebugLog: '1',
locale: 'en',
loglevel: 'error',
token: 'atoken',
timezone: 'cet',
username: 'foo',
loglevel: 'error',
});

const settings = new GmpSettings(storage, {});

expect(settings.enableStoreDebugLog).toEqual(true);
expect(settings.locale).toEqual('en');
expect(settings.loglevel).toEqual('error');
expect(settings.token).toEqual('atoken');
expect(settings.timezone).toEqual('cet');
expect(settings.username).toEqual('foo');
expect(settings.loglevel).toEqual('error');

expect(storage.setItem).toHaveBeenCalledTimes(1);
expect(storage.setItem).toHaveBeenCalledWith('loglevel', 'error');

settings.enableStoreDebugLog = undefined;
expect(storage.removeItem).toBeCalledWith('enableStoreDebugLog');

settings.locale = undefined;
expect(storage.removeItem).toBeCalledWith('locale');

settings.loglevel = undefined;
expect(storage.removeItem).toBeCalledWith('loglevel');

settings.token = undefined;
expect(storage.removeItem).toBeCalledWith('token');

Expand All @@ -223,33 +249,42 @@ describe('GmpSettings tests', () => {

settings.username = undefined;
expect(storage.removeItem).toBeCalledWith('username');

settings.loglevel = undefined;
expect(storage.removeItem).toBeCalledWith('loglevel');
});

test('should freeze properties', () => {
const storage = createStorage();
const settings = new GmpSettings(storage, {
reloadinterval: 10,
disableLoginForm: true,
guestUsername: 'guest',
guestPassword: 'pass',
locale: 'en',
loglevel: 'error',
manualUrl: 'http://manual',
manualLanguageMapping: {foo: 'bar'},
protocol: 'http',
protocoldocurl: 'http://protocol',
reloadinterval: 10,
server: 'localhost',
token: 'atoken',
timeout: 30000,
timezone: 'cet',
username: 'foo',
guestUsername: 'guest',
guestPassword: 'pass',
disableLoginForm: true,
vendorVersion: 'foobar',
vendorLabel: 'foo.bar',
});

expect(() => {
settings.disableLoginForm = false;
}).toThrow();
expect(settings.disableLoginForm).toEqual(true);
expect(() => {
settings.guestUsername = 'foo';
}).toThrow();
expect(settings.guestUsername).toEqual('guest');
expect(() => {
settings.guestPassword = 'foo';
}).toThrow();
expect(settings.guestPassword).toEqual('pass');
expect(() => {
settings.manualUrl = 'foo';
}).toThrow();
Expand All @@ -270,18 +305,6 @@ describe('GmpSettings tests', () => {
settings.protocol = 'foo';
}).toThrow();
expect(settings.protocol).toEqual('http');
expect(() => {
settings.guestUsername = 'foo';
}).toThrow();
expect(settings.guestUsername).toEqual('guest');
expect(() => {
settings.guestPassword = 'foo';
}).toThrow();
expect(settings.guestPassword).toEqual('pass');
expect(() => {
settings.disableLoginForm = false;
}).toThrow();
expect(settings.disableLoginForm).toEqual(true);
expect(() => {
settings.vendorVersion = 'barfoo';
}).toThrow();
Expand Down
33 changes: 27 additions & 6 deletions gsa/src/gmp/gmpsettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class GmpSettings {
constructor(storage = global.localStorage, options = {}) {
const {
disableLoginForm = false,
enableStoreDebugLog,
guestUsername,
guestPassword,
loglevel = storage.loglevel,
manualUrl = DEFAULT_MANUAL_URL,
manualLanguageMapping,
Expand All @@ -51,25 +54,27 @@ class GmpSettings {
reloadinterval = DEFAULT_RELOAD_INTERVAL,
server = global.location.host,
timeout,
guestUsername,
guestPassword,
vendorVersion,
vendorLabel,
} = {...options};
this.storage = storage;

if (isDefined(enableStoreDebugLog)) {
this.enableStoreDebugLog = enableStoreDebugLog;
}

this.loglevel = isDefined(loglevel) ? loglevel : DEFAULT_LOG_LEVEL;
this.reloadinterval = reloadinterval;
this.timeout = timeout;

setAndFreeze(this, 'disableLoginForm', disableLoginForm);
setAndFreeze(this, 'guestUsername', guestUsername);
setAndFreeze(this, 'guestPassword', guestPassword);
setAndFreeze(this, 'manualUrl', manualUrl);
setAndFreeze(this, 'manualLanguageMapping', manualLanguageMapping);
setAndFreeze(this, 'protocol', protocol);
setAndFreeze(this, 'protocoldocurl', protocoldocurl);
setAndFreeze(this, 'server', server);
setAndFreeze(this, 'protocol', protocol);
setAndFreeze(this, 'guestUsername', guestUsername);
setAndFreeze(this, 'guestPassword', guestPassword);
setAndFreeze(this, 'disableLoginForm', disableLoginForm);
setAndFreeze(this, 'vendorVersion', vendorVersion);
setAndFreeze(this, 'vendorLabel', vendorLabel);
}
Expand Down Expand Up @@ -113,6 +118,22 @@ class GmpSettings {
set loglevel(value) {
set(this.storage, 'loglevel', value);
}

get enableStoreDebugLog() {
const enabled = this.storage.enableStoreDebugLog;
if (isDefined(enabled)) {
return enabled === '1';
}
return enabled;
}

set enableStoreDebugLog(value) {
let storeValue;
if (isDefined(value)) {
storeValue = value ? '1' : '0';
}
set(this.storage, 'enableStoreDebugLog', storeValue);
}
}

export default GmpSettings;
Expand Down
Loading