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

Filter fixes #1288

Merged
merged 10 commits into from
Apr 17, 2019
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ $ cd gsa && git checkout gsa-8.0 && git log

## gsa 8.0.1 (unreleased)

* Set first=1 when starting delta report selection #1288
* Fix pagination with default filter (reset filter.id if filter is changed) #1288
* Fix setting filters at report details page with less and greater then relations
and quotes in the value #1288
* Update dialog for Task Wizard, Advanced Task Wizard and Modify Task Wizard #1287
* Add errordialog to fix missing error messages in trashcan #1286
* Display current result, comparable result and diff between results for delta
Expand Down
146 changes: 145 additions & 1 deletion gsa/src/gmp/models/__tests__/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,50 @@ describe('Filter parse from keywords', () => {
const filter2 = Filter.fromString(filterstring);
expect(filter.equals(filter2)).toEqual(true);
});

test('should parse keywords with xml encoded values', () => {
const elem = {
keywords: {
keyword: [
{
column: 'foo',
relation: '=',
value: '"bar"',
},
{
column: 'bar',
relation: '=',
value: ''foo'',
},
],
},
};

const filter = new Filter(elem);
expect(filter.toFilterString()).toEqual('foo="bar" bar=\'foo\'');
});

test('should parse keywords with xml encoded relations', () => {
const elem = {
keywords: {
keyword: [
{
column: 'foo',
relation: '<',
value: 'bar',
},
{
column: 'bar',
relation: '>',
value: 'foo',
},
],
},
};

const filter = new Filter(elem);
expect(filter.toFilterString()).toEqual('foo<bar bar>foo');
});
});

describe('Filter set', () => {
Expand Down Expand Up @@ -291,6 +335,15 @@ describe('Filter set', () => {
filter.set('first', '-666');
expect(filter.get('first')).toEqual(1);
});

test('should reset filter id', () => {
const filter = new Filter({_id: 'foo'});

expect(filter.id).toEqual('foo');

filter.set('first', '0');
expect(filter.id).toBeUndefined();
});
});

describe('Filter has', () => {
Expand Down Expand Up @@ -333,6 +386,14 @@ describe('Filter delete', () => {
expect(filter.delete('def').toFilterString()).toEqual('abc=1 ~def');
expect(filter.delete('~def').toFilterString()).toEqual('abc=1 ~def');
});

test('should reset filter id', () => {
const filter = Filter.fromString('abc=1');
filter.id = 'foo';
filter.delete('abc');

expect(filter.id).toBeUndefined();
});
});

describe('Filter equal', () => {
Expand Down Expand Up @@ -660,8 +721,17 @@ describe('Filter next', () => {
expect(filter.get('first')).toBe(11);
expect(filter.get('rows')).toBe(10);
});
});

test('should reset filter id', () => {
let filter = Filter.fromString('first=1 rows=10');
filter.id = 'foo';

expect(filter.id).toEqual('foo');

filter = filter.next();
expect(filter.id).toBeUndefined();
});
});
describe('Filter first', () => {
test('should set first if undefined', () => {
let filter = Filter.fromString('');
Expand All @@ -681,6 +751,16 @@ describe('Filter first', () => {

expect(filter.get('first')).toBe(1);
});

test('should reset filter id', () => {
let filter = Filter.fromString('first=1 rows=10');
filter.id = 'foo';

expect(filter.id).toEqual('foo');

filter = filter.first();
expect(filter.id).toBeUndefined();
});
});

describe('Filter previous', () => {
Expand All @@ -706,6 +786,16 @@ describe('Filter previous', () => {
expect(filter.get('first')).toBe(1);
expect(filter.get('rows')).toBe(10);
});

test('should reset filter id', () => {
let filter = Filter.fromString('first=1 rows=10');
filter.id = 'foo';

expect(filter.id).toEqual('foo');

filter = filter.previous();
expect(filter.id).toBeUndefined();
});
});

describe('Filter getSortOrder', () => {
Expand Down Expand Up @@ -766,6 +856,16 @@ describe('Filter setSortOrder', () => {
expect(filter.has('sort-reverse')).toEqual(false);
expect(filter.get('sort')).toEqual('foo');
});

test('should reset filter id', () => {
const filter = Filter.fromString('sort-reverse=foo');
filter.id = 'foo';

expect(filter.id).toEqual('foo');

filter.setSortOrder('foo');
expect(filter.id).toBeUndefined();
});
});

describe('Filter setSortBy', () => {
Expand Down Expand Up @@ -793,6 +893,16 @@ describe('Filter setSortBy', () => {

expect(filter.get('sort-reverse')).toEqual('foo');
});

test('should reset filter id', () => {
const filter = Filter.fromString('sort-reverse=bar');
filter.id = 'foo';

expect(filter.id).toEqual('foo');

filter.setSortBy('foo');
expect(filter.id).toBeUndefined();
});
});

describe('Filter simple', () => {
Expand Down Expand Up @@ -829,6 +939,16 @@ describe('Filter simple', () => {
expect(filter).not.toBe(simple);
expect(simple.has('sort-reverse')).toEqual(false);
});

test('should reset filter id', () => {
const filter = Filter.fromString('sort-reverse=foo foo=bar');
filter.id = 'foo';

expect(filter.id).toEqual('foo');

const simple = filter.simple();
expect(simple.id).toBeUndefined();
});
});

describe('Filter merge extra keywords', () => {
Expand Down Expand Up @@ -878,6 +998,20 @@ describe('Filter merge extra keywords', () => {
expect(filter3.get('apply_overrides')).toBe(1);
expect(filter3.get('min_qod')).toBe(80);
});

test('should reset filter id', () => {
const filter1 = Filter.fromString('abc=1');
filter1.id = 'f1';
const filter2 = Filter.fromString(
'apply_overrides=1 overrides=1 ' +
'autofp=1 delta_states=1 first=1 levels=hml min_qod=70 notes=1 ' +
'result_hosts_only=1 rows=10 sort=name timezone=CET',
);
filter2.id = 'f2';

const filter3 = filter1.mergeExtraKeywords(filter2);
expect(filter3.id).toBeUndefined();
});
});

describe('filter and', () => {
Expand All @@ -900,6 +1034,16 @@ describe('filter and', () => {
'apply_overrides=1 min_qod=70 bar=2',
);
});

test('should reset filter id', () => {
const filter1 = Filter.fromString('foo=1');
filter1.id = 'f1';
const filter2 = Filter.fromString('bar=2');
filter2.id = 'f2';
const filter3 = filter1.and(filter2);

expect(filter3.id).toBeUndefined();
});
});

describe('filter hasTerm', () => {
Expand Down
33 changes: 30 additions & 3 deletions gsa/src/gmp/models/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {forEach, map} from '../utils/array';

import Model from '../model.js';

import {setProperties} from '../parser';

import convert from './filter/convert.js';
import FilterTerm, {AND} from './filter/filterterm.js';
import {EXTRA_KEYWORDS} from './filter/keywords.js';
Expand Down Expand Up @@ -51,6 +53,12 @@ class Filter extends Model {
this.terms = [];
}

setProperties({id, ...properties}) {
// override setProperties to allow changing the id
setProperties(properties, this);
this.id = id;
}

/**
* Parse properties from the passed element object for being set in this
* Filter model.
Expand All @@ -74,7 +82,7 @@ class Filter extends Model {
});
delete ret.keywords;
} else if (isDefined(ret.term)) {
this.parseString(ret.term);
this._parseString(ret.term);

// ret.term should not be part of the public api
// but it's helpful for debug purposes
Expand Down Expand Up @@ -200,6 +208,18 @@ class Filter extends Model {
return this;
}

/**
* Reset filter id of the current filter
*
* @private
*
* @return {Filter} This filter.
*/
_resetFilterId() {
this.id = undefined;
return this;
}

/**
* Calls passed function for each FilterTerm in this Filter
*
Expand Down Expand Up @@ -348,6 +368,7 @@ class Filter extends Model {
* @return {Filter} This filter
*/
set(keyword, value, relation = '=') {
this._resetFilterId(); // reset id because the filter has changed
const converted = convert(keyword, value, relation);
this._setTerm(new FilterTerm(converted));
return this;
Expand Down Expand Up @@ -380,6 +401,7 @@ class Filter extends Model {
const index = this._getIndex(key);
if (index !== -1) {
this.terms.splice(index, 1);
this._resetFilterId(); // filter has changed
}
return this;
}
Expand Down Expand Up @@ -569,6 +591,8 @@ class Filter extends Model {
if (nonExtraTerms.length > 0) {
this._addTerm(AND);
}

this._resetFilterId(); // filter has changed
return this._merge(filter);
}

Expand Down Expand Up @@ -631,17 +655,20 @@ class Filter extends Model {
*/
mergeExtraKeywords(filter) {
const f = this.copy();
f._resetFilterId();
return f._mergeExtraKeywords(filter);
}

/**
* Parses FilterTerms from filterstring and adds them to this Filter
*
* @private
*
* @param {String} filterstring Filter representation as a string
*
* @return {Filter} This filter.
*/
parseString(filterstring) {
_parseString(filterstring) {
if (isString(filterstring)) {
const fterms = filterstring.split(' ');
for (let fterm of fterms) {
Expand All @@ -667,7 +694,7 @@ class Filter extends Model {
static fromString(filterstring, filter) {
const f = new Filter();

f.parseString(filterstring);
f._parseString(filterstring);
f._mergeExtraKeywords(filter);

return f;
Expand Down
34 changes: 34 additions & 0 deletions gsa/src/gmp/models/filter/__tests__/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,40 @@ describe('convert tests', () => {
value: 'regexp',
});
});

test('should convert xml encoded values', () => {
expect(convert('foo', '&amp;bar&amp;', '=')).toEqual({
keyword: 'foo',
value: '&bar&',
relation: '=',
});

expect(convert('foo', '&quot;bar&quot;', '=')).toEqual({
keyword: 'foo',
value: '"bar"',
relation: '=',
});

expect(convert('foo', '&apos;bar&apos;', '=')).toEqual({
keyword: 'foo',
value: "'bar'",
relation: '=',
});
});

test('should convert xml encoded relations', () => {
expect(convert('foo', 'bar', '&gt;')).toEqual({
keyword: 'foo',
value: 'bar',
relation: '>',
});

expect(convert('foo', 'bar', '&lt;')).toEqual({
keyword: 'foo',
value: 'bar',
relation: '<',
});
});
});

// vim: set ts=2 sw=2 tw=80:
Loading