<s> from __future__ import unicode_literals <EOL> from django . contrib . auth . models import AnonymousUser <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . sessions . middleware import SessionMiddleware <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . test import RequestFactory <EOL> from django . views . generic import DetailView <EOL> import pytest <EOL> from machina . apps . forum_permission . middleware import ForumPermissionMiddleware <EOL> from machina . core . db . models import get_model <EOL> from machina . core . loading import get_class <EOL> from machina . test . factories import create_forum <EOL> from machina . test . factories import UserFactory <EOL> Forum = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PermissionHandler = get_class ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assign_perm = get_class ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PermissionRequiredMixin = get_class ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ pytest . mark . django_db <EOL> class TestPermissionRequiredMixin ( object ) : <EOL> @ pytest . fixture ( autouse = True ) <EOL> def setup ( self ) : <EOL> self . user = UserFactory . create ( ) <EOL> self . factory = RequestFactory ( ) <EOL> self . perm_handler = PermissionHandler ( ) <EOL> self . forum = create_forum ( ) <EOL> assign_perm ( '<STR_LIT>' , self . user , self . forum ) <EOL> self . mixin = PermissionRequiredMixin ( ) <EOL> def get_request ( self , url = '<STR_LIT:/>' ) : <EOL> request = self . factory . get ( '<STR_LIT:/>' ) <EOL> middleware = SessionMiddleware ( ) <EOL> middleware . process_request ( request ) <EOL> request . session . save ( ) <EOL> return request <EOL> def test_should_raise_if_the_permission_required_attribute_is_set_with_an_incorrect_value ( self ) : <EOL> self . mixin . object = self . forum <EOL> self . mixin . permission_required = <NUM_LIT:10> <EOL> request = self . get_request ( ) <EOL> request . user = self . user <EOL> ForumPermissionMiddleware ( ) . process_request ( request ) <EOL> with pytest . raises ( ImproperlyConfigured ) : <EOL> self . mixin . check_permissions ( request ) <EOL> def test_should_redirect_anonymous_users_to_login_url_if_access_is_not_granted ( self ) : <EOL> self . mixin . object = self . forum <EOL> self . mixin . permission_required = '<STR_LIT>' <EOL> request = self . get_request ( ) <EOL> request . user = AnonymousUser ( ) <EOL> ForumPermissionMiddleware ( ) . process_request ( request ) <EOL> response = self . mixin . dispatch ( request ) <EOL> assert response . status_code == <NUM_LIT> <EOL> def test_should_return_http_response_forbiden_to_logged_in_users_if_access_is_not_granted ( self ) : <EOL> self . mixin . object = self . forum <EOL> self . mixin . permission_required = [ '<STR_LIT>' , ] <EOL> request = self . get_request ( ) <EOL> request . user = self . user <EOL> ForumPermissionMiddleware ( ) . process_request ( request ) <EOL> with pytest . raises ( PermissionDenied ) : <EOL> self . mixin . dispatch ( request ) <EOL> def test_should_return_a_valid_response_if_access_is_granted ( self ) : <EOL> class ForumTestView ( PermissionRequiredMixin , DetailView ) : <EOL> permission_required = [ '<STR_LIT>' , ] <EOL> def get_queryset ( self ) : <EOL> return Forum . objects . all ( ) <EOL> forum_view = ForumTestView ( ) <EOL> request = self . get_request ( ) <EOL> request . user = self . user <EOL> ForumPermissionMiddleware ( ) . process_request ( request ) <EOL> response = forum_view . dispatch ( request , pk = self . forum . pk ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> def test_should_consider_controlled_object_prior_to_builtin_objet_or_get_object_attributes ( self ) : <EOL> forum = self . forum <EOL> class UserTestView ( PermissionRequiredMixin , DetailView ) : <EOL> permission_required = [ '<STR_LIT>' , ] <EOL> def get_queryset ( self ) : <EOL> return User . objects . all ( ) <EOL> def get_controlled_object ( self ) : <EOL> return forum <EOL> user_view = UserTestView ( ) <EOL> request = self . get_request ( ) <EOL> request . user = self . user <EOL> ForumPermissionMiddleware ( ) . process_request ( request ) <EOL> response = user_view . dispatch ( request , pk = self . user . pk ) <EOL> assert response . status_code == <NUM_LIT:200> </s>
