Projet

Général

Profil

0001-tests-change-post-patch-to-pass-json-to-event-view-6.patch

Nicolas Roche, 14 février 2022 17:38

Télécharger (11,2 ko)

Voir les différences:

Subject: [PATCH 1/2] tests: change post/patch to pass json to event view
 (#60351)

 tests/api/test_event.py | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
tests/api/test_event.py
201 201
        'places': ['This field is required.'],
202 202
    }
203 203

  
204 204
    # add with errors in datetime parts
205 205
    params = {
206 206
        'start_datetime': '2021-11-15 minuit',
207 207
        'places': 10,
208 208
    }
209
    resp = app.post(api_url, params=params, status=400)
209
    resp = app.post_json(api_url, params=params, status=400)
210 210
    assert resp.json['err']
211 211
    assert resp.json['err_desc'] == 'invalid payload'
212 212
    assert 'Datetime has wrong format' in resp.json['errors']['start_datetime'][0]
213 213

  
214 214
    # add an event
215 215
    params = {
216 216
        'start_datetime': '2021-11-15 15:38',
217 217
        'places': 10,
218 218
    }
219
    resp = app.post(api_url, params=params)
219
    resp = app.post_json(api_url, params=params)
220 220
    assert not resp.json['err']
221 221
    assert resp.json['data']['id'] == 'foo-bar-event'
222 222
    assert {'api', 'disabled', 'places'}.issubset(resp.json['data'].keys())
223 223
    assert {'recurrence_days', 'recurrence_week_interval', 'recurrence_end_date'}.isdisjoint(
224 224
        resp.json['data'].keys()
225 225
    )
226 226
    event = Event.objects.filter(agenda=agenda).get(slug='foo-bar-event')
227 227
    assert str(event.start_datetime) == '2021-11-15 14:38:00+00:00'
......
236 236
        'publication_datetime': '2021-09-20 10:00',
237 237
        'places': 11,
238 238
        'waiting_list_places': 3,
239 239
        'label': 'FOO camp',
240 240
        'description': 'An event',
241 241
        'pricing': 'free',
242 242
        'url': 'http://example.org/foo/bar/?',
243 243
    }
244
    resp = app.post(api_url, params=params)
244
    resp = app.post_json(api_url, params=params)
245 245
    assert not resp.json['err']
246 246
    assert resp.json['data']['id'] == 'foo-camp'
247 247
    assert {'api', 'disabled', 'places'}.issubset(resp.json['data'].keys())
248 248
    assert {'recurrence_days', 'recurrence_week_interval', 'recurrence_end_date'}.isdisjoint(
249 249
        resp.json['data'].keys()
250 250
    )
251 251
    event = Event.objects.filter(agenda=agenda).get(slug='foo-camp')
252 252
    assert event.duration == 42
......
259 259
    assert str(event.publication_datetime.tzinfo) == 'UTC'
260 260

  
261 261
    # add with errors in recurrence_days list
262 262
    params = {
263 263
        'start_datetime': '2021-11-15 15:38',
264 264
        'places': 10,
265 265
        'recurrence_days': 'oups',
266 266
    }
267
    resp = app.post(api_url, params=params, status=400)
267
    resp = app.post_json(api_url, params=params, status=400)
268 268
    assert resp.json['err']
269 269
    assert resp.json['err_desc'] == 'invalid payload'
270 270
    assert resp.json['errors']['recurrence_days']['0'][0] == 'A valid integer is required.'
271 271
    params = {
272 272
        'start_datetime': '2021-11-15 15:38',
273 273
        'places': 10,
274 274
        'recurrence_days': '7',
275 275
    }
276
    resp = app.post(api_url, params=params, status=400)
276
    resp = app.post_json(api_url, params=params, status=400)
277 277
    assert resp.json['err']
278 278
    assert resp.json['err_desc'] == 'invalid payload'
279 279
    assert resp.json['errors']['recurrence_days']['0'][0] == 'Ensure this value is less than or equal to 6.'
280 280

  
281 281
    # add a recurrent event
282 282
    params = {
283 283
        'start_datetime': '2021-11-15 15:38',
284 284
        'places': 12,
285 285
        'recurrence_days': '0,3,5',
286 286
        'recurrence_week_interval': '2',
287 287
        'description': 'A recurrent event',
288 288
    }
289 289
    assert Event.objects.filter(agenda=agenda).count() == 2
290
    resp = app.post(api_url, params=params)
290
    resp = app.post_json(api_url, params=params)
291 291
    assert Event.objects.filter(agenda=agenda).count() == 3
292 292
    assert not resp.json['err']
293 293
    assert resp.json['data']['id'] == 'foo-bar-event-1'
294 294
    assert {'api', 'disabled', 'places'}.isdisjoint(resp.json['data'].keys())
295 295
    assert {'recurrence_days', 'recurrence_week_interval', 'recurrence_end_date'}.issubset(
296 296
        resp.json['data'].keys()
297 297
    )
298 298
    event = Event.objects.filter(agenda=agenda).get(slug='foo-bar-event-1')
......
305 305
    params = {
306 306
        'start_datetime': '2021-11-15 15:38',
307 307
        'places': 13,
308 308
        'recurrence_days': '0,3,5',  # Monday, Tuesday, Saturday
309 309
        'recurrence_week_interval': '2',
310 310
        'recurrence_end_date': '2021-12-27',
311 311
        'description': 'A recurrent event having recurrences',
312 312
    }
313
    resp = app.post(api_url, params=params)
313
    resp = app.post_json(api_url, params=params)
314 314
    assert not resp.json['err']
315 315
    assert resp.json['data']['id'] == 'foo-bar-event-2'
316 316
    assert {'api', 'disabled', 'places'}.isdisjoint(resp.json['data'].keys())
317 317
    assert {'recurrence_days', 'recurrence_week_interval', 'recurrence_end_date'}.issubset(
318 318
        resp.json['data'].keys()
319 319
    )
320 320
    event = Event.objects.filter(agenda=agenda).get(slug='foo-bar-event-2')
321 321
    assert Event.objects.filter(agenda=agenda).count() == 13
......
382 382
    event = Event.objects.create(agenda=agenda, start_datetime=now(), places=10, waiting_list_places=5)
383 383
    api_url = '/api/agenda/%s/event/%s/' % (agenda.slug, event.slug)
384 384

  
385 385
    # update with errors in datetime parts
386 386
    params = {
387 387
        'start_datetime': '2021-11-15 minuit',
388 388
        'recurrence_days': '7, 8',
389 389
    }
390
    resp = app.patch(api_url, params=params, status=400)
390
    resp = app.patch_json(api_url, params=params, status=400)
391 391
    assert resp.json['err']
392 392
    assert resp.json['err_desc'] == 'invalid payload'
393 393
    assert 'Datetime has wrong format' in resp.json['errors']['start_datetime'][0]
394 394
    assert resp.json['errors']['recurrence_days']['0'][0] == 'Ensure this value is less than or equal to 6.'
395 395

  
396 396
    # update with almost all optional managed fields
397 397
    params = {
398 398
        'start_datetime': '2021-11-15 15:38',
......
400 400
        'publication_datetime': '2021-09-20 12:00',
401 401
        'places': 8,
402 402
        'waiting_list_places': 3,
403 403
        'label': 'FOO camp',
404 404
        'description': 'An event',
405 405
        'pricing': 'free',
406 406
        'url': 'http://example.org/foo/bar/?',
407 407
    }
408
    resp = app.patch(api_url, params=params)
408
    resp = app.patch_json(api_url, params=params)
409 409
    assert not resp.json['err']
410 410
    event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
411 411
    assert event.duration == 42
412 412
    assert event.places == 8
413 413
    assert event.waiting_list_places == 3
414 414
    assert event.label == 'FOO camp'
415 415
    assert event.description == 'An event'
416 416
    assert event.pricing == 'free'
......
419 419
    assert str(event.publication_datetime.tzinfo) == 'UTC'
420 420

  
421 421
    # update event as a recurring event
422 422
    params = {
423 423
        'recurrence_days': '0,3,5',
424 424
        'recurrence_week_interval': 2,
425 425
        'recurrence_end_date': '2021-12-27',
426 426
    }
427
    resp = app.patch(api_url, params=params)
427
    resp = app.patch_json(api_url, params=params)
428 428
    assert not resp.json['err']
429 429
    event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
430 430
    assert event.recurrences.count() == 9
431 431
    assert [x.places for x in event.recurrences.all()] == [8] * 9
432 432

  
433 433
    recurrence_slug = event.recurrences.first().slug
434 434
    assert recurrence_slug == 'foo-bar-event--2021-11-15-1538'
435 435
    recurrence_url = '/api/agenda/%s/event/%s/' % (agenda.slug, 'foo-bar-event:2021-11-15-1538')
......
440 440
        'duration': 43,
441 441
        'places': 9,
442 442
        'waiting_list_places': 4,
443 443
        'label': 'BAR camp',
444 444
        'description': 'An occurence of an event recurrence',
445 445
        'pricing': '5€',
446 446
        'url': 'http://example.org/bar/bar/',
447 447
    }
448
    resp = app.patch(recurrence_url, params=params)
448
    resp = app.patch_json(recurrence_url, params=params)
449 449
    assert not resp.json['err']
450 450
    recurrence = Event.objects.filter(agenda=agenda).get(slug=recurrence_slug)
451 451
    assert recurrence.duration == 43
452 452
    assert recurrence.places == 9
453 453
    assert recurrence.waiting_list_places == 4
454 454
    assert recurrence.label == 'BAR camp'
455 455
    assert recurrence.description == 'An occurence of an event recurrence'
456 456
    assert recurrence.pricing == '5€'
457 457
    assert recurrence.url == 'http://example.org/bar/bar/'
458 458

  
459 459
    # try to update protected fields of one of the event recurrencies
460 460
    params = {
461 461
        'publication_datetime': '2021-11-15 12:00',
462 462
    }
463
    resp = app.patch(recurrence_url, params=params, status=400)
463
    resp = app.patch_json(recurrence_url, params=params, status=400)
464 464
    assert resp.json['err']
465 465
    assert 'cannot be modified' in resp.json['err_desc']
466 466

  
467 467
    #  update protected fields of one of the event recurrencies providing same value
468 468
    assert 'cannot be modified' in resp.json['err_desc']
469 469
    params = {
470 470
        'recurrence_week_interval': 1,
471 471
    }
472
    resp = app.patch(recurrence_url, params=params)
472
    resp = app.patch_json(recurrence_url, params=params)
473 473
    assert not resp.json['err']
474 474

  
475 475
    booking = Booking.objects.create(event=event.recurrences.all()[2])
476 476

  
477 477
    # update unprotected fields
478 478
    params = {
479 479
        'places': 7,
480 480
    }
481
    resp = app.patch(api_url, params=params)
481
    resp = app.patch_json(api_url, params=params)
482 482
    assert not resp.json['err']
483 483
    event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
484 484
    assert [x.places for x in event.recurrences.all()] == [7] * 9
485 485

  
486 486
    # try to update recurring event protected fields
487 487
    params = {
488 488
        'recurrence_days': '1,2',
489 489
    }
490
    resp = app.patch(api_url, params=params, status=400)
490
    resp = app.patch_json(api_url, params=params, status=400)
491 491
    assert resp.json['err']
492 492
    assert 'cannot be modified' in resp.json['err_desc']
493 493

  
494 494
    assert booking.event.start_datetime.date() == datetime.date(2021, 11, 20)
495 495

  
496 496
    # try to reduce recurrence end date before booked event
497 497
    params = {
498 498
        'recurrence_end_date': '2021-11-20',
499 499
    }
500
    resp = app.patch(api_url, params=params, status=400)
500
    resp = app.patch_json(api_url, params=params, status=400)
501 501
    assert resp.json['err']
502 502
    assert 'bookings exist after this date.' in resp.json['err_desc']
503 503

  
504 504
    # reduce recurrence end date after booked event
505 505
    params = {
506 506
        'recurrence_end_date': '2021-11-21',
507 507
    }
508
    resp = app.patch(api_url, params=params)
508
    resp = app.patch_json(api_url, params=params)
509 509
    assert not resp.json['err']
510 510
    event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
511 511
    assert event.recurrences.count() == 3
512 512

  
513 513
    booking.cancel()
514 514

  
515 515
    # update no more protected fields
516 516
    params = {
517 517
        'recurrence_days': '1,2,3,4,5',
518 518
        'recurrence_week_interval': 1,
519 519
    }
520
    resp = app.patch(api_url, params=params)
520
    resp = app.patch_json(api_url, params=params)
521 521
    assert not resp.json['err']
522 522
    event = Event.objects.filter(agenda=agenda).get(slug=event.slug)
523 523
    assert event.recurrences.count() == 5
524 524

  
525 525

  
526 526
@pytest.mark.freeze_time('2021-11-01 10:00')
527 527
def test_delete_event(app, user):
528 528
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
529
-