Projet

Général

Profil

0005-tests-simplify-user-synchronization-API-tests-67901.patch

Benjamin Dauvergne, 02 novembre 2022 14:22

Télécharger (6,31 ko)

Voir les différences:

Subject: [PATCH 5/7] tests: simplify user synchronization API tests (#67901)

 tests/api/test_user_synchronization.py | 120 +++++++++++++------------
 1 file changed, 65 insertions(+), 55 deletions(-)
tests/api/test_user_synchronization.py
18 18
import random
19 19
import uuid
20 20

  
21
import pytest
21 22
from django.contrib.contenttypes.models import ContentType
22 23
from django.urls import reverse
23 24

  
24 25
from authentic2.a2_rbac.models import SEARCH_OP, Role
25
from authentic2.a2_rbac.utils import get_default_ou
26 26
from authentic2.apps.journal.models import Event, EventType
27 27
from authentic2.custom_user.models import User
28 28

  
29
from ..utils import basic_authorization_header
29
URL = '/api/users/synchronization/'
30 30

  
31 31

  
32
def test_basic(app, simple_user):
33
    headers = basic_authorization_header(simple_user)
34
    uuids = []
35
    for _ in range(100):
36
        user = User.objects.create(first_name='ben', last_name='dauve')
37
        uuids.append(user.uuid)
38
    unknown_uuids = [uuid.uuid4().hex for i in range(100)]
39
    url = reverse('a2-api-users-synchronization')
32
@pytest.fixture
33
def user(simple_user):
34
    role = Role.objects.get_admin_role(
35
        ContentType.objects.get_for_model(User), name='role', slug='role', operation=SEARCH_OP
36
    )
37
    role.members.add(simple_user)
38
    return simple_user
39

  
40

  
41
@pytest.fixture
42
def app(app, user):
43
    app.set_authorization(('Basic', (user.username, user.username)))
44
    return app
45

  
46

  
47
@pytest.fixture
48
def users(db):
49
    return [User.objects.create(first_name='john', last_name='doe') for _ in range(10)]
50

  
51

  
52
@pytest.fixture
53
def uuids(users):
54
    return [user.uuid for user in users]
55

  
56

  
57
def test_url(app, simple_user):
58
    # URL is publikc api, check it
59
    assert URL == reverse('a2-api-users-synchronization')
60

  
61

  
62
def test_authentication_required(app):
63
    app.set_authorization(None)
64
    app.post_json(URL, status=401)
65

  
66

  
67
def test_permission_required(app, user):
68
    user.roles.clear()
69
    app.post_json(URL, status=403)
70

  
71

  
72
@pytest.fixture(scope='session')
73
def unknown_uuids():
74
    return [uuid.uuid4().hex for i in range(10)]
75

  
76

  
77
@pytest.fixture
78
def payload(uuids, unknown_uuids):
40 79
    content = {
41 80
        'known_uuids': uuids + unknown_uuids,
42 81
    }
43 82
    random.shuffle(content['known_uuids'])
83
    return content
44 84

  
45
    # test permission check
46
    response = app.post_json(url, params=content, headers=headers, status=403)
47
    r = Role.objects.get_admin_role(
48
        ContentType.objects.get_for_model(User), name='role', slug='role', operation=SEARCH_OP
49
    )
50
    r.members.add(simple_user)
51
    response = app.post_json(url, params=content, headers=headers)
85

  
86
def test_basic(app, payload, unknown_uuids):
87
    response = app.post_json(URL, params=payload)
52 88
    assert response.json['result'] == 1
53 89
    assert set(response.json['unknown_uuids']) == set(unknown_uuids)
54 90

  
55 91

  
56
def test_full_known_users(app, admin):
57
    headers = basic_authorization_header(admin)
58
    uuids = []
59
    for _ in range(100):
60
        user = User.objects.create(first_name='jim', last_name='jam')
61
        uuids.append(user.uuid)
62
    unknown_uuids = [uuid.uuid4().hex for i in range(100)]
63
    url = reverse('a2-api-users-synchronization')
64
    content = {
65
        'known_uuids': uuids + unknown_uuids,
66
        'full_known_users': 1,
67
    }
68
    random.shuffle(content['known_uuids'])
69
    response = app.post_json(url, params=content, headers=headers)
92
def test_full_known_users(app, payload):
93
    payload['full_known_users'] = 1
94
    response = app.post_json(URL, params=payload)
70 95
    assert response.json['result'] == 1
71 96

  
72 97
    # known users returned as part of api's full mode:
73
    assert len(response.json['known_users']) == 100
98
    assert len(response.json['known_users']) == 10
74 99
    for user_dict in response.json['known_users']:
75
        assert user_dict['first_name'] == 'jim'
76
        assert user_dict['last_name'] == 'jam'
100
        assert user_dict['first_name'] == 'john'
101
        assert user_dict['last_name'] == 'doe'
77 102
        assert {
78 103
            'uuid',
79 104
            'email',
......
87 112
        }.issubset(set(user_dict.keys()))
88 113

  
89 114

  
90
def test_timestamp(app, admin):
91
    headers = basic_authorization_header(admin)
92
    url = reverse('a2-api-users-synchronization')
115
def test_timestamp(app, users):
93 116
    now = datetime.datetime.now()
94

  
95
    ou = get_default_ou()
96
    users = []
97

  
98
    for i in range(6):
99
        users.append(
100
            User.objects.create(
101
                first_name='john%s' % i,
102
                last_name='doe',
103
                username='user%s' % i,
104
                email='user%s' % i,
105
                ou=ou,
106
            )
107
        )
117
    users = users[:6]
108 118

  
109 119
    for i, event_name in enumerate(
110 120
        [
......
128 138
        'timestamp': (now - datetime.timedelta(days=3)).isoformat(),
129 139
    }
130 140

  
131
    response = app.post(url, params=content, headers=headers)
141
    response = app.post(URL, params=content)
132 142

  
133 143
    for user in users[:3]:
134 144
        assert user.uuid in response.json['modified_users_uuids']
......
142 152

  
143 153
    content['timestamp'] = (now - datetime.timedelta(days=7)).isoformat()
144 154

  
145
    response = app.post(url, params=content, headers=headers)
155
    response = app.post(URL, params=content)
146 156

  
147 157
    for user in users[:3]:
148 158
        assert user.uuid not in response.json['modified_users_uuids']
......
154 164
    for user in users[3:]:
155 165
        user.delete()
156 166

  
157
    response = app.post(url, params=content, headers=headers)
167
    response = app.post(URL, params=content)
158 168

  
159 169
    assert not response.json['modified_users_uuids']
160 170
    for user in users:
......
163 173
    for user in users[:3]:
164 174
        content['known_uuids'].remove(user.uuid)
165 175

  
166
    response = app.post(url, params=content, headers=headers)
176
    response = app.post(URL, params=content)
167 177

  
168 178
    assert not response.json['modified_users_uuids']
169 179
    assert len(response.json['unknown_uuids']) == 3
170
-