| 1 |
# (c) 2018 Red Hat Inc. |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from __future__ import absolute_import, division, print_function |
| 5 |
|
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
import json |
| 9 |
|
| 10 |
from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 11 |
from units.compat import mock |
| 12 |
from units.compat import unittest |
| 13 |
|
| 14 |
from ansible.errors import AnsibleConnectionFailure |
| 15 |
from ansible.module_utils.connection import ConnectionError |
| 16 |
from ansible.module_utils.six import BytesIO, StringIO |
| 17 |
from ansible_collections.check_point.mgmt.plugins.httpapi.checkpoint import ( |
| 18 |
HttpApi, |
| 19 |
) |
| 20 |
|
| 21 |
EXPECTED_BASE_HEADERS = {"Content-Type": "application/json"} |
| 22 |
|
| 23 |
|
| 24 |
class FakeCheckpointHttpApiPlugin(HttpApi): |
| 25 |
def __init__(self, conn): |
| 26 |
super(FakeCheckpointHttpApiPlugin, self).__init__(conn) |
| 27 |
self.hostvars = {"domain": None} |
| 28 |
|
| 29 |
def get_option(self, option): |
| 30 |
return self.hostvars[option] |
| 31 |
|
| 32 |
def set_option(self, option, value): |
| 33 |
self.hostvars[option] = value |
| 34 |
|
| 35 |
|
| 36 |
class TestCheckpointHttpApi(unittest.TestCase): |
| 37 |
def setUp(self): |
| 38 |
self.connection_mock = mock.Mock() |
| 39 |
self.checkpoint_plugin = FakeCheckpointHttpApiPlugin( |
| 40 |
self.connection_mock |
| 41 |
) |
| 42 |
self.checkpoint_plugin._load_name = "httpapi" |
| 43 |
|
| 44 |
def test_login_raises_exception_when_username_and_password_are_not_provided( |
| 45 |
self, |
| 46 |
): |
| 47 |
with self.assertRaises(AnsibleConnectionFailure) as res: |
| 48 |
self.checkpoint_plugin.login(None, None) |
| 49 |
assert "Username and password are required" in str(res.exception) |
| 50 |
|
| 51 |
def test_login_raises_exception_when_invalid_response(self): |
| 52 |
self.connection_mock.send.return_value = self._connection_response( |
| 53 |
{"NOSIDKEY": "NOSIDVALUE"} |
| 54 |
) |
| 55 |
|
| 56 |
with self.assertRaises(ConnectionError) as res: |
| 57 |
self.checkpoint_plugin.login("foo", "bar") |
| 58 |
|
| 59 |
assert ( |
| 60 |
"Server returned response without token info during connection authentication" |
| 61 |
in str(res.exception) |
| 62 |
) |
| 63 |
|
| 64 |
def test_send_request_should_return_error_info_when_http_error_raises( |
| 65 |
self, |
| 66 |
): |
| 67 |
self.connection_mock.send.side_effect = HTTPError( |
| 68 |
"http://testhost.com", |
| 69 |
500, |
| 70 |
"", |
| 71 |
{}, |
| 72 |
StringIO('{"errorMessage": "ERROR"}'), |
| 73 |
) |
| 74 |
|
| 75 |
resp = self.checkpoint_plugin.send_request("/test", None) |
| 76 |
|
| 77 |
assert resp == (500, {"errorMessage": "ERROR"}) |
| 78 |
|
| 79 |
def test_login_to_global_domain(self): |
| 80 |
temp_domain = self.checkpoint_plugin.hostvars["domain"] |
| 81 |
self.checkpoint_plugin.hostvars["domain"] = "test_domain" |
| 82 |
self.connection_mock.send.return_value = self._connection_response( |
| 83 |
{"sid": "SID", "uid": "UID"} |
| 84 |
) |
| 85 |
|
| 86 |
self.checkpoint_plugin.login("USERNAME", "PASSWORD") |
| 87 |
|
| 88 |
self.connection_mock.send.assert_called_once_with( |
| 89 |
"/web_api/login", mock.ANY, headers=mock.ANY, method=mock.ANY |
| 90 |
) |
| 91 |
self.checkpoint_plugin.hostvars["domain"] = temp_domain |
| 92 |
|
| 93 |
@staticmethod |
| 94 |
def _connection_response(response, status=200): |
| 95 |
response_mock = mock.Mock() |
| 96 |
response_mock.getcode.return_value = status |
| 97 |
response_text = ( |
| 98 |
json.dumps(response) if isinstance(response) is dict else response |
| 99 |
) |
| 100 |
response_data = BytesIO( |
| 101 |
response_text.encode() if response_text else "".encode() |
| 102 |
) |
| 103 |
return response_mock, response_data |