Изменить сессию для pytest - PullRequest
0 голосов
/ 05 апреля 2020

Я пытаюсь изменить session, добавив some_id для проверки страницы, но моя модификация в сеансе не показывает, если я распечатываю session в @id_required во время тестирования, и запрос перенаправляется на страницу входа (не загружается /download). Как я могу изменить сеанс так, чтобы он мог загружать /download

def create_app(*args, **kwargs) -> Flask:
    app = Flask(__name__, *args, **kwargs)
    app.config.from_pyfile('config.py')

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, World!'

    return app

приспособление pytest

@pytest.fixture(name='app', scope='session')
def _app():
    """Flask app instance spun up in separate process."""
    import multiprocessing
    app = create_app()

    def run():
        app.run('0.0.0.0')

    with app.test_client() as c:
        with c.session_transaction() as session:
            session['some_id'] = '1'

            d = multiprocessing.Process(target=run)
            d.start()
            print('Checking for web server...')
            assert requests.get('http://localhost:5000/hello').status_code == 200
            yield app
            d.terminate()

Test

def test_app_connection(app):
    resp = requests.get('http://localhost:5000/download')
    assert 'Download' in resp.text

View

def id_required(func):
    @wraps(func)
    def wrap(*args, **kwargs):
        if 'tech_id' in session:
            return func(*args, **kwargs)
        else:
            return redirect(url_for('login.login'))


@app.route('/download', methods=['GET', 'POST'])
@id_required # <- Checks if 'some_id' in session else redirects to login page
def download():
    ...
    return render_template('download.html', some_id=str(session['some_id']))
...