加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – (unittest)测试Flask-Security:无法通过登录页面

发布时间:2020-12-16 21:30:20 所属栏目:Python 来源:网络整理
导读:我正在尝试将测试添加到我的基本应用程序中.访问所有内容需要登录. 这是我的测试用例类: class MyAppTestCase(FlaskTestCaseMixin): def _create_app(self): raise NotImplementedError def _create_fixtures(self): self.user = EmployeeFactory() def set
我正在尝试将测试添加到我的基本应用程序中.访问所有内容需要登录.

这是我的测试用例类:

class MyAppTestCase(FlaskTestCaseMixin):

    def _create_app(self):
        raise NotImplementedError

    def _create_fixtures(self):
        self.user = EmployeeFactory()

    def setUp(self):
        super(MyAppTestCase,self).setUp()
        self.app = self._create_app()
        self.client = self.app.test_client()
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        self._create_fixtures()
        self._create_csrf_token()

    def tearDown(self):
        super(MyAppTestCase,self).tearDown()
        db.drop_all()
        self.app_context.pop()

    def _post(self,route,data=None,content_type=None,follow_redirects=True,headers=None):
        content_type = content_type or 'application/x-www-form-urlencoded'
        return self.client.post(route,data=data,follow_redirects=follow_redirects,content_type=content_type,headers=headers)

    def _login(self,email=None,password=None):
        email = email or self.user.email
        password = password or 'password'
        data = {
            'email': email,'password': password,'remember': 'y'
            }
        return self._post('/login',data=data)


class MyFrontendTestCase(MyAppTestCase):

    def _create_app(self):
        return create_app(settings)

    def setUp(self):
        super(MyFrontendTestCase,self).setUp()
        self._login()

我正在运行我的测试在终端中使用nosetests,如下所示:source my_env / bin / activate&& nosetests –exe

像这样的基本测试失败:

class CoolTestCase(MyFrontendTestCase):

    def test_logged_in(self):
        r = self._login()
        self.assertIn('MyAppName',r.data)

    def test_authenticated_access(self):
        r = self.get('/myroute/')
        self.assertIn('MyAppName',r.data)

从输出中,我看到r.data只是登录页面的HTML,没有错误(例如,错误的用户名或密码)或警报(“请登录以访问此页面”).

我在setUp进程中登录,所以test_authenticated_access应该让我访问/ myroute /或者使用闪烁的消息“请登录访问此页面”将我重定向到登录页面.但事实并非如此.

我无法弄清楚出了什么问题.我的测试基于我在Flask文档和this app boilerplate中找到的测试

解决方法

经过一周的自杀之后,我终于明白了…

有几个问题:

> Flask-Security和Flask-SSLify之间存在一些冲突.虽然自动https重定向在实际的Web服务器上运行良好,但在测试中它可以防止完全登录.我通过制作假测试路线并尝试POST随机数据来解决这个问题.测试客户端没有POST工作.为了解决这个问题,我不得不将测试配置DEBUG更改为True.请注意,这不是一个很好的解决方法,因为CSS和JS之类的东西不会编译,其他应用程序行为可能会有所不同……
> Flask-Security不适用于factory_boy(或者我没有正确使用factory_boy?).我试图通过factory_boy创建用户和角色,因为我看到了一些使用它的示例应用程序教程.在我解决了上述问题之后,它一直告诉我用户不存在.我最终窃取了Flask-Security自己的测试代码,用于创建具有不同角色的假用户.

有问题的代码:

session = db.create_scoped_session()

class RoleFactory(SQLAlchemyModelFactory):
    FACTORY_FOR = Role
    FACTORY_SESSION = session

    id = Sequence(int)
    name = 'admin'
    description = 'Administrator'


class EmployeeFactory(SQLAlchemyModelFactory):
    FACTORY_FOR = Employee
    FACTORY_SESSION = session

    id = Sequence(int)
    email = Sequence(lambda n: 'user{0}@app.com'.format(n))
    password = LazyAttribute(lambda a: encrypt_password('password'))
    username = Sequence(lambda n: 'user{0}'.format(n))
    #last_login_at = datetime.utcnow()
    #current_login_at = datetime.utcnow()
    last_login_ip = '127.0.0.1'
    current_login_ip = '127.0.0.1'
    login_count = 1
    roles = LazyAttribute(lambda _: [RoleFactory()])
    active = True

这是我的测试用例:

class MyAppTestCase(FlaskTestCaseMixin,MyTestCase):

    def _create_app(self):
        raise NotImplementedError

    def _create_fixtures(self):
        #self.user = EmployeeFactory()
        populate_data(1)

    def setUp(self):
        super(MyAppTestCase,password=None):
        email = email or 'matt@lp.com' #self.user.email
        password = password or 'password'
        data = {
            'email': email,data=data)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读