def test_init_rest(): """Test REST extension initialization.""" app = Flask('testapp') Babel(app) Mail(app) InvenioDB(app) ext = InvenioAccountsREST(app) assert 'invenio-accounts' in app.extensions assert 'security' not in app.blueprints.keys() app = Flask('testapp') Babel(app) Mail(app) InvenioDB(app) ext = InvenioAccountsREST() assert 'invenio-accounts' not in app.extensions assert 'security' not in app.blueprints.keys() ext.init_app(app) assert 'invenio-accounts' in app.extensions assert 'security' not in app.blueprints.keys() app = Flask('testapp') app.config['ACCOUNTS_REGISTER_BLUEPRINT'] = True Babel(app) Mail(app) InvenioDB(app) ext = InvenioAccountsREST() assert 'invenio-accounts' not in app.extensions assert 'security' not in app.blueprints.keys() ext.init_app(app) assert 'invenio-accounts' in app.extensions assert 'security' in app.blueprints.keys()
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() def init_app(app): app.config.update( LOGIN_DISABLED=False, MAIL_SUPPRESS_SEND=True, OAUTH2_CACHE_TYPE='simple', OAUTHLIB_INSECURE_TRANSPORT=True, SECRET_KEY='CHANGE_ME', SECURITY_DEPRECATED_PASSWORD_SCHEMES=[], SECURITY_PASSWORD_HASH='plaintext', SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO', SECURITY_PASSWORD_SCHEMES=['plaintext'], SQLALCHEMY_DATABASE_URI=os.getenv( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///' + os.path.join(instance_path, 'test.db')), SQLALCHEMY_TRACK_MODIFICATIONS=True, TESTING=True, WTF_CSRF_ENABLED=False, ) Babel(app) Mail(app) Menu(app) Breadcrumbs(app) InvenioDB(app) InvenioOAuth2Server(app) api_app = Flask('testapiapp', instance_path=instance_path) api_app.config.update(APPLICATION_ROOT='/api', ACCOUNTS_REGISTER_BLUEPRINT=True) init_app(api_app) InvenioAccountsREST(api_app) InvenioOAuth2ServerREST(api_app) app = Flask('testapp', instance_path=instance_path) init_app(app) InvenioAccountsUI(app) app.register_blueprint(accounts_blueprint) app.register_blueprint(server_blueprint) app.register_blueprint(settings_blueprint) app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {'/api': api_app.wsgi_app}) with app.app_context(): if str(db.engine.url) != 'sqlite://' and \ not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.create_all() def teardown(): with app.app_context(): if str(db.engine.url) != 'sqlite://': drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def app_with_flexible_registration(request): """Flask application fixture with Invenio Accounts.""" from webargs import fields from invenio_accounts.views.rest import RegisterView, use_kwargs class MyRegisterView(RegisterView): post_args = { **RegisterView.post_args, 'active': fields.Boolean(required=True) } @use_kwargs(post_args) def post(self, **kwargs): """Register a user.""" return super(MyRegisterView, self).post(**kwargs) api_app = _app_factory() InvenioREST(api_app) InvenioAccountsREST(api_app) api_app.config['ACCOUNTS_REST_AUTH_VIEWS']['register'] = MyRegisterView api_app.register_blueprint(create_blueprint(api_app)) _database_setup(api_app, request) yield api_app
def base_app(): """Flask application fixture without InvenioStats.""" from invenio_stats.config import STATS_EVENTS instance_path = tempfile.mkdtemp() app_ = Flask('testapp', instance_path=instance_path) stats_events = { 'file-download': deepcopy(STATS_EVENTS['file-download']), 'record-view': { 'signal': 'invenio_records_ui.signals.record_viewed', 'event_builders': ['invenio_stats.contrib.event_builders' '.record_view_event_builder'] } } stats_events.update({'event_{}'.format(idx): {} for idx in range(5)}) app_.config.update(dict( CELERY_ALWAYS_EAGER=True, CELERY_TASK_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND='memory', CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_TASK_EAGER_PROPAGATES=True, CELERY_RESULT_BACKEND='cache', SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite://'), SQLALCHEMY_TRACK_MODIFICATIONS=True, TESTING=True, OAUTH2SERVER_CLIENT_ID_SALT_LEN=64, OAUTH2SERVER_CLIENT_SECRET_SALT_LEN=60, OAUTH2SERVER_TOKEN_PERSONAL_SALT_LEN=60, STATS_MQ_EXCHANGE=Exchange( 'test_events', type='direct', delivery_mode='transient', # in-memory queue durable=True, ), SECRET_KEY='asecretkey', SERVER_NAME='localhost', STATS_QUERIES={'bucket-file-download-histogram': {}, 'bucket-file-download-total': {}, 'test-query': {}, 'test-query2': {}}, STATS_EVENTS=stats_events, STATS_AGGREGATIONS={'file-download-agg': {}} )) FlaskCeleryExt(app_) InvenioAccounts(app_) InvenioAccountsREST(app_) InvenioDB(app_) InvenioRecords(app_) InvenioFilesREST(app_) InvenioPIDStore(app_) InvenioCache(app_) InvenioQueues(app_) InvenioOAuth2Server(app_) InvenioOAuth2ServerREST(app_) InvenioSearch(app_, entry_point_group=None) with app_.app_context(): yield app_ shutil.rmtree(instance_path)
def test_init_rest(): """Test REST extension initialization.""" app = Flask("testapp") app.config["SECRET_KEY"] = "CHANGEME" Babel(app) Mail(app) InvenioDB(app) ext = InvenioAccountsREST(app) assert "invenio-accounts" in app.extensions assert "security" not in app.blueprints.keys() assert "security_email_templates" in app.blueprints.keys() app = Flask("testapp") app.config["SECRET_KEY"] = "CHANGEME" Babel(app) Mail(app) InvenioDB(app) ext = InvenioAccountsREST() assert "invenio-accounts" not in app.extensions assert "security" not in app.blueprints.keys() ext.init_app(app) assert "invenio-accounts" in app.extensions assert "security" not in app.blueprints.keys() assert "security_email_templates" in app.blueprints.keys() app = Flask("testapp") app.config["SECRET_KEY"] = "CHANGEME" app.config["ACCOUNTS_REGISTER_BLUEPRINT"] = True Babel(app) Mail(app) InvenioDB(app) ext = InvenioAccountsREST() assert "invenio-accounts" not in app.extensions assert "security" not in app.blueprints.keys() ext.init_app(app) assert "invenio-accounts" in app.extensions assert "security" in app.blueprints.keys() assert "security_email_templates" in app.blueprints.keys()
def base_app(events_config, aggregations_config): """Flask application fixture without InvenioStats.""" instance_path = tempfile.mkdtemp() app_ = Flask('testapp', instance_path=instance_path) app_.config.update(dict( CELERY_ALWAYS_EAGER=True, CELERY_TASK_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND='memory', CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_TASK_EAGER_PROPAGATES=True, CELERY_RESULT_BACKEND='cache', SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite://'), SQLALCHEMY_TRACK_MODIFICATIONS=True, # Bump the ES client timeout for slower environments (like Travis CI) SEARCH_CLIENT_CONFIG={'timeout': 30, 'max_retries': 5}, TESTING=True, OAUTH2SERVER_CLIENT_ID_SALT_LEN=64, OAUTH2SERVER_CLIENT_SECRET_SALT_LEN=60, OAUTH2SERVER_TOKEN_PERSONAL_SALT_LEN=60, STATS_MQ_EXCHANGE=Exchange( 'test_events', type='direct', delivery_mode='transient', # in-memory queue durable=True, ), SECRET_KEY='asecretkey', SERVER_NAME='localhost', STATS_QUERIES={}, STATS_EVENTS=events_config, STATS_AGGREGATIONS=aggregations_config, )) FlaskCeleryExt(app_) InvenioAccounts(app_) InvenioAccountsREST(app_) InvenioDB(app_) InvenioRecords(app_) InvenioFilesREST(app_) InvenioPIDStore(app_) InvenioCache(app_) InvenioQueues(app_) InvenioOAuth2Server(app_) InvenioOAuth2ServerREST(app_) InvenioSearch(app_, entry_point_group=None) with app_.app_context(): yield app_ shutil.rmtree(instance_path)
def api(request): """Flask application fixture.""" api_app = _app_factory( dict( SQLALCHEMY_DATABASE_URI=os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), SERVER_NAME='localhost', TESTING=True, )) InvenioREST(api_app) InvenioAccountsREST(api_app) api_app.register_blueprint(create_blueprint(api_app)) _database_setup(api_app, request) yield api_app
('fr', 'French'), ('it', 'Italian'), ], ) InvenioAssets(app) InvenioTheme(app) InvenioI18N(app) Breadcrumbs(app) InvenioDB(app) InvenioAdmin(app) InvenioAccess(app) OAuth2Provider(app) InvenioOAuth2ServerREST(app) accounts = InvenioAccountsUI(app) InvenioAccountsREST(app) app.register_blueprint(blueprint_account) InvenioOAuth2Server(app) # Register blueprints app.register_blueprint(settings_blueprint) app.register_blueprint(server_blueprint) app.register_blueprint(blueprint_admin_ui) with app.app_context(): # Register a test scope current_oauth2server.register_scope( Scope('test:scope', help_text='Access to the homepage', group='test')) # @app.route('/jwt', methods=['GET'])