11import os
22from unittest import mock
3+ from datetime import datetime , timedelta
34
45
56from django .conf import settings
@@ -355,7 +356,8 @@ def test_update_dref_image(self):
355356 "file" : file2 .id ,
356357 "caption" : "Test Caption"
357358 }
358- ]
359+ ],
360+ "modified_at" : datetime .now ()
359361 }
360362 self .client .force_authenticate (self .user )
361363 response = self .client .patch (url , data = data , format = "json" )
@@ -372,7 +374,8 @@ def test_update_dref_image(self):
372374 "file" : file4 .id ,
373375 "caption" : "Test Caption"
374376 }
375- ]
377+ ],
378+ "modified_at" : datetime .now ()
376379 }
377380 self .client .force_authenticate (self .ifrc_user )
378381 response = self .client .patch (url , data , format = 'multipart' )
@@ -385,7 +388,8 @@ def test_update_dref_image(self):
385388 "file" : file4 .id ,
386389 "caption" : "Test Caption"
387390 }
388- ]
391+ ],
392+ "modified_at" : datetime .now ()
389393 }
390394 self .client .force_authenticate (self .ifrc_user )
391395 response = self .client .patch (url , data )
@@ -398,7 +402,8 @@ def test_update_dref_image(self):
398402 "file" : file5 .id ,
399403 "caption" : "Test Caption"
400404 }
401- ]
405+ ],
406+ "modified_at" : datetime .now ()
402407 }
403408 self .client .force_authenticate (self .ifrc_user )
404409 response = self .client .patch (url , data )
@@ -436,31 +441,42 @@ def test_dref_options(self):
436441 self .assertIn ('needs_identified' , response .data )
437442 self .assertIn ('national_society_actions' , response .data )
438443
439- def test_dref_is_published (self ):
444+ @mock .patch ('django.utils.timezone.now' )
445+ def test_dref_is_published (self , mock_now ):
440446 """
441447 Test for dref if is_published = True
442448 """
449+ initial_now = datetime (2011 , 11 , 11 )
450+ mock_now .return_value = initial_now
451+
443452 dref = DrefFactory .create (
444- title = 'test' , created_by = self .user ,
445- is_published = True
453+ title = 'test' ,
454+ created_by = self .user ,
455+ is_published = True ,
446456 )
447457 url = f'/api/v2/dref/{ dref .id } /'
448458 data = {
449- "title" : "New Update Title"
459+ "title" : "New Update Title" ,
460+ "modified_at" : initial_now ,
450461 }
451462 self .client .force_authenticate (self .user )
452463 response = self .client .patch (url , data )
453464 self .assert_400 (response )
454465
455466 # create new dref with is_published = False
456467 not_published_dref = DrefFactory .create (
457- title = 'test' , created_by = self .user ,
468+ title = 'test' ,
469+ created_by = self .user ,
458470 )
459471 url = f'/api/v2/dref/{ not_published_dref .id } /'
460472 self .client .force_authenticate (self .user )
461473 response = self .client .patch (url , data )
462474 self .assert_200 (response )
463475
476+ data ['modified_at' ] = initial_now - timedelta (seconds = 10 )
477+ response = self .client .patch (url , data )
478+ self .assert_400 (response )
479+
464480 # test dref published endpoint
465481 url = f'/api/v2/dref/{ not_published_dref .id } /publish/'
466482 data = {}
@@ -795,3 +811,83 @@ def test_dref_for_assessment_report(self):
795811 response = self .client .post (url , data )
796812 self .assertEqual (response .status_code , 201 )
797813 self .assertEqual (Dref .objects .count (), old_count + 1 )
814+
815+ def test_dref_for_super_user (self ):
816+ user1 = UserFactory .create (
817+ 818+ first_name = 'Test' ,
819+ last_name = 'User1' ,
820+ password = 'admin123' ,
821+ 822+ is_superuser = True ,
823+ )
824+ user2 = UserFactory .create (
825+ 826+ first_name = 'Test' ,
827+ last_name = 'User2' ,
828+ password = 'admin123' ,
829+ 830+ )
831+ user3 = UserFactory .create (
832+ 833+ first_name = 'Test' ,
834+ last_name = 'User3' ,
835+ password = 'admin123' ,
836+ 837+ )
838+ dref1 = DrefFactory .create (
839+ title = 'Test Title' ,
840+ created_by = user2 ,
841+ )
842+ DrefFactory .create (
843+ title = 'Test Title New' ,
844+ )
845+
846+ # authenticate with user1(superuser)
847+ # user1 should be able to view all dref
848+ url = '/api/v2/dref/'
849+ self .client .force_authenticate (user1 )
850+ response = self .client .get (url )
851+ self .assertEqual (response .status_code , 200 )
852+ self .assertEqual (len (response .data ['results' ]), 2 )
853+
854+ # authenticate with User2
855+ self .client .force_authenticate (user2 )
856+ response = self .client .get (url )
857+ self .assertEqual (response .status_code , 200 )
858+ self .assertEqual (len (response .data ['results' ]), 1 )
859+ self .assertEqual (response .data ['results' ][0 ]['id' ], dref1 .id )
860+
861+ # authenticate with User3
862+ self .client .force_authenticate (user3 )
863+ response = self .client .get (url )
864+ self .assertEqual (response .status_code , 200 )
865+ self .assertEqual (len (response .data ['results' ]), 0 )
866+
867+ def test_dref_latest_update (self ):
868+ dref = DrefFactory .create (
869+ title = 'Test Title' ,
870+ created_by = self .user ,
871+ modified_at = datetime (2022 , 4 , 18 , 2 , 29 , 39 , 793615 )
872+ )
873+ url = f'/api/v2/dref/{ dref .id } /'
874+ data = {
875+ 'title' : "New title" ,
876+ }
877+
878+ # without `modified_at`
879+ self .client .force_authenticate (self .user )
880+ response = self .client .patch (url , data = data )
881+ self .assertEqual (response .status_code , 400 )
882+
883+ # with `modified_at` less than instance `modified_at`
884+ data ['modified_at' ] = datetime (2022 , 2 , 18 , 2 , 29 , 39 , 793615 )
885+ self .client .force_authenticate (self .user )
886+ response = self .client .patch (url , data = data )
887+ self .assertEqual (response .status_code , 400 )
888+
889+ data ['modified_at' ] = datetime .now ()
890+ response = self .client .patch (url , data = data )
891+ self .assertEqual (response .status_code , 200 )
892+ # Title should be latest since modified_at is greater than modified_at in database
893+ self .assertEqual (response .data ['title' ], "New title" )
0 commit comments