<s> import wtforms <EOL> from nose . tools import eq_ , ok_ <EOL> from flask import Flask , session <EOL> from werkzeug . wsgi import DispatcherMiddleware <EOL> from werkzeug . test import Client <EOL> from wtforms import fields <EOL> from flask . ext . admin import Admin , form <EOL> from flask . ext . admin . _compat import iteritems , itervalues <EOL> from flask . ext . admin . model import base , filters <EOL> def wtforms2_and_up ( func ) : <EOL> """<STR_LIT>""" <EOL> if int ( wtforms . __version__ [ <NUM_LIT:0> ] ) < <NUM_LIT:2> : <EOL> func . __test__ = False <EOL> return func <EOL> class Model ( object ) : <EOL> def __init__ ( self , id = None , c1 = <NUM_LIT:1> , c2 = <NUM_LIT:2> , c3 = <NUM_LIT:3> ) : <EOL> self . id = id <EOL> self . col1 = c1 <EOL> self . col2 = c2 <EOL> self . col3 = c3 <EOL> class Form ( form . BaseForm ) : <EOL> col1 = fields . StringField ( ) <EOL> col2 = fields . StringField ( ) <EOL> col3 = fields . StringField ( ) <EOL> class SimpleFilter ( filters . BaseFilter ) : <EOL> def apply ( self , query ) : <EOL> query . _applied = True <EOL> return query <EOL> def operation ( self ) : <EOL> return '<STR_LIT:test>' <EOL> class MockModelView ( base . BaseModelView ) : <EOL> def __init__ ( self , model , name = None , category = None , endpoint = None , url = None , <EOL> ** kwargs ) : <EOL> for k , v in iteritems ( kwargs ) : <EOL> setattr ( self , k , v ) <EOL> super ( MockModelView , self ) . __init__ ( model , name , category , endpoint , url ) <EOL> self . created_models = [ ] <EOL> self . updated_models = [ ] <EOL> self . deleted_models = [ ] <EOL> self . search_arguments = [ ] <EOL> self . all_models = { <NUM_LIT:1> : Model ( <NUM_LIT:1> ) , <EOL> <NUM_LIT:2> : Model ( <NUM_LIT:2> ) } <EOL> self . last_id = <NUM_LIT:3> <EOL> def get_pk_value ( self , model ) : <EOL> return model . id <EOL> def scaffold_list_columns ( self ) : <EOL> columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if self . column_exclude_list : <EOL> return filter ( lambda x : x not in self . column_exclude_list , columns ) <EOL> return columns <EOL> def init_search ( self ) : <EOL> return bool ( self . column_searchable_list ) <EOL> def scaffold_filters ( self , name ) : <EOL> return [ SimpleFilter ( name ) ] <EOL> def scaffold_sortable_columns ( self ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def scaffold_form ( self ) : <EOL> return Form <EOL> def get_list ( self , page , sort_field , sort_desc , search , filters ) : <EOL> self . search_arguments . append ( ( page , sort_field , sort_desc , search , filters ) ) <EOL> return len ( self . all_models ) , itervalues ( self . all_models ) <EOL> def get_one ( self , id ) : <EOL> return self . all_models . get ( int ( id ) ) <EOL> def create_model ( self , form ) : <EOL> model = Model ( self . last_id ) <EOL> self . last_id += <NUM_LIT:1> <EOL> form . populate_obj ( model ) <EOL> self . created_models . append ( model ) <EOL> self . all_models [ model . id ] = model <EOL> return True <EOL> def update_model ( self , form , model ) : <EOL> form . populate_obj ( model ) <EOL> self . updated_models . append ( model ) <EOL> return True <EOL> def delete_model ( self , model ) : <EOL> self . deleted_models . append ( model ) <EOL> return True <EOL> def setup ( ) : <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = False <EOL> app . secret_key = '<STR_LIT:1>' <EOL> admin = Admin ( app ) <EOL> return app , admin <EOL> def test_mockview ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model ) <EOL> admin . add_view ( view ) <EOL> eq_ ( view . model , Model ) <EOL> eq_ ( view . name , '<STR_LIT>' ) <EOL> eq_ ( view . endpoint , '<STR_LIT>' ) <EOL> eq_ ( view . _sortable_columns , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> eq_ ( view . _create_form_class , Form ) <EOL> eq_ ( view . _edit_form_class , Form ) <EOL> eq_ ( view . _search_supported , False ) <EOL> eq_ ( view . _filters , None ) <EOL> client = app . test_client ( ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> rv = client . post ( '<STR_LIT>' , <EOL> data = dict ( col1 = '<STR_LIT>' , col2 = '<STR_LIT>' , col3 = '<STR_LIT>' ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> eq_ ( len ( view . created_models ) , <NUM_LIT:1> ) <EOL> model = view . created_models . pop ( ) <EOL> eq_ ( model . id , <NUM_LIT:3> ) <EOL> eq_ ( model . col1 , '<STR_LIT>' ) <EOL> eq_ ( model . col2 , '<STR_LIT>' ) <EOL> eq_ ( model . col3 , '<STR_LIT>' ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> data = rv . data . decode ( '<STR_LIT:utf-8>' ) <EOL> ok_ ( '<STR_LIT>' in data ) <EOL> rv = client . post ( '<STR_LIT>' , <EOL> data = dict ( col1 = '<STR_LIT>' , col2 = '<STR_LIT>' , col3 = '<STR_LIT>' ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> eq_ ( len ( view . updated_models ) , <NUM_LIT:1> ) <EOL> model = view . updated_models . pop ( ) <EOL> eq_ ( model . col1 , '<STR_LIT>' ) <EOL> eq_ ( model . col2 , '<STR_LIT>' ) <EOL> eq_ ( model . col3 , '<STR_LIT>' ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> rv = client . post ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> eq_ ( rv . headers [ '<STR_LIT:location>' ] , '<STR_LIT>' ) <EOL> dummy_app = Flask ( '<STR_LIT>' ) <EOL> dispatched_app = DispatcherMiddleware ( dummy_app , { '<STR_LIT>' : app } ) <EOL> dispatched_client = Client ( dispatched_app ) <EOL> app_iter , status , headers = dispatched_client . post ( <EOL> '<STR_LIT>' , <EOL> data = dict ( col1 = '<STR_LIT>' , col2 = '<STR_LIT>' , col3 = '<STR_LIT>' , _continue_editing = '<STR_LIT:True>' ) ) <EOL> eq_ ( status , '<STR_LIT>' ) <EOL> eq_ ( headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> model = view . updated_models . pop ( ) <EOL> eq_ ( model . col1 , '<STR_LIT>' ) <EOL> def test_permissions ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model ) <EOL> admin . add_view ( view ) <EOL> client = app . test_client ( ) <EOL> view . can_create = False <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> view . can_edit = False <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> view . can_delete = False <EOL> rv = client . post ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> def test_templates ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model ) <EOL> admin . add_view ( view ) <EOL> client = app . test_client ( ) <EOL> view . list_template = '<STR_LIT>' <EOL> view . create_template = '<STR_LIT>' <EOL> view . edit_template = '<STR_LIT>' <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . data , b'<STR_LIT>' ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . data , b'<STR_LIT>' ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . data , b'<STR_LIT>' ) <EOL> def test_list_columns ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model , <EOL> column_list = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> column_labels = dict ( col1 = '<STR_LIT>' ) ) <EOL> admin . add_view ( view ) <EOL> eq_ ( len ( view . _list_columns ) , <NUM_LIT:2> ) <EOL> eq_ ( view . _list_columns , [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> client = app . test_client ( ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> data = rv . data . decode ( '<STR_LIT:utf-8>' ) <EOL> ok_ ( '<STR_LIT>' in data ) <EOL> ok_ ( '<STR_LIT>' not in data ) <EOL> def test_exclude_columns ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model , column_exclude_list = [ '<STR_LIT>' ] ) <EOL> admin . add_view ( view ) <EOL> eq_ ( view . _list_columns , [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> client = app . test_client ( ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> data = rv . data . decode ( '<STR_LIT:utf-8>' ) <EOL> ok_ ( '<STR_LIT>' in data ) <EOL> ok_ ( '<STR_LIT>' not in data ) <EOL> def test_sortable_columns ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model , column_sortable_list = [ '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> admin . add_view ( view ) <EOL> eq_ ( view . _sortable_columns , dict ( col1 = '<STR_LIT>' , col2 = '<STR_LIT>' ) ) <EOL> def test_column_searchable_list ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model , column_searchable_list = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> admin . add_view ( view ) <EOL> eq_ ( view . _search_supported , True ) <EOL> def test_column_filters ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model , column_filters = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> admin . add_view ( view ) <EOL> eq_ ( len ( view . _filters ) , <NUM_LIT:2> ) <EOL> eq_ ( view . _filters [ <NUM_LIT:0> ] . name , '<STR_LIT>' ) <EOL> eq_ ( view . _filters [ <NUM_LIT:1> ] . name , '<STR_LIT>' ) <EOL> eq_ ( [ ( f [ '<STR_LIT:index>' ] , f [ '<STR_LIT>' ] ) for f in view . _filter_groups [ u'<STR_LIT>' ] ] , [ ( <NUM_LIT:0> , '<STR_LIT:test>' ) ] ) <EOL> eq_ ( [ ( f [ '<STR_LIT:index>' ] , f [ '<STR_LIT>' ] ) for f in view . _filter_groups [ u'<STR_LIT>' ] ] , [ ( <NUM_LIT:1> , '<STR_LIT:test>' ) ] ) <EOL> def test_filter_list_callable ( ) : <EOL> app , admin = setup ( ) <EOL> flt = SimpleFilter ( '<STR_LIT:test>' , options = lambda : ( ( '<STR_LIT:1>' , '<STR_LIT>' ) , ( '<STR_LIT:2>' , '<STR_LIT>' ) ) ) <EOL> view = MockModelView ( Model , column_filters = [ flt ] ) <EOL> admin . add_view ( view ) <EOL> opts = flt . get_options ( view ) <EOL> eq_ ( len ( opts ) , <NUM_LIT:2> ) <EOL> eq_ ( opts , [ ( '<STR_LIT:1>' , u'<STR_LIT>' ) , ( '<STR_LIT:2>' , u'<STR_LIT>' ) ] ) <EOL> def test_form ( ) : <EOL> pass <EOL> @ wtforms2_and_up <EOL> def test_csrf ( ) : <EOL> from datetime import timedelta <EOL> from wtforms . csrf . session import SessionCSRF <EOL> from wtforms . meta import DefaultMeta <EOL> class SecureForm ( form . BaseForm ) : <EOL> class Meta ( DefaultMeta ) : <EOL> csrf = True <EOL> csrf_class = SessionCSRF <EOL> csrf_secret = b'<STR_LIT>' <EOL> csrf_time_limit = timedelta ( minutes = <NUM_LIT:20> ) <EOL> @ property <EOL> def csrf_context ( self ) : <EOL> return session <EOL> class SecureModelView ( MockModelView ) : <EOL> form_base_class = SecureForm <EOL> def scaffold_form ( self ) : <EOL> return SecureForm <EOL> def get_csrf_token ( data ) : <EOL> data = data . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> token = data . split ( '<STR_LIT:">' ) [ <NUM_LIT:0> ] <EOL> return token <EOL> app , admin = setup ( ) <EOL> view = SecureModelView ( Model , endpoint = '<STR_LIT>' ) <EOL> admin . add_view ( view ) <EOL> client = app . test_client ( ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> ok_ ( u'<STR_LIT>' in rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> csrf_token = get_csrf_token ( rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> rv = client . post ( '<STR_LIT>' , data = dict ( name = '<STR_LIT>' ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> rv = client . post ( '<STR_LIT>' , data = dict ( name = '<STR_LIT>' , <EOL> csrf_token = csrf_token ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> ok_ ( u'<STR_LIT>' in rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> csrf_token = get_csrf_token ( rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> rv = client . post ( '<STR_LIT>' , <EOL> data = dict ( name = '<STR_LIT>' ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> rv = client . post ( '<STR_LIT>' , <EOL> data = dict ( name = '<STR_LIT>' , csrf_token = csrf_token ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> ok_ ( u'<STR_LIT>' in rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> csrf_token = get_csrf_token ( rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> rv = client . post ( '<STR_LIT>' , <EOL> data = dict ( id = "<STR_LIT:1>" , url = "<STR_LIT>" ) , follow_redirects = True ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> ok_ ( u'<STR_LIT>' not in rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> ok_ ( u'<STR_LIT>' in rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> rv = client . post ( '<STR_LIT>' , <EOL> data = dict ( id = "<STR_LIT:1>" , url = "<STR_LIT>" , csrf_token = csrf_token ) , <EOL> follow_redirects = True ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> ok_ ( u'<STR_LIT>' in rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_custom_form ( ) : <EOL> app , admin = setup ( ) <EOL> class TestForm ( form . BaseForm ) : <EOL> pass <EOL> view = MockModelView ( Model , form = TestForm ) <EOL> admin . add_view ( view ) <EOL> eq_ ( view . _create_form_class , TestForm ) <EOL> eq_ ( view . _edit_form_class , TestForm ) <EOL> ok_ ( not hasattr ( view . _create_form_class , '<STR_LIT>' ) ) <EOL> def check_class_name ( ) : <EOL> class DummyView ( MockModelView ) : <EOL> pass <EOL> view = DummyView ( Model ) <EOL> eq_ ( view . name , '<STR_LIT>' ) </s>
