Passed
Pull Request — master (#344)
by Jaspar
01:48
created

SendTasksTestCase.test_args()   A

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2020 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see<http://www.gnu.org/licenses/>
18
19
20
import unittest
21
from unittest.mock import patch, MagicMock
22
from pathlib import Path
23
from argparse import Namespace
24
from lxml import etree
25
from . import GmpMockFactory, load_script
26
27
CWD = Path(__file__).absolute().parent
28
29
30
class SendTasksTestCase(unittest.TestCase):
31
    def setUp(self):
32
        self.send_tasks = load_script(
33
            (CWD.parent.parent / 'scripts'), 'send-tasks'
34
        )
35
36
    @patch('builtins.input', lambda *args: 'y')
37
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
38
    def test_sent_task(self, mock_gmp: GmpMockFactory):
39
        task_xml_path = CWD / 'example_task.xml'
40
        task_xml_str = task_xml_path.read_text()
41
42
        self.send_tasks.numerical_option = MagicMock(return_value=1)
43
44
        configs_file = CWD / 'get_configs.xml'
45
        configs = configs_file.read_text()
46
        mock_gmp.mock_response('get_configs', configs)
47
48
        mock_gmp.mock_response(
49
            'get_scanners',
50
            '<get_scanners_response status="200" status_text="OK">'
51
            '<scanner id="c1c85af7-0cca-4690-8ccc-c79feb5588cf">'
52
            '<name>as</name>'
53
            '</scanner>'
54
            '<scanner id="6acd0832-df90-11e4-b9d5-28d24461215b">'
55
            '<name>CVE</name>'
56
            '</scanner>'
57
            '<scanner id="08b69003-5fc2-4037-a479-93b440211c73">'
58
            '<name>OpenVAS Default</name>'
59
            '</scanner>'
60
            '</get_scanners_response>',
61
        )
62
63
        mock_gmp.mock_response(
64
            'get_targets',
65
            '<get_targets_response status="200" status_text="OK">'
66
            '<target id="60f95d0e-029e-4931-a13a-b1d11260517d">'
67
            '<name>own</name>'
68
            '</target>'
69
            '<target id="ead9576c-5a4d-4081-b98d-ccd77d5d16f8">'
70
            '<name>Target for xn</name>'
71
            '</target>'
72
            '<target id="6c9f73f5-f14c-42bf-ab44-edb8d2493dbc">'
73
            '<name>Unnamed</name>'
74
            '</target>'
75
            '<target id="a1f478c1-27d0-4d8c-959f-150625186421">'
76
            '<name>work</name>'
77
            '</target>'
78
            '<target id="5ca97fe1-694d-4e4a-bd4c-55529719d17e">'
79
            '<name>work2</name>'
80
            '</target>'
81
            '</get_targets_response>',
82
        )
83
84
        mock_gmp.mock_response(
85
            'create_task',
86
            '<create_task_response status="201" status_text="OK,'
87
            'resource created" id="c8ef0597-e2c1-4e23-869f-072fa2914bf2"/>',
88
        )
89
90
        task = etree.XML(task_xml_str)
91
92
        tasks = self.send_tasks.parse_send_xml_tree(mock_gmp.gmp_protocol, task)
93
        self.assertEqual(tasks, ['c8ef0597-e2c1-4e23-869f-072fa2914bf2'])
94
95
    def test_args(self):
96
        args = Namespace(script=['foo'])
97
        with self.assertRaises(SystemExit):
98
            self.send_tasks.check_args(args)
99
100
        args2 = Namespace(script=['foo', 'bar', 'baz'])
101
102
        with self.assertRaises(SystemExit):
103
            self.send_tasks.check_args(args2)
104