Skip to content

Commit

Permalink
Merge pull request #1034 from swaterkamp/Tests
Browse files Browse the repository at this point in the history
More model tests #4
  • Loading branch information
swaterkamp authored Oct 24, 2018
2 parents 86dbd36 + d81fe15 commit cda1aab
Show file tree
Hide file tree
Showing 4 changed files with 527 additions and 1 deletion.
253 changes: 253 additions & 0 deletions gsa/src/gmp/models/__tests__/host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/* Greenbone Security Assistant
*
* Authors:
* Steffen Waterkamp <[email protected]>
*
* Copyright:
* Copyright (C) 2018 Greenbone Networks GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import Asset from 'gmp/models/asset';
import Host from 'gmp/models/host';
import {testModel} from 'gmp/models/testing';

import {parseDate} from 'gmp/parser';

/* eslint-disable max-len */

testModel(Host, 'host');

describe('Host model tests', () => {

test('should be instance of Asset', () => {
const host = new Host({});

expect(host).toBeInstanceOf(Asset);
});

test('should parse severity', () => {
const elem = {
host: {
severity: {
value: '8.5',
},
},
};
const elem2 = {
host: {
severity: {
value: '10',
},
},
};
const host = new Host(elem);
const host2 = new Host(elem2);

expect(host.severity).toEqual(8.5);
expect(host2.severity).toEqual(10);
});

test('should parse Identifiers and always return an array', () => {
const elem = {
identifiers: {
identifier: [
{
_id: '123abc',
name: 'foo',
value: 'bar:/3',
creation_time: '2018-10-10T13:31:00+01:00',
modification_time: '2018-10-10T13:32:00+01:00',
source: {
_id: '42xy',
type: 'teip',
data: 'ipsum',
deleted: '0',
},
os: {
title: 'teitl',
},
},
],
},
};
const host = new Host(elem);
const host2 = new Host({});
const res = [
{
creationTime: parseDate('2018-10-10T13:31:00+01:00'),
id: '123abc',
modificationTime: parseDate('2018-10-10T13:32:00+01:00'),
name: 'foo',
os: {title: 'teitl'},
source: {
id: '42xy',
source_type: 'teip',
data: 'ipsum',
deleted: '0',
},
value: 'bar:/3',
},
];
expect(host.identifiers).toEqual(res);
expect(host2.identifiers).toEqual([]);
});

test('should return hostname identifier, set alternative or undefined', () => {
const elem = {
identifiers: {
identifier: [
{
name: 'hostname',
value: 'foo',
},
],
},
};
const elem2 = {
identifiers: {
identifier: [
{
name: 'DNS-via-TargetDefinition',
value: 'bar',
},
],
},
};
const elem3 = {
identifiers: {
identifier: [
{
name: 'notavailable',
value: 'no',
},
],
},
};
const host = new Host(elem);
const host2 = new Host(elem2);
const host3 = new Host(elem3);

expect(host.hostname).toEqual('foo');
expect(host2.hostname).toEqual('bar');
expect(host3.hostname).toBeUndefined();
});

test('should return ip identifier', () => {
const elem = {
identifiers: {
identifier: [
{
name: 'ip',
value: '123.456.789.42',
},
],
},
};
const host = new Host(elem);

expect(host.ip).toEqual('123.456.789.42');
});

test('should parse details', () => {
const elem = {
host: {
detail: [
{
name: 'foo',
value: 'c:/.2',
source: {
_id: '123abc',
},
},
{
name: 'bar',
value: 'c:/.3',
source: {
_id: '42xy',
},
},
],
},
};
const res = {
foo: {
source: {
id: '123abc',
},
value: 'c:/.2',
},
bar: {
source: {
id: '42xy',
},
value: 'c:/.3',
},
};
const host = new Host(elem);

expect(host.details).toEqual(res);
});

test('should parse routes', () => {
const elem = {
host: {
routes: {
route: [
{
host: [
{
_id: '123abc',
_distance: '0',
_same_source: '1',
ip: '123.456.789.42',
},
],
},
],
},
},
};
const res = [
[
{
ip: '123.456.789.42',
id: '123abc',
distance: 0,
same_source: 1,
},
],
];
const host = new Host(elem);

expect(host.routes).toEqual(res);
});

test('should return empty array if no routes are given', () => {
const host = new Host({});

expect(host.routes).toEqual([]);
});

test('should delete host attribute', () => {
const elem = {
host: {},
};
const host = new Host(elem);

expect(host.host).toBeUndefined();
});
});
129 changes: 129 additions & 0 deletions gsa/src/gmp/models/__tests__/note.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* Greenbone Security Assistant
*
* Authors:
* Steffen Waterkamp <[email protected]>
*
* Copyright:
* Copyright (C) 2018 Greenbone Networks GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import Model from 'gmp/model';
import Note from 'gmp/models/note';
import {testModel} from 'gmp/models/testing';

import {NO_VALUE, YES_VALUE} from 'gmp/parser';

testModel(Note, 'note');

describe('Note model tests', () => {

test('should parse severity', () => {
const note = new Note({severity: '8.5'});
const note2 = new Note({severity: '10'});
const note3 = new Note({});

expect(note.severity).toEqual(8.5);
expect(note2.severity).toEqual(10);
expect(note3.severity).toBeUndefined();
});

test('should parse active as yes/no correctly', () => {
const note1 = new Note({active: '0'});
const note2 = new Note({active: '1'});

expect(note1.active).toEqual(NO_VALUE);
expect(note2.active).toEqual(YES_VALUE);
});

test('should parse text_excerpt as yes/no correctly', () => {
const note1 = new Note({text_excerpt: '0'});
const note2 = new Note({text_excerpt: '1'});

expect(note1.text_excerpt).toEqual(NO_VALUE);
expect(note2.text_excerpt).toEqual(YES_VALUE);
});

test('should parse hosts or return empty array', () => {
const elem = {
hosts: '123.456.789.42, 987.654.321.1',
};
const note1 = new Note(elem);
const note2 = new Note({hosts: ''});

expect(note1.hosts).toEqual(['123.456.789.42', '987.654.321.1']);
expect(note2.hosts).toEqual([]);
});

test('should delete port if it is empty, pass it on otherwise', () => {
const note1 = new Note({port: 'general/tcp'});
const note2 = new Note({port: ''});

expect(note1.port).toEqual('general/tcp');
expect(note2.port).toBeUndefined();
});

test('isExcerpt() should return correct true/false', () => {
const note1 = new Note({text_excerpt: '1'});
const note2 = new Note({text_excerpt: '0'});

expect(note1.isExcerpt()).toEqual(true);
expect(note2.isExcerpt()).toEqual(false);
});

test('should return task if it is a model element', () => {
const elem1 = {
task: {
_id: '123abc',
},
};
const elem2 = {
task: {
_id: '',
},
};
const note1 = new Note(elem1);
const note2 = new Note(elem2);
const note3 = new Note({});

expect(note1.task).toBeInstanceOf(Model);
expect(note2.task).toBeUndefined();
expect(note3.task).toBeUndefined();
});

test('should return result if it is a model element', () => {
const elem1 = {
result: {
_id: '123abc',
},
};
const elem2 = {
result: {
_id: '',
},
};
const note1 = new Note(elem1);
const note2 = new Note(elem2);
const note3 = new Note({});

expect(note1.result).toBeInstanceOf(Model);
expect(note2.result).toBeUndefined();
expect(note3.result).toBeUndefined();
});

});

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

0 comments on commit cda1aab

Please sign in to comment.