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

Pat*_*Yan 5 python flask flask-login flask-security python-unittest

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

这是我的测试用例类:

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()
Run Code Online (Sandbox Code Playgroud)

我正在运行我的测试使用终端中的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)
Run Code Online (Sandbox Code Playgroud)

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

我在这个setUp过程中登录,所以test_authenticated_access 应该让我访问/myroute/或重定向到登录页面,并显示闪烁的消息"请登录访问此页面".但事实并非如此.

我无法弄清楚出了什么问题.我的测试基于我在Flask文档和此应用程序样板中找到的测试

Pat*_*Yan 3

经过一周的自杀后,我终于想通了……

有几个问题:

  1. Flask-Security 和 Flask-SSLify 之间存在一些冲突。尽管自动 https 重定向在实际的 Web 服务器上运行良好,但在测试中它会完全阻止登录。我通过制作假测试路线并尝试POST随机数据来解决这个问题。POST在测试客户端中没有工作。为了解决这个问题,我必须将我的测试配置更改DEBUG为True. 请注意,这不是一个很好的解决方法,因为 CSS 和 JS 等内容不会被编译,并且其他应用程序行为可能会有所不同......

  2. 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
Run Code Online (Sandbox Code Playgroud)

这是我的测试用例:

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, 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 'matt@lp.com' #self.user.email
        password = password or 'password'
        data = {
            'email': email,
            'password': password,
            'remember': 'y'
            }
        return self._post('/login', data=data)
Run Code Online (Sandbox Code Playgroud)