Debian Codegraph @

ansible_collections/check_point/mgmt/tests/units/modules/test_checkpoint_access_rule.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_access_rule,
35 )
36
37 OBJECT = {
38 "layer": "foo",
39 "position": "bar",
40 "name": "baz",
41 "source": [{"name": "lol"}],
42 "destination": [{"name": "Any"}],
43 "action": {"name": "drop"},
44 "enabled": True,
45 }
46 PAYLOAD = {"layer": "foo", "position": "bar", "name": "baz"}
47
48
49 class TestCheckpointAccessRule(object):
50 module = _checkpoint_access_rule
51
52 @pytest.fixture(autouse=True)
53 def module_mock(self, mocker):
54 return mocker.patch.multiple(
55 basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json
56 )
57
58 @pytest.fixture
59 def connection_mock(self, mocker):
60 connection_class_mock = mocker.patch(
61 "ansible_collections.check_point.mgmt.plugins.modules._checkpoint_access_rule.Connection"
62 )
63 return connection_class_mock.return_value
64
65 @pytest.fixture
66 def get_access_rule_200(self, mocker):
67 mock_function = mocker.patch(
68 "ansible_collections.check_point.mgmt.plugins.modules._checkpoint_access_rule.get_access_rule"
69 )
70 mock_function.return_value = (200, OBJECT)
71 return mock_function.return_value
72
73 @pytest.fixture
74 def get_access_rule_404(self, mocker):
75 mock_function = mocker.patch(
76 "ansible_collections.check_point.mgmt.plugins.modules._checkpoint_access_rule.get_access_rule"
77 )
78 mock_function.return_value = (404, "Object not found")
79 return mock_function.return_value
80
81 def test_create(self, get_access_rule_404, connection_mock):
82 connection_mock.send_request.return_value = (200, OBJECT)
83 result = self._run_module(PAYLOAD)
84
85 assert result["changed"]
86 assert "checkpoint_access_rules" in result
87
88 def test_create_idempotent(self, get_access_rule_200, connection_mock):
89 connection_mock.send_request.return_value = (200, PAYLOAD)
90 result = self._run_module(PAYLOAD)
91
92 assert not result["changed"]
93
94 def test_update(self, get_access_rule_200, connection_mock):
95 payload_for_update = {"enabled": False}
96 payload_for_update.update(PAYLOAD)
97 connection_mock.send_request.return_value = (200, payload_for_update)
98 result = self._run_module(payload_for_update)
99
100 assert result["changed"]
101 assert not result["checkpoint_access_rules"]["enabled"]
102
103 def test_delete(self, get_access_rule_200, connection_mock):
104 connection_mock.send_request.return_value = (200, OBJECT)
105 payload_for_delete = {"state": "absent"}
106 payload_for_delete.update(PAYLOAD)
107 result = self._run_module(payload_for_delete)
108
109 assert result["changed"]
110
111 def test_delete_idempotent(self, get_access_rule_404, connection_mock):
112 payload = {"name": "baz", "state": "absent"}
113 connection_mock.send_request.return_value = (200, OBJECT)
114 result = self._run_module(payload)
115
116 assert not result["changed"]
117
118 def _run_module(self, module_args):
119 set_module_args(module_args)
120 with pytest.raises(AnsibleExitJson) as ex:
121 self.module.main()
122 return ex.value.args[0]
123
124 def _run_module_with_fail_json(self, module_args):
125 set_module_args(module_args)
126 with pytest.raises(AnsibleFailJson) as exc:
127 self.module.main()
128 result = exc.value.args[0]
129 return result