Browse Source

Fix users in group and groups for user exact filters

List users in a group by name and list groups for user by name
throws HTTP 500 error in case of exact filters because filter_by
query used in case of exact filter is not able to filter using
'name' attribute.
This patch fixes this issue by replacing filter_by with filter query
in case of exact_filter similar to the inexact_filter query.

Closes-Bug: 1521772
Change-Id: Ib44513540370c0701a439f11a49e0dc338601fae
tags/9.0.0.0b2
Ankit Agrawal 3 years ago
parent
commit
ee2859a61a

+ 13
- 14
keystone/common/sql/core.py View File

@@ -290,42 +290,41 @@ def _filter(model, query, hints):
290 290
         satisfied_filters.append(filter_)
291 291
         return query.filter(query_term)
292 292
 
293
-    def exact_filter(model, filter_, cumulative_filter_dict):
293
+    def exact_filter(model, query, filter_, satisfied_filters):
294 294
         """Applies an exact filter to a query.
295 295
 
296 296
         :param model: the table model in question
297
+        :param query: query to apply filters to
297 298
         :param dict filter_: describes this filter
298
-        :param dict cumulative_filter_dict: describes the set of exact filters
299
-                                            built up so far
300
-
299
+        :param list satisfied_filters: filter_ will be added if it is
300
+                                       satisfied.
301
+        :returns query: query updated to add any exact filters we could
302
+                        satisfy
301 303
         """
302 304
         key = filter_['name']
303 305
 
304 306
         col = getattr(model, key)
305 307
         if isinstance(col.property.columns[0].type, sql.types.Boolean):
306
-            cumulative_filter_dict[key] = (
307
-                utils.attr_as_boolean(filter_['value']))
308
+            filter_val = utils.attr_as_boolean(filter_['value'])
308 309
         else:
309 310
             _WontMatch.check(filter_['value'], col)
310
-            cumulative_filter_dict[key] = filter_['value']
311
+            filter_val = filter_['value']
312
+
313
+        satisfied_filters.append(filter_)
314
+        return query.filter(col == filter_val)
311 315
 
312 316
     try:
313
-        filter_dict = {}
314 317
         satisfied_filters = []
315 318
         for filter_ in hints.filters:
316 319
             if filter_['name'] not in model.attributes:
317 320
                 continue
318 321
             if filter_['comparator'] == 'equals':
319
-                exact_filter(model, filter_, filter_dict)
320
-                satisfied_filters.append(filter_)
322
+                query = exact_filter(model, query, filter_,
323
+                                     satisfied_filters)
321 324
             else:
322 325
                 query = inexact_filter(model, query, filter_,
323 326
                                        satisfied_filters)
324 327
 
325
-        # Apply any exact filters we built up
326
-        if filter_dict:
327
-            query = query.filter_by(**filter_dict)
328
-
329 328
         # Remove satisfied filters, then the caller will know remaining filters
330 329
         for filter_ in satisfied_filters:
331 330
             hints.filters.remove(filter_)

+ 0
- 2
keystone/tests/unit/test_backend.py View File

@@ -6512,7 +6512,6 @@ class FilterTests(filtering.FilterTests):
6512 6512
         self._delete_test_data('user', user_list)
6513 6513
         self._delete_test_data('group', group_list)
6514 6514
 
6515
-    @test_utils.wip('Waiting on bug #1521772 to be fixed')
6516 6515
     def test_groups_for_user_exact_filtered(self):
6517 6516
         """Test exact filters doesn't break groups_for_user listing."""
6518 6517
         group_list, user_list, hints = self._groups_for_user_data()
@@ -6589,7 +6588,6 @@ class FilterTests(filtering.FilterTests):
6589 6588
         self._delete_test_data('user', user_list)
6590 6589
         self._delete_entity('group')(group['id'])
6591 6590
 
6592
-    @test_utils.wip('Waiting on bug #1521772 to be fixed')
6593 6591
     def test_list_users_in_group_exact_filtered(self):
6594 6592
         user_list, group, hints = self._list_users_in_group_data()
6595 6593
         hints.add_filter('name', 'Arthur Rimbaud', comparator='equals')

+ 0
- 4
keystone/tests/unit/test_backend_ldap.py View File

@@ -3298,7 +3298,3 @@ class LdapFilterTests(test_backend.FilterTests, unit.TestCase):
3298 3298
         # The LDAP identity driver currently does not support filtering on the
3299 3299
         # listing users for a given group, so will fail this test.
3300 3300
         super(LdapFilterTests, self).test_list_users_in_group_exact_filtered()
3301
-
3302
-    @wip('Waiting on bug #1521772 to be fixed')
3303
-    def test_groups_for_user_exact_filtered(self):
3304
-        super(LdapFilterTests, self).test_groups_for_user_exact_filtered()

Loading…
Cancel
Save