本文整理汇总了Python中sqlalchemy_wrapper.SQLAlchemy类的典型用法代码示例。如果您正苦于以下问题:Python SQLAlchemy类的具体用法?Python SQLAlchemy怎么用?Python SQLAlchemy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SQLAlchemy类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_authenticate_with_token
def test_authenticate_with_token():
from time import time
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, token_life=3 * 60)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.session.commit()
token = user.get_token()
auth_user = auth.authenticate({'token': token})
assert auth_user
token = '555' + user.get_token()
auth_user = auth.authenticate({'token': token})
assert not auth_user
auth_user = auth.authenticate({'token': ''})
assert not auth_user
timestamp = int(time()) - auth.token_life + 1
token = user.get_token(timestamp)
auth_user = auth.authenticate({'token': token})
assert auth_user
timestamp = int(time()) - auth.token_life - 1
token = user.get_token(timestamp)
auth_user = auth.authenticate({'token': token})
assert not auth_user
开发者ID:jpscaletti,项目名称:authcode,代码行数:32,代码来源:test_auth.py
示例2: test_replace_hash_password_method
def test_replace_hash_password_method():
"""Can the library work the same with custom ``has_password`` and
``password_is_valid`` methods?
"""
class CustomAuth(authcode.Auth):
def hash_password(self, secret):
secret = self.prepare_password(secret)
return secret[::-1]
def password_is_valid(self, secret, hashed):
secret = self.prepare_password(secret)
if secret is None or hashed is None:
return False
return self.hash_password(secret) == hashed
db = SQLAlchemy('sqlite:///:memory:')
auth = CustomAuth(SECRET_KEY, db=db)
User = auth.User
db.create_all()
credentials = {'login': u'meh', 'password': 'foobar'}
user = User(**credentials)
db.session.add(user)
db.session.commit()
assert user.password == 'foobar'[::-1]
assert user.has_password('foobar')
auth_user = auth.authenticate(credentials)
assert user.login == auth_user.login
auth_user = auth.authenticate({})
assert not auth_user
开发者ID:jpscaletti,项目名称:authcode,代码行数:34,代码来源:test_auth.py
示例3: test_authenticate_with_password
def test_authenticate_with_password():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db)
User = auth.User
db.create_all()
credentials = {'login': u'meh', 'password': 'foobar'}
user = User(**credentials)
db.session.add(user)
db.session.commit()
auth_user = auth.authenticate(credentials)
assert user.login == auth_user.login
auth_user = auth.authenticate({})
assert not auth_user
auth_user = auth.authenticate({'login': u'meh'})
assert not auth_user
auth_user = auth.authenticate({'login': u'wtf', 'password': 'foobar'})
assert not auth_user
auth_user = auth.authenticate({'login': u'meh', 'password': 'lalala'})
assert not auth_user
开发者ID:jpscaletti,项目名称:authcode,代码行数:26,代码来源:test_auth.py
示例4: test_models_mixins
def test_models_mixins():
db = SQLAlchemy('sqlite:///:memory:')
class UserMixin(object):
email = db.Column(db.Unicode(300))
def __repr__(self):
return 'overwrited'
class RoleMixin(object):
description = db.Column(db.UnicodeText)
auth = authcode.Auth(SECRET_KEY, db=db, UserMixin=UserMixin, RoleMixin=RoleMixin)
User = auth.User
Role = auth.Role
db.create_all()
user = User(login=u'meh', password='foobar', email=u'[email protected]')
db.session.add(user)
db.flush()
assert User.__tablename__ == 'users'
assert user.login == u'meh'
assert user.email == u'[email protected]'
assert hasattr(user, 'password')
assert hasattr(user, 'last_sign_in')
assert repr(user) == 'overwrited'
assert hasattr(Role, 'description')
开发者ID:jpscaletti,项目名称:authcode,代码行数:29,代码来源:test_models.py
示例5: test_user_has_empty_password
def test_user_has_empty_password():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, password_minlen=0)
User = auth.User
db.create_all()
user = User(login=u'meh', password=u'')
db.session.add(user)
db.session.commit()
assert user.password != u''
auth_user = auth.authenticate({'login': u'meh', 'password': u''})
assert auth_user
auth_user = auth.authenticate({})
assert not auth_user
auth_user = auth.authenticate({'login': u'meh', 'password': None})
assert not auth_user
auth_user = auth.authenticate({'login': u'meh'})
assert not auth_user
auth_user = auth.authenticate({'login': u'wtf', 'password': ''})
assert not auth_user
auth_user = auth.authenticate({'login': u'meh', 'password': 'lalala'})
assert not auth_user
开发者ID:jpscaletti,项目名称:authcode,代码行数:30,代码来源:test_auth.py
示例6: test_init_app
def test_init_app():
class FakeApp(object):
pass
app = FakeApp()
db = SQLAlchemy(URI1, app)
assert app.databases
db.init_app(app)
assert len(app.databases) == 1
开发者ID:aadu,项目名称:sqlalchemy-wrapper,代码行数:9,代码来源:test_main.py
示例7: test_define_table
def test_define_table():
db = SQLAlchemy(URI1)
db.Table('foobar',
db.Column('foo', db.UnicodeText),
db.Column('bar', db.UnicodeText))
db.Table('fizzbuzz', db.metadata,
db.Column('fizz', db.Integer),
db.Column('buzz', db.Integer))
db.create_all()
开发者ID:aadu,项目名称:sqlalchemy-wrapper,代码行数:9,代码来源:test_main.py
示例8: _get_flask_app
def _get_flask_app(roles=False, **kwargs):
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(
SECRET_KEY, db=db, roles=roles, password_minlen=3, **kwargs)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.add(user)
user2 = User(login=u'foo', password='bar')
db.add(user2)
db.commit()
app = Flask('test')
app.secret_key = os.urandom(32)
app.testing = True
@app.route('/protected/')
@auth.protected()
def protected():
return u'Welcome'
authcode.setup_for_flask(auth, app)
auth.session = {}
return auth, app, user
开发者ID:jpscaletti,项目名称:authcode,代码行数:26,代码来源:test_views.py
示例9: test_user_model_to_dict
def test_user_model_to_dict():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.commit()
user_dict = user.to_dict()
assert user_dict
开发者ID:jpscaletti,项目名称:authcode,代码行数:11,代码来源:test_models.py
示例10: test_role_model
def test_role_model():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
Role = auth.Role
db.create_all()
role = Role(name=u'admin')
db.session.add(role)
db.commit()
assert role.name == u'admin'
assert repr(role) == '<Role admin>'
开发者ID:jpscaletti,项目名称:authcode,代码行数:11,代码来源:test_models.py
示例11: test_automatic_case_insensitiveness
def test_automatic_case_insensitiveness():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db)
User = auth.User
db.create_all()
user = User(login=u'MeH', password='foobar')
db.session.add(user)
db.session.commit()
assert user.login == u'meh'
assert User.by_login(u'MEH') == User.by_login(u'MeH') == user
开发者ID:jpscaletti,项目名称:authcode,代码行数:11,代码来源:test_auth.py
示例12: test_backwards_compatibility
def test_backwards_compatibility():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.commit()
assert user._password == user.password
user._password = 'raw'
assert user.password == 'raw'
开发者ID:jpscaletti,项目名称:authcode,代码行数:12,代码来源:test_models.py
示例13: test_set_raw_password
def test_set_raw_password():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.session.commit()
assert user.password != 'foobar'
user.set_raw_password('foobar')
assert user.password == 'foobar'
开发者ID:jpscaletti,项目名称:authcode,代码行数:12,代码来源:test_models.py
示例14: test_id_mixin
def test_id_mixin():
db = SQLAlchemy(URI1)
class IDMixin(object):
id = db.Column(db.Integer, primary_key=True)
class Model(db.Model, IDMixin):
field = db.Column(db.String)
db.create_all()
assert Model.__tablename__ == 'models'
assert hasattr(Model, 'id')
开发者ID:aadu,项目名称:sqlalchemy-wrapper,代码行数:13,代码来源:test_main.py
示例15: test_get_uhmac
def test_get_uhmac():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.session.commit()
assert user.get_uhmac()
assert user.get_uhmac() == user.get_uhmac()
开发者ID:jpscaletti,项目名称:authcode,代码行数:13,代码来源:test_auth.py
示例16: test_automatic_password_hashing
def test_automatic_password_hashing():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, hash='pbkdf2_sha512', rounds=10)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.session.commit()
assert user.password
assert user.password != 'foobar'
assert user.has_password('foobar')
开发者ID:jpscaletti,项目名称:authcode,代码行数:14,代码来源:test_auth.py
示例17: test_login_logout
def test_login_logout():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.session.commit()
session = {}
auth.login(user, session=session)
print(session)
assert session[auth.session_key] == user.get_uhmac()
auth.logout(session=session)
assert auth.session_key not in session
开发者ID:jpscaletti,项目名称:authcode,代码行数:17,代码来源:test_auth.py
示例18: test_user_model
def test_user_model():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
assert auth.users_model_name == 'User'
assert auth.roles_model_name == 'Role'
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.commit()
assert user.login == u'meh'
assert user.email == user.login
assert hasattr(user, 'password')
assert hasattr(user, 'last_sign_in')
assert repr(user) == '<User meh>'
开发者ID:jpscaletti,项目名称:authcode,代码行数:17,代码来源:test_models.py
示例19: test_sql_injection
def test_sql_injection():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.session.commit()
tests = [
"1'; DELETE FROM users",
'1"; DELETE FROM users',
"1' --",
]
for passw in tests:
user.set_raw_password(passw)
db.session.commit()
assert user.password == passw
开发者ID:jpscaletti,项目名称:authcode,代码行数:18,代码来源:test_auth.py
示例20: test_disable_update_on_authenticate
def test_disable_update_on_authenticate():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, hash='pbkdf2_sha512',
update_hash=False)
User = auth.User
db.create_all()
credentials = {'login': u'meh', 'password': 'foobar'}
user = User(**credentials)
db.session.add(user)
db.session.commit()
deprecated_hash = ph.hex_sha1.encrypt(credentials['password'])
assert user.password != deprecated_hash
user.set_raw_password(deprecated_hash)
db.session.commit()
assert user.password == deprecated_hash
auth_user = auth.authenticate(credentials)
assert auth_user.password == deprecated_hash
开发者ID:jpscaletti,项目名称:authcode,代码行数:20,代码来源:test_auth.py
注:本文中的sqlalchemy_wrapper.SQLAlchemy类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论