| 1 |
# Copyright (c) 2021 Florian Dambrine <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
from ansible.module_utils.common.dict_transformations import dict_merge |
| 10 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 11 |
AnsibleExitJson, |
| 12 |
AnsibleFailJson, |
| 13 |
ModuleTestCase, |
| 14 |
set_module_args, |
| 15 |
) |
| 16 |
|
| 17 |
from ansible_collections.community.general.plugins.modules import ( |
| 18 |
pritunl_org, |
| 19 |
) |
| 20 |
from ansible_collections.community.general.tests.unit.plugins.module_utils.test__pritunl_api import ( |
| 21 |
PritunlDeleteOrganizationMock, |
| 22 |
PritunlListOrganizationAfterPostMock, |
| 23 |
PritunlListOrganizationMock, |
| 24 |
PritunlPostOrganizationMock, |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
class TestPritunlOrg(ModuleTestCase): |
| 29 |
def setUp(self): |
| 30 |
super().setUp() |
| 31 |
self.module = pritunl_org |
| 32 |
|
| 33 |
def tearDown(self): |
| 34 |
super().tearDown() |
| 35 |
|
| 36 |
def patch_add_pritunl_organization(self, **kwds): |
| 37 |
return patch( |
| 38 |
"ansible_collections.community.general.plugins.module_utils._pritunl_api._post_pritunl_organization", |
| 39 |
autospec=True, |
| 40 |
**kwds, |
| 41 |
) |
| 42 |
|
| 43 |
def patch_delete_pritunl_organization(self, **kwds): |
| 44 |
return patch( |
| 45 |
"ansible_collections.community.general.plugins.module_utils._pritunl_api._delete_pritunl_organization", |
| 46 |
autospec=True, |
| 47 |
**kwds, |
| 48 |
) |
| 49 |
|
| 50 |
def patch_get_pritunl_organizations(self, **kwds): |
| 51 |
return patch( |
| 52 |
"ansible_collections.community.general.plugins.module_utils._pritunl_api._get_pritunl_organizations", |
| 53 |
autospec=True, |
| 54 |
**kwds, |
| 55 |
) |
| 56 |
|
| 57 |
def test_without_parameters(self): |
| 58 |
"""Test without parameters""" |
| 59 |
with set_module_args({}): |
| 60 |
with self.assertRaises(AnsibleFailJson): |
| 61 |
self.module.main() |
| 62 |
|
| 63 |
def test_present(self): |
| 64 |
"""Test Pritunl organization creation.""" |
| 65 |
org_params = {"name": "NewOrg"} |
| 66 |
with set_module_args( |
| 67 |
dict_merge( |
| 68 |
{ |
| 69 |
"pritunl_api_token": "token", |
| 70 |
"pritunl_api_secret": "secret", |
| 71 |
"pritunl_url": "https://pritunl.domain.com", |
| 72 |
}, |
| 73 |
org_params, |
| 74 |
) |
| 75 |
): |
| 76 |
# Test creation |
| 77 |
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock): |
| 78 |
with self.patch_add_pritunl_organization(side_effect=PritunlPostOrganizationMock): |
| 79 |
with self.assertRaises(AnsibleExitJson) as create_result: |
| 80 |
self.module.main() |
| 81 |
|
| 82 |
create_exc = create_result.exception.args[0] |
| 83 |
|
| 84 |
self.assertTrue(create_exc["changed"]) |
| 85 |
self.assertEqual(create_exc["response"]["name"], org_params["name"]) |
| 86 |
self.assertEqual(create_exc["response"]["user_count"], 0) |
| 87 |
|
| 88 |
# Test module idempotency |
| 89 |
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationAfterPostMock): |
| 90 |
with self.patch_add_pritunl_organization(side_effect=PritunlPostOrganizationMock): |
| 91 |
with self.assertRaises(AnsibleExitJson) as idempotent_result: |
| 92 |
self.module.main() |
| 93 |
|
| 94 |
idempotent_exc = idempotent_result.exception.args[0] |
| 95 |
|
| 96 |
# Ensure both calls resulted in the same returned value |
| 97 |
# except for changed which should be false the second time |
| 98 |
for k, v in idempotent_exc.items(): |
| 99 |
if k == "changed": |
| 100 |
self.assertFalse(v) |
| 101 |
else: |
| 102 |
self.assertEqual(create_exc[k], v) |
| 103 |
|
| 104 |
def test_absent(self): |
| 105 |
"""Test organization removal from Pritunl.""" |
| 106 |
org_params = {"name": "NewOrg"} |
| 107 |
with set_module_args( |
| 108 |
dict_merge( |
| 109 |
{ |
| 110 |
"state": "absent", |
| 111 |
"pritunl_api_token": "token", |
| 112 |
"pritunl_api_secret": "secret", |
| 113 |
"pritunl_url": "https://pritunl.domain.com", |
| 114 |
}, |
| 115 |
org_params, |
| 116 |
) |
| 117 |
): |
| 118 |
# Test deletion |
| 119 |
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationAfterPostMock): |
| 120 |
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock): |
| 121 |
with self.assertRaises(AnsibleExitJson) as delete_result: |
| 122 |
self.module.main() |
| 123 |
|
| 124 |
delete_exc = delete_result.exception.args[0] |
| 125 |
|
| 126 |
self.assertTrue(delete_exc["changed"]) |
| 127 |
self.assertEqual(delete_exc["response"], {}) |
| 128 |
|
| 129 |
# Test module idempotency |
| 130 |
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock): |
| 131 |
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock): |
| 132 |
with self.assertRaises(AnsibleExitJson) as idempotent_result: |
| 133 |
self.module.main() |
| 134 |
|
| 135 |
idempotent_exc = idempotent_result.exception.args[0] |
| 136 |
|
| 137 |
# Ensure both calls resulted in the same returned value |
| 138 |
# except for changed which should be false the second time |
| 139 |
self.assertFalse(idempotent_exc["changed"]) |
| 140 |
self.assertEqual(idempotent_exc["response"], delete_exc["response"]) |
| 141 |
|
| 142 |
def test_absent_with_existing_users(self): |
| 143 |
"""Test organization removal with attached users should fail except if force is true.""" |
| 144 |
module_args = { |
| 145 |
"state": "absent", |
| 146 |
"pritunl_api_token": "token", |
| 147 |
"pritunl_api_secret": "secret", |
| 148 |
"pritunl_url": "https://pritunl.domain.com", |
| 149 |
"name": "GumGum", |
| 150 |
} |
| 151 |
with set_module_args(module_args): |
| 152 |
# Test deletion |
| 153 |
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock): |
| 154 |
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock): |
| 155 |
with self.assertRaises(AnsibleFailJson) as failure_result: |
| 156 |
self.module.main() |
| 157 |
|
| 158 |
failure_exc = failure_result.exception.args[0] |
| 159 |
|
| 160 |
self.assertRegex(failure_exc["msg"], "Can not remove organization") |
| 161 |
|
| 162 |
# Switch force=True which should run successfully |
| 163 |
with set_module_args(dict_merge(module_args, {"force": True})): |
| 164 |
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock): |
| 165 |
with self.patch_delete_pritunl_organization(side_effect=PritunlDeleteOrganizationMock): |
| 166 |
with self.assertRaises(AnsibleExitJson) as delete_result: |
| 167 |
self.module.main() |
| 168 |
|
| 169 |
delete_exc = delete_result.exception.args[0] |
| 170 |
|
| 171 |
self.assertTrue(delete_exc["changed"]) |