Completed
Push — master ( 48663a...f3cde9 )
by
unknown
31s queued 11s
created

tests.protocols.gmpv7.test_create_user   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 162
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpCreateUserTestCase.test_create_user() 0 7 1
A GmpCreateUserTestCase.test_create_user_with_password() 0 8 1
A GmpCreateUserTestCase.test_create_user_with_ifaces() 0 34 1
A GmpCreateUserTestCase.test_create_user_missing_name() 0 9 3
A GmpCreateUserTestCase.setUp() 0 3 1
A GmpCreateUserTestCase.test_create_user_with_hosts() 0 34 1
A GmpCreateUserTestCase.test_create_user_with_role_ids() 0 8 1
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
22
from gvm.protocols.gmpv7 import Gmp
23
24
from .. import MockConnection
25
26
class GmpCreateUserTestCase(unittest.TestCase):
27
28
    def setUp(self):
29
        self.connection = MockConnection()
30
        self.gmp = Gmp(self.connection)
31
32
    def test_create_user_missing_name(self):
33
        with self.assertRaises(RequiredArgument):
34
            self.gmp.create_user(
35
                name=None,
36
            )
37
38
        with self.assertRaises(RequiredArgument):
39
            self.gmp.create_user(
40
                name='',
41
            )
42
43
    def test_create_user(self):
44
        self.gmp.create_user(
45
            name='foo',
46
        )
47
48
        self.connection.send.has_been_called_with(
49
            '<create_user>'
50
            '<name>foo</name>'
51
            '</create_user>'
52
        )
53
54
    def test_create_user_with_password(self):
55
        self.gmp.create_user(
56
            name='foo',
57
            password='bar',
58
        )
59
60
        self.connection.send.has_been_called_with(
61
            '<create_user>'
62
            '<name>foo</name>'
63
            '<password>bar</password>'
64
            '</create_user>'
65
        )
66
67
    def test_create_user_with_hosts(self):
68
        self.gmp.create_user(
69
            name='foo',
70
            hosts=['h1', 'h2'],
71
            hosts_allow=True,
72
        )
73
74
        self.connection.send.has_been_called_with(
75
            '<create_user>'
76
            '<name>foo</name>'
77
            '<hosts allow="1">h1,h2</hosts>'
78
            '</create_user>'
79
        )
80
81
        self.gmp.create_user(
82
            name='foo',
83
            hosts=['h1', 'h2'],
84
        )
85
86
        self.connection.send.has_been_called_with(
87
            '<create_user>'
88
            '<name>foo</name>'
89
            '<hosts allow="0">h1,h2</hosts>'
90
            '</create_user>'
91
        )
92
93
        self.gmp.create_user(
94
            name='foo',
95
            hosts=['h1', 'h2'],
96
            hosts_allow=False,
97
        )
98
99
        self.connection.send.has_been_called_with(
100
            '<create_user>'
101
            '<name>foo</name>'
102
            '<hosts allow="0">h1,h2</hosts>'
103
            '</create_user>'
104
        )
105
106
    def test_create_user_with_ifaces(self):
107
        self.gmp.create_user(
108
            name='foo',
109
            ifaces=['h1', 'h2'],
110
            ifaces_allow=True,
111
        )
112
113
        self.connection.send.has_been_called_with(
114
            '<create_user>'
115
            '<name>foo</name>'
116
            '<ifaces allow="1">h1,h2</ifaces>'
117
            '</create_user>'
118
        )
119
120
        self.gmp.create_user(
121
            name='foo',
122
            ifaces=['h1', 'h2'],
123
        )
124
125
        self.connection.send.has_been_called_with(
126
            '<create_user>'
127
            '<name>foo</name>'
128
            '<ifaces allow="0">h1,h2</ifaces>'
129
            '</create_user>'
130
        )
131
132
        self.gmp.create_user(
133
            name='foo',
134
            ifaces=['h1', 'h2'],
135
            ifaces_allow=False,
136
        )
137
138
        self.connection.send.has_been_called_with(
139
            '<create_user>'
140
            '<name>foo</name>'
141
            '<ifaces allow="0">h1,h2</ifaces>'
142
            '</create_user>'
143
        )
144
145
    def test_create_user_with_role_ids(self):
146
        self.gmp.create_user(
147
            name='foo',
148
            role_ids=['r1', 'r2'],
149
        )
150
151
        self.connection.send.has_been_called_with(
152
            '<create_user>'
153
            '<name>foo</name>'
154
            '<role id="r1"/>'
155
            '<role id="r2"/>'
156
            '</create_user>'
157
        )
158
159
160
if __name__ == '__main__':
161
    unittest.main()
162