<s> from __future__ import print_function <EOL> import authcode <EOL> from sqlalchemy_wrapper import SQLAlchemy <EOL> from helpers import SECRET_KEY <EOL> def test_user_model ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , roles = True ) <EOL> assert auth . users_model_name == '<STR_LIT>' <EOL> assert auth . roles_model_name == '<STR_LIT>' <EOL> User = auth . User <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> db . commit ( ) <EOL> assert user . login == u'<STR_LIT>' <EOL> assert user . email == user . login <EOL> assert hasattr ( user , '<STR_LIT:password>' ) <EOL> assert hasattr ( user , '<STR_LIT>' ) <EOL> assert repr ( user ) == '<STR_LIT>' <EOL> def test_user_model_to_dict ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , roles = True ) <EOL> User = auth . User <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> db . commit ( ) <EOL> user_dict = user . to_dict ( ) <EOL> assert user_dict <EOL> def test_backwards_compatibility ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db ) <EOL> User = auth . User <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> db . commit ( ) <EOL> assert user . _password == user . password <EOL> user . _password = '<STR_LIT>' <EOL> assert user . password == '<STR_LIT>' <EOL> def test_user_model_methods ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db ) <EOL> User = auth . User <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> db . commit ( ) <EOL> assert User . by_id ( user . id ) == user <EOL> assert User . by_id ( <NUM_LIT> ) is None <EOL> assert User . by_login ( u'<STR_LIT>' ) == user <EOL> assert User . by_login ( u'<STR_LIT>' ) is None <EOL> assert user . has_password ( '<STR_LIT>' ) <EOL> assert not user . has_password ( '<STR_LIT>' ) <EOL> assert user . get_token ( ) <EOL> assert user . get_uhmac ( ) <EOL> def test_set_raw_password ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , roles = True ) <EOL> User = auth . User <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> assert user . password != '<STR_LIT>' <EOL> user . set_raw_password ( '<STR_LIT>' ) <EOL> assert user . password == '<STR_LIT>' <EOL> def test_role_model ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , roles = True ) <EOL> Role = auth . Role <EOL> db . create_all ( ) <EOL> role = Role ( name = u'<STR_LIT>' ) <EOL> db . session . add ( role ) <EOL> db . commit ( ) <EOL> assert role . name == u'<STR_LIT>' <EOL> assert repr ( role ) == '<STR_LIT>' <EOL> def test_role_model_to_dict ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , roles = True ) <EOL> Role = auth . Role <EOL> db . create_all ( ) <EOL> role = Role ( name = u'<STR_LIT>' ) <EOL> db . session . add ( role ) <EOL> db . commit ( ) <EOL> role_dict = role . to_dict ( ) <EOL> assert role_dict <EOL> def test_role_model_methods ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , roles = True ) <EOL> Role = auth . Role <EOL> db . create_all ( ) <EOL> role = Role ( name = u'<STR_LIT>' ) <EOL> db . session . add ( role ) <EOL> db . commit ( ) <EOL> assert Role . by_id ( role . id ) == role <EOL> assert Role . by_id ( <NUM_LIT> ) is None <EOL> assert Role . by_name ( u'<STR_LIT>' ) == role <EOL> assert Role . by_name ( u'<STR_LIT>' ) is None <EOL> assert Role . get_or_create ( u'<STR_LIT>' ) == role <EOL> role2 = Role . get_or_create ( u'<STR_LIT>' ) <EOL> db . commit ( ) <EOL> assert role2 != role <EOL> assert db . query ( Role ) . count ( ) == <NUM_LIT:2> <EOL> assert list ( role . users ) == [ ] <EOL> assert list ( role2 . users ) == [ ] <EOL> def test_add_role ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , roles = True ) <EOL> User = auth . User <EOL> Role = auth . Role <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> role = Role ( name = u'<STR_LIT>' ) <EOL> db . session . add ( role ) <EOL> db . session . commit ( ) <EOL> assert hasattr ( auth , '<STR_LIT>' ) <EOL> assert hasattr ( User , '<STR_LIT>' ) <EOL> user . add_role ( '<STR_LIT>' ) <EOL> db . session . commit ( ) <EOL> assert user . has_role ( '<STR_LIT>' ) <EOL> assert db . query ( Role ) . count ( ) == <NUM_LIT:2> <EOL> assert list ( user . roles ) == [ Role . by_name ( '<STR_LIT>' ) ] <EOL> user . add_role ( '<STR_LIT>' ) <EOL> db . session . commit ( ) <EOL> assert user . has_role ( '<STR_LIT>' ) <EOL> assert db . query ( Role ) . count ( ) == <NUM_LIT:2> <EOL> assert list ( user . roles ) == [ Role . by_name ( '<STR_LIT>' ) ] <EOL> user . add_role ( '<STR_LIT>' ) <EOL> db . session . commit ( ) <EOL> assert user . has_role ( '<STR_LIT>' ) <EOL> result = sorted ( [ role . name for role in user . roles ] ) <EOL> assert result == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert db . query ( Role ) . count ( ) == <NUM_LIT:2> <EOL> def test_remove_role ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , roles = True ) <EOL> User = auth . User <EOL> Role = auth . Role <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> assert hasattr ( auth , '<STR_LIT>' ) <EOL> assert hasattr ( User , '<STR_LIT>' ) <EOL> user . add_role ( '<STR_LIT>' ) <EOL> db . session . commit ( ) <EOL> assert user . has_role ( '<STR_LIT>' ) <EOL> assert db . query ( Role ) . count ( ) == <NUM_LIT:1> <EOL> user . remove_role ( '<STR_LIT>' ) <EOL> db . session . commit ( ) <EOL> assert not user . has_role ( '<STR_LIT>' ) <EOL> assert list ( user . roles ) == [ ] <EOL> assert db . query ( Role ) . count ( ) == <NUM_LIT:1> <EOL> user . remove_role ( '<STR_LIT>' ) <EOL> db . session . commit ( ) <EOL> assert not user . has_role ( '<STR_LIT>' ) <EOL> assert list ( user . roles ) == [ ] <EOL> assert db . query ( Role ) . count ( ) == <NUM_LIT:1> <EOL> user . remove_role ( '<STR_LIT>' ) <EOL> db . session . commit ( ) <EOL> assert db . query ( Role ) . count ( ) == <NUM_LIT:1> <EOL> def test_models_mixins ( ) : <EOL> db = SQLAlchemy ( '<STR_LIT>' ) <EOL> class UserMixin ( object ) : <EOL> email = db . Column ( db . Unicode ( <NUM_LIT> ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class RoleMixin ( object ) : <EOL> description = db . Column ( db . UnicodeText ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db , UserMixin = UserMixin , RoleMixin = RoleMixin ) <EOL> User = auth . User <EOL> Role = auth . Role <EOL> db . create_all ( ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' , email = u'<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> db . flush ( ) <EOL> assert User . __tablename__ == '<STR_LIT>' <EOL> assert user . login == u'<STR_LIT>' <EOL> assert user . email == u'<STR_LIT>' <EOL> assert hasattr ( user , '<STR_LIT:password>' ) <EOL> assert hasattr ( user , '<STR_LIT>' ) <EOL> assert repr ( user ) == '<STR_LIT>' <EOL> assert hasattr ( Role , '<STR_LIT:description>' ) <EOL> def test_naked_sqlalchemy ( ) : <EOL> from sqlalchemy import create_engine <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy . orm import scoped_session , sessionmaker <EOL> engine = create_engine ( '<STR_LIT>' ) <EOL> class DB ( object ) : <EOL> Session = scoped_session ( sessionmaker ( bind = engine ) ) <EOL> Model = declarative_base ( ) <EOL> @ property <EOL> def session ( self ) : <EOL> return self . Session ( ) <EOL> db = DB ( ) <EOL> auth = authcode . Auth ( SECRET_KEY , db = db ) <EOL> User = auth . User <EOL> db . Model . metadata . create_all ( bind = engine ) <EOL> user = User ( login = u'<STR_LIT>' , password = '<STR_LIT>' ) <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> assert User . by_id ( user . id ) == user <EOL> assert User . by_id ( <NUM_LIT> ) is None <EOL> assert User . by_login ( u'<STR_LIT>' ) == user <EOL> assert User . by_login ( u'<STR_LIT>' ) is None <EOL> assert user . has_password ( '<STR_LIT>' ) <EOL> assert not user . has_password ( '<STR_LIT>' ) <EOL> assert user . get_token ( ) <EOL> assert user . get_uhmac ( ) </s>
