<s> import json <EOL> from django . contrib . auth . models import User , Group , Permission <EOL> from django . contrib . auth . management import create_permissions <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from rest_framework import status <EOL> from rest_framework_extensions . test import APITestCase <EOL> from rest_framework_extensions . compat import get_model_name <EOL> from tests_app . testutils import basic_auth_header <EOL> from . urls import urlpatterns <EOL> from . models import PermissionsComment <EOL> class ExtendedDjangoObjectPermissionTestMixin ( object ) : <EOL> def setUp ( self ) : <EOL> from guardian . shortcuts import assign_perm <EOL> create = User . objects . create_user <EOL> users = { <EOL> '<STR_LIT>' : create ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:password>' ) , <EOL> '<STR_LIT>' : create ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:password>' ) , <EOL> '<STR_LIT>' : create ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:password>' ) , <EOL> '<STR_LIT>' : create ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:password>' ) , <EOL> } <EOL> Permission . objects . get_or_create ( <EOL> codename = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> content_type = ContentType . objects . get_for_model ( PermissionsComment ) <EOL> ) <EOL> everyone = Group . objects . create ( name = '<STR_LIT>' ) <EOL> model_name = get_model_name ( PermissionsComment ) <EOL> app_label = PermissionsComment . _meta . app_label <EOL> f = '<STR_LIT>' . format <EOL> perms = { <EOL> '<STR_LIT>' : f ( '<STR_LIT>' , model_name ) , <EOL> '<STR_LIT>' : f ( '<STR_LIT>' , model_name ) , <EOL> '<STR_LIT>' : f ( '<STR_LIT>' , model_name ) <EOL> } <EOL> for perm in perms . values ( ) : <EOL> perm = '<STR_LIT>' . format ( app_label , perm ) <EOL> assign_perm ( perm , everyone ) <EOL> everyone . user_set . add ( * users . values ( ) ) <EOL> readers = Group . objects . create ( name = '<STR_LIT>' ) <EOL> writers = Group . objects . create ( name = '<STR_LIT>' ) <EOL> deleters = Group . objects . create ( name = '<STR_LIT>' ) <EOL> model = PermissionsComment . objects . create ( text = '<STR_LIT:foo>' , id = <NUM_LIT:1> ) <EOL> assign_perm ( perms [ '<STR_LIT>' ] , readers , model ) <EOL> assign_perm ( perms [ '<STR_LIT>' ] , writers , model ) <EOL> assign_perm ( perms [ '<STR_LIT>' ] , deleters , model ) <EOL> readers . user_set . add ( users [ '<STR_LIT>' ] , users [ '<STR_LIT>' ] ) <EOL> writers . user_set . add ( users [ '<STR_LIT>' ] , users [ '<STR_LIT>' ] ) <EOL> deleters . user_set . add ( users [ '<STR_LIT>' ] , users [ '<STR_LIT>' ] ) <EOL> self . credentials = { } <EOL> for user in users . values ( ) : <EOL> self . credentials [ user . username ] = basic_auth_header ( user . username , '<STR_LIT:password>' ) <EOL> class ExtendedDjangoObjectPermissionsTest_should_inherit_standard ( ExtendedDjangoObjectPermissionTestMixin , <EOL> APITestCase ) : <EOL> urls = urlpatterns <EOL> def test_can_delete_permissions ( self ) : <EOL> response = self . client . delete ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> def test_cannot_delete_permissions ( self ) : <EOL> response = self . client . delete ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_403_FORBIDDEN ) <EOL> def test_can_update_permissions ( self ) : <EOL> response = self . client . patch ( <EOL> '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( { '<STR_LIT:text>' : '<STR_LIT>' } ) , <EOL> ** { <EOL> '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( response . data . get ( '<STR_LIT:text>' ) , '<STR_LIT>' ) <EOL> def test_cannot_update_permissions ( self ) : <EOL> response = self . client . patch ( <EOL> '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( { '<STR_LIT:text>' : '<STR_LIT>' } ) , <EOL> ** { <EOL> '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> def test_cannot_update_permissions_non_existing ( self ) : <EOL> response = self . client . patch ( <EOL> '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( { '<STR_LIT:text>' : '<STR_LIT>' } ) , <EOL> ** { <EOL> '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> def test_can_read_permissions ( self ) : <EOL> response = self . client . get ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_cannot_read_permissions ( self ) : <EOL> response = self . client . get ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> def test_can_read_list_permissions ( self ) : <EOL> response = self . client . get ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( response . data [ <NUM_LIT:0> ] . get ( '<STR_LIT:id>' ) , <NUM_LIT:1> ) <EOL> def test_cannot_read_list_permissions ( self ) : <EOL> response = self . client . get ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertListEqual ( response . data , [ ] ) <EOL> class ExtendedDjangoObjectPermissionsTest_without_hiding_forbidden_objects ( ExtendedDjangoObjectPermissionTestMixin , <EOL> APITestCase ) : <EOL> urls = urlpatterns <EOL> def test_can_delete_permissions ( self ) : <EOL> response = self . client . delete ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> def test_cannot_delete_permissions ( self ) : <EOL> response = self . client . delete ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_403_FORBIDDEN ) <EOL> def test_can_update_permissions ( self ) : <EOL> response = self . client . patch ( <EOL> '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( { '<STR_LIT:text>' : '<STR_LIT>' } ) , <EOL> ** { <EOL> '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( response . data . get ( '<STR_LIT:text>' ) , '<STR_LIT>' ) <EOL> def test_cannot_update_permissions ( self ) : <EOL> response = self . client . patch ( <EOL> '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( { '<STR_LIT:text>' : '<STR_LIT>' } ) , <EOL> ** { <EOL> '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_403_FORBIDDEN ) <EOL> def test_cannot_update_permissions_non_existing ( self ) : <EOL> response = self . client . patch ( <EOL> '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( { '<STR_LIT:text>' : '<STR_LIT>' } ) , <EOL> ** { <EOL> '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> def test_can_read_permissions ( self ) : <EOL> response = self . client . get ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_cannot_read_permissions ( self ) : <EOL> response = self . client . get ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_403_FORBIDDEN ) <EOL> def test_can_read_list_permissions ( self ) : <EOL> response = self . client . get ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( response . data [ <NUM_LIT:0> ] . get ( '<STR_LIT:id>' ) , <NUM_LIT:1> ) <EOL> def test_cannot_read_list_permissions ( self ) : <EOL> response = self . client . get ( <EOL> '<STR_LIT>' , <EOL> ** { '<STR_LIT>' : self . credentials [ '<STR_LIT>' ] } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertListEqual ( response . data , [ ] ) </s>
