current_user возвращает анонимного пользователя во время тестирования приложения flask с pytest - PullRequest
0 голосов
/ 17 марта 2020

Я пытаюсь создать тесты для моего flask приложения, используя pytest. и при попытке проверить представление с помощью current_user current_user всегда является анонимным.

, прикрепляя код ниже:

views.py

@issue.route('/support', methods=['GET', 'POST'])
@login_required
@role_required('admin', 'agent')
def support():
    # Pre-populate the email field if the user is signed in.
    form = SupportForm(obj=current_user)

    if form.validate_on_submit():
        i = Issue()
        i.partnership_id = current_user.partnership_id
        i.partnership_account_id = current_user.partnership_account_id

        form.populate_obj(i)
        i.save()

        # This prevents circular imports.
        from buyercall.blueprints.issue.tasks import deliver_support_email

        deliver_support_email.delay(i.id)

        flash(_('Help is on the way, expect a response shortly.'), 'success')
        return redirect(url_for('issue.support'))

    return render_template('issue/support.jinja2', form=form)

conftest.py

@pytest.yield_fixture(scope='session')
def app():
    """
    Setup our flask test app, this only gets executed once.

    :return: Flask app
    """
    db_uri = '{0}_test'.format(settings.SQLALCHEMY_DATABASE_URI)
    params = {
        'DEBUG': True,
        'TESTING': True,
        'LOGIN_DISABLED': True,
        'WTF_CSRF_ENABLED': False,
        'SQLALCHEMY_DATABASE_URI': db_uri
    }

    _app = create_app(settings_override=params)

    # Establish an application context before running the tests.
    with _app.test_request_context():
        ctx = _app.app_context()
        ctx.push()
        yield _app
        ctx.pop()


@pytest.yield_fixture(scope='function')
def client(app):
    """
    Setup an app client, this gets executed for each test function.

    :param app: Pytest fixture
    :return: Flask app client
    """
    yield app.test_client()

test_views.py

class TestSettings(ViewTestMixin):
   def test_support_page_while_logged_in(self):
       """ Support page renders successfully with pre-populated e-mail. """
       self.login()
       response = self.client.get(url_for('issue.support'))

       assert_status_with_message(200, response,
                                  'value="admin@localhost.com"')

при запуске pytest ниже ошибка.

AttributeError: у объекта 'AnonymousUserMixin' нет атрибута 'role'

как я могу решить эту проблему? заранее спасибо

...