| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright (c) 2022-2026 The Project Contributors. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
# |
| 17 |
# For a detailed list of copyright holders and contribution history, |
| 18 |
# please refer to the CONTRIBUTORS.md file in the project root. |
| 19 |
|
| 20 |
from __future__ import (absolute_import, division, print_function) |
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
import sys |
| 24 |
from unittest.mock import patch, MagicMock |
| 25 |
from ansible_collections.community.sap_libs.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args |
| 26 |
|
| 27 |
sys.modules['pyrfc'] = MagicMock() |
| 28 |
sys.modules['pyrfc.Connection'] = MagicMock() |
| 29 |
|
| 30 |
from ansible_collections.community.sap_libs.plugins.modules import sap_user |
| 31 |
|
| 32 |
|
| 33 |
class TestSAPRfcModule(ModuleTestCase): |
| 34 |
|
| 35 |
def setUp(self): |
| 36 |
super(TestSAPRfcModule, self).setUp() |
| 37 |
self.module = sap_user |
| 38 |
|
| 39 |
def tearDown(self): |
| 40 |
super(TestSAPRfcModule, self).tearDown() |
| 41 |
|
| 42 |
def define_rfc_connect(self, mocker): |
| 43 |
return mocker.patch(self.module.call_rfc_method) |
| 44 |
|
| 45 |
def test_without_required_parameters(self): |
| 46 |
"""Failure must occurs when all parameters are missing""" |
| 47 |
with self.assertRaises(AnsibleFailJson): |
| 48 |
with set_module_args({}): |
| 49 |
self.module.main() |
| 50 |
|
| 51 |
def test_error_user_create(self): |
| 52 |
"""test fail to create user""" |
| 53 |
|
| 54 |
args = { |
| 55 |
"conn_username": "DDIC", |
| 56 |
"conn_password": "Test1234", |
| 57 |
"host": "10.1.8.9", |
| 58 |
"username": "ADMIN", |
| 59 |
"firstname": "first_admin", |
| 60 |
"lastname": "last_admin", |
| 61 |
"email": "[email protected]", |
| 62 |
"password": "Test123456", |
| 63 |
"useralias": "ADMIN", |
| 64 |
"company": "DEFAULT_COMPANY" |
| 65 |
} |
| 66 |
|
| 67 |
with patch.object(self.module, 'check_user') as check: |
| 68 |
check.return_value = False |
| 69 |
|
| 70 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 71 |
RAW.return_value = {'RETURN': [{'FIELD': 'BNAME', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 72 |
'LOG_NO': '', 'MESSAGE': 'Something went wrong', 'MESSAGE_V1': 'ADMIN', |
| 73 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '199', |
| 74 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'E'}]} |
| 75 |
|
| 76 |
with self.assertRaises(AnsibleFailJson) as result: |
| 77 |
with set_module_args(args): |
| 78 |
sap_user.main() |
| 79 |
self.assertEqual(result.exception.args[0]['msg'], 'Something went wrong') |
| 80 |
|
| 81 |
def test_success(self): |
| 82 |
"""test execute user create success""" |
| 83 |
|
| 84 |
args = { |
| 85 |
"conn_username": "DDIC", |
| 86 |
"conn_password": "Test1234", |
| 87 |
"host": "10.1.8.9", |
| 88 |
"username": "ADMIN", |
| 89 |
"firstname": "first_admin", |
| 90 |
"lastname": "last_admin", |
| 91 |
"email": "[email protected]", |
| 92 |
"password": "Test123456", |
| 93 |
"useralias": "ADMIN", |
| 94 |
"company": "DEFAULT_COMPANY" |
| 95 |
} |
| 96 |
with patch.object(self.module, 'check_user') as check: |
| 97 |
check.return_value = False |
| 98 |
|
| 99 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 100 |
RAW.return_value = {'RETURN': [{'FIELD': 'BNAME', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 101 |
'LOG_NO': '', 'MESSAGE': 'User ADMIN created', 'MESSAGE_V1': 'ADMIN', |
| 102 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '102', |
| 103 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'S'}]} |
| 104 |
|
| 105 |
with self.assertRaises(AnsibleExitJson) as result: |
| 106 |
with set_module_args(args): |
| 107 |
sap_user.main() |
| 108 |
self.assertEqual(result.exception.args[0]['msg'], 'User ADMIN created') |
| 109 |
|
| 110 |
def test_no_changes(self): |
| 111 |
"""test execute user no changes""" |
| 112 |
|
| 113 |
args = { |
| 114 |
"conn_username": "DDIC", |
| 115 |
"conn_password": "Test1234", |
| 116 |
"host": "10.1.8.9", |
| 117 |
"username": "ADMIN", |
| 118 |
"firstname": "first_admin", |
| 119 |
"lastname": "last_admin", |
| 120 |
"email": "[email protected]", |
| 121 |
"password": "Test123456", |
| 122 |
"useralias": "ADMIN", |
| 123 |
"company": "DEFAULT_COMPANY" |
| 124 |
} |
| 125 |
with patch.object(self.module, 'check_user') as check: |
| 126 |
check.return_value = True |
| 127 |
|
| 128 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 129 |
RAW.return_value = {'RETURN': [{'FIELD': 'BNAME', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 130 |
'LOG_NO': '', 'MESSAGE': '', 'MESSAGE_V1': 'ADMIN', |
| 131 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '029', |
| 132 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'S'}]} |
| 133 |
|
| 134 |
with patch.object(self.module, 'all') as DETAIL: |
| 135 |
DETAIL.return_value = True |
| 136 |
|
| 137 |
with self.assertRaises(AnsibleExitJson) as result: |
| 138 |
with set_module_args(args): |
| 139 |
sap_user.main() |
| 140 |
self.assertEqual(result.exception.args[0]['msg'], 'No changes where made.') |
| 141 |
|
| 142 |
def test_absent(self): |
| 143 |
"""test execute user delete success""" |
| 144 |
|
| 145 |
args = { |
| 146 |
"state": "absent", |
| 147 |
"conn_username": "DDIC", |
| 148 |
"conn_password": "Test1234", |
| 149 |
"host": "10.1.8.9", |
| 150 |
"username": "ADMIN", |
| 151 |
} |
| 152 |
with patch.object(self.module, 'check_user') as check: |
| 153 |
check.return_value = True |
| 154 |
|
| 155 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 156 |
RAW.return_value = {'RETURN': [{'FIELD': 'BNAME', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 157 |
'LOG_NO': '', 'MESSAGE': 'User ADMIN deleted', 'MESSAGE_V1': 'ADMIN', |
| 158 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '102', |
| 159 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'S'}]} |
| 160 |
|
| 161 |
with self.assertRaises(AnsibleExitJson) as result: |
| 162 |
with set_module_args(args): |
| 163 |
sap_user.main() |
| 164 |
self.assertEqual(result.exception.args[0]['msg'], 'User ADMIN deleted') |
| 165 |
|
| 166 |
def test_lock(self): |
| 167 |
"""test execute user lock success""" |
| 168 |
|
| 169 |
args = { |
| 170 |
"state": "lock", |
| 171 |
"conn_username": "DDIC", |
| 172 |
"conn_password": "Test1234", |
| 173 |
"host": "10.1.8.9", |
| 174 |
"username": "ADMIN", |
| 175 |
} |
| 176 |
with patch.object(self.module, 'check_user') as check: |
| 177 |
check.return_value = True |
| 178 |
|
| 179 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 180 |
RAW.return_value = {'RETURN': [{'FIELD': 'BNAME', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 181 |
'LOG_NO': '', 'MESSAGE': 'User ADMIN locked', 'MESSAGE_V1': 'ADMIN', |
| 182 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '206', |
| 183 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'S'}]} |
| 184 |
|
| 185 |
with self.assertRaises(AnsibleExitJson) as result: |
| 186 |
with set_module_args(args): |
| 187 |
sap_user.main() |
| 188 |
self.assertEqual(result.exception.args[0]['msg'], 'User ADMIN locked') |
| 189 |
|
| 190 |
def test_unlock(self): |
| 191 |
"""test execute user lock success""" |
| 192 |
|
| 193 |
args = { |
| 194 |
"state": "lock", |
| 195 |
"conn_username": "DDIC", |
| 196 |
"conn_password": "Test1234", |
| 197 |
"host": "10.1.8.9", |
| 198 |
"username": "ADMIN", |
| 199 |
} |
| 200 |
with patch.object(self.module, 'check_user') as check: |
| 201 |
check.return_value = True |
| 202 |
|
| 203 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 204 |
RAW.return_value = {'RETURN': [{'FIELD': 'BNAME', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 205 |
'LOG_NO': '', 'MESSAGE': 'User ADMIN unlocked', 'MESSAGE_V1': 'ADMIN', |
| 206 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '210', |
| 207 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'S'}]} |
| 208 |
|
| 209 |
with self.assertRaises(AnsibleExitJson) as result: |
| 210 |
with set_module_args(args): |
| 211 |
sap_user.main() |
| 212 |
self.assertEqual(result.exception.args[0]['msg'], 'User ADMIN unlocked') |