Skip to content

Commit

Permalink
Merge pull request #1205 from swaterkamp/GetTags
Browse files Browse the repository at this point in the history
Fix getting a tag
  • Loading branch information
bjoernricks authored Mar 8, 2019
2 parents 3d9b630 + b3be980 commit 2b2f043
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 4 deletions.
163 changes: 163 additions & 0 deletions gsa/src/gmp/commands/__tests__/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* Copyright (C) 2019 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* 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 {TagCommand} from '../tags';

import {
createActionResultResponse,
createEntityResponse,
createHttp,
} from '../testing';

describe('TagCommand tests', () => {
test('should create new tag with resources', () => {
const response = createActionResultResponse();
const fakeHttp = createHttp(response);

expect.hasAssertions();

const cmd = new TagCommand(fakeHttp);
return cmd
.create({
name: 'name',
comment: 'comment',
active: '1',
resource_ids: ['id1', 'id2'],
resource_type: 'type',
resources_action: 'action',
})
.then(resp => {
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
data: {
cmd: 'create_tag',
tag_name: 'name',
comment: 'comment',
active: '1',
'resource_ids:': ['id1', 'id2'],
resource_type: 'type',
resources_action: 'action',
tag_value: '',
},
});

const {data} = resp;
expect(data.id).toEqual('foo');
});
});

test('should return single tag', () => {
const response = createEntityResponse('tag', {_id: 'foo'});
const fakeHttp = createHttp(response);

expect.hasAssertions();

const cmd = new TagCommand(fakeHttp);
return cmd.get({id: 'foo'}).then(resp => {
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
args: {
cmd: 'get_tag',
tag_id: 'foo',
},
});

const {data} = resp;
expect(data.id).toEqual('foo');
});
});

test('should save a tag', () => {
const response = createActionResultResponse();
const fakeHttp = createHttp(response);

expect.hasAssertions();

const cmd = new TagCommand(fakeHttp);
return cmd
.save({
id: 'foo',
name: 'bar',
comment: 'ipsum',
active: '1',
filter: 'fil',
resource_ids: ['id1'],
resource_type: 'type',
resources_action: 'action',
value: 'lorem',
})
.then(() => {
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
data: {
cmd: 'save_tag',
tag_id: 'foo',
tag_name: 'bar',
comment: 'ipsum',
active: '1',
filter: 'fil',
'resource_ids:': ['id1'],
resource_type: 'type',
resources_action: 'action',
tag_value: 'lorem',
},
});
});
});

test('should enable a tag', () => {
const response = createActionResultResponse();
const fakeHttp = createHttp(response);

expect.hasAssertions();

const cmd = new TagCommand(fakeHttp);
return cmd
.enable({
id: 'foo',
})
.then(() => {
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
data: {
cmd: 'toggle_tag',
tag_id: 'foo',
enable: '1',
},
});
});
});

test('should disable a tag', () => {
const response = createActionResultResponse();
const fakeHttp = createHttp(response);

expect.hasAssertions();

const cmd = new TagCommand(fakeHttp);
return cmd
.disable({
id: 'foo',
})
.then(() => {
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
data: {
cmd: 'toggle_tag',
tag_id: 'foo',
enable: '0',
},
});
});
});
});
7 changes: 3 additions & 4 deletions gsa/src/gmp/commands/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ import EntityCommand from './entity';

const log = logger.getLogger('gmp.commands.tags');

class TagCommand extends EntityCommand {
export class TagCommand extends EntityCommand {
constructor(http) {
super(http, 'tag', Tag);
}

getElementFromRoot(root) {
// response does contain two get_tags_response elements
return root.get_tag.get_tags_response[0].tag;
return root.get_tag.get_tags_response.tag;
}

create({
Expand Down Expand Up @@ -112,7 +111,7 @@ class TagCommand extends EntityCommand {
}
}

class TagsCommand extends EntitiesCommand {
export class TagsCommand extends EntitiesCommand {
constructor(http) {
super(http, 'tag', Tag);
}
Expand Down

0 comments on commit 2b2f043

Please sign in to comment.