<s> import logging <EOL> from unittest . util import safe_repr <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import Group <EOL> from django . http import HttpRequest <EOL> from django . test import TestCase <EOL> from guardian . shortcuts import assign_perm , remove_perm , get_perms <EOL> from rest_framework import status <EOL> from rest_framework . request import Request <EOL> from rest_framework . test import APIClient <EOL> logger = logging . getLogger ( __name__ ) <EOL> def get_fake_request ( ) : <EOL> django_request = HttpRequest ( ) <EOL> django_request . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> django_request . META [ '<STR_LIT>' ] = <NUM_LIT> <EOL> return Request ( django_request ) <EOL> def group_has_perm ( group , perm , obj = None ) : <EOL> if obj : <EOL> return perm in get_perms ( group , obj ) <EOL> else : <EOL> for gperm in group . permissions . all ( ) : <EOL> if gperm . codename == perm : <EOL> return True <EOL> return False <EOL> class PermissionsMixin ( object ) : <EOL> permission_tests = { } <EOL> @ classmethod <EOL> def _error_check_permissions ( cls ) : <EOL> if not cls . permission_tests : <EOL> return False <EOL> if not isinstance ( cls . permission_tests , dict ) : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in cls . permission_tests : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in cls . permission_tests : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> return True <EOL> def set_up_perms ( self ) : <EOL> self . obj = self . permission_tests [ '<STR_LIT>' ] ( <EOL> ** self . permission_tests . get ( '<STR_LIT>' , { } ) <EOL> ) <EOL> self . obj . save ( ) <EOL> def test_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _error_check_permissions ( ) : <EOL> return <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> endpoint = self . permission_tests [ '<STR_LIT>' ] . format ( self . obj . pk ) <EOL> for perm_type in self . permission_tests [ '<STR_LIT>' ] : <EOL> method = perm_type [ '<STR_LIT>' ] <EOL> response = getattr ( self . client , method ) ( endpoint , perm_type . get ( '<STR_LIT:data>' , { } ) ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> assign_perm ( self . permission_tests [ '<STR_LIT>' ] % perm_type [ '<STR_LIT>' ] , <EOL> self . user , self . obj ) <EOL> response = getattr ( self . client , method ) ( endpoint , perm_type . get ( '<STR_LIT:data>' , { } ) ) <EOL> expected_code = perm_type . get ( '<STR_LIT:code>' , status . HTTP_200_OK ) <EOL> self . assertEqual ( response . status_code , expected_code ) <EOL> remove_perm ( self . permission_tests [ '<STR_LIT>' ] % perm_type [ '<STR_LIT>' ] , <EOL> self . user , self . obj ) <EOL> def _add_admin_object_permission ( self ) : <EOL> endpoint = self . permission_tests [ '<STR_LIT>' ] . format ( self . obj . pk ) <EOL> endpoint += '<STR_LIT>' <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . post ( endpoint , { '<STR_LIT:user>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> response = self . client . post ( endpoint , { '<STR_LIT:user>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> self . assertTrue ( self . user . has_perm ( self . permission_tests [ '<STR_LIT>' ] % '<STR_LIT>' , self . obj ) ) <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> def test_add_object_permissions ( self ) : <EOL> endpoint = self . permission_tests [ '<STR_LIT>' ] . format ( self . obj . pk ) <EOL> endpoint += '<STR_LIT>' <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . get ( endpoint ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> self . _add_admin_object_permission ( ) <EOL> response = self . client . get ( endpoint ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_partial_update_object_permissions ( self ) : <EOL> orig_endpoint = self . permission_tests [ '<STR_LIT>' ] . format ( self . obj . pk ) <EOL> endpoint = orig_endpoint + '<STR_LIT>' <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . put ( endpoint , { '<STR_LIT:user>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> self . _add_admin_object_permission ( ) <EOL> response = self . client . put ( endpoint , { '<STR_LIT:user>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> response = self . client . patch ( endpoint , { '<STR_LIT:user>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertTrue ( self . user . has_perm ( self . permission_tests [ '<STR_LIT>' ] % '<STR_LIT>' , self . obj ) ) <EOL> response = self . client . get ( orig_endpoint ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> response = self . client . get ( endpoint ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class StackdioTestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . client = APIClient ( ) <EOL> self . user = get_user_model ( ) . objects . get ( username = '<STR_LIT>' ) <EOL> self . admin = get_user_model ( ) . objects . get ( username = '<STR_LIT>' ) <EOL> self . group = Group . objects . get ( name = '<STR_LIT>' ) <EOL> self . user . groups . add ( self . group ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . set_up_perms ( ) <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> user_model = get_user_model ( ) <EOL> user_model . objects . create_superuser ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> first_name = '<STR_LIT>' , last_name = '<STR_LIT>' ) <EOL> user_model . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> first_name = '<STR_LIT>' , last_name = '<STR_LIT>' ) <EOL> Group . objects . create ( name = '<STR_LIT>' ) <EOL> def assertCallable ( self , obj , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not callable ( obj ) : <EOL> standardMsg = '<STR_LIT>' % ( safe_repr ( obj ) ) <EOL> self . fail ( self . _formatMessage ( msg , standardMsg ) ) </s>
