summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Callaghan <dcallagh@redhat.com>2017-10-20 17:33:35 +1000
committerDan Callaghan <dcallagh@redhat.com>2017-10-20 17:34:46 +1000
commit5b999ca043a41e135c6f876a1f4728ef6926f582 (patch)
tree3b00817f6889e2ddb44532e5cb44d417274df530
parent5807bc1a3cb21e7daefae5f180eb56bbe66cb3f6 (diff)
flush session before returning from PATCH /users/<username>
This ensures the queries in user_full_json() to populate the various resource counts are properly updated to the changes just made (particularly when the change was to remove the user and clean up all their resources). Change-Id: Ic2be8a7266adf40e56d9307ece0a67dada7317a4
-rw-r--r--IntegrationTests/src/bkr/inttest/server/selenium/test_users.py35
-rw-r--r--Server/bkr/server/user.py1
2 files changed, 36 insertions, 0 deletions
diff --git a/IntegrationTests/src/bkr/inttest/server/selenium/test_users.py b/IntegrationTests/src/bkr/inttest/server/selenium/test_users.py
index 9bc43f6..c3ff29f 100644
--- a/IntegrationTests/src/bkr/inttest/server/selenium/test_users.py
+++ b/IntegrationTests/src/bkr/inttest/server/selenium/test_users.py
@@ -584,6 +584,41 @@ class UserHTTPTest(DatabaseTestCase):
self.assertEqual(group.activity[-1].action, u'Removed')
self.assertEqual(group.activity[-1].old_value, user.user_name)
+ def test_user_resource_counts_are_accurate_when_removing(self):
+ with session.begin():
+ user = data_setup.create_user()
+ job = data_setup.create_job(owner=user)
+ data_setup.mark_job_running(job)
+ owned_system = data_setup.create_system(owner=user)
+ loaned_system = data_setup.create_system()
+ loaned_system.loaned = user
+ owned_pool = data_setup.create_system_pool(owning_user=user)
+ group = data_setup.create_group(owner=user)
+ s = requests.Session()
+ requests_login(s)
+ response = s.get(get_server_base() + 'users/%s' % user.user_name,
+ headers={'Accept': 'application/json'})
+ response.raise_for_status()
+ self.assertEquals(response.json()['job_count'], 1)
+ self.assertEquals(response.json()['reservation_count'], 1)
+ self.assertEquals(response.json()['loan_count'], 1)
+ self.assertEquals(response.json()['owned_system_count'], 1)
+ self.assertEquals(response.json()['owned_pool_count'], 1)
+ response = patch_json(get_server_base() + 'users/%s' % user.user_name,
+ data={'removed': 'now'}, session=s)
+ response.raise_for_status()
+ # The bug was that the counts in the PATCH response would still show
+ # their old values, because the queries for producing the counts were
+ # being run before all the removals were flushed to the database.
+ # Note that job_count stays as 1, because the job has been cancelled
+ # but it will still be running until the next iteration of beakerd's
+ # update_dirty_jobs.
+ self.assertEquals(response.json()['job_count'], 1)
+ self.assertEquals(response.json()['reservation_count'], 0)
+ self.assertEquals(response.json()['loan_count'], 0)
+ self.assertEquals(response.json()['owned_system_count'], 0)
+ self.assertEquals(response.json()['owned_pool_count'], 0)
+
def test_unremove_account(self):
with session.begin():
user = data_setup.create_user()
diff --git a/Server/bkr/server/user.py b/Server/bkr/server/user.py
index 16a9639..0427660 100644
--- a/Server/bkr/server/user.py
+++ b/Server/bkr/server/user.py
@@ -349,6 +349,7 @@ def update_user(username):
_remove(user, method=u'HTTP')
else:
raise ValueError('"removed" value must be "now" or null')
+ session.flush()
response = jsonify(user_full_json(user))
if renamed:
response.headers.add('Location', absolute_url(user.href))