Beispiel #1
0
    def setUp(self):
        from sqlalchemy import Column, Integer, String, create_engine, orm
        from sqlalchemy.schema import MetaData
        from sqlalchemy.ext.declarative import declarative_base
        metadata = MetaData()
        Model = declarative_base(metadata=metadata)
        class User(Model):
            __tablename__ = 'users'
            id = Column(Integer, primary_key=True)
            login = Column(String(255), nullable=False, unique=True)
            password = Column(String(255), nullable=False)
        auth = SqlaModelAuth(User)

        class Env(web.AppEnvironment):

            @cached_property
            def db(self):
                return orm.sessionmaker(bind=create_engine('sqlite://'))()

            @cached_property
            def template(self):
                return MockTemplateManager()

        @web.request_filter
        def make_env(env, data, nxt):
            metadata.create_all(env.db.bind)
            user = User(login='******', password=encrypt_password('123'))
            env.db.add(user)
            env.db.commit()
            try:
                return nxt(env, data)
            finally:
                env.db.close()

        def anonymouse(env, data):
            self.assertTrue(hasattr(env, 'user'))
            self.assertEqual(env.user, None)
            return web.Response('ok')

        def no_anonymouse(env, data):
            self.assertTrue(hasattr(env, 'user'))
            self.assertEqual(env.user.login, 'user name')
            return web.Response('ok')

        app = make_env | web.cases(
            auth.login(),
            auth.logout(),
            auth | web.cases(
                web.match('/a', 'a') | anonymouse,
                web.match('/b', 'b') | auth_required | no_anonymouse,
            ),
        )
        self.app = web.Application(app, Env)
Beispiel #2
0
    def setUp(self):
        from sqlalchemy import Column, Integer, String, create_engine, orm
        from sqlalchemy.schema import MetaData
        from sqlalchemy.ext.declarative import declarative_base
        metadata = MetaData()
        Model = declarative_base(metadata=metadata)
        class User(Model):
            __tablename__ = 'users'
            id = Column(Integer, primary_key=True)
            login = Column(String(255), nullable=False, unique=True)
            password = Column(String(255), nullable=False)
        auth = SqlaModelAuth(User)

        class Env(web.AppEnvironment):

            @cached_property
            def db(self):
                return orm.sessionmaker(bind=create_engine('sqlite://'))()

            @cached_property
            def template(self):
                return MockTemplateManager()

        @web.request_filter
        def make_env(env, data, nxt):
            metadata.create_all(env.db.bind)
            user = User(login='******', password=encrypt_password('123'))
            env.db.add(user)
            env.db.commit()
            try:
                return nxt(env, data)
            finally:
                env.db.close()

        def anonymouse(env, data):
            self.assertTrue(hasattr(env, 'user'))
            self.assertEqual(env.user, None)
            return web.Response('ok')

        def no_anonymouse(env, data):
            self.assertTrue(hasattr(env, 'user'))
            self.assertEqual(env.user.login, 'user name')
            return web.Response('ok')

        app = make_env | web.cases(
            auth.login(),
            auth.logout(),
            auth | web.cases(
                web.match('/a', 'a') | anonymouse,
                web.match('/b', 'b') | auth_required | no_anonymouse,
            ),
        )
        self.app = web.Application(app, Env)
Beispiel #3
0
 def logout(self, redirect_to=None):
     return SqlaModelAuth.logout(self, redirect_to=redirect_to)
Beispiel #4
0
 def logout(self, redirect_to=None):
     return SqlaModelAuth.logout(self, redirect_to=redirect_to)