Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_pritunl_user_info.py

raw
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_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
10 AnsibleExitJson,
11 AnsibleFailJson,
12 ModuleTestCase,
13 set_module_args,
14 )
15
16 from ansible_collections.community.general.plugins.modules import (
17 pritunl_user_info,
18 )
19 from ansible_collections.community.general.tests.unit.plugins.module_utils.test__pritunl_api import (
20 PritunlListOrganizationMock,
21 PritunlListUserMock,
22 )
23
24
25 class TestPritunlUserInfo(ModuleTestCase):
26 def setUp(self):
27 super().setUp()
28 self.module = pritunl_user_info
29
30 def tearDown(self):
31 super().tearDown()
32
33 def patch_get_pritunl_users(self, **kwds):
34 return patch(
35 "ansible_collections.community.general.plugins.module_utils._pritunl_api._get_pritunl_users",
36 autospec=True,
37 **kwds,
38 )
39
40 def patch_get_pritunl_organizations(self, **kwds):
41 return patch(
42 "ansible_collections.community.general.plugins.module_utils._pritunl_api._get_pritunl_organizations",
43 autospec=True,
44 **kwds,
45 )
46
47 def test_without_parameters(self):
48 """Test without parameters"""
49 with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock) as org_mock:
50 with self.patch_get_pritunl_users(side_effect=PritunlListUserMock) as user_mock:
51 with set_module_args({}):
52 with self.assertRaises(AnsibleFailJson):
53 self.module.main()
54
55 self.assertEqual(org_mock.call_count, 0)
56 self.assertEqual(user_mock.call_count, 0)
57
58 def test_missing_organization(self):
59 """Failure must occur when the requested organization is not found."""
60 with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock) as org_mock:
61 with self.patch_get_pritunl_users(side_effect=PritunlListUserMock) as user_mock:
62 with self.assertRaises(AnsibleFailJson) as result:
63 with set_module_args(
64 {
65 "pritunl_api_token": "token",
66 "pritunl_api_secret": "secret",
67 "pritunl_url": "https://pritunl.domain.com",
68 "organization": "Unknown",
69 }
70 ):
71 self.module.main()
72
73 self.assertEqual(org_mock.call_count, 1)
74 self.assertEqual(user_mock.call_count, 0)
75
76 exc = result.exception.args[0]
77 self.assertRegex(exc["msg"], "Can not list users from the organization")
78
79 def test_get_all_client_users_from_organization(self):
80 """
81 The list of all Pritunl client users from the organization must be returned when no user specified.
82 """
83 expected_user_type = "client"
84 with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock) as org_mock:
85 with self.patch_get_pritunl_users(side_effect=PritunlListUserMock) as user_mock:
86 with self.assertRaises(AnsibleExitJson) as result:
87 with set_module_args(
88 {
89 "pritunl_api_token": "token",
90 "pritunl_api_secret": "secret",
91 "pritunl_url": "https://pritunl.domain.com",
92 "organization": "GumGum",
93 }
94 ):
95 self.module.main()
96
97 self.assertEqual(org_mock.call_count, 1)
98 self.assertEqual(user_mock.call_count, 1)
99
100 exc = result.exception.args[0]
101 # module should not report changes
102 self.assertFalse(exc["changed"])
103 # user_type when not provided is set client and should only return client user type
104 self.assertEqual(len(exc["users"]), 1)
105 for user in exc["users"]:
106 self.assertEqual(user["type"], expected_user_type)
107
108 def test_get_specific_server_user_from_organization(self):
109 """
110 Retrieving a specific user from the organization must return a single record.
111 """
112 expected_user_type = "server"
113 expected_user_name = "ops"
114 with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock) as org_mock:
115 with self.patch_get_pritunl_users(side_effect=PritunlListUserMock) as user_mock:
116 with self.assertRaises(AnsibleExitJson) as result:
117 with set_module_args(
118 {
119 "pritunl_api_token": "token",
120 "pritunl_api_secret": "secret",
121 "pritunl_url": "https://pritunl.domain.com",
122 "organization": "GumGum",
123 "user_name": expected_user_name,
124 "user_type": expected_user_type,
125 }
126 ):
127 self.module.main()
128
129 self.assertEqual(org_mock.call_count, 1)
130 self.assertEqual(user_mock.call_count, 1)
131
132 exc = result.exception.args[0]
133 # module should not report changes
134 self.assertFalse(exc["changed"])
135 self.assertEqual(len(exc["users"]), 1)
136 for user in exc["users"]:
137 self.assertEqual(user["type"], expected_user_type)
138 self.assertEqual(user["name"], expected_user_name)