1# -*- coding: utf-8 -*-
2#
3# Copyright: (c) 2018, F5 Networks Inc.
4# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6from __future__ import (absolute_import, division, print_function)
7__metaclass__ = type
8
9import os
10import json
11import pytest
12import sys
13
14if sys.version_info < (2, 7):
15    pytestmark = pytest.mark.skip("F5 Ansible modules require Python >= 2.7")
16
17from ansible.module_utils.basic import AnsibleModule
18
19from ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_http import (
20    ApiParameters, ModuleParameters, ModuleManager, ArgumentSpec
21)
22from ansible_collections.f5networks.f5_modules.tests.unit.compat import unittest
23from ansible_collections.f5networks.f5_modules.tests.unit.compat.mock import Mock, patch
24from ansible_collections.f5networks.f5_modules.tests.unit.modules.utils import set_module_args
25
26
27fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
28fixture_data = {}
29
30
31def load_fixture(name):
32    path = os.path.join(fixture_path, name)
33
34    if path in fixture_data:
35        return fixture_data[path]
36
37    with open(path) as f:
38        data = f.read()
39
40    try:
41        data = json.loads(data)
42    except Exception:
43        pass
44
45    fixture_data[path] = data
46    return data
47
48
49class TestParameters(unittest.TestCase):
50    def test_module_parameters(self):
51        args = dict(
52            name='foo',
53            parent='bar',
54            description='This is a Test',
55            proxy_type='transparent',
56            insert_xforwarded_for=True,
57            redirect_rewrite='all',
58            encrypt_cookies=['FooCookie'],
59            encrypt_cookie_secret='12345'
60        )
61
62        p = ModuleParameters(params=args)
63        assert p.name == 'foo'
64        assert p.parent == '/Common/bar'
65        assert p.description == 'This is a Test'
66        assert p.proxy_type == 'transparent'
67        assert p.insert_xforwarded_for == 'enabled'
68        assert p.redirect_rewrite == 'all'
69        assert p.encrypt_cookies == ['FooCookie']
70        assert p.encrypt_cookie_secret == '12345'
71
72    def test_api_parameters(self):
73        args = load_fixture('load_ltm_http_profile_1.json')
74        p = ApiParameters(params=args)
75        assert p.name == 'http'
76        assert p.insert_xforwarded_for == 'disabled'
77        assert p.description == 'none'
78
79
80class TestManager(unittest.TestCase):
81    def setUp(self):
82        self.spec = ArgumentSpec()
83        self.p2 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_http.tmos_version')
84        self.p3 = patch('ansible_collections.f5networks.f5_modules.plugins.modules.bigip_profile_http.send_teem')
85        self.m2 = self.p2.start()
86        self.m2.return_value = '14.1.0'
87        self.m3 = self.p3.start()
88        self.m3.return_value = True
89
90    def tearDown(self):
91        self.p2.stop()
92        self.p3.stop()
93
94    def test_create(self, *args):
95        # Configure the arguments that would be sent to the Ansible module
96        set_module_args(dict(
97            name='foo',
98            insert_xforwarded_for='yes',
99            parent='bar',
100            provider=dict(
101                server='localhost',
102                password='password',
103                user='admin'
104            )
105        ))
106
107        module = AnsibleModule(
108            argument_spec=self.spec.argument_spec,
109            supports_check_mode=self.spec.supports_check_mode
110        )
111        mm = ModuleManager(module=module)
112
113        # Override methods to force specific logic in the module to happen
114        mm.exists = Mock(return_value=False)
115        mm.create_on_device = Mock(return_value=True)
116
117        results = mm.exec_module()
118
119        assert results['changed'] is True
120        assert results['insert_xforwarded_for'] == 'yes'
121