| 1 |
# Copyright (c) 2017-2021 Felix Fontein |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import pytest |
| 8 |
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import ( |
| 9 |
MagicMock, |
| 10 |
) |
| 11 |
|
| 12 |
lxmletree = pytest.importorskip("lxml.etree") |
| 13 |
|
| 14 |
from ansible.module_utils.common.text.converters import to_native |
| 15 |
|
| 16 |
from ansible_collections.community.dns.plugins.module_utils._wsdl import ( |
| 17 |
Composer, |
| 18 |
Parser, |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
def test_composer_generation(): |
| 23 |
composer = Composer(MagicMock(), api="https://example.com/api") |
| 24 |
composer.add_simple_command( |
| 25 |
"test", |
| 26 |
int_value=42, |
| 27 |
str_value="bar", |
| 28 |
list_value=[1, 2, 3], |
| 29 |
dict_value={ |
| 30 |
"hello": "world", |
| 31 |
"list": [2, 3, 5, 7], |
| 32 |
}, |
| 33 |
) |
| 34 |
command = to_native( |
| 35 |
lxmletree.tostring(composer._root, pretty_print=True) |
| 36 |
).splitlines() |
| 37 |
|
| 38 |
print(command) |
| 39 |
|
| 40 |
expected_lines = [ |
| 41 |
" <SOAP-ENV:Header/>", |
| 42 |
" <SOAP-ENV:Body>", |
| 43 |
' <ns0:test xmlns:ns0="https://example.com/api">', |
| 44 |
' <int_value xsi:type="xsd:int">42</int_value>', |
| 45 |
' <str_value xsi:type="xsd:string">bar</str_value>', |
| 46 |
' <list_value xsi:type="SOAP-ENC:Array">', |
| 47 |
' <item xsi:type="xsd:int">1</item>', |
| 48 |
' <item xsi:type="xsd:int">2</item>', |
| 49 |
' <item xsi:type="xsd:int">3</item>', |
| 50 |
" </list_value>", |
| 51 |
' <dict_value xmlns:ns0="http://xml.apache.org/xml-soap" xsi:type="ns0:Map">', |
| 52 |
" <item>", |
| 53 |
' <key xsi:type="xsd:string">hello</key>', |
| 54 |
' <value xsi:type="xsd:string">world</value>', |
| 55 |
" </item>", |
| 56 |
" <item>", |
| 57 |
' <key xsi:type="xsd:string">list</key>', |
| 58 |
' <value xsi:type="SOAP-ENC:Array">', |
| 59 |
' <item xsi:type="xsd:int">2</item>', |
| 60 |
' <item xsi:type="xsd:int">3</item>', |
| 61 |
' <item xsi:type="xsd:int">5</item>', |
| 62 |
' <item xsi:type="xsd:int">7</item>', |
| 63 |
" </value>", |
| 64 |
" </item>", |
| 65 |
" </dict_value>", |
| 66 |
" </ns0:test>", |
| 67 |
" </SOAP-ENV:Body>", |
| 68 |
"</SOAP-ENV:Envelope>", |
| 69 |
] |
| 70 |
|
| 71 |
assert command[1:] == expected_lines |
| 72 |
|
| 73 |
for part in [ |
| 74 |
"<SOAP-ENV:Envelope", |
| 75 |
' xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"', |
| 76 |
' xmlns:xsd="http://www.w3.org/2001/XMLSchema"', |
| 77 |
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"', |
| 78 |
' xmlns:ns2="auth"', |
| 79 |
' xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"', |
| 80 |
' SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"', |
| 81 |
]: |
| 82 |
assert part in command[0] |
| 83 |
|
| 84 |
|
| 85 |
def test_parsing(): |
| 86 |
input_string = "\n".join( # noqa: FLY002 |
| 87 |
[ |
| 88 |
'<?xml version="1.0" encoding="UTF-8"?>', |
| 89 |
"<SOAP-ENV:Envelope" |
| 90 |
' xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"' |
| 91 |
' xmlns:ns1="https://example.com/api"' |
| 92 |
' xmlns:xsd="http://www.w3.org/2001/XMLSchema"' |
| 93 |
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' |
| 94 |
' xmlns:ns2="http://xml.apache.org/xml-soap"' |
| 95 |
' xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"' |
| 96 |
' SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"' |
| 97 |
">", |
| 98 |
" <SOAP-ENV:Header>", |
| 99 |
" <ns1:authenticateResponse>", |
| 100 |
' <return xsi:type="xsd:boolean">true</return>', |
| 101 |
" </ns1:authenticateResponse>", |
| 102 |
" </SOAP-ENV:Header>", |
| 103 |
" <SOAP-ENV:Body>", |
| 104 |
" <ns1:getZoneResponse>", |
| 105 |
' <return xsi:type="ns2:Map">', |
| 106 |
" <item>", |
| 107 |
' <key xsi:type="xsd:string">id</key>', |
| 108 |
' <value xsi:type="xsd:int">1</value>', |
| 109 |
" </item>", |
| 110 |
" <item>", |
| 111 |
' <key xsi:type="xsd:string">user</key>', |
| 112 |
' <value xsi:type="xsd:int">2</value>', |
| 113 |
" </item>", |
| 114 |
" <item>", |
| 115 |
' <key xsi:type="xsd:string">name</key>', |
| 116 |
' <value xsi:type="xsd:string">example.com</value>', |
| 117 |
" </item>", |
| 118 |
" <item>", |
| 119 |
' <key xsi:type="xsd:string">email</key>', |
| 120 |
' <value xsi:type="xsd:string">[email protected]</value>', |
| 121 |
" </item>", |
| 122 |
" <item>", |
| 123 |
' <key xsi:type="xsd:string">ttl</key>', |
| 124 |
' <value xsi:type="xsd:int">10800</value>', |
| 125 |
" </item>", |
| 126 |
" <item>", |
| 127 |
' <key xsi:type="xsd:string">nameserver</key>', |
| 128 |
' <value xsi:type="xsd:string">ns1.hostserv.eu</value>', |
| 129 |
" </item>", |
| 130 |
" <item>", |
| 131 |
' <key xsi:type="xsd:string">serial</key>', |
| 132 |
' <value xsi:type="xsd:string">1234567890</value>', |
| 133 |
" </item>", |
| 134 |
" <item>", |
| 135 |
' <key xsi:type="xsd:string">serialLastUpdate</key>', |
| 136 |
' <value xsi:type="xsd:int">0</value>', |
| 137 |
" </item>", |
| 138 |
" <item>", |
| 139 |
' <key xsi:type="xsd:string">refresh</key>', |
| 140 |
' <value xsi:type="xsd:int">7200</value>', |
| 141 |
" </item>", |
| 142 |
" <item>", |
| 143 |
' <key xsi:type="xsd:string">retry</key>', |
| 144 |
' <value xsi:type="xsd:int">120</value>', |
| 145 |
" </item>", |
| 146 |
" <item>", |
| 147 |
' <key xsi:type="xsd:string">expire</key>', |
| 148 |
' <value xsi:type="xsd:int">1234567</value>', |
| 149 |
" </item>", |
| 150 |
" <item>", |
| 151 |
' <key xsi:type="xsd:string">template</key>', |
| 152 |
' <value xsi:nil="true"/>', |
| 153 |
" </item>", |
| 154 |
" <item>", |
| 155 |
' <key xsi:type="xsd:string">ns3</key>', |
| 156 |
' <value xsi:type="xsd:int">1</value>', |
| 157 |
" </item>", |
| 158 |
" <item>", |
| 159 |
' <key xsi:type="xsd:string">records</key>', |
| 160 |
' <value SOAP-ENC:arrayType="ns2:Map[2]" xsi:type="SOAP-ENC:Array">', |
| 161 |
' <item xsi:type="ns2:Map">', |
| 162 |
" <item>", |
| 163 |
' <key xsi:type="xsd:string">id</key>', |
| 164 |
' <value xsi:type="xsd:int">3</value>', |
| 165 |
" </item>", |
| 166 |
" <item>", |
| 167 |
' <key xsi:type="xsd:string">zone</key>', |
| 168 |
' <value xsi:type="xsd:int">4</value>', |
| 169 |
" </item>", |
| 170 |
" <item>", |
| 171 |
' <key xsi:type="xsd:string">type</key>', |
| 172 |
' <value xsi:type="xsd:string">A</value>', |
| 173 |
" </item>", |
| 174 |
" <item>", |
| 175 |
' <key xsi:type="xsd:string">prefix</key>', |
| 176 |
' <value xsi:type="xsd:string"></value>', |
| 177 |
" </item>", |
| 178 |
" <item>", |
| 179 |
' <key xsi:type="xsd:string">target</key>', |
| 180 |
' <value xsi:type="xsd:string">1.2.3.4</value>', |
| 181 |
" </item>", |
| 182 |
" <item>", |
| 183 |
' <key xsi:type="xsd:string">ttl</key>', |
| 184 |
' <value xsi:type="xsd:int">3600</value>', |
| 185 |
" </item>", |
| 186 |
" <item>", |
| 187 |
' <key xsi:type="xsd:string">comment</key>', |
| 188 |
' <value xsi:nil="true"/>', |
| 189 |
" </item>", |
| 190 |
" <item>", |
| 191 |
' <key xsi:type="xsd:string">priority</key>', |
| 192 |
' <value xsi:nil="true"/>', |
| 193 |
" </item>", |
| 194 |
" </item>", |
| 195 |
' <item xsi:type="ns2:Map">', |
| 196 |
" <item>", |
| 197 |
' <key xsi:type="xsd:string">id</key>', |
| 198 |
' <value xsi:type="xsd:int">5</value>', |
| 199 |
" </item>", |
| 200 |
" <item>", |
| 201 |
' <key xsi:type="xsd:string">zone</key>', |
| 202 |
' <value xsi:type="xsd:int">4</value>', |
| 203 |
" </item>", |
| 204 |
" <item>", |
| 205 |
' <key xsi:type="xsd:string">type</key>', |
| 206 |
' <value xsi:type="xsd:string">A</value>', |
| 207 |
" </item>", |
| 208 |
" <item>", |
| 209 |
' <key xsi:type="xsd:string">prefix</key>', |
| 210 |
' <value xsi:type="xsd:string">*</value>', |
| 211 |
" </item>", |
| 212 |
" <item>", |
| 213 |
' <key xsi:type="xsd:string">target</key>', |
| 214 |
' <value xsi:type="xsd:string">1.2.3.5</value>', |
| 215 |
" </item>", |
| 216 |
" <item>", |
| 217 |
' <key xsi:type="xsd:string">ttl</key>', |
| 218 |
' <value xsi:type="xsd:int">3600</value>', |
| 219 |
" </item>", |
| 220 |
" <item>", |
| 221 |
' <key xsi:type="xsd:string">comment</key>', |
| 222 |
' <value xsi:nil="true"/>', |
| 223 |
" </item>", |
| 224 |
" <item>", |
| 225 |
' <key xsi:type="xsd:string">priority</key>', |
| 226 |
' <value xsi:nil="true"/>', |
| 227 |
" </item>", |
| 228 |
" </item>", |
| 229 |
" </value>", |
| 230 |
" </item>", |
| 231 |
" </return>", |
| 232 |
" </ns1:getZoneResponse>", |
| 233 |
" </SOAP-ENV:Body>", |
| 234 |
"</SOAP-ENV:Envelope>", |
| 235 |
] |
| 236 |
).encode("utf-8") |
| 237 |
|
| 238 |
parser = Parser("https://example.com/api", lxmletree.fromstring(input_string)) |
| 239 |
assert parser.get_header("authenticateResponse") is True |
| 240 |
assert len(parser._header) == 1 |
| 241 |
assert parser.get_result("getZoneResponse") == { |
| 242 |
"id": 1, |
| 243 |
"user": 2, |
| 244 |
"name": "example.com", |
| 245 |
"email": "[email protected]", |
| 246 |
"ttl": 10800, |
| 247 |
"nameserver": "ns1.hostserv.eu", |
| 248 |
"serial": "1234567890", |
| 249 |
"serialLastUpdate": 0, |
| 250 |
"refresh": 7200, |
| 251 |
"retry": 120, |
| 252 |
"expire": 1234567, |
| 253 |
"template": None, |
| 254 |
"ns3": 1, |
| 255 |
"records": [ |
| 256 |
{ |
| 257 |
"id": 3, |
| 258 |
"zone": 4, |
| 259 |
"type": "A", |
| 260 |
"prefix": None, |
| 261 |
"target": "1.2.3.4", |
| 262 |
"ttl": 3600, |
| 263 |
"comment": None, |
| 264 |
"priority": None, |
| 265 |
}, |
| 266 |
{ |
| 267 |
"id": 5, |
| 268 |
"zone": 4, |
| 269 |
"type": "A", |
| 270 |
"prefix": "*", |
| 271 |
"target": "1.2.3.5", |
| 272 |
"ttl": 3600, |
| 273 |
"comment": None, |
| 274 |
"priority": None, |
| 275 |
}, |
| 276 |
], |
| 277 |
} |
| 278 |
assert len(parser._body) == 1 |