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

我正在尝试将测试添加到我的基本应用程序中.访问所有内容需要登录.

这是我的测试用例类:

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)

相关文章

我最近重新拾起了计算机视觉,借助Python的opencv还有face_r...
说到Pooling,相信学习过CNN的朋友们都不会感到陌生。Poolin...
记得大一学Python的时候,有一个题目是判断一个数是否是复数...
文章目录 3 直方图Histogramplot1. 基本直方图的绘制 Basic ...
文章目录 5 小提琴图Violinplot1. 基础小提琴图绘制 Basic v...
文章目录 4 核密度图Densityplot1. 基础核密度图绘制 Basic ...