3
3
from unittest import mock
4
4
5
5
import django
6
- import pytest
7
6
from django .conf import settings
8
7
from django .contrib .auth .models import AnonymousUser , Group , Permission , User
9
8
from django .db import models
14
13
HTTP_HEADER_ENCODING , authentication , generics , permissions , serializers ,
15
14
status , views
16
15
)
17
- from rest_framework .compat import PY36
18
16
from rest_framework .routers import DefaultRouter
19
17
from rest_framework .test import APIRequestFactory
20
18
from tests .models import BasicModel
@@ -607,7 +605,6 @@ def test_several_levels_and_precedence(self):
607
605
)
608
606
assert composed_perm ().has_permission (request , None ) is True
609
607
610
- @pytest .mark .skipif (not PY36 , reason = "assert_called_once() not available" )
611
608
def test_or_lazyness (self ):
612
609
request = factory .get ('/1' , format = 'json' )
613
610
request .user = AnonymousUser ()
@@ -616,19 +613,18 @@ def test_or_lazyness(self):
616
613
with mock .patch .object (permissions .IsAuthenticated , 'has_permission' , return_value = False ) as mock_deny :
617
614
composed_perm = (permissions .AllowAny | permissions .IsAuthenticated )
618
615
hasperm = composed_perm ().has_permission (request , None )
619
- self . assertIs ( hasperm , True )
620
- mock_allow .assert_called_once ()
616
+ assert hasperm is True
617
+ assert mock_allow .call_count == 1
621
618
mock_deny .assert_not_called ()
622
619
623
620
with mock .patch .object (permissions .AllowAny , 'has_permission' , return_value = True ) as mock_allow :
624
621
with mock .patch .object (permissions .IsAuthenticated , 'has_permission' , return_value = False ) as mock_deny :
625
622
composed_perm = (permissions .IsAuthenticated | permissions .AllowAny )
626
623
hasperm = composed_perm ().has_permission (request , None )
627
- self . assertIs ( hasperm , True )
628
- mock_deny .assert_called_once ()
629
- mock_allow .assert_called_once ()
624
+ assert hasperm is True
625
+ assert mock_deny .call_count == 1
626
+ assert mock_allow .call_count == 1
630
627
631
- @pytest .mark .skipif (not PY36 , reason = "assert_called_once() not available" )
632
628
def test_object_or_lazyness (self ):
633
629
request = factory .get ('/1' , format = 'json' )
634
630
request .user = AnonymousUser ()
@@ -637,19 +633,18 @@ def test_object_or_lazyness(self):
637
633
with mock .patch .object (permissions .IsAuthenticated , 'has_object_permission' , return_value = False ) as mock_deny :
638
634
composed_perm = (permissions .AllowAny | permissions .IsAuthenticated )
639
635
hasperm = composed_perm ().has_object_permission (request , None , None )
640
- self . assertIs ( hasperm , True )
641
- mock_allow .assert_called_once ()
636
+ assert hasperm is True
637
+ assert mock_allow .call_count == 1
642
638
mock_deny .assert_not_called ()
643
639
644
640
with mock .patch .object (permissions .AllowAny , 'has_object_permission' , return_value = True ) as mock_allow :
645
641
with mock .patch .object (permissions .IsAuthenticated , 'has_object_permission' , return_value = False ) as mock_deny :
646
642
composed_perm = (permissions .IsAuthenticated | permissions .AllowAny )
647
643
hasperm = composed_perm ().has_object_permission (request , None , None )
648
- self . assertIs ( hasperm , True )
649
- mock_deny .assert_called_once ()
650
- mock_allow .assert_called_once ()
644
+ assert hasperm is True
645
+ assert mock_deny .call_count == 1
646
+ assert mock_allow .call_count == 1
651
647
652
- @pytest .mark .skipif (not PY36 , reason = "assert_called_once() not available" )
653
648
def test_and_lazyness (self ):
654
649
request = factory .get ('/1' , format = 'json' )
655
650
request .user = AnonymousUser ()
@@ -658,19 +653,18 @@ def test_and_lazyness(self):
658
653
with mock .patch .object (permissions .IsAuthenticated , 'has_permission' , return_value = False ) as mock_deny :
659
654
composed_perm = (permissions .AllowAny & permissions .IsAuthenticated )
660
655
hasperm = composed_perm ().has_permission (request , None )
661
- self . assertIs ( hasperm , False )
662
- mock_allow .assert_called_once ()
663
- mock_deny .assert_called_once ()
656
+ assert hasperm is False
657
+ assert mock_allow .call_count == 1
658
+ assert mock_deny .call_count == 1
664
659
665
660
with mock .patch .object (permissions .AllowAny , 'has_permission' , return_value = True ) as mock_allow :
666
661
with mock .patch .object (permissions .IsAuthenticated , 'has_permission' , return_value = False ) as mock_deny :
667
662
composed_perm = (permissions .IsAuthenticated & permissions .AllowAny )
668
663
hasperm = composed_perm ().has_permission (request , None )
669
- self .assertIs (hasperm , False )
664
+ assert hasperm is False
665
+ assert mock_deny .call_count == 1
670
666
mock_allow .assert_not_called ()
671
- mock_deny .assert_called_once ()
672
667
673
- @pytest .mark .skipif (not PY36 , reason = "assert_called_once() not available" )
674
668
def test_object_and_lazyness (self ):
675
669
request = factory .get ('/1' , format = 'json' )
676
670
request .user = AnonymousUser ()
@@ -679,14 +673,14 @@ def test_object_and_lazyness(self):
679
673
with mock .patch .object (permissions .IsAuthenticated , 'has_object_permission' , return_value = False ) as mock_deny :
680
674
composed_perm = (permissions .AllowAny & permissions .IsAuthenticated )
681
675
hasperm = composed_perm ().has_object_permission (request , None , None )
682
- self . assertIs ( hasperm , False )
683
- mock_allow .assert_called_once ()
684
- mock_deny .assert_called_once ()
676
+ assert hasperm is False
677
+ assert mock_allow .call_count == 1
678
+ assert mock_deny .call_count == 1
685
679
686
680
with mock .patch .object (permissions .AllowAny , 'has_object_permission' , return_value = True ) as mock_allow :
687
681
with mock .patch .object (permissions .IsAuthenticated , 'has_object_permission' , return_value = False ) as mock_deny :
688
682
composed_perm = (permissions .IsAuthenticated & permissions .AllowAny )
689
683
hasperm = composed_perm ().has_object_permission (request , None , None )
690
- self .assertIs (hasperm , False )
684
+ assert hasperm is False
685
+ assert mock_deny .call_count == 1
691
686
mock_allow .assert_not_called ()
692
- mock_deny .assert_called_once ()
0 commit comments