5

我正在尝试向我的基本应用程序添加测试。访问所有内容都需要登录。

这是我的测试用例类:

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()

我正在运行我的测试,在终端中使用鼻子测试,如下所示: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 文档和这个应用程序样板中找到的那些

4

2 回答 2

2

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

有几个问题:

  1. Flask-Security 和 Flask-SSLify 之间存在一些冲突。尽管自动 https 重定向在实际 Web 服务器上运行良好,但在测试中它会完全阻止登录。我通过制作虚假的测试路线并尝试POST随机数据来解决这个问题。没有POST在测试客户端工作。为了解决这个问题,我不得不将我的测试配置更改DEBUGTrue. 请注意,这不是一个很好的解决方法,因为 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

这是我的测试用例:

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)
于 2014-04-30T13:23:09.743 回答
2

在使用 Flask-Security(使用 Flask-Login)应用程序进行测试时,我也遇到了登录/注销问题。我通过模拟flask_login._get_user函数并设置app.login_manager._login_disabled为 True 解决了这个问题。

模拟用户的上下文管理器:

class LoginLogoutMixin(object):
    @contextmanager
    def login(self, user):
        mock_get_user = patch('flask_login._get_user', Mock(return_value=user))
        self.app.login_manager._login_disabled = True
        mock_get_user.start()
        yield
        mock_get_user.stop()
        self.app.login_manager._login_disabled = False

以及它的用法示例:

class ProtectedViewTestCase(LoginLogoutMixin, TestCase):

    def setUp(self):
        super(ProtectedViewTestCase, self).setUp()
        # create user here

    def test_protected_view(self):
        with self.login(self.user):
            rv = self.client.get(url_for('protected_view'))
            self.assert200(rv)
于 2015-02-24T17:48:00.233 回答