Completed
Push — master ( 4ac98b...5a81ff )
by Juan José
12s
created

GmpCreateReportCommandTestCase.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) 2018 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
21
from gvm.errors import RequiredArgument, InvalidArgument
22
from gvm.protocols.gmpv7 import Gmp
23
24
from .. import MockConnection
25
26
27
class GmpImportReportTestCase(unittest.TestCase):
28
29
    TASK_ID = '00000000-0000-0000-0000-000000000001'
30
    TASK_NAME = 'unit test task'
31
    COMMENT = 'This is a comment'
32
    REPORT_XML_STRING = (
33
        '<report id="67a62fb7-b238-4f0e-bc48-59bde8939cdc">'
34
        '<results max="1" start="1">'
35
        '<result id="f180b40f-49dd-4856-81ed-8c1195afce80">'
36
        '<severity>0.0</severity>'
37
        '<nvt oid="1.3.6.1.4.1.25623.1.0.10330"/>'
38
        '<host>132.67.253.114</host>'
39
        '</result></results></report>'
40
    )
41
42
    def setUp(self):
43
        self.connection = MockConnection()
44
        self.gmp = Gmp(self.connection)
45
46
    def test_import_report_with_task_id(self):
47
        self.gmp.import_report(
48
            self.REPORT_XML_STRING,
49
            task_id=self.TASK_ID,
50
        )
51
52
        self.connection.send.has_been_called_with(
53
            '<create_report>'
54
            '<task id="{task}"/>'
55
            '{report}'
56
            '</create_report>'.format(
57
                task=self.TASK_ID, report=self.REPORT_XML_STRING)
58
        )
59
60
    def test_import_report_with_task_name(self):
61
        self.gmp.import_report(
62
            self.REPORT_XML_STRING,
63
            task_name=self.TASK_NAME,
64
            task_comment=self.COMMENT,
65
        )
66
67
        self.connection.send.has_been_called_with(
68
            '<create_report>'
69
            '<task>'
70
            '<name>{task}</name>'
71
            '<comment>{comment}</comment>'
72
            '</task>'
73
            '{report}'
74
            '</create_report>'.format(
75
                task=self.TASK_NAME, comment=self.COMMENT,
76
                report=self.REPORT_XML_STRING)
77
        )
78
79
    def test_import_report_missing_report(self):
80
        with self.assertRaises(RequiredArgument):
81
            self.gmp.import_report(
82
                None,
83
                task_name=self.TASK_NAME,
84
                task_comment=self.COMMENT,
85
            )
86
87
        with self.assertRaises(RequiredArgument):
88
            self.gmp.import_report(
89
                '',
90
                task_name=self.TASK_NAME,
91
                task_comment=self.COMMENT,
92
            )
93
94
    def test_import_report_missing_task(self):
95
        with self.assertRaises(RequiredArgument):
96
            self.gmp.import_report(
97
                self.REPORT_XML_STRING,
98
            )
99
100
    def test_import_report_invalid_xml(self):
101
        with self.assertRaises(InvalidArgument):
102
            self.gmp.import_report(
103
                'Foo', # not root tag
104
                task_name=self.TASK_NAME,
105
                task_comment=self.COMMENT,
106
            )
107
108
        with self.assertRaises(InvalidArgument):
109
            self.gmp.import_report(
110
                '<Foo>', # missing closing tag
111
                task_name=self.TASK_NAME,
112
                task_comment=self.COMMENT,
113
            )
114
115
    def test_import_report_with_in_assets(self):
116
        self.gmp.import_report(
117
            self.REPORT_XML_STRING,
118
            task_name=self.TASK_NAME,
119
            in_assets=False,
120
        )
121
122
        self.connection.send.has_been_called_with(
123
            '<create_report>'
124
            '<task>'
125
            '<name>{task}</name>'
126
            '</task>'
127
            '<in_assets>0</in_assets>'
128
            '{report}'
129
            '</create_report>'.format(
130
                task=self.TASK_NAME,
131
                report=self.REPORT_XML_STRING)
132
        )
133
134
        self.gmp.import_report(
135
            self.REPORT_XML_STRING,
136
            task_name=self.TASK_NAME,
137
            in_assets=True,
138
        )
139
140
        self.connection.send.has_been_called_with(
141
            '<create_report>'
142
            '<task>'
143
            '<name>{task}</name>'
144
            '</task>'
145
            '<in_assets>1</in_assets>'
146
            '{report}'
147
            '</create_report>'.format(
148
                task=self.TASK_NAME,
149
                report=self.REPORT_XML_STRING)
150
        )
151
152
153
if __name__ == '__main__':
154
    unittest.main()
155