| 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_company |
| 31 |
|
| 32 |
|
| 33 |
class TestSAPRfcModule(ModuleTestCase): |
| 34 |
|
| 35 |
def setUp(self): |
| 36 |
super(TestSAPRfcModule, self).setUp() |
| 37 |
self.module = sap_company |
| 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 company""" |
| 53 |
|
| 54 |
args = { |
| 55 |
"conn_username": "DDIC", |
| 56 |
"conn_password": "Test1234", |
| 57 |
"host": "10.1.8.9", |
| 58 |
"company_id": "Comp_ID", |
| 59 |
"name": "Test_comp", |
| 60 |
"name_2": "LTD", |
| 61 |
"country": "DE", |
| 62 |
"time_zone": "UTC", |
| 63 |
"city": "City", |
| 64 |
"post_code": "12345", |
| 65 |
"street": "test_street", |
| 66 |
"street_no": "1", |
| 67 |
"e_mail": "[email protected]", |
| 68 |
} |
| 69 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 70 |
RAW.return_value = {'RETURN': [{'FIELD': '', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 71 |
'LOG_NO': '', 'MESSAGE': 'Something went wrong', 'MESSAGE_V1': 'ADMIN', |
| 72 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '199', |
| 73 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'E'}]} |
| 74 |
|
| 75 |
with self.assertRaises(AnsibleFailJson) as result: |
| 76 |
with set_module_args(args): |
| 77 |
sap_company.main() |
| 78 |
self.assertEqual(result.exception.args[0]['msg'], 'Something went wrong') |
| 79 |
|
| 80 |
def test_success(self): |
| 81 |
"""test execute company create success""" |
| 82 |
|
| 83 |
args = { |
| 84 |
"conn_username": "DDIC", |
| 85 |
"conn_password": "Test1234", |
| 86 |
"host": "10.1.8.9", |
| 87 |
"company_id": "Comp_ID", |
| 88 |
"name": "Test_comp", |
| 89 |
"name_2": "LTD", |
| 90 |
"country": "DE", |
| 91 |
"time_zone": "UTC", |
| 92 |
"city": "City", |
| 93 |
"post_code": "12345", |
| 94 |
"street": "test_street", |
| 95 |
"street_no": "1", |
| 96 |
"e_mail": "[email protected]", |
| 97 |
} |
| 98 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 99 |
RAW.return_value = {'RETURN': [{'FIELD': '', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 100 |
'LOG_NO': '', 'MESSAGE': 'Company address COMP_ID created', 'MESSAGE_V1': 'ADMIN', |
| 101 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '102', |
| 102 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'S'}]} |
| 103 |
|
| 104 |
with self.assertRaises(AnsibleExitJson) as result: |
| 105 |
with set_module_args(args): |
| 106 |
sap_company.main() |
| 107 |
self.assertEqual(result.exception.args[0]['msg'], 'Company address COMP_ID created') |
| 108 |
|
| 109 |
def test_no_changes(self): |
| 110 |
"""test execute company no changes""" |
| 111 |
|
| 112 |
args = { |
| 113 |
"conn_username": "DDIC", |
| 114 |
"conn_password": "Test1234", |
| 115 |
"host": "10.1.8.9", |
| 116 |
"company_id": "Comp_ID", |
| 117 |
"name": "Test_comp", |
| 118 |
"name_2": "LTD", |
| 119 |
"country": "DE", |
| 120 |
"time_zone": "UTC", |
| 121 |
"city": "City", |
| 122 |
"post_code": "12345", |
| 123 |
"street": "test_street", |
| 124 |
"street_no": "1", |
| 125 |
"e_mail": "[email protected]", |
| 126 |
} |
| 127 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 128 |
RAW.return_value = {'RETURN': [{'FIELD': '', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 129 |
'LOG_NO': '', 'MESSAGE': 'Company address COMP_ID changed', 'MESSAGE_V1': 'ADMIN', |
| 130 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '079', |
| 131 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'S'}]} |
| 132 |
|
| 133 |
with self.assertRaises(AnsibleExitJson) as result: |
| 134 |
with set_module_args(args): |
| 135 |
sap_company.main() |
| 136 |
self.assertEqual(result.exception.args[0]['msg'], 'No changes where made.') |
| 137 |
|
| 138 |
def test_absent(self): |
| 139 |
"""test execute company delete success""" |
| 140 |
|
| 141 |
args = { |
| 142 |
"state": "absent", |
| 143 |
"conn_username": "DDIC", |
| 144 |
"conn_password": "Test1234", |
| 145 |
"host": "10.1.8.9", |
| 146 |
"company_id": "Comp_ID", |
| 147 |
} |
| 148 |
with patch.object(self.module, 'call_rfc_method') as RAW: |
| 149 |
RAW.return_value = {'RETURN': [{'FIELD': '', 'ID': '01', 'LOG_MSG_NO': '000000', |
| 150 |
'LOG_NO': '', 'MESSAGE': 'Company address COMP_ID deleted', 'MESSAGE_V1': 'ADMIN', |
| 151 |
'MESSAGE_V2': '', 'MESSAGE_V3': '', 'MESSAGE_V4': '', 'NUMBER': '080', |
| 152 |
'PARAMETER': '', 'ROW': 0, 'SYSTEM': '', 'TYPE': 'S'}]} |
| 153 |
|
| 154 |
with self.assertRaises(AnsibleExitJson) as result: |
| 155 |
with set_module_args(args): |
| 156 |
sap_company.main() |
| 157 |
self.assertEqual(result.exception.args[0]['msg'], 'Company address COMP_ID deleted') |