<s> from flask import g , Flask <EOL> from flask_pundit import ( <EOL> FlaskPundit , <EOL> verify_authorized , <EOL> verify_policy_scoped ) <EOL> from models . post import Post <EOL> from models . comment import Comment <EOL> from nose . tools import ( <EOL> assert_raises , <EOL> ok_ , <EOL> eq_ ) <EOL> from unittest import TestCase <EOL> import json <EOL> class TestUsage ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . app = Flask ( '<STR_LIT:test>' ) <EOL> self . app . debug = True <EOL> self . pundit = FlaskPundit ( policies_path = '<STR_LIT>' ) <EOL> self . pundit . init_app ( self . app ) <EOL> self . client = self . app . test_client ( ) <EOL> def test_authorize_with_record_for_admin ( self ) : <EOL> def do_authorize_stuff ( ) : <EOL> post = Post ( <NUM_LIT:1> ) <EOL> return self . pundit . authorize ( post ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def admin_get_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> is_authorized = do_authorize_stuff ( ) <EOL> ok_ ( self . pundit . _verify_authorized ( ) ) <EOL> if is_authorized : <EOL> return '<STR_LIT>' , <NUM_LIT:200> <EOL> else : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_authorize_with_record_for_staff ( self ) : <EOL> def do_authorize_stuff ( ) : <EOL> post = Post ( <NUM_LIT:1> ) <EOL> return self . pundit . authorize ( post ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def admin_get_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT>' } <EOL> is_authorized = do_authorize_stuff ( ) <EOL> ok_ ( self . pundit . _verify_authorized ( ) ) <EOL> if is_authorized : <EOL> return '<STR_LIT>' , <NUM_LIT:200> <EOL> else : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT> ) <EOL> def test_authorize_with_model_for_admin ( self ) : <EOL> def do_authorize_stuff ( ) : <EOL> return self . pundit . authorize ( Post , action = '<STR_LIT>' ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def admin_create_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> is_authorized = do_authorize_stuff ( ) <EOL> ok_ ( self . pundit . _verify_authorized ( ) ) <EOL> if is_authorized : <EOL> return '<STR_LIT>' , <NUM_LIT:200> <EOL> else : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_authorize_with_policy_class_specified_for_admin ( self ) : <EOL> def do_authorize_stuff ( ) : <EOL> return self . pundit . authorize ( Comment ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def admin_get_comment ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> is_authorized = do_authorize_stuff ( ) <EOL> ok_ ( self . pundit . _verify_authorized ( ) ) <EOL> if is_authorized : <EOL> return '<STR_LIT>' , <NUM_LIT:200> <EOL> else : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_verify_authorized_decorator_success ( self ) : <EOL> def do_authorize_stuff ( ) : <EOL> post = Post ( <NUM_LIT:1> ) <EOL> return self . pundit . authorize ( post ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> @ verify_authorized <EOL> def admin_get_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> is_authorized = do_authorize_stuff ( ) <EOL> if is_authorized : <EOL> return '<STR_LIT>' , <NUM_LIT:200> <EOL> else : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_verify_authorized_decorator_raises_exception ( self ) : <EOL> def do_authorize_stuff ( ) : <EOL> post = Post ( <NUM_LIT:1> ) <EOL> return self . pundit . authorize ( post ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> @ verify_authorized <EOL> def admin_get_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> return '<STR_LIT>' , <NUM_LIT:200> <EOL> assert_raises ( RuntimeError , self . client . get , <EOL> '<STR_LIT>' ) <EOL> def test_policy_scoped_admin ( self ) : <EOL> def do_policy_scope_stuff ( ) : <EOL> return self . pundit . policy_scope ( Post ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def admin_get_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> scoped_posts = do_policy_scope_stuff ( ) <EOL> ok_ ( self . pundit . _verify_policy_scoped ( ) ) <EOL> return json . dumps ( { '<STR_LIT>' : scoped_posts } ) <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . data , '<STR_LIT>' ) <EOL> def test_policy_scoped_staff ( self ) : <EOL> def do_policy_scope_stuff ( ) : <EOL> return self . pundit . policy_scope ( Post ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def admin_get_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT>' } <EOL> scoped_posts = do_policy_scope_stuff ( ) <EOL> ok_ ( self . pundit . _verify_policy_scoped ( ) ) <EOL> return json . dumps ( { '<STR_LIT>' : scoped_posts } ) <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . data , '<STR_LIT>' ) <EOL> def test_policy_scope_with_policy_class_specified_for_admin ( self ) : <EOL> def do_policy_scope_stuff ( ) : <EOL> return self . pundit . policy_scope ( Comment ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def admin_get_comments ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> scoped_comments = do_policy_scope_stuff ( ) <EOL> ok_ ( self . pundit . _verify_policy_scoped ( ) ) <EOL> return json . dumps ( { '<STR_LIT>' : scoped_comments } ) <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . data , '<STR_LIT>' ) <EOL> def test_verify_policy_scoped_decorator_success ( self ) : <EOL> def do_policy_scope_stuff ( ) : <EOL> return self . pundit . policy_scope ( Post ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> @ verify_policy_scoped <EOL> def admin_get_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> scoped_posts = do_policy_scope_stuff ( ) <EOL> return json . dumps ( { '<STR_LIT>' : scoped_posts } ) <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . data , '<STR_LIT>' ) <EOL> def test_verify_policy_scoped_decorator_raises_exception ( self ) : <EOL> def do_policy_scope_stuff ( ) : <EOL> return self . pundit . policy_scope ( Post ) <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> @ verify_policy_scoped <EOL> def admin_get_post ( ) : <EOL> g . user = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> return json . dumps ( { '<STR_LIT>' : [ ] } ) <EOL> assert_raises ( RuntimeError , self . client . get , <EOL> '<STR_LIT>' ) </s>
