From 1795ac71c31b0553857b3310c8554f4732bfda87 Mon Sep 17 00:00:00 2001 From: Adrian Turjak Date: Tue, 16 Feb 2021 12:15:29 +1300 Subject: [PATCH] Fix an issue when username_is_email=False for Quotas The email function was incorrectly trying to get the user email when sending out a quota update email. This has been fixed, and a test added to confirm. Change-Id: Id76af3ff50f752764a0ea25281443104d44adeb1 --- adjutant/actions/v1/resources.py | 4 +- .../actions/v1/tests/test_resource_actions.py | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/adjutant/actions/v1/resources.py b/adjutant/actions/v1/resources.py index 4652cbb..9655abe 100644 --- a/adjutant/actions/v1/resources.py +++ b/adjutant/actions/v1/resources.py @@ -299,8 +299,8 @@ class UpdateProjectQuotasAction(BaseAction, QuotaMixin): return self.action.task.keystone_user["username"] else: id_manager = user_store.IdentityManager() - user = id_manager.users.get(self.keystone_user["user_id"]) - email = user.email + user = id_manager.get_user(self.action.task.keystone_user["user_id"]) + email = getattr(user, "email", None) if email: return email diff --git a/adjutant/actions/v1/tests/test_resource_actions.py b/adjutant/actions/v1/tests/test_resource_actions.py index 94b2696..4aa5621 100644 --- a/adjutant/actions/v1/tests/test_resource_actions.py +++ b/adjutant/actions/v1/tests/test_resource_actions.py @@ -24,6 +24,8 @@ from adjutant.actions.v1.resources import ( ) from adjutant.api.models import Task from adjutant.common.tests.fake_clients import ( + FakeProject, + FakeUser, FakeManager, setup_identity_cache, get_fake_neutron, @@ -811,3 +813,57 @@ class QuotaActionTests(AdjutantTestCase): # Still set to default self.assertEqual(octaviaquota["load_balancer"], 1) self.assertEqual(trove_quota["instances"], 3) + + @conf_utils.modify_conf( + CONF, + operations={ + "adjutant.identity.username_is_email": [ + { + "operation": "override", + "value": False, + } + ] + }, + ) + def test_update_quota_username_not_email(self): + """ + Test that the action correctly handles its username is not email case. + """ + + project = FakeProject(name="test_project") + user = FakeUser( + name="test", + password="123", + email="test@example.com", + ) + setup_identity_cache(projects=[project], users=[user]) + setup_mock_caches("RegionOne", "test_project_id") + + task = Task.objects.create( + keystone_user={"user_id": user.id, "roles": ["admin"]} + ) + + data = { + "project_id": "test_project_id", + "size": "medium", + "regions": ["RegionOne"], + "user_id": user.id, + } + action = UpdateProjectQuotasAction(data, task=task, order=1) + + user_email = action.get_email() + self.assertEqual(user_email, user.email) + + # Now we reset the user/task/action and try without an email + user = FakeUser( + name="test", + password="123", + ) + setup_identity_cache(projects=[project], users=[user]) + task = Task.objects.create( + keystone_user={"user_id": user.id, "roles": ["admin"]} + ) + action = UpdateProjectQuotasAction(data, task=task, order=1) + + user_email = action.get_email() + self.assertEqual(user_email, None)