Passed
Pull Request — master (#314)
by Jaspar
01:33
created

SendTargetTestCase.setUp()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
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
import unittest
20
from unittest.mock import patch
21
import importlib
22
from pathlib import Path
23
from argparse import Namespace
24
from lxml import etree
25
from . import GmpMockFactory
26
27
28
CWD = Path(__file__).absolute().parent
29
30
31
class SendTargetTestCase(unittest.TestCase):
32
    def setUp(self):
33
        self.send_targets = importlib.import_module(
34
            'scripts.send-targets', 'gvmtools'
35
        )
36
37
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
38
    def test_sent_target(self, mock_gmp: GmpMockFactory):
39
        target_xml_path = CWD / 'example_target.xml'
40
        target_xml_str = target_xml_path.read_text()
41
42
        mock_gmp.mock_response(
43
            'get_credentials',
44
            """
45
<get_credentials_response status="200" status_text="OK">
46
    <credential id="6da5b7de-92ad-4dd2-8610-d5711b9c5937">
47
    </credential>
48
    <credential id="7802648d-1a31-4f69-bb30-00766a1ae1e6">
49
    </credential>
50
    <credential id="70a63257-4923-4bf4-a9bb-dd8b710b2d80">
51
    </credential>
52
    <credential id="2bac0c76-795e-4742-b17a-808a0ec8e409">
53
    </credential>
54
</get_credentials_response>
55
        """,
56
        )
57
        mock_gmp.mock_response(
58
            'create_target',
59
            '<create_target_response status="201" status_text="OK,'
60
            'resource created" id="6c9f73f5-f14c-42bf-ab44-edb8d2493dbc"/>',
61
        )
62
63
        target = etree.XML(target_xml_str)
64
65
        self.send_targets.parse_send_xml_tree(mock_gmp.gmp_protocol, target)
66
67
    @patch('builtins.input', lambda *args: 'n')
68
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
69
    def test_sent_target_no_credential(self, mock_gmp: GmpMockFactory):
70
        target_xml_path = CWD / 'example_target.xml'
71
        target_xml_str = target_xml_path.read_text()
72
73
        mock_gmp.mock_response(
74
            'get_credentials',
75
            '<get_credentials_response status="200" status_text="OK">'
76
            '<credential id="70a63257-4923-4bf4-a9bb-dd8b710b2d80">'
77
            '</credential>'
78
            '<credential id="2bac0c76-795e-4742-b17a-808a0ec8e409">'
79
            '</credential>'
80
            '</get_credentials_response>',
81
        )
82
        mock_gmp.mock_response(
83
            'create_target',
84
            '<create_target_response status="201" status_text="OK,'
85
            'resource created" id="6c9f73f5-f14c-42bf-ab44-edb8d2493dbc"/>',
86
        )
87
88
        target = etree.XML(target_xml_str)
89
90
        with self.assertRaises(SystemExit):
91
            self.send_targets.parse_send_xml_tree(mock_gmp.gmp_protocol, target)
92
93
    def test_args(self):
94
        args = Namespace(script=['foo'])
95
        with self.assertRaises(SystemExit):
96
            self.send_targets.check_args(args)
97
98
        args2 = Namespace(script=['foo', 'bar', 'baz'])
99
100
        with self.assertRaises(SystemExit):
101
            self.send_targets.check_args(args2)
102
103
    @patch('builtins.input', lambda *args: 'y')
104
    def test_yes(self):
105
        yes = self.send_targets.yes_or_no('foo?')
106
        self.assertTrue(yes)
107
108
    @patch('builtins.input', lambda *args: 'n')
109
    def test_no(self):
110
        no = self.send_targets.yes_or_no('bar?')
111
        self.assertFalse(no)
112
113
    def test_error_and_exit(self):
114
        with self.assertRaises(SystemExit):
115
            self.send_targets.error_and_exit('foo')
116
117
    def test_create_xml_tree(self):
118
        target_xml_path = CWD / 'example_target.xml'
119
120
        tree = self.send_targets.create_xml_tree(str(target_xml_path))
121
        self.assertIsInstance(
122
            tree, etree._Element  # pylint: disable=protected-access
123
        )
124
        self.assertEqual(tree.tag, 'get_targets_response')
125
126
    def test_create_xml_tree_invalid_file(self):
127
        target_xml_path = CWD / 'invalid_file.xml'
128
129
        with self.assertRaises(SystemExit):
130
            with self.assertRaises(OSError):
131
                self.send_targets.create_xml_tree(str(target_xml_path))
132
133
    def test_create_xml_tree_invalid_xml(self):
134
        target_xml_path = CWD / 'invalid_xml.xml'
135
136
        with self.assertRaises(SystemExit):
137
            with self.assertRaises(etree.Error):
138
                self.send_targets.create_xml_tree(str(target_xml_path))
139