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_user_device_category 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_user_device_category.Connection') 35 return connection_class_mock 36 37 38fos_instance = FortiOSHandler(connection_mock) 39 40 41def test_user_device_category_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 'user_device_category': { 51 'comment': 'Comment.', 52 'desc': 'test_value_4', 53 'name': 'default_name_5' 54 }, 55 'vdom': 'root'} 56 57 is_error, changed, response = fortios_user_device_category.fortios_user(input_data, fos_instance) 58 59 expected_data = { 60 'comment': 'Comment.', 61 'desc': 'test_value_4', 62 'name': 'default_name_5' 63 } 64 65 set_method_mock.assert_called_with('user', 'device-category', 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_user_device_category_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 'user_device_category': { 83 'comment': 'Comment.', 84 'desc': 'test_value_4', 85 'name': 'default_name_5' 86 }, 87 'vdom': 'root'} 88 89 is_error, changed, response = fortios_user_device_category.fortios_user(input_data, fos_instance) 90 91 expected_data = { 92 'comment': 'Comment.', 93 'desc': 'test_value_4', 94 'name': 'default_name_5' 95 } 96 97 set_method_mock.assert_called_with('user', 'device-category', 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_user_device_category_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 'user_device_category': { 115 'comment': 'Comment.', 116 'desc': 'test_value_4', 117 'name': 'default_name_5' 118 }, 119 'vdom': 'root'} 120 121 is_error, changed, response = fortios_user_device_category.fortios_user(input_data, fos_instance) 122 123 delete_method_mock.assert_called_with('user', 'device-category', 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_user_device_category_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 'user_device_category': { 141 'comment': 'Comment.', 142 'desc': 'test_value_4', 143 'name': 'default_name_5' 144 }, 145 'vdom': 'root'} 146 147 is_error, changed, response = fortios_user_device_category.fortios_user(input_data, fos_instance) 148 149 delete_method_mock.assert_called_with('user', 'device-category', 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_user_device_category_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 'user_device_category': { 167 'comment': 'Comment.', 168 'desc': 'test_value_4', 169 'name': 'default_name_5' 170 }, 171 'vdom': 'root'} 172 173 is_error, changed, response = fortios_user_device_category.fortios_user(input_data, fos_instance) 174 175 expected_data = { 176 'comment': 'Comment.', 177 'desc': 'test_value_4', 178 'name': 'default_name_5' 179 } 180 181 set_method_mock.assert_called_with('user', 'device-category', 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_user_device_category_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 'user_device_category': { 199 'random_attribute_not_valid': 'tag', 200 'comment': 'Comment.', 201 'desc': 'test_value_4', 202 'name': 'default_name_5' 203 }, 204 'vdom': 'root'} 205 206 is_error, changed, response = fortios_user_device_category.fortios_user(input_data, fos_instance) 207 208 expected_data = { 209 'comment': 'Comment.', 210 'desc': 'test_value_4', 211 'name': 'default_name_5' 212 } 213 214 set_method_mock.assert_called_with('user', 'device-category', 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