From 27db90531aa63f3972af9cd6dd74b4ed1b67f870 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sun, 30 Jan 2022 19:58:57 +0100 Subject: [PATCH 4/6] tests_rbac: add randomized tests on role parenting (#57500) --- tests_rbac/test_rbac.py | 43 +++++++++++++++++++++++++++++++++++++++++ tox.ini | 3 ++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tests_rbac/test_rbac.py b/tests_rbac/test_rbac.py index d07a3a73..44fb189e 100644 --- a/tests_rbac/test_rbac.py +++ b/tests_rbac/test_rbac.py @@ -234,3 +234,46 @@ def test_all_members(db): assert member.direct == [r1] if member == u2: assert member.direct == [] + + +def test_random_role_parenting(db): + import random + + import numpy as np + + c = 15 + roles = [Role.objects.create(id=i, name=f'role{i}') for i in range(c)] + m = [[False] * c for i in range(c)] + m = np.zeros((c, c), dtype=bool) + + def check(i): + one = np.identity(c, dtype=bool) + z = m + for i in range(c): + new_z = np.matmul(z, m | one) + if np.array_equal(z, new_z): + break + z = new_z + real = np.zeros((c, c), dtype=bool) + for parent_id, child_id in RoleParenting.objects.values_list('parent_id', 'child_id'): + real[parent_id][child_id] = True + assert np.array_equal(real, z & ~one) + + from time import time + + for i in range(2 * c * c): + a = random.randint(0, c - 1) + b = random.randint(0, c - 1) + if a == b: + continue + t = time() + if random.randint(0, 10) < 8: + print(f'add {a} <- {b}') + roles[a].add_child(roles[b]) + m[a][b] = True + else: + print(f'remove {a} <- {b}') + roles[a].remove_child(roles[b]) + m[a][b] = False + print('duration', time() - t) + check(i) diff --git a/tox.ini b/tox.ini index 59966ceb..66cafc4d 100644 --- a/tox.ini +++ b/tox.ini @@ -73,6 +73,7 @@ deps = enum34<=1.1.6 ldaptools>=0.15 oldldap: python-ldap<3 + rbac: numpy py2: django-appconf<1.0.4 py2: django-filter<2 py3: django-filter @@ -169,4 +170,4 @@ branch = True dynamic_context = test_function [coverage:html] -show_contexts = True \ No newline at end of file +show_contexts = True -- 2.34.1