File: test_modify_user.py

package info (click to toggle)
python-gvm 26.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,132 kB
  • sloc: python: 44,662; makefile: 18
file content (152 lines) | stat: -rw-r--r-- 4,535 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# SPDX-FileCopyrightText: 2018-2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

from gvm.errors import RequiredArgument
from gvm.protocols.gmp.requests.v224 import UserAuthType


class GmpModifyUserTestMixin:
    def test_modify_user(self):
        self.gmp.modify_user(user_id="u1")

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1"/>'
        )

    def test_modify_user_missing_user_id(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.modify_user(user_id=None)

        with self.assertRaises(RequiredArgument):
            self.gmp.modify_user(user_id="")

    def test_modify_user_with_new_name(self):
        self.gmp.modify_user(user_id="u1", name="foo")

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b"<new_name>foo</new_name>"
            b"</modify_user>"
        )

    def test_modify_user_with_new_comment(self):
        self.gmp.modify_user(user_id="u1", comment="foo")

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b"<comment>foo</comment>"
            b"</modify_user>"
        )

    def test_modify_user_with_role_ids(self):
        self.gmp.modify_user(user_id="u1", role_ids=[])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1"/>'
        )

        self.gmp.modify_user(user_id="u1", role_ids=["r1"])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1"><role id="r1"/></modify_user>'
        )

        self.gmp.modify_user(user_id="u1", role_ids=["r1", "r2"])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b'<role id="r1"/>'
            b'<role id="r2"/>'
            b"</modify_user>"
        )

    def test_modify_user_with_group_ids(self):
        self.gmp.modify_user(user_id="u1", role_ids=[])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1"/>'
        )

        self.gmp.modify_user(user_id="u1", group_ids=["r1"])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b'<groups><group id="r1"/></groups>'
            b"</modify_user>"
        )

        self.gmp.modify_user(user_id="u1", group_ids=["r1", "r2"])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b"<groups>"
            b'<group id="r1"/>'
            b'<group id="r2"/>'
            b"</groups>"
            b"</modify_user>"
        )

    def test_modify_user_with_password(self):
        self.gmp.modify_user(user_id="u1", password="foo")

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b"<password>foo</password>"
            b"</modify_user>"
        )

    def test_modify_user_with_auth_source(self):
        self.gmp.modify_user(
            user_id="u1", auth_source=UserAuthType.LDAP_CONNECT
        )

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b"<sources><source>ldap_connect</source></sources>"
            b"</modify_user>"
        )

    def test_modify_user_with_hosts(self):
        self.gmp.modify_user(user_id="u1", hosts=[])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1"/>'
        )

        self.gmp.modify_user(user_id="u1", hosts=["foo"])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b'<hosts allow="0">foo</hosts>'
            b"</modify_user>"
        )

        self.gmp.modify_user(user_id="u1", hosts=["foo", "bar"])

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b'<hosts allow="0">foo,bar</hosts>'
            b"</modify_user>"
        )

        self.gmp.modify_user(
            user_id="u1", hosts=["foo", "bar"], hosts_allow=False
        )

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b'<hosts allow="0">foo,bar</hosts>'
            b"</modify_user>"
        )

        self.gmp.modify_user(
            user_id="u1", hosts=["foo", "bar"], hosts_allow=True
        )

        self.connection.send.has_been_called_with(
            b'<modify_user user_id="u1">'
            b'<hosts allow="1">foo,bar</hosts>'
            b"</modify_user>"
        )