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_webfilter_content
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_webfilter_content.Connection')
35    return connection_class_mock
36
37
38fos_instance = FortiOSHandler(connection_mock)
39
40
41def test_webfilter_content_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        'webfilter_content': {
51            'comment': 'Optional comments.',
52            'id': '4',
53            'name': 'default_name_5'
54        },
55        'vdom': 'root'}
56
57    is_error, changed, response = fortios_webfilter_content.fortios_webfilter(input_data, fos_instance)
58
59    expected_data = {
60        'comment': 'Optional comments.',
61        'id': '4',
62        'name': 'default_name_5'
63    }
64
65    set_method_mock.assert_called_with('webfilter', 'content', data=expected_data, vdom='root')
66    schema_method_mock.assert_not_called()
67    assert not is_error
68    assert changed
69    assert response['status'] == 'success'
70    assert response['http_status'] == 200
71
72
73def test_webfilter_content_creation_fails(mocker):
74    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
75
76    set_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500}
77    set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result)
78
79    input_data = {
80        'username': 'admin',
81        'state': 'present',
82        'webfilter_content': {
83            'comment': 'Optional comments.',
84            'id': '4',
85            'name': 'default_name_5'
86        },
87        'vdom': 'root'}
88
89    is_error, changed, response = fortios_webfilter_content.fortios_webfilter(input_data, fos_instance)
90
91    expected_data = {
92        'comment': 'Optional comments.',
93        'id': '4',
94        'name': 'default_name_5'
95    }
96
97    set_method_mock.assert_called_with('webfilter', 'content', data=expected_data, vdom='root')
98    schema_method_mock.assert_not_called()
99    assert is_error
100    assert not changed
101    assert response['status'] == 'error'
102    assert response['http_status'] == 500
103
104
105def test_webfilter_content_removal(mocker):
106    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
107
108    delete_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200}
109    delete_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.delete', return_value=delete_method_result)
110
111    input_data = {
112        'username': 'admin',
113        'state': 'absent',
114        'webfilter_content': {
115            'comment': 'Optional comments.',
116            'id': '4',
117            'name': 'default_name_5'
118        },
119        'vdom': 'root'}
120
121    is_error, changed, response = fortios_webfilter_content.fortios_webfilter(input_data, fos_instance)
122
123    delete_method_mock.assert_called_with('webfilter', 'content', mkey=ANY, vdom='root')
124    schema_method_mock.assert_not_called()
125    assert not is_error
126    assert changed
127    assert response['status'] == 'success'
128    assert response['http_status'] == 200
129
130
131def test_webfilter_content_deletion_fails(mocker):
132    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
133
134    delete_method_result = {'status': 'error', 'http_method': 'POST', 'http_status': 500}
135    delete_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.delete', return_value=delete_method_result)
136
137    input_data = {
138        'username': 'admin',
139        'state': 'absent',
140        'webfilter_content': {
141            'comment': 'Optional comments.',
142            'id': '4',
143            'name': 'default_name_5'
144        },
145        'vdom': 'root'}
146
147    is_error, changed, response = fortios_webfilter_content.fortios_webfilter(input_data, fos_instance)
148
149    delete_method_mock.assert_called_with('webfilter', 'content', mkey=ANY, vdom='root')
150    schema_method_mock.assert_not_called()
151    assert is_error
152    assert not changed
153    assert response['status'] == 'error'
154    assert response['http_status'] == 500
155
156
157def test_webfilter_content_idempotent(mocker):
158    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
159
160    set_method_result = {'status': 'error', 'http_method': 'DELETE', 'http_status': 404}
161    set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result)
162
163    input_data = {
164        'username': 'admin',
165        'state': 'present',
166        'webfilter_content': {
167            'comment': 'Optional comments.',
168            'id': '4',
169            'name': 'default_name_5'
170        },
171        'vdom': 'root'}
172
173    is_error, changed, response = fortios_webfilter_content.fortios_webfilter(input_data, fos_instance)
174
175    expected_data = {
176        'comment': 'Optional comments.',
177        'id': '4',
178        'name': 'default_name_5'
179    }
180
181    set_method_mock.assert_called_with('webfilter', 'content', data=expected_data, vdom='root')
182    schema_method_mock.assert_not_called()
183    assert not is_error
184    assert not changed
185    assert response['status'] == 'error'
186    assert response['http_status'] == 404
187
188
189def test_webfilter_content_filter_foreign_attributes(mocker):
190    schema_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.schema')
191
192    set_method_result = {'status': 'success', 'http_method': 'POST', 'http_status': 200}
193    set_method_mock = mocker.patch('ansible.module_utils.network.fortios.fortios.FortiOSHandler.set', return_value=set_method_result)
194
195    input_data = {
196        'username': 'admin',
197        'state': 'present',
198        'webfilter_content': {
199            'random_attribute_not_valid': 'tag',
200            'comment': 'Optional comments.',
201            'id': '4',
202            'name': 'default_name_5'
203        },
204        'vdom': 'root'}
205
206    is_error, changed, response = fortios_webfilter_content.fortios_webfilter(input_data, fos_instance)
207
208    expected_data = {
209        'comment': 'Optional comments.',
210        'id': '4',
211        'name': 'default_name_5'
212    }
213
214    set_method_mock.assert_called_with('webfilter', 'content', data=expected_data, vdom='root')
215    schema_method_mock.assert_not_called()
216    assert not is_error
217    assert changed
218    assert response['status'] == 'success'
219    assert response['http_status'] == 200
220