Debian Codegraph @

ansible_collections/check_point/mgmt/tests/units/modules/test_checkpoint_session.py

raw
1 # Copyright (c) 2018 Red Hat
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 from __future__ import absolute_import, division, print_function
20
21 __metaclass__ = type
22
23 import pytest
24 from units.modules.utils import (
25 set_module_args,
26 exit_json,
27 fail_json,
28 AnsibleFailJson,
29 AnsibleExitJson,
30 )
31
32 from ansible.module_utils import basic
33 from ansible_collections.check_point.mgmt.plugins.modules import (
34 _checkpoint_session,
35 )
36
37 OBJECT = {"uid": "1234"}
38 PAYLOAD = {}
39
40
41 class TestCheckpointAccessRule(object):
42 module = _checkpoint_session
43
44 @pytest.fixture(autouse=True)
45 def module_mock(self, mocker):
46 return mocker.patch.multiple(
47 basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json
48 )
49
50 @pytest.fixture
51 def connection_mock(self, mocker):
52 connection_class_mock = mocker.patch(
53 "ansible_collections.check_point.mgmt.plugins.modules._checkpoint_session.Connection"
54 )
55 return connection_class_mock.return_value
56
57 @pytest.fixture
58 def get_session_200(self, mocker):
59 mock_function = mocker.patch(
60 "ansible_collections.check_point.mgmt.plugins.modules._checkpoint_session.get_session"
61 )
62 mock_function.return_value = (200, OBJECT)
63 return mock_function.return_value
64
65 def test_publish(self, get_session_200, connection_mock):
66 connection_mock.send_request.return_value = (200, OBJECT)
67 result = self._run_module(PAYLOAD)
68
69 assert result["changed"]
70 assert "checkpoint_session" in result
71
72 def _run_module(self, module_args):
73 set_module_args(module_args)
74 with pytest.raises(AnsibleExitJson) as ex:
75 self.module.main()
76 return ex.value.args[0]
77
78 def _run_module_with_fail_json(self, module_args):
79 set_module_args(module_args)
80 with pytest.raises(AnsibleFailJson) as exc:
81 self.module.main()
82 result = exc.value.args[0]
83 return result