12
12
HTTP_HEADER_ENCODING , authentication , generics , permissions , serializers ,
13
13
status , views
14
14
)
15
+ from rest_framework .exceptions import ErrorDetail
15
16
from rest_framework .routers import DefaultRouter
16
17
from rest_framework .test import APIRequestFactory
17
18
from tests .models import BasicModel
18
19
19
20
factory = APIRequestFactory ()
20
21
21
- CUSTOM_MESSAGE_1 = 'Custom: You cannot access this resource'
22
- CUSTOM_MESSAGE_2 = 'Custom: You do not have permission to view this resource'
22
+ DEFAULT_MESSAGE = ErrorDetail ('You do not have permission to perform this action.' , 'permission_denied' )
23
+ CUSTOM_MESSAGE_1 = ErrorDetail ('Custom: You cannot access this resource' , 'permission_denied_custom' )
24
+ CUSTOM_MESSAGE_2 = ErrorDetail ('Custom: You do not have permission to view this resource' , 'permission_denied_custom' )
23
25
INVERTED_MESSAGE = 'Inverted: Your account already active'
24
26
25
27
@@ -519,18 +521,6 @@ class DeniedViewWithDetailAND3(PermissionInstanceView):
519
521
permission_classes = (BasicPermWithDetail & AnotherBasicPermWithDetail ,)
520
522
521
523
522
- class DeniedViewWithDetailOR1 (PermissionInstanceView ):
523
- permission_classes = (BasicPerm | BasicPermWithDetail ,)
524
-
525
-
526
- class DeniedViewWithDetailOR2 (PermissionInstanceView ):
527
- permission_classes = (BasicPermWithDetail | BasicPerm ,)
528
-
529
-
530
- class DeniedViewWithDetailOR3 (PermissionInstanceView ):
531
- permission_classes = (BasicPermWithDetail | AnotherBasicPermWithDetail ,)
532
-
533
-
534
524
class DeniedViewWithDetailNOT (PermissionInstanceView ):
535
525
permission_classes = (~ BasicPermWithDetail ,)
536
526
@@ -548,23 +538,11 @@ class DeniedObjectViewWithDetailAND1(PermissionInstanceView):
548
538
549
539
550
540
class DeniedObjectViewWithDetailAND2 (PermissionInstanceView ):
551
- permission_classes = (permissions .AllowAny & AnotherBasicObjectPermWithDetail )
541
+ permission_classes = (permissions .AllowAny & AnotherBasicObjectPermWithDetail , )
552
542
553
543
554
544
class DeniedObjectViewWithDetailAND3 (PermissionInstanceView ):
555
- permission_classes = (AnotherBasicObjectPermWithDetail & BasicObjectPermWithDetail )
556
-
557
-
558
- class DeniedObjectViewWithDetailOR1 (PermissionInstanceView ):
559
- permission_classes = (BasicObjectPerm | BasicObjectPermWithDetail )
560
-
561
-
562
- class DeniedObjectViewWithDetailOR2 (PermissionInstanceView ):
563
- permission_classes = (BasicObjectPermWithDetail | BasicObjectPerm ,)
564
-
565
-
566
- class DeniedObjectViewWithDetailOR3 (PermissionInstanceView ):
567
- permission_classes = (BasicObjectPermWithDetail | AnotherBasicObjectPermWithDetail ,)
545
+ permission_classes = (AnotherBasicObjectPermWithDetail & BasicObjectPermWithDetail ,)
568
546
569
547
570
548
class DeniedObjectViewWithDetailNOT (PermissionInstanceView ):
@@ -579,10 +557,6 @@ class DeniedObjectViewWithDetailNOT(PermissionInstanceView):
579
557
denied_view_with_detail_and_2 = DeniedViewWithDetailAND2 .as_view ()
580
558
denied_view_with_detail_and_3 = DeniedViewWithDetailAND3 .as_view ()
581
559
582
- denied_view_with_detail_or_1 = DeniedViewWithDetailOR1 .as_view ()
583
- denied_view_with_detail_or_2 = DeniedViewWithDetailOR2 .as_view ()
584
- denied_view_with_detail_or_3 = DeniedViewWithDetailOR3 .as_view ()
585
-
586
560
denied_view_with_detail_not = DeniedObjectViewWithDetailNOT .as_view ()
587
561
588
562
denied_object_view = DeniedObjectView .as_view ()
@@ -593,10 +567,6 @@ class DeniedObjectViewWithDetailNOT(PermissionInstanceView):
593
567
denied_object_view_with_detail_and_2 = DeniedObjectViewWithDetailAND2 .as_view ()
594
568
denied_object_view_with_detail_and_3 = DeniedObjectViewWithDetailAND3 .as_view ()
595
569
596
- denied_object_view_with_detail_or_1 = DeniedObjectViewWithDetailOR1 .as_view ()
597
- denied_object_view_with_detail_or_2 = DeniedObjectViewWithDetailOR2 .as_view ()
598
- denied_object_view_with_detail_or_3 = DeniedObjectViewWithDetailOR3 .as_view ()
599
-
600
570
denied_object_view_with_detail_not = DeniedObjectViewWithDetailNOT .as_view ()
601
571
602
572
@@ -606,21 +576,18 @@ def setUp(self):
606
576
User .
objects .
create_user (
'username' ,
'[email protected] ' ,
'password' )
607
577
credentials = basic_auth_header ('username' , 'password' )
608
578
self .request = factory .get ('/1' , format = 'json' , HTTP_AUTHORIZATION = credentials )
609
- self .custom_code = 'permission_denied_custom'
610
579
611
580
def test_permission_denied (self ):
612
581
response = denied_view (self .request , pk = 1 )
613
582
detail = response .data .get ('detail' )
614
583
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
615
- self .assertNotEqual (detail , CUSTOM_MESSAGE_1 )
616
- self .assertNotEqual (detail .code , self .custom_code )
584
+ self .assertEqual (detail , DEFAULT_MESSAGE )
617
585
618
586
def test_permission_denied_with_custom_detail (self ):
619
587
response = denied_view_with_detail (self .request , pk = 1 )
620
588
detail = response .data .get ('detail' )
621
589
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
622
590
self .assertEqual (detail , CUSTOM_MESSAGE_1 )
623
- self .assertEqual (detail .code , self .custom_code )
624
591
625
592
def test_permission_denied_with_custom_detail_and_1 (self ):
626
593
response = denied_view_with_detail_and_1 (self .request , pk = 1 )
@@ -641,23 +608,31 @@ def test_permission_denied_with_custom_detail_and_3(self):
641
608
self .assertEqual (detail , CUSTOM_MESSAGE_1 )
642
609
643
610
def test_permission_denied_with_custom_detail_or_1 (self ):
644
- response = denied_view_with_detail_or_1 (self .request , pk = 1 )
645
- detail = response .data .get ('detail' )
611
+ view = PermissionInstanceView .as_view (
612
+ permission_classes = (BasicPerm | BasicPermWithDetail ,),
613
+ )
614
+ response = view (self .request , pk = 1 )
615
+ detail = response .data
646
616
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
647
- self .assertEqual (detail , CUSTOM_MESSAGE_1 )
617
+ self .assertEqual (detail , [ DEFAULT_MESSAGE , CUSTOM_MESSAGE_1 ] )
648
618
649
619
def test_permission_denied_with_custom_detail_or_2 (self ):
650
- response = denied_view_with_detail_or_2 (self .request , pk = 1 )
651
- detail = response .data .get ('detail' )
620
+ view = PermissionInstanceView .as_view (
621
+ permission_classes = (BasicPermWithDetail | BasicPerm ,),
622
+ )
623
+ response = view (self .request , pk = 1 )
624
+ detail = response .data
652
625
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
653
- self .assertEqual (detail , CUSTOM_MESSAGE_1 )
626
+ self .assertEqual (detail , [ CUSTOM_MESSAGE_1 , DEFAULT_MESSAGE ] )
654
627
655
628
def test_permission_denied_with_custom_detail_or_3 (self ):
656
- response = denied_view_with_detail_or_3 (self .request , pk = 1 )
657
- detail = response .data .get ('detail' )
629
+ view = PermissionInstanceView .as_view (
630
+ permission_classes = (BasicPermWithDetail | AnotherBasicPermWithDetail ,),
631
+ )
632
+ response = view (self .request , pk = 1 )
633
+ detail = response .data
658
634
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
659
- expected_message = '"{0}" OR "{1}"' .format (CUSTOM_MESSAGE_1 , CUSTOM_MESSAGE_2 )
660
- self .assertEqual (detail , expected_message )
635
+ self .assertEqual (detail , [CUSTOM_MESSAGE_1 , CUSTOM_MESSAGE_2 ])
661
636
662
637
def test_permission_denied_with_custom_detail_not (self ):
663
638
response = denied_view_with_detail_not (self .request , pk = 1 )
@@ -669,15 +644,13 @@ def test_permission_denied_for_object(self):
669
644
response = denied_object_view (self .request , pk = 1 )
670
645
detail = response .data .get ('detail' )
671
646
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
672
- self .assertNotEqual (detail , CUSTOM_MESSAGE_1 )
673
- self .assertNotEqual (detail .code , self .custom_code )
647
+ self .assertEqual (detail , DEFAULT_MESSAGE )
674
648
675
649
def test_permission_denied_for_object_with_custom_detail (self ):
676
650
response = denied_object_view_with_detail (self .request , pk = 1 )
677
651
detail = response .data .get ('detail' )
678
652
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
679
653
self .assertEqual (detail , CUSTOM_MESSAGE_1 )
680
- self .assertEqual (detail .code , self .custom_code )
681
654
682
655
def test_permission_denied_for_object_with_custom_detail_and_1 (self ):
683
656
response = denied_object_view_with_detail_and_1 (self .request , pk = 1 )
@@ -698,23 +671,33 @@ def test_permission_denied_for_object_with_custom_detail_and_3(self):
698
671
self .assertEqual (detail , CUSTOM_MESSAGE_2 )
699
672
700
673
def test_permission_denied_for_object_with_custom_detail_or_1 (self ):
701
- response = denied_object_view_with_detail_or_1 (self .request , pk = 1 )
702
- detail = response .data .get ('detail' )
674
+ view = PermissionInstanceView .as_view (
675
+ permission_classes = (BasicObjectPerm | BasicObjectPermWithDetail ,),
676
+ )
677
+ response = view (self .request , pk = 1 )
678
+ detail = response .data
703
679
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
704
- self .assertEqual (detail , CUSTOM_MESSAGE_1 )
680
+ self .assertEqual (detail , [ DEFAULT_MESSAGE , CUSTOM_MESSAGE_1 ] )
705
681
706
682
def test_permission_denied_for_object_with_custom_detail_or_2 (self ):
707
- response = denied_object_view_with_detail_or_2 (self .request , pk = 1 )
708
- detail = response .data .get ('detail' )
683
+ view = PermissionInstanceView .as_view (
684
+ permission_classes = (BasicObjectPermWithDetail | BasicObjectPerm ,),
685
+ )
686
+ response = view (self .request , pk = 1 )
687
+ detail = response .data
709
688
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
710
- self .assertEqual (detail , CUSTOM_MESSAGE_1 )
689
+ self .assertEqual (detail , [ CUSTOM_MESSAGE_1 , DEFAULT_MESSAGE ] )
711
690
712
691
def test_permission_denied_for_object_with_custom_detail_or_3 (self ):
713
- response = denied_object_view_with_detail_or_3 (self .request , pk = 1 )
714
- detail = response .data .get ('detail' )
692
+ view = PermissionInstanceView .as_view (
693
+ permission_classes = (
694
+ BasicObjectPermWithDetail | AnotherBasicObjectPermWithDetail ,
695
+ ),
696
+ )
697
+ response = view (self .request , pk = 1 )
698
+ detail = response .data
715
699
self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
716
- expected_message = '"{0}" OR "{1}"' .format (CUSTOM_MESSAGE_1 , CUSTOM_MESSAGE_2 )
717
- self .assertEqual (detail , expected_message )
700
+ self .assertEqual (detail , [CUSTOM_MESSAGE_1 , CUSTOM_MESSAGE_2 ])
718
701
719
702
def test_permission_denied_for_object_with_custom_detail_not (self ):
720
703
response = denied_object_view_with_detail_not (self .request , pk = 1 )
0 commit comments