Files
ansible-collections-openstack/tests/unit/modules/cloud/openstack/test_identity_user.py
T
Maximilian Stinsky cfb7a39221 identity_user: support Keystone user resource options
Add an `options` parameter to the identity_user module, mapping 1:1 to
Keystone's user resource `options` field exposed by openstacksdk
(`User.options`). This allows setting options such as
`ignore_password_expiry` and `ignore_change_password_upon_first_use`.

On update, desired options are merged with the user's existing options
so that options not managed by the module are preserved, and the
operation is idempotent when the desired values already match.

Change-Id: I16cff99e86f0676a5c16121e8eca2b7a13ea9d6e
Signed-off-by: Maximilian Stinsky <maximilian.stinsky-damke@wiit.cloud>
2026-09-22 10:24:31 +02:00

171 lines
5.9 KiB
Python

import unittest
from unittest import mock
from ansible_collections.openstack.cloud.plugins.modules import identity_user
from ansible_collections.openstack.cloud.tests.unit.modules.utils import (
AnsibleExitJson,
ModuleTestCase,
set_module_args,
)
class FakeUser(dict):
'''A dict-like stand-in for an openstacksdk identity.v3.user.User.'''
def to_dict(self, computed=False):
return dict(self)
class FakeSDK(object):
class exceptions:
class OpenStackCloudException(Exception):
pass
class TestIdentityUser(ModuleTestCase):
def _run_module(self, module_args, identity):
set_module_args(module_args)
conn = mock.Mock()
conn.identity = identity
with mock.patch.object(
identity_user.IdentityUserModule,
'openstack_cloud_from_module',
return_value=(FakeSDK(), conn),
):
identity_user.main()
def _new_identity(self, existing_user=None):
identity = mock.Mock()
if existing_user is None:
identity.find_user.return_value = None
else:
identity.find_user.return_value = existing_user
identity.get_user.return_value = existing_user
return identity
# -- create -----------------------------------------------------------
def test_create_user_with_options(self):
identity = self._new_identity(existing_user=None)
identity.create_user.return_value = FakeUser(
id='1', name='demouser', is_enabled=True,
options={'ignore_password_expiry': True,
'ignore_change_password_upon_first_use': True},
)
with self.assertRaises(AnsibleExitJson) as ex:
self._run_module({
'name': 'demouser',
'options': {
'ignore_password_expiry': True,
'ignore_change_password_upon_first_use': True,
},
}, identity)
result = ex.exception.args[0]
self.assertTrue(result['changed'])
identity.create_user.assert_called_once()
_, kwargs = identity.create_user.call_args
self.assertEqual(
{'ignore_password_expiry': True,
'ignore_change_password_upon_first_use': True},
kwargs['options'])
identity.update_user.assert_not_called()
def test_create_user_without_options(self):
identity = self._new_identity(existing_user=None)
identity.create_user.return_value = FakeUser(
id='1', name='demouser', is_enabled=True, options={})
with self.assertRaises(AnsibleExitJson) as ex:
self._run_module({'name': 'demouser'}, identity)
result = ex.exception.args[0]
self.assertTrue(result['changed'])
identity.create_user.assert_called_once()
_, kwargs = identity.create_user.call_args
self.assertNotIn('options', kwargs)
# -- update -----------------------------------------------------------
def _existing_user(self, **options):
return FakeUser(
id='1', name='demouser', is_enabled=True, options=options)
def test_update_user_options_idempotent(self):
user = self._existing_user(
ignore_password_expiry=True,
ignore_change_password_upon_first_use=True,
)
identity = self._new_identity(existing_user=user)
with self.assertRaises(AnsibleExitJson) as ex:
self._run_module({
'name': 'demouser',
'options': {
'ignore_password_expiry': True,
'ignore_change_password_upon_first_use': True,
},
}, identity)
result = ex.exception.args[0]
self.assertFalse(result['changed'])
identity.update_user.assert_not_called()
def test_update_user_options_change(self):
user = self._existing_user(ignore_password_expiry=False)
identity = self._new_identity(existing_user=user)
identity.update_user.return_value = FakeUser(
id='1', name='demouser', is_enabled=True,
options={'ignore_password_expiry': True})
with self.assertRaises(AnsibleExitJson) as ex:
self._run_module({
'name': 'demouser',
'options': {'ignore_password_expiry': True},
}, identity)
result = ex.exception.args[0]
self.assertTrue(result['changed'])
identity.update_user.assert_called_once_with(
'1', options={'ignore_password_expiry': True})
def test_update_user_options_merge_preserves_existing(self):
user = self._existing_user(
lock_password=True,
ignore_password_expiry=False,
)
identity = self._new_identity(existing_user=user)
identity.update_user.return_value = FakeUser(
id='1', name='demouser', is_enabled=True,
options={'lock_password': True, 'ignore_password_expiry': True})
with self.assertRaises(AnsibleExitJson) as ex:
self._run_module({
'name': 'demouser',
'options': {'ignore_password_expiry': True},
}, identity)
result = ex.exception.args[0]
self.assertTrue(result['changed'])
identity.update_user.assert_called_once_with(
'1',
options={'lock_password': True, 'ignore_password_expiry': True})
def test_update_user_without_options_preserves_existing(self):
# When no options are specified, existing options must not be touched.
user = self._existing_user(ignore_password_expiry=True)
identity = self._new_identity(existing_user=user)
with self.assertRaises(AnsibleExitJson) as ex:
self._run_module({'name': 'demouser'}, identity)
result = ex.exception.args[0]
self.assertFalse(result['changed'])
identity.update_user.assert_not_called()
if __name__ == '__main__':
unittest.main()