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

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