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

SendTargetTestCase.test_error_and_exit()   A

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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