Skip to content

Commit

Permalink
tests: refactor to have app creation in conftest
Browse files Browse the repository at this point in the history
* the init_common has to be called too. this is necessary that the test
  with the extend security forms is working.
  • Loading branch information
utnapischtim committed Jun 3, 2023
1 parent d00c7d9 commit d6d795a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
19 changes: 13 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-2023 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -24,8 +24,8 @@
from sqlalchemy_utils.functions import create_database, database_exists, drop_database

from invenio_userprofiles import InvenioUserProfiles
from invenio_userprofiles.ext import finalize_app
from invenio_userprofiles.views import blueprint_ui_init
from invenio_userprofiles.ext import init_common
from invenio_userprofiles.views import blueprint, blueprint_ui_init


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -100,16 +100,16 @@ def delete_user_from_cache(exception):
def _init_userprofiles_app(app_):
"""Init UserProfiles modules."""
InvenioUserProfiles(app_)
app_.register_blueprint(blueprint)
app_.register_blueprint(blueprint_ui_init)
init_common(app_)
return app_


@pytest.fixture
def app(base_app):
"""Flask application."""
app_ = _init_userprofiles_app(base_app)
finalize_app(app_)
return app
return _init_userprofiles_app(base_app)


@pytest.fixture
Expand All @@ -119,3 +119,10 @@ def app_with_csrf(base_app):
WTF_CSRF_ENABLED=True,
)
return _init_userprofiles_app(base_app)


@pytest.fixture
def app_with_extend_security_forms(base_app):
"""Flask application with EXTEND_SECURITY_FORMS enabled."""
base_app.config.update(USERPROFILES_EXTEND_SECURITY_FORMS=True)
return _init_userprofiles_app(base_app)
14 changes: 5 additions & 9 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ def prefix(name, data):
return data


def test_profile_in_registration(base_app):
def test_profile_in_registration(app_with_extend_security_forms):
"""Test accounts registration form."""
base_app.config.update(USERPROFILES_EXTEND_SECURITY_FORMS=True)
InvenioUserProfiles(base_app)
base_app.register_blueprint(blueprint_ui_init)
app = base_app

with app.test_request_context():
with app_with_extend_security_forms.test_request_context():
register_url = url_for_security("register")

with app.test_client() as client:
with app_with_extend_security_forms.test_client() as client:
resp = client.get(register_url)
assert "profile.username" in resp.get_data(as_text=True)
assert "profile.full_name" in resp.get_data(as_text=True)
Expand All @@ -50,13 +46,13 @@ def test_profile_in_registration(base_app):
}
resp = client.post(register_url, data=data, follow_redirects=True)

with app.test_request_context():
with app_with_extend_security_forms.test_request_context():
user = User.query.filter_by(email="[email protected]").one()
assert user.username == "TestUser"
assert user.user_profile["full_name"] == "Test C. User"
assert user.user_profile["affiliations"] == "Test Org"

with app.test_client() as client:
with app_with_extend_security_forms.test_client() as client:
resp = client.get(register_url)
data = {
"email": "[email protected]",
Expand Down

0 comments on commit d6d795a

Please sign in to comment.