1# Copyright 2019 Fortinet, Inc.
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with Ansible.  If not, see <https://www.gnu.org/licenses/>.
15
16# Make coding more python3-ish
17from __future__ import (absolute_import, division, print_function)
18__metaclass__ = type
19
20import os
21import json
22import pytest
23from mock import ANY
24from ansible.module_utils.network.fortios.fortios import FortiOSHandler
25
26try:
27    from ansible.modules.network.fortios import fortios_system_proxy_arp
28except ImportError:
29    pytest.skip("Could not load required modules for testing", allow_module_level=True)
30
31
32@pytest.fixture(autouse=True)
33def connection_mock(mocker):
34    connection_class_mock = mocker.patch('ansible.modules.network.fortios.fortios_system_proxy_arp.Connection')
35    return connection_class_mock
36
37
38fos_instance = FortiOSHandler(connection_mock)
39
40
41def test_system_proxy_arp_creation(mocker):
42    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
43
44    set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200}
45    set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result)
46
47    input_data = {
48        'username': 'admin',
49        'state': 'present',
50        'system_proxy_arp': {
51            'end_ip': 'test_value_3',
52            'id': '4',
53            'interface': 'test_value_5',
54            'ip': 'test_value_6'
55        },
56        'vdom': 'root'}
57
58    is_error, changed, response = fortios_system_proxy_arp.fortios_system(input_data, fos_instance)
59
60    expected_data = {
61        'end-ip': 'test_value_3',
62        'id': '4',
63        'interface': 'test_value_5',
64        'ip': 'test_value_6'
65    }
66
67    set_method_mock.assert_called_with('system', 'proxy-arp', data=expected_data, vdom='root')
68    schema_method_mock.assert_not_called()
69    assert not is_error
70    assert changed
71    assert response['status'] == 'success'
72    assert response['http_status'] == 200
73
74
75def test_system_proxy_arp_creation_fails(mocker):
76    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
77
78    set_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500}
79    set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result)
80
81    input_data = {
82        'username': 'admin',
83        'state': 'present',
84        'system_proxy_arp': {
85            'end_ip': 'test_value_3',
86            'id': '4',
87            'interface': 'test_value_5',
88            'ip': 'test_value_6'
89        },
90        'vdom': 'root'}
91
92    is_error, changed, response = fortios_system_proxy_arp.fortios_system(input_data, fos_instance)
93
94    expected_data = {
95        'end-ip': 'test_value_3',
96        'id': '4',
97        'interface': 'test_value_5',
98        'ip': 'test_value_6'
99    }
100
101    set_method_mock.assert_called_with('system', 'proxy-arp', data=expected_data, vdom='root')
102    schema_method_mock.assert_not_called()
103    assert is_error
104    assert not changed
105    assert response['status'] == 'error'
106    assert response['http_status'] == 500
107
108
109def test_system_proxy_arp_removal(mocker):
110    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
111
112    delete_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200}
113    delete_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.delete', return_value=delete_method_result)
114
115    input_data = {
116        'username': 'admin',
117        'state': 'absent',
118        'system_proxy_arp': {
119            'end_ip': 'test_value_3',
120            'id': '4',
121            'interface': 'test_value_5',
122            'ip': 'test_value_6'
123        },
124        'vdom': 'root'}
125
126    is_error, changed, response = fortios_system_proxy_arp.fortios_system(input_data, fos_instance)
127
128    delete_method_mock.assert_called_with('system', 'proxy-arp', mkey=ANY, vdom='root')
129    schema_method_mock.assert_not_called()
130    assert not is_error
131    assert changed
132    assert response['status'] == 'success'
133    assert response['http_status'] == 200
134
135
136def test_system_proxy_arp_deletion_fails(mocker):
137    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
138
139    delete_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500}
140    delete_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.delete', return_value=delete_method_result)
141
142    input_data = {
143        'username': 'admin',
144        'state': 'absent',
145        'system_proxy_arp': {
146            'end_ip': 'test_value_3',
147            'id': '4',
148            'interface': 'test_value_5',
149            'ip': 'test_value_6'
150        },
151        'vdom': 'root'}
152
153    is_error, changed, response = fortios_system_proxy_arp.fortios_system(input_data, fos_instance)
154
155    delete_method_mock.assert_called_with('system', 'proxy-arp', mkey=ANY, vdom='root')
156    schema_method_mock.assert_not_called()
157    assert is_error
158    assert not changed
159    assert response['status'] == 'error'
160    assert response['http_status'] == 500
161
162
163def test_system_proxy_arp_idempotent(mocker):
164    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
165
166    set_method_result = {'status': 'error', 'http_method': 'DELETE', 'http_status': 404}
167    set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result)
168
169    input_data = {
170        'username': 'admin',
171        'state': 'present',
172        'system_proxy_arp': {
173            'end_ip': 'test_value_3',
174            'id': '4',
175            'interface': 'test_value_5',
176            'ip': 'test_value_6'
177        },
178        'vdom': 'root'}
179
180    is_error, changed, response = fortios_system_proxy_arp.fortios_system(input_data, fos_instance)
181
182    expected_data = {
183        'end-ip': 'test_value_3',
184        'id': '4',
185        'interface': 'test_value_5',
186        'ip': 'test_value_6'
187    }
188
189    set_method_mock.assert_called_with('system', 'proxy-arp', data=expected_data, vdom='root')
190    schema_method_mock.assert_not_called()
191    assert not is_error
192    assert not changed
193    assert response['status'] == 'error'
194    assert response['http_status'] == 404
195
196
197def test_system_proxy_arp_filter_foreign_attributes(mocker):
198    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
199
200    set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200}
201    set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result)
202
203    input_data = {
204        'username': 'admin',
205        'state': 'present',
206        'system_proxy_arp': {
207            'random_attribute_not_valid': 'tag',
208            'end_ip': 'test_value_3',
209            'id': '4',
210            'interface': 'test_value_5',
211            'ip': 'test_value_6'
212        },
213        'vdom': 'root'}
214
215    is_error, changed, response = fortios_system_proxy_arp.fortios_system(input_data, fos_instance)
216
217    expected_data = {
218        'end-ip': 'test_value_3',
219        'id': '4',
220        'interface': 'test_value_5',
221        'ip': 'test_value_6'
222    }
223
224    set_method_mock.assert_called_with('system', 'proxy-arp', data=expected_data, vdom='root')
225    schema_method_mock.assert_not_called()
226    assert not is_error
227    assert changed
228    assert response['status'] == 'success'
229    assert response['http_status'] == 200
230