summaryrefslogtreecommitdiffstats
path: root/ansible_collections/dellemc/openmanage/tests/unit/plugins/modules/test_idrac_user.py
blob: 0ef6e6da317c0e073bad956e431a1d9ca01b9cc4 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# -*- coding: utf-8 -*-

#
# Dell OpenManage Ansible Modules
# Version 8.2.0
# Copyright (C) 2020-2023 Dell Inc. or its subsidiaries. All Rights Reserved.

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import pytest
import json
from ansible_collections.dellemc.openmanage.plugins.modules import idrac_user
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError
from ansible.module_utils.urls import ConnectionError, SSLValidationError
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule
from mock import MagicMock
from ansible.module_utils._text import to_text
from io import StringIO

MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.'


class TestIDRACUser(FakeAnsibleModule):
    module = idrac_user

    @pytest.fixture
    def idrac_user_mock(self):
        idrac_obj = MagicMock()
        return idrac_obj

    @pytest.fixture
    def idrac_connection_user_mock(self, mocker, idrac_user_mock):
        idrac_conn_mock = mocker.patch(MODULE_PATH + 'idrac_user.iDRACRedfishAPI',
                                       return_value=idrac_user_mock)
        idrac_conn_mock.return_value.__enter__.return_value = idrac_user_mock
        return idrac_conn_mock

    def test_get_payload(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args)
        resp = self.module.get_payload(f_module, 1, action="update")
        assert resp["Users.1.UserName"] == idrac_default_args["new_user_name"]

    def test_get_payload_2(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "custom_privilege": 17, "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args)
        resp = self.module.get_payload(f_module, 1)
        assert resp["Users.1.Privilege"] == idrac_default_args["custom_privilege"]

    def test_convert_payload_xml(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        payload = {"Users.1.UserName": idrac_default_args["user_name"],
                   "Users.1.Password": idrac_default_args["user_password"],
                   "Users.1.Enable": idrac_default_args["enable"],
                   "Users.1.Privilege": idrac_default_args["privilege"],
                   "Users.1.IpmiLanPrivilege": idrac_default_args["ipmi_lan_privilege"],
                   "Users.1.IpmiSerialPrivilege": idrac_default_args["ipmi_serial_privilege"],
                   "Users.1.SolEnable": idrac_default_args["sol_enable"],
                   "Users.1.ProtocolEnable": idrac_default_args["protocol_enable"],
                   "Users.1.AuthenticationProtocol": idrac_default_args["authentication_protocol"],
                   "Users.1.PrivacyProtocol": idrac_default_args["privacy_protocol"]}
        xml_payload, json_payload = self.module.convert_payload_xml(payload)
        assert json_payload["Users.1#SolEnable"] is True

    def test_remove_user_account_check_mode_1(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "absent", "user_name": "user_name", "new_user_name": None,
                                   "user_password": None, "privilege": None, "ipmi_lan_privilege": None,
                                   "ipmi_serial_privilege": None, "enable": False, "sol_enable": False,
                                   "protocol_enable": False, "authentication_protocol": "SHA",
                                   "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=True)
        slot_id = 1
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        with pytest.raises(Exception) as exc:
            self.module.remove_user_account(f_module, idrac_connection_user_mock, slot_uri, slot_id)
        assert exc.value.args[0] == "Changes found to commit!"

    def test_remove_user_account_check_mode_2(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "absent", "user_name": "user_name", "new_user_name": None,
                                   "user_password": None, "privilege": None, "ipmi_lan_privilege": None,
                                   "ipmi_serial_privilege": None, "enable": False, "sol_enable": False,
                                   "protocol_enable": False, "authentication_protocol": "SHA",
                                   "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=True)
        with pytest.raises(Exception) as exc:
            self.module.remove_user_account(f_module, idrac_connection_user_mock, None, None)
        assert exc.value.args[0] == "No changes found to commit!"

    def test_remove_user_account_check_mode_3(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "absent", "user_name": "user_name", "new_user_name": None,
                                   "user_password": None, "privilege": None, "ipmi_lan_privilege": None,
                                   "ipmi_serial_privilege": None, "enable": False, "sol_enable": False,
                                   "protocol_enable": False, "authentication_protocol": "SHA",
                                   "privacy_protocol": "AES"})
        idrac_connection_user_mock.remove_user_account.return_value = {"success": True}
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        slot_id = 1
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        mocker.patch(MODULE_PATH + 'idrac_user.time.sleep', return_value=None)
        self.module.remove_user_account(f_module, idrac_connection_user_mock, slot_uri, slot_id)

    def test_remove_user_account_check_mode_4(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "absent", "user_name": "user_name", "new_user_name": None,
                                   "user_password": None, "privilege": None, "ipmi_lan_privilege": None,
                                   "ipmi_serial_privilege": None, "enable": False, "sol_enable": False,
                                   "protocol_enable": False, "authentication_protocol": "SHA",
                                   "privacy_protocol": "AES"})
        idrac_connection_user_mock.remove_user_account.return_value = {"success": True}
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        with pytest.raises(Exception) as exc:
            self.module.remove_user_account(f_module, idrac_connection_user_mock, None, None)
        assert exc.value.args[0] == 'The user account is absent.'

    def test_get_user_account_1(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.export_scp",
                     return_value=MagicMock())
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.get_idrac_local_account_attr",
                     return_value={"Users.2#UserName": "test_user", "Users.3#UserName": ""})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        response = self.module.get_user_account(f_module, idrac_connection_user_mock)
        assert response[0]["Users.2#UserName"] == "test_user"
        assert response[3] == 3
        assert response[4] == "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/3"

    def test_get_user_account_2(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.export_scp",
                     return_value=MagicMock())
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.get_idrac_local_account_attr",
                     return_value={"Users.2#UserName": "test_user", "Users.3#UserName": "test"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        response = self.module.get_user_account(f_module, idrac_connection_user_mock)
        assert response[2] == 3
        assert response[1] == "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/3"

    def test_get_user_account_invalid_name(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        with pytest.raises(Exception) as err:
            self.module.get_user_account(f_module, idrac_connection_user_mock)
        assert err.value.args[0] == "User name is not valid."

    def test_create_or_modify_account_1(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        idrac_connection_user_mock.get_server_generation = (13, "2.70.70.70")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.import_scp",
                     return_value={"Message": "Successfully created a request."})
        empty_slot_id = 2
        empty_slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(empty_slot_id)
        user_attr = {"User.2#UserName": "test_user"}
        mocker.patch(MODULE_PATH + 'idrac_user.time.sleep', return_value=None)
        response = self.module.create_or_modify_account(f_module, idrac_connection_user_mock, None, None,
                                                        empty_slot_id, empty_slot_uri, user_attr)
        assert response[1] == "Successfully created user account."

    def test_create_or_modify_account_2(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        idrac_connection_user_mock.get_server_generation = (13, "2.70.70.70")
        mocker.patch(MODULE_PATH + 'idrac_user.time.sleep', return_value=None)
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.import_scp",
                     return_value={"Message": "Successfully created a request."})
        slot_id = 2
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"User.2#UserName": "test_user"}
        response = self.module.create_or_modify_account(f_module, idrac_connection_user_mock, slot_uri, slot_id,
                                                        None, None, user_attr)
        assert response[1] == "Successfully updated user account."

    def test_create_or_modify_account_3(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        idrac_connection_user_mock.get_server_generation = (13, "2.70.70.70")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.import_scp",
                     return_value={"Message": "Successfully created a request."})
        slot_id = 2
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"Users.1#UserName": "test_user"}
        with pytest.raises(Exception) as exc:
            self.module.create_or_modify_account(f_module, idrac_connection_user_mock, slot_uri, slot_id,
                                                 None, None, user_attr)
        assert exc.value.args[0] == "Requested changes are already present in the user slot."

    def test_create_or_modify_account_4(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=True)
        idrac_connection_user_mock.get_server_generation = (13, "2.70.70.70")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.import_scp",
                     return_value={"Message": "Successfully created a request."})
        slot_id = 2
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"Users.1#UserName": "test_user"}
        with pytest.raises(Exception) as exc:
            self.module.create_or_modify_account(f_module, idrac_connection_user_mock, slot_uri, slot_id,
                                                 None, None, user_attr)
        assert exc.value.args[0] == "No changes found to commit!"

    def test_create_or_modify_account_5(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=True)
        idrac_connection_user_mock.get_server_generation = (13, "2.70.70.70")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.2#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.import_scp",
                     return_value={"Message": "Successfully created a request."})
        slot_id = 2
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"Users.1#UserName": "test_user"}
        with pytest.raises(Exception) as exc:
            self.module.create_or_modify_account(f_module, idrac_connection_user_mock, slot_uri, slot_id,
                                                 None, None, user_attr)
        assert exc.value.args[0] == "Changes found to commit!"

    def test_create_or_modify_account_6(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        idrac_connection_user_mock.get_server_generation = (14, "3.60.60.60")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.invoke_request",
                     return_value={"Message": "Successfully created a request."})
        slot_id = 2
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"User.2#UserName": "test_user"}
        response = self.module.create_or_modify_account(f_module, idrac_connection_user_mock, None, None,
                                                        slot_id, slot_uri, user_attr)
        assert response[1] == "Successfully created user account."

    def test_create_or_modify_account_7(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=True)
        idrac_connection_user_mock.get_server_generation = (14, "3.60.60.60")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.invoke_request",
                     return_value={"Message": "Successfully created a request."})
        slot_id = 2
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"User.2#UserName": "test_user"}
        with pytest.raises(Exception) as exc:
            self.module.create_or_modify_account(f_module, idrac_connection_user_mock, None, None,
                                                 slot_id, slot_uri, user_attr)
        assert exc.value.args[0] == "Changes found to commit!"

    def test_create_or_modify_account_8(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        idrac_connection_user_mock.get_server_generation = (14, "3.60.60.60")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.invoke_request",
                     return_value={"Message": "Successfully created a request."})
        slot_id = 2
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"User.2#UserName": "test_user"}
        response = self.module.create_or_modify_account(f_module, idrac_connection_user_mock, slot_uri, slot_id,
                                                        None, None, user_attr)
        assert response[1] == "Successfully updated user account."

    def test_create_or_modify_account_both_slot_empty_input(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        idrac_connection_user_mock.get_server_generation = (14, "3.60.60.60")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.invoke_request",
                     return_value={"Message": "Successfully created a request."})
        slot_id = 2
        slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"User.2#UserName": "test_user"}
        response = self.module.create_or_modify_account(f_module, idrac_connection_user_mock, slot_id, slot_uri,
                                                        slot_id, slot_uri, user_attr)
        assert response[1] == "Successfully updated user account."

    def test_create_or_modify_account_both_slot_empty_none_input(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        idrac_connection_user_mock.get_server_generation = (14, "3.60.60.60")
        mocker.patch(MODULE_PATH + "idrac_user.get_payload", return_value={"Users.2#UserName": "test_user"})
        mocker.patch(MODULE_PATH + "idrac_user.convert_payload_xml",
                     return_value=("<xml-data>", {"Users.1#UserName": "test_user"}))
        mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.invoke_request",
                     return_value={"Message": "Successfully created a request."})
        # slot_id = 2
        # slot_uri = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/".format(slot_id)
        user_attr = {"User.2#UserName": "test_user"}
        with pytest.raises(Exception) as exc:
            self.module.create_or_modify_account(f_module, idrac_connection_user_mock, None, None,
                                                 None, None, user_attr)
        assert exc.value.args[0] == "Maximum number of users reached. Delete a user account and retry the operation."

    @pytest.mark.parametrize("exc_type", [SSLValidationError, URLError, ValueError, TypeError,
                                          ConnectionError, HTTPError, ImportError, RuntimeError])
    def test_main_execptions(self, exc_type, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        json_str = to_text(json.dumps({"data": "out"}))
        if exc_type not in [HTTPError, SSLValidationError]:
            mocker.patch(MODULE_PATH + "idrac_user.create_or_modify_account",
                         side_effect=exc_type('test'))
        else:
            mocker.patch(MODULE_PATH + "idrac_user.create_or_modify_account",
                         side_effect=exc_type('https://testhost.com', 400, 'http error message',
                                              {"accept-type": "application/json"}, StringIO(json_str)))
        if exc_type != URLError:
            result = self._run_module_with_fail_json(idrac_default_args)
            assert result['failed'] is True
        else:
            result = self._run_module(idrac_default_args)
        assert 'msg' in result

    def test_main_error(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "absent", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        obj = MagicMock()
        obj.json_data = {"error": {"message": "Some Error Occured"}}
        mocker.patch(MODULE_PATH + "idrac_user.remove_user_account", return_value=(obj, "error"))
        result = self._run_module_with_fail_json(idrac_default_args)
        assert result['failed'] is True
        assert result['msg'] == "Some Error Occured"

    def test_main_error_oem(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "absent", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        obj = MagicMock()
        obj.json_data = {"Oem": {"Dell": {"Message": "Unable to complete application of configuration profile values."}}}
        mocker.patch(MODULE_PATH + "idrac_user.remove_user_account", return_value=(obj, "error"))
        result = self._run_module_with_fail_json(idrac_default_args)
        assert result['failed'] is True
        assert result['msg'] == "Unable to complete application of configuration profile values."

    def test_main_create_oem(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        obj = MagicMock()
        obj.json_data = {"Oem": {"Dell": {"Message": "This Message Does Not Exists"}}}
        mocker.patch(MODULE_PATH + "idrac_user.create_or_modify_account", return_value=(obj, "created"))
        # with pytest.raises(Exception) as exc:
        result = self._run_module(idrac_default_args)
        assert result['changed'] is True
        assert result['msg'] == "created"

    def test_main_state_some(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "some", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "privilege": "Administrator", "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        result = self._run_module_with_fail_json(idrac_default_args)
        assert result['failed'] is True
        assert result['msg'] == "value of state must be one of: present, absent, got: some"

    def test_validate_input(self, idrac_connection_user_mock, idrac_default_args, mocker):
        idrac_default_args.update({"state": "present", "new_user_name": "new_user_name",
                                   "user_name": "test", "user_password": "password",
                                   "custom_privilege": 512, "ipmi_lan_privilege": "Administrator",
                                   "ipmi_serial_privilege": "Administrator", "enable": True,
                                   "sol_enable": True, "protocol_enable": True,
                                   "authentication_protocol": "SHA", "privacy_protocol": "AES"})
        f_module = self.get_module_mock(params=idrac_default_args, check_mode=False)
        with pytest.raises(Exception) as err:
            self.module.validate_input(f_module)
        assert err.value.args[0] == "custom_privilege value should be from 0 to 511."

        idrac_default_args.update({"state": "absent"})
        ret = self.module.validate_input(f_module)
        assert ret is None

    def test_compare_payload(self, idrac_connection_user_mock, idrac_default_args, mocker):
        json_payload = {"Users.1#Password": "MyDummyPassword"}
        is_change_required = self.module.compare_payload(json_payload, None)
        assert is_change_required is True

        json_payload = {"Users.1#Privilege": "123"}
        idrac_attr = {"Users.1#Privilege": "123"}
        is_change_required = self.module.compare_payload(json_payload, idrac_attr)
        assert is_change_required is False

        json_payload = {"Users.1#Privilege": "123"}
        idrac_attr = {"Users.1#Privilege": "124"}
        is_change_required = self.module.compare_payload(json_payload, idrac_attr)
        assert is_change_required is True