sqlalchemy_wrapper.SQLAlchemy

Here are the examples of the python api sqlalchemy_wrapper.SQLAlchemy taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

80 Examples 7

Page 1 Selected Page 2

Example 1

Project: sqlalchemy-wrapper Source File: test_main.py
def test_flask_hooks_0_9():
    teardown_appcontext_spy = mock.Mock()

    class AppWithTearDownAppContext(object):
        def teardown_appcontext(self, f):
            teardown_appcontext_spy()

    app = AppWithTearDownAppContext()
    SQLAlchemy(URI1, app=app)
    assert teardown_appcontext_spy.call_count

Example 2

Project: sqlalchemy-wrapper Source File: test_main.py
def test_aggregated_query():
    db = SQLAlchemy(URI1)

    class Unit(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.String(60))
        price = db.Column(db.Integer)

    db.create_all()
    db.add(Unit(price=25))
    db.add(Unit(price=5))
    db.add(Unit(price=10))
    db.add(Unit(price=3))
    db.commit()

    res = db.query(db.func.sum(Unit.price).label('price')).first()
    assert res.price == 43

Example 3

Project: authcode Source File: test_setup_for_bottle.py
def test_setup_bottle_partial_views_1():
    app = Bottle()
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db, views='sign_in sign_out'.split())
    authcode.setup_for_bottle(auth, app)
    names = [route.name for route in app.routes]

    assert 'auth_sign_in' in names
    assert 'auth_sign_out' in names
    assert 'auth_reset_password' not in names
    assert 'auth_change_password' not in names

Example 4

Project: sqlalchemy-wrapper Source File: test_helpers.py
def test_mixin_overwritten_tablename():
    """Test for a tablename defined in a mixin but overwritten.
    """
    db = SQLAlchemy('sqlite://')

    class EmployeeMixin(object):
        __tablename__ = 'mixin_tablename'

        @declared_attr
        def id(cls):
            return db.Column(db.Integer, primary_key=True)

    class Engineer(EmployeeMixin, db.Model):
        __tablename__ = 'mixin_overwritten_tablename'
        name = db.Column(db.String(50))

    assert Engineer.__tablename__ == 'mixin_overwritten_tablename'
    db.session.expunge_all()

Example 5

Project: authcode Source File: test_setup_for_shake.py
def test_setup_for_shake():
    shake = pytest.importorskip("shake")
    app = shake.Shake(__file__, {})
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db)
    authcode.setup_for_shake(auth, app)
    assert auth.render == app.render

Example 6

Project: authcode Source File: test_models.py
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>'

Example 7

Project: sqlalchemy-wrapper Source File: test_main.py
Function: test_query
def test_query():
    db = SQLAlchemy(URI1)
    ToDo = create_test_model(db)
    db.create_all()

    db.add(ToDo('First', 'The text'))
    db.add(ToDo('Second', 'The text'))
    db.flush()

    titles = ' '.join(x.title for x in db.query(ToDo).all())
    assert titles == 'First Second'

    data = db.query(ToDo).filter(ToDo.title == 'First').all()
    assert len(data) == 1

Example 8

Project: sqlalchemy-wrapper Source File: test_main.py
def test_multiple_databases():
    db1 = SQLAlchemy(URI1)
    db2 = SQLAlchemy(URI2)
    ToDo1 = create_test_model(db1)
    ToDo2 = create_test_model(db2)
    db1.create_all()
    db2.create_all()

    db1.add(ToDo1('A', 'a'))
    db1.add(ToDo1('B', 'b'))
    db2.add(ToDo2('Q', 'q'))
    db1.add(ToDo1('C', 'c'))
    db1.commit()
    db2.commit()

    assert db1.query(ToDo1).count() == 3
    assert db2.query(ToDo2).count() == 1

Example 9

Project: sqlalchemy-wrapper Source File: test_main.py
def test_custom_metaclass():

    class _CustomMeta(_BoundDeclarativeMeta):

        def __init__(self, name, bases, dic):
            _BoundDeclarativeMeta.__init__(self, name, bases, dic)
            if hasattr(self, 'id'):
                setattr(self, 'test', 1)

    db = SQLAlchemy(URI1, metaclass=_CustomMeta)

    class Model(db.Model):
        id = db.Column(db.Integer, primary_key=True)

    db.create_all()

    assert Model.test == 1

Example 10

Project: authcode Source File: test_auth.py
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()

Example 11

Project: sqlalchemy-wrapper Source File: test_helpers.py
def test_mixin_no_tablename():
    """Test for a tablename defined in a mixin.
    """
    db = SQLAlchemy('sqlite://')

    class BaseMixin(object):
        @declared_attr
        def id(cls):
            return db.Column(db.Integer, primary_key=True)

    class MEngineer(BaseMixin, db.Model):
        name = db.Column(db.String(50))

    assert MEngineer.__tablename__ == 'm_engineers'
    db.session.expunge_all()

Example 12

Project: authcode Source File: test_setup_for_shake.py
def test_setup_shake_false_render():
    shake = pytest.importorskip("shake")

    app = shake.Shake(__file__, {})
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db)

    authcode.setup_for_shake(auth, app, render=None)
    assert auth.send_email
    assert auth.render == app.render
    assert app.render.env.globals['csrf_token']
    assert app.render.env.globals['auth']

Example 13

Project: authcode Source File: test_models.py
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'

Example 14

Project: sqlalchemy-wrapper Source File: test_main.py
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()

Example 15

Project: authcode Source File: test_auth.py
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')

Example 16

Project: sqlalchemy-wrapper Source File: test_main.py
def test_flask_hooks_old():
    after_request_spy = mock.Mock()
    on_exception_spy = mock.Mock()

    class App(object):
        def after_request(self, f):
            after_request_spy()

        def on_exception(self, f):
            on_exception_spy()

    app = App()
    SQLAlchemy(URI1, app=app)
    assert after_request_spy.call_count
    assert on_exception_spy.call_count

Example 17

Project: authcode Source File: test_setup_for_bottle.py
def test_setup_for_bottle():
    app = Bottle()
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db)
    authcode.setup_for_bottle(auth, app)

    assert auth.render == bottle.template
    assert auth.request is not None
    assert auth.request == bottle.request
    assert bottle.BaseTemplate.defaults['csrf_token']
    assert bottle.BaseTemplate.defaults['auth']

Example 18

Project: sqlalchemy-wrapper Source File: test_main.py
def test_model_helpers():
    db = SQLAlchemy()

    class Row(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.String(60), nullable=False)
        created_at = db.Column(db.DateTime, nullable=False,
                               default=datetime.utcnow)

    db.create_all()
    db.add(Row(name='a'))
    db.flush()
    row = db.query(Row).first()

    assert str(row) == '<Row>'
    assert dict(row)['name'] == 'a'

Example 19

Project: sqlalchemy-wrapper Source File: test_main.py
Function: test_api
def test_api():
    db = SQLAlchemy()
    assert db.metadata == db.Model.metadata
    db.drop_all()
    db.reflect()
    db.rollback()

Example 20

Project: sqlalchemy-wrapper Source File: test_main.py
Function: test_get_engine
def test_get_engine():
    class FakeApp(object):
        pass

    app1 = FakeApp()
    app2 = FakeApp()
    db = SQLAlchemy(URI1, app=app1)

    assert SQLAlchemy().get_engine(app1) == db.engine
    assert SQLAlchemy().get_engine(app2) is None

Example 21

Project: authcode Source File: test_setup_for_bottle.py
def test_setup_bottle_views_urls():
    app = Bottle()
    db = SQLAlchemy('sqlite:///', app)
    config = {
        'url_sign_in': '/ingresar/',
        'url_sign_out': '/salir/',
        'url_reset_password': '/restablecer-contrasena/',
        'url_change_password': '/cambiar-contrasena/',
    }
    auth = authcode.Auth(SECRET_KEY, db=db, **config)

    authcode.setup_for_bottle(auth, app)
    rules = dict((route.name, route.rule) for route in app.routes)

    assert rules['auth_sign_in'] == config['url_sign_in']
    assert rules['auth_sign_out'] == config['url_sign_out']
    assert rules['auth_change_password'] == config['url_change_password']
    assert rules['auth_reset_password'] == config['url_reset_password'] + '<token>/'

Example 22

Project: authcode Source File: test_setup_for_bottle.py
def test_setup_bottle_views_callable_urls():
    app = Bottle()
    db = SQLAlchemy('sqlite:///', app)
    config = {
        'url_sign_in': lambda: '/my-login',
        'url_reset_password': lambda: '/reset-secret',
    }
    auth = authcode.Auth(SECRET_KEY, db=db, **config)

    authcode.setup_for_bottle(auth, app)
    rules = dict((route.name, route.rule) for route in app.routes)

    assert rules['auth_sign_in'] == '/my-login'
    assert rules['auth_reset_password'] == '/reset-secret/<token>/'

Example 23

Project: authcode Source File: test_setup_for_bottle.py
def test_setup_bottle_partial_views_2():
    app = Bottle()
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db, views='change_password'.split())
    authcode.setup_for_bottle(auth, app)
    names = [route.name for route in app.routes]

    assert 'auth_sign_in' not in names
    assert 'auth_sign_out' not in names
    assert 'auth_reset_password' not in names
    assert 'auth_change_password' in names

Example 24

Project: authcode Source File: test_setup_for_shake.py
def test_setup_shake_render():
    shake = pytest.importorskip("shake")

    app = shake.Shake(__file__, {})
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db)

    authcode.setup_for_shake(auth, app)
    assert auth.send_email
    assert auth.render == app.render
    assert app.render.env.globals['csrf_token']
    assert app.render.env.globals['auth']

Example 25

Project: authcode Source File: test_auth.py
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

Example 26

Project: authcode Source File: test_setup_for_shake.py
def test_setup_shake_views_callable_urls():
    shake = pytest.importorskip("shake")

    app = shake.Shake(__file__, {})
    db = SQLAlchemy('sqlite:///', app)
    config = {
        'url_sign_in': lambda: '/my-login',
        'url_reset_password': lambda: '/reset-secret',
    }
    auth = authcode.Auth(SECRET_KEY, db=db, **config)

    authcode.setup_for_shake(auth, app)
    rules = app.url_map._rules
    endpoints = dict([(ru.endpoint.__name__, ru.rule) for ru in rules])

    assert endpoints['auth_sign_in'] == '/my-login'
    assert endpoints['auth_reset_password'] == '/reset-secret/<token>/'

Example 27

Project: authcode Source File: test_setup_for_shake.py
def test_setup_shake_no_views():
    shake = pytest.importorskip("shake")

    app = shake.Shake(__file__, {})
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db, views=[])

    authcode.setup_for_shake(auth, app)
    assert len(app.url_map._rules) == 0

Example 28

Project: authcode Source File: test_models.py
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

Example 29

Project: sqlalchemy-wrapper Source File: test_helpers.py
def test_mixin_tablename():
    """Test for a tablename defined in a mixin.
    """
    db = SQLAlchemy('sqlite://')

    class EmployeeMixin(object):
        __tablename__ = 'mixin_tablename'

        @declared_attr
        def id(cls):
            return db.Column(db.Integer, primary_key=True)

    class Engineer(EmployeeMixin, db.Model):
        name = db.Column(db.String(50))

    assert Engineer.__tablename__ == 'mixin_tablename'
    db.session.expunge_all()

Example 30

Project: authcode Source File: test_auth.py
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

Example 31

Project: sqlalchemy-wrapper Source File: test_helpers.py
def test_declared_attr_mixin_tablename():
    """Test for a tablename defined as a @declared_attr in a mixin.
    """
    db = SQLAlchemy('sqlite://')

    class EmployeeMixin(object):
        @declared_attr
        def __tablename__(cls):
            return 'declared_attr_mixin_tablename'

        @declared_attr
        def id(cls):
            return db.Column(db.Integer, primary_key=True)

    class Engineer(EmployeeMixin, db.Model):
        name = db.Column(db.String(50))

    assert Engineer.__tablename__ == 'declared_attr_mixin_tablename'
    db.session.expunge_all()

Example 32

Project: authcode Source File: test_models.py
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'

Example 33

Project: sqlalchemy-wrapper Source File: test_main.py
Function: 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

Example 34

Project: authcode Source File: test_setup_for_bottle.py
def test_setup_bottle_no_views():
    app = Bottle()
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db, views=[])

    authcode.setup_for_bottle(auth, app)
    assert not len(app.routes)

Example 35

Project: sqlalchemy-wrapper Source File: test_main.py
def test_flask_hooks_0_8():
    teardown_request_spy = mock.Mock()

    class AppWithTearDownRequest(object):
        def teardown_request(self, f):
            teardown_request_spy()

    app = AppWithTearDownRequest()
    SQLAlchemy(URI1, app=app)
    assert teardown_request_spy.call_count

Example 36

Project: authcode Source File: test_models.py
def test_role_model_to_dict():
    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()

    role_dict = role.to_dict()
    assert role_dict

Example 37

Project: sqlalchemy-wrapper Source File: test_main.py
def test_bottle_hooks():
    hook_spy = mock.Mock()

    class App(object):
        def hook(self, name):
            def decorator(f):
                hook_spy()
            return decorator

    app = App()
    SQLAlchemy(URI1, app=app)
    assert hook_spy.call_count

Example 38

Project: authcode Source File: test_auth.py
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

Example 39

Project: sqlalchemy-wrapper Source File: test_main.py
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')

Example 40

Project: authcode Source File: test_auth.py
def test_disabled_case_insensitiveness():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db, case_insensitive=False)
    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 not User.by_login(u'meh')
    assert not User.by_login(u'MEH')
    assert User.by_login(u'MeH') == user

Example 41

Project: authcode Source File: test_models.py
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>'

Example 42

Project: authcode Source File: test_setup_for_shake.py
def test_setup_shake_default_views():
    shake = pytest.importorskip("shake")

    app = shake.Shake(__file__, {})
    db = SQLAlchemy('sqlite:///', app)
    auth = authcode.Auth(SECRET_KEY, db=db)

    authcode.setup_for_shake(auth, app)
    assert len(app.url_map._rules) == 5

Example 43

Project: authcode Source File: test_auth.py
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

Example 44

Project: authcode Source File: test_auth.py
def test_update_on_authenticate():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db, hash='pbkdf2_sha512',
                         update_hash=True)
    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.startswith('$pbkdf2-sha512$')

    deprecated_hash = ph.hex_sha1.encrypt(credentials['password'])
    user.set_raw_password(deprecated_hash)
    db.session.commit()
    assert user.password == deprecated_hash

    auth_user = auth.authenticate(credentials)
    new_hash = auth_user.password
    assert new_hash != deprecated_hash
    assert new_hash.startswith('$pbkdf2-sha512$')

Example 45

Project: authcode Source File: test_auth.py
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

Example 46

Project: authcode Source File: test_auth.py
def test_get_token():
    from time import time, sleep
    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()

    token1 = user.get_token()
    sleep(1)
    token2 = user.get_token()
    assert token1 != token2

    timestamp = time()
    token1 = user.get_token(timestamp)
    token2 = user.get_token(timestamp)
    assert token1 == token2

Example 47

Project: authcode Source File: test_auth.py
def test_prefix():
    db = SQLAlchemy('sqlite:///:memory:')
    auth1 = authcode.Auth(SECRET_KEY, db=db, roles=True, prefix='foobar')

    assert auth1.users_model_name == 'FoobarUser'
    assert auth1.roles_model_name == 'FoobarRole'
    assert auth1.views_prefix == 'foobar_'
    assert auth1.url_sign_in == '/foobar/sign-in/'
    assert auth1.url_sign_out == '/foobar/sign-out/'
    assert auth1.url_reset_password == '/foobar/reset-password/'
    assert auth1.url_change_password == '/foobar/change-password/'

    auth2 = authcode.Auth(SECRET_KEY, db=db, roles=True, prefix='meh')

    assert auth2.users_model_name == 'MehUser'
    assert auth2.roles_model_name == 'MehRole'
    assert auth2.views_prefix == 'meh_'
    assert auth2.url_sign_in == '/meh/sign-in/'
    assert auth2.url_sign_out == '/meh/sign-out/'
    assert auth2.url_reset_password == '/meh/reset-password/'
    assert auth2.url_change_password == '/meh/change-password/'

Example 48

Project: authcode Source File: test_auth.py
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

Example 49

Project: authcode Source File: test_auth.py
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

Example 50

Project: authcode Source File: test_auth.py
def test_user_has_none_password():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db)

    User = auth.User

    db.create_all()
    user = User(login=u'meh', password=None)
    db.session.add(user)
    db.session.commit()

    assert user.password is None

    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
See More Examples - Go to Next Page
Page 1 Selected Page 2