Projet

Général

Profil

0004-tests_rbac-add-randomized-tests-on-role-parenting-57.patch

Benjamin Dauvergne, 01 février 2022 00:48

Télécharger (2,36 ko)

Voir les différences:

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(-)
tests_rbac/test_rbac.py
234 234
            assert member.direct == [r1]
235 235
        if member == u2:
236 236
            assert member.direct == []
237

  
238

  
239
def test_random_role_parenting(db):
240
    import random
241

  
242
    import numpy as np
243

  
244
    c = 15
245
    roles = [Role.objects.create(id=i, name=f'role{i}') for i in range(c)]
246
    m = [[False] * c for i in range(c)]
247
    m = np.zeros((c, c), dtype=bool)
248

  
249
    def check(i):
250
        one = np.identity(c, dtype=bool)
251
        z = m
252
        for i in range(c):
253
            new_z = np.matmul(z, m | one)
254
            if np.array_equal(z, new_z):
255
                break
256
            z = new_z
257
        real = np.zeros((c, c), dtype=bool)
258
        for parent_id, child_id in RoleParenting.objects.values_list('parent_id', 'child_id'):
259
            real[parent_id][child_id] = True
260
        assert np.array_equal(real, z & ~one)
261

  
262
    from time import time
263

  
264
    for i in range(2 * c * c):
265
        a = random.randint(0, c - 1)
266
        b = random.randint(0, c - 1)
267
        if a == b:
268
            continue
269
        t = time()
270
        if random.randint(0, 10) < 8:
271
            print(f'add {a} <- {b}')
272
            roles[a].add_child(roles[b])
273
            m[a][b] = True
274
        else:
275
            print(f'remove {a} <- {b}')
276
            roles[a].remove_child(roles[b])
277
            m[a][b] = False
278
        print('duration', time() - t)
279
        check(i)
tox.ini
73 73
  enum34<=1.1.6
74 74
  ldaptools>=0.15
75 75
  oldldap: python-ldap<3
76
  rbac: numpy
76 77
  py2: django-appconf<1.0.4
77 78
  py2: django-filter<2
78 79
  py3: django-filter
......
169 170
dynamic_context = test_function
170 171

  
171 172
[coverage:html]
172
show_contexts = True
173
show_contexts = True
173
-