def test_init(): """Test extension initialization.""" app = Flask('testapp') app.config.update( ACCOUNTS_USE_CELERY=False ) Babel(app) Mail(app) Menu(app) InvenioDB(app) InvenioAccounts(app) ext = InvenioUserProfiles(app) assert 'invenio-userprofiles' in app.extensions app = Flask('testapp') app.config.update( ACCOUNTS_USE_CELERY=False ) Babel(app) Mail(app) Menu(app) InvenioDB(app) InvenioAccounts(app) ext = InvenioUserProfiles() assert 'invenio-userprofiles' not in app.extensions ext.init_app(app) assert 'invenio-userprofiles' in app.extensions
def test_admin(app): """Test flask-admin interace.""" InvenioUserProfiles(app) assert isinstance(user_profile_adminview, dict) assert 'model' in user_profile_adminview assert 'modelview' in user_profile_adminview admin = Admin(app, name="Test") user_model = user_profile_adminview.pop('model') user_view = user_profile_adminview.pop('modelview') admin.add_view(user_view(user_model, db.session, **user_profile_adminview)) with app.test_request_context(): request_url = url_for('userprofile.index_view') with app.app_context(): with app.test_client() as client: res = client.get( request_url, follow_redirects=True ) assert res.status_code == 200 assert b'Display Name' in (res.get_data()) assert b'Full Name' in (res.get_data())
def app(): """Create fixture app.""" # Set temporary instance path for sqlite instance_path = tempfile.mkdtemp() _app = Flask('testapp', instance_path=instance_path) InvenioAccess(_app) InvenioAccounts(_app) InvenioDB(_app) InvenioUserProfiles(_app) InvenioRecords(_app) InvenioFilesREST(_app) # UserProfile(_app) _app.config.update( SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), TESTING=True, SECRET_KEY='test', ) with _app.app_context(): yield _app # Teardown instance path. shutil.rmtree(instance_path)
def userprofiles_app(app): """Configure userprofiles module.""" app.config.update( USERPROFILES_EXTEND_SECURITY_FORMS=True, WTF_CSRF_ENABLED=True, ) InvenioUserProfiles(app) app.register_blueprint(blueprint_ui_init) return app
def test_init(): """Test extension initialization.""" app = Flask('testapp') app.config.update( ACCOUNTS_USE_CELERY=False, SECRET_KEY="test_key", ) Babel(app) Mail(app) Menu(app) InvenioDB(app) InvenioAccounts(app) ext = InvenioUserProfiles(app) assert 'invenio-userprofiles' in app.extensions app = Flask('testapp') app.config.update( ACCOUNTS_USE_CELERY=False, SECRET_KEY="test_key", ) Babel(app) Mail(app) Menu(app) InvenioDB(app) InvenioAccounts(app) ext = InvenioUserProfiles() assert 'invenio-userprofiles' not in app.extensions ext.init_app(app) assert 'invenio-userprofiles' in app.extensions
def test_profile_updating(base_app): base_app.config.update(USERPROFILES_EXTEND_SECURITY_FORMS=True) InvenioUserProfiles(base_app) app = base_app with app.app_context(): user = User(email='lollorosso', password='******') db.session.add(user) db.session.commit() assert user.profile is None profile = UserProfile(username='******', full_name='Test T. User') user.profile = profile user.profile.username = '******' assert user.profile.username == 'Different_Name' assert profile.username == 'Different_Name'
def test_profile_in_registration(base_app): """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(): register_url = url_for_security('register') with app.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) data = { 'email': '*****@*****.**', 'password': '******', 'profile.username': '******', 'profile.full_name': 'Test C. User', } resp = client.post(register_url, data=data, follow_redirects=True) with app.test_request_context(): user = User.query.filter_by(email='*****@*****.**').one() assert user.profile.username == 'TestUser' assert user.profile.full_name == 'Test C. User' with app.test_client() as client: resp = client.get(register_url) data = { 'email': '*****@*****.**', 'password': '******', 'profile.username': '******', 'profile.full_name': 'Same Username', } resp = client.post(register_url, data=data) assert resp.status_code == 200 assert 'profile.username' in resp.get_data(as_text=True)
MAIL_SUPPRESS_SEND=True, SECRET_KEY='CHANGE_ME', SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), SQLALCHEMY_TRACK_MODIFICATIONS=True, WTF_CSRF_ENABLED=False, ) Babel(app) InvenioMail(app) InvenioI18N(app) InvenioDB(app) if INVENIO_ASSETS_AVAILABLE: InvenioAssets(app) if INVENIO_THEME_AVAILABLE: InvenioTheme(app) InvenioAccounts(app) app.register_blueprint(blueprint) InvenioUserProfiles(app) app.register_blueprint(blueprint2) app.register_blueprint(blueprint_api_init) app.register_blueprint(blueprint_ui_init) InvenioAdmin(app, permission_factory=lambda x: x, view_class_factory=lambda x: x) @app.route('/') def index(): """Example index page route.""" return redirect(url_for('invenio_userprofiles.profile'))
def userprofiles_fixture(views_fixture): """Fixture with userprofiles module.""" views_fixture.config.update(USERPROFILES_EXTEND_SECURITY_FORMS=True, ) InvenioUserProfiles(views_fixture) views_fixture.register_blueprint(blueprint_ui_init) return views_fixture
def _init_userprofiles(app_): """Init userprofiles module.""" InvenioUserProfiles(app_) app_.register_blueprint(blueprint_ui_init) return app_
def app(base_app): """Flask application.""" InvenioUserProfiles(base_app) base_app.register_blueprint(blueprint_ui_init) return base_app
def app(request, accounts_rest_permission_factory): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask(__name__, instance_path=instance_path) InvenioAccess(app) InvenioAccounts(app) InvenioAccountsREST(app) InvenioOAuth2Server(app) InvenioOAuth2ServerREST(app) InvenioDB(app) Babel(app) Mail(app) Menu(app) Breadcrumbs(app) # this is done mainly for coverage so that tests are run with and without # userprofiles being loaded in the app if not hasattr(request, 'param') or \ 'with_profiles' not in request.param or \ request.param['with_profiles']: # tests without invenio-userprofiles being installed at all try: from invenio_userprofiles import InvenioUserProfiles InvenioUserProfiles(app) except ImportError: pass read_role = accounts_rest_permission_factory['read_role'] update_role = accounts_rest_permission_factory['update_role'] delete_role = accounts_rest_permission_factory['delete_role'] read_roles = accounts_rest_permission_factory['read_roles_list'] create_role = accounts_rest_permission_factory['create_role'] assign_role = accounts_rest_permission_factory['assign_role'] unassign_role = accounts_rest_permission_factory['unassign_role'] role_users = accounts_rest_permission_factory['read_role_users_list'] user_roles = accounts_rest_permission_factory['read_user_roles_list'] read_user_prop = accounts_rest_permission_factory['read_user_properties'] mod_user_prop = accounts_rest_permission_factory['update_user_properties'] read_users = accounts_rest_permission_factory['read_users_list'] app.config.update( ACCOUNTS_REST_READ_ROLE_PERMISSION_FACTORY=read_role, ACCOUNTS_REST_UPDATE_ROLE_PERMISSION_FACTORY=update_role, ACCOUNTS_REST_DELETE_ROLE_PERMISSION_FACTORY=delete_role, ACCOUNTS_REST_READ_ROLES_LIST_PERMISSION_FACTORY=read_roles, ACCOUNTS_REST_CREATE_ROLE_PERMISSION_FACTORY=create_role, ACCOUNTS_REST_ASSIGN_ROLE_PERMISSION_FACTORY=assign_role, ACCOUNTS_REST_UNASSIGN_ROLE_PERMISSION_FACTORY=unassign_role, ACCOUNTS_REST_READ_ROLE_USERS_LIST_PERMISSION_FACTORY=role_users, ACCOUNTS_REST_READ_USER_ROLES_LIST_PERMISSION_FACTORY=user_roles, ACCOUNTS_REST_READ_USER_PROPERTIES_PERMISSION_FACTORY=read_user_prop, ACCOUNTS_REST_UPDATE_USER_PROPERTIES_PERMISSION_FACTORY=mod_user_prop, ACCOUNTS_REST_READ_USERS_LIST_PERMISSION_FACTORY=read_users, OAUTH2SERVER_CLIENT_ID_SALT_LEN=40, OAUTH2SERVER_CLIENT_SECRET_SALT_LEN=60, OAUTH2SERVER_TOKEN_PERSONAL_SALT_LEN=60, SECRET_KEY='changeme', TESTING=True, SERVER_NAME='localhost', SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), SECURITY_SEND_PASSWORD_CHANGE_EMAIL=False) from invenio_oauth2server.views.server import blueprint with app.app_context(): db_.create_all() yield app with app.app_context(): db_.drop_all()