<s> from flask import Flask <EOL> from flask_bouncer import Bouncer , requires , skip_authorization , ensure <EOL> from werkzeug . exceptions import Unauthorized <EOL> from bouncer . constants import * <EOL> from nose . tools import * <EOL> from . models import Article , User <EOL> from . helpers import user_set <EOL> @ raises ( Unauthorized ) <EOL> def test_lock_it_down_raise_exception ( ) : <EOL> app = Flask ( "<STR_LIT>" ) <EOL> app . debug = True <EOL> bouncer = Bouncer ( app , ensure_authorization = True ) <EOL> @ bouncer . authorization_method <EOL> def define_authorization ( user , they ) : <EOL> they . can ( '<STR_LIT>' , Article ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def articles_index ( ) : <EOL> return "<STR_LIT>" <EOL> client = app . test_client ( ) <EOL> jonathan = User ( name = '<STR_LIT>' , admin = False ) <EOL> with user_set ( app , jonathan ) : <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> def test_ensure_and_requires_while_locked_down ( ) : <EOL> app = Flask ( "<STR_LIT>" ) <EOL> app . debug = True <EOL> bouncer = Bouncer ( app , ensure_authorization = True ) <EOL> @ bouncer . authorization_method <EOL> def define_authorization ( user , they ) : <EOL> they . can ( READ , Article ) <EOL> they . can ( EDIT , Article , author_id = user . id ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires ( READ , Article ) <EOL> def articles_index ( ) : <EOL> return "<STR_LIT>" <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def edit_post ( post_id ) : <EOL> jonathan = User ( name = '<STR_LIT>' , admin = False , id = <NUM_LIT:1> ) <EOL> article = Article ( author_id = jonathan . id ) <EOL> ensure ( EDIT , article ) <EOL> return "<STR_LIT>" <EOL> client = app . test_client ( ) <EOL> jonathan = User ( name = '<STR_LIT>' , admin = False , id = <NUM_LIT:1> ) <EOL> with user_set ( app , jonathan ) : <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> eq_ ( b"<STR_LIT>" , resp . data ) <EOL> resp = client . post ( '<STR_LIT>' ) <EOL> eq_ ( b"<STR_LIT>" , resp . data ) <EOL> def test_bypass_route ( ) : <EOL> app = Flask ( "<STR_LIT>" ) <EOL> app . debug = True <EOL> bouncer = Bouncer ( app , ensure_authorization = True ) <EOL> @ bouncer . authorization_method <EOL> def define_authorization ( user , they ) : <EOL> they . can ( '<STR_LIT>' , Article ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ skip_authorization <EOL> def articles_index ( ) : <EOL> return "<STR_LIT>" <EOL> client = app . test_client ( ) <EOL> jonathan = User ( name = '<STR_LIT>' , admin = False ) <EOL> with user_set ( app , jonathan ) : <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> eq_ ( b"<STR_LIT>" , resp . data ) </s>
