Projet

Général

Profil

0001-provisionning-count-db-queries-55043.patch

Emmanuel Cazenave, 22 juin 2021 17:16

Télécharger (6,8 ko)

Voir les différences:

Subject: [PATCH 1/4] provisionning: count db queries (#55043)

 tests_multitenant/test_hobo_notify.py | 139 ++++++++++++++++++++++++++
 1 file changed, 139 insertions(+)
tests_multitenant/test_hobo_notify.py
2 2

  
3 3
import logging
4 4

  
5
from django.db import connection
6
from django.test.utils import CaptureQueriesContext
5 7
import pytest
6 8

  
7 9
pytestmark = pytest.mark.django_db
......
205 207
            assert caplog.records[-1].args == (u'Service petite enfance2', u'12345')
206 208

  
207 209

  
210
def test_hobo_notify_roles_db_queries(caplog, tenants):
211
    from django.contrib.auth.models import Group
212
    from tenant_schemas.utils import tenant_context
213

  
214
    from hobo.agent.common.management.commands.hobo_notify import Command
215
    from hobo.agent.common.models import Role
216

  
217
    # test provision
218
    for tenant in tenants:
219
        with tenant_context(tenant):
220
            notification = {
221
                '@type': 'provision',
222
                'audience': ['%s/saml/metadata' % tenant.get_base_url()],
223
                'objects': {
224
                    '@type': 'role',
225
                    'data': [
226
                        {
227
                            'uuid': '12345',
228
                            'name': 'Service petite enfance',
229
                            'slug': 'service-petite-enfance',
230
                            'description': 'Role du service petite enfance %s' % tenant.domain_url,
231
                            'details': 'Some details',
232
                            'emails': ['foo@bar.com', 'test@entrouvert.org'],
233
                            'emails_to_members': False,
234
                        },
235
                        {
236
                            'uuid': '6789',
237
                            'name': 'Autre service',
238
                            'slug': 'autre service',
239
                            'description': "Role d'un autre service petite enfance %s" % tenant.domain_url,
240
                        },
241
                    ],
242
                },
243
            }
244
            with CaptureQueriesContext(connection) as ctx:
245
                Command.process_notification(tenant, notification)
246
                assert len(ctx.captured_queries) == 36
247
            assert Group.objects.count() == 2
248
            assert Role.objects.count() == 2
249

  
250
    # test provision full
251
    for tenant in tenants:
252
        with tenant_context(tenant):
253
            notification = {
254
                '@type': 'provision',
255
                'full': True,
256
                'audience': ['%s/saml/metadata' % tenant.get_base_url()],
257
                'objects': {
258
                    '@type': 'role',
259
                    'data': [
260
                        {
261
                            'uuid': '12sed45',
262
                            'name': 'Le dernier service',
263
                            'slug': 'le-dernier-service',
264
                            'description': '',
265
                        }
266
                    ],
267
                },
268
            }
269
            with CaptureQueriesContext(connection) as ctx:
270
                Command.process_notification(tenant, notification)
271
                assert len(ctx.captured_queries) == 45
272
            assert Group.objects.count() == 1
273
            assert Role.objects.count() == 1
274

  
275
    # provision again
276
    for tenant in tenants:
277
        with tenant_context(tenant):
278
            notification = {
279
                '@type': 'provision',
280
                'audience': ['%s/saml/metadata' % tenant.get_base_url()],
281
                'objects': {
282
                    '@type': 'role',
283
                    'data': [
284
                        {
285
                            'uuid': '12345',
286
                            'name': 'Service petite enfance',
287
                            'slug': 'service-petite-enfance',
288
                            'description': 'Role du service petite enfance %s' % tenant.domain_url,
289
                            'details': 'Some details',
290
                            'emails': ['foo@bar.com', 'test@entrouvert.org'],
291
                            'emails_to_members': False,
292
                        },
293
                        {
294
                            'uuid': '6789',
295
                            'name': 'Autre service',
296
                            'slug': 'autre service',
297
                            'description': "Role d'un autre service petite enfance %s" % tenant.domain_url,
298
                        },
299
                    ],
300
                },
301
            }
302
            Command.process_notification(tenant, notification)
303
            assert Group.objects.count() == 3
304
            assert Role.objects.count() == 3
305

  
306
    # test deprovision
307
    for tenant in tenants:
308
        with tenant_context(tenant):
309
            notification = {
310
                '@type': 'deprovision',
311
                'audience': ['%s/saml/metadata' % tenant.get_base_url()],
312
                'objects': {
313
                    '@type': 'role',
314
                    'data': [
315
                        {
316
                            'uuid': '12sed45',
317
                            'name': 'Le dernier service',
318
                            'slug': 'le-dernier-service',
319
                            'description': '',
320
                        },
321
                        {
322
                            'uuid': '12345',
323
                            'name': 'Service petite enfance',
324
                            'slug': 'service-petite-enfance',
325
                            'description': 'Role du service petite enfance %s' % tenant.domain_url,
326
                            'details': 'Some details',
327
                            'emails': ['foo@bar.com', 'test@entrouvert.org'],
328
                            'emails_to_members': False,
329
                        },
330
                        {
331
                            'uuid': '6789',
332
                            'name': 'Autre service',
333
                            'slug': 'autre service',
334
                            'description': "Role d'un autre service petite enfance %s" % tenant.domain_url,
335
                        },
336
                    ],
337
                },
338
            }
339
            with CaptureQueriesContext(connection) as ctx:
340
                Command.process_notification(tenant, notification)
341
                assert len(ctx.captured_queries) == 39
342

  
343
            assert Group.objects.count() == 0
344
            assert Role.objects.count() == 0
345

  
346

  
208 347
def test_provision_users(tenants):
209 348
    from django.contrib.auth import get_user_model
210 349
    from django.contrib.auth.models import Group
211
-