def test_init(): """Test extension initialization.""" app = Flask('testapp') ext = InvenioAdmin(app) assert 'invenio-admin' in app.extensions app = Flask('testapp') ext = InvenioAdmin() assert 'invenio-admin' not in app.extensions ext.init_app(app) assert 'invenio-admin' in app.extensions
def test_invenio_theme_loading_order(): """Test base template set correctly by invenio_theme.""" app = Flask('testapp') InvenioAdmin(app) assert app.config.get('ADMIN_BASE_TEMPLATE') is None app = Flask('testapp') InvenioTheme(app) InvenioAdmin(app) assert app.config.get('ADMIN_BASE_TEMPLATE') is not None app = Flask('testapp') InvenioAdmin(app) InvenioTheme(app) assert app.config.get('ADMIN_BASE_TEMPLATE') is not None
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) Babel(app) FlaskCLI(app) InvenioDB(app) LoginManager(app) app.admin_app = InvenioAdmin(app) protected_view = protected_adminview_factory(TestModelView) app.admin_app.admin.add_view(protected_view(TestModel, db.session)) app.config.update( TESTING=True, SECRET_KEY="SECRET_KEY", ) with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def test_entry_points(): """Test admin views discovery through entry points.""" from flask_principal import Permission app = Flask('testapp') admin_app = InvenioAdmin(app, permission_factory=lambda x: Permission(), view_class_factory=lambda x: x) # Check if model views were added by checking the labels of menu items menu_items = {str(item.name): item for item in admin_app.admin.menu()} assert 'OneAndTwo' in menu_items # Category for ModelOne and ModelTwo assert 'Four' in menu_items # Category for ModelOne and ModelTwo assert 'Model One' not in menu_items # ModelOne should go to a category assert 'Model Two' not in menu_items # ModelTwo should go to a category assert 'Model Three' in menu_items # ModelThree goes straight to menu assert isinstance(menu_items['Model Three'], flask_admin.menu.MenuView) assert isinstance(menu_items['OneAndTwo'], flask_admin.menu.MenuCategory) assert menu_items['OneAndTwo'].is_category() assert not menu_items['Model Three'].is_category() submenu_items = { str(item.name): item for item in menu_items['OneAndTwo'].get_children() } assert 'Model One' in submenu_items assert 'Model Two' in submenu_items assert not submenu_items['Model One'].is_category() assert not submenu_items['Model Two'].is_category() assert isinstance(submenu_items['Model One'], flask_admin.menu.MenuView) assert isinstance(submenu_items['Model Two'], flask_admin.menu.MenuView) four_item = menu_items['Four'].get_children()[0] assert four_item.name == 'View number Four' assert isinstance(four_item, flask_admin.menu.MenuView)
def test_admin_views(app, db, dummy_location): """Test admin views.""" app.config['SECRET_KEY'] = 'CHANGEME' InvenioAdmin(app, permission_factory=None, view_class_factory=lambda x: x) b1 = Bucket.create(location=dummy_location) obj = ObjectVersion.create(b1, 'test').set_location('placeuri', 1, 'chk') db.session.commit() with app.test_client() as client: res = client.get('/admin/bucket/') assert res.status_code == 200 assert str(b1.id) in res.get_data(as_text=True) res = client.get('/admin/fileinstance/') assert res.status_code == 200 assert str(obj.file_id) in res.get_data(as_text=True) res = client.get('/admin/location/') assert res.status_code == 200 assert str(b1.location.name) in res.get_data(as_text=True) res = client.get('/admin/objectversion/') assert res.status_code == 200 assert str(obj.version_id) in res.get_data(as_text=True)
def test_invalid_entry_points(): """Test invalid admin views discovery through entry points.""" app = Flask('testapp') admin_app = InvenioAdmin() with pytest.raises(Exception) as e: admin_app.init_app(app, entry_point_group='invenio_admin.views_invalid') assert '"view_class"' in str(e)
def test_entry_points(): """Test admin views discovery through entry points.""" app = Flask('testapp') admin_app = InvenioAdmin(app) # Check if model views were added by checking the labels of menu items menu_items = [item.name for item in admin_app.admin.menu()] assert 'Model One' in menu_items assert 'Model Two' in menu_items assert 'Model Three' in menu_items
def app(request): """Flask application fixture.""" instance_path = tempfile.mkdtemp() app = Flask('testapp', instance_path=instance_path) app.config.update( TESTING=True, SECRET_KEY='SECRET_KEY', ADMIN_LOGIN_ENDPOINT='login', SQLALCHEMY_TRACK_MODIFICATIONS=True, ) Babel(app) InvenioDB(app) Principal(app) LoginManager(app) # Install login and access loading. @app.login_manager.user_loader def load_user(user_id): return TestUser.get(user_id) @app.route('/login/') def login(): from flask import current_app from flask import request as flask_request user = TestUser.get(flask_request.args.get('user', 1)) login_user(user) identity_changed.send(current_app._get_current_object(), identity=Identity(user.id)) return "Logged In" @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): identity.user = current_user identity.provides.add(UserNeed(current_user.id)) if current_user.id == 1: identity.provides.add(action_admin_access) # Register admin view InvenioAdmin(app, permission_factory=lambda x: Permission(action_admin_access)) app.extensions['invenio-admin'].register_view(TestModelView, TestModel) # Create database with app.app_context(): if not database_exists(str(db.engine.url)): create_database(str(db.engine.url)) db.drop_all() db.create_all() def teardown(): with app.app_context(): drop_database(str(db.engine.url)) shutil.rmtree(instance_path) request.addfinalizer(teardown) return app
def test_base_template_override(): """Test base template is set up correctly.""" app = Flask('testapp') base_template = 'test_base_template.html' app.config['ADMIN_BASE_TEMPLATE'] = base_template InvenioTheme(app) state = InvenioAdmin(app) assert app.config.get('ADMIN_BASE_TEMPLATE') == base_template # Force call of before_first_request registered triggers. app.try_trigger_before_first_request_functions() assert state.admin.base_template == base_template
def factory(**config): app = Flask("testapp", instance_path=instance_path) app.config.update(**config) Babel(app) InvenioDB(app) no_permissions = dict(permission_factory=None, view_class_factory=lambda x: x) InvenioAdmin(app, **no_permissions) InvenioBanners(app) app.register_blueprint(blueprint) app.register_blueprint(api_blueprint) return app
def admin_fixture(pages_fixture): """Admin fixture.""" def unprotected_factory(base_class): class UnprotectedAdminView(base_class): def is_accesible(self): return True def inaccessible_callback(self, name, **kwargs): pass return UnprotectedAdminView InvenioAdmin( pages_fixture, view_class_factory=unprotected_factory, ) return pages_fixture
def test_entry_points(): """Test admin views discovery through entry points.""" app = Flask('testapp') admin_app = InvenioAdmin(app) # Check if model views were added by checking the labels of menu items menu_items = {str(item.name): item for item in admin_app.admin.menu()} assert 'OneAndTwo' in menu_items # Category for ModelOne and ModelTwo assert 'Model One' not in menu_items # ModelOne should go to a category assert 'Model Two' not in menu_items # ModelTwo should go to a category assert 'Model Three' in menu_items # ModelThree goes straight to menu assert isinstance(menu_items['Model Three'], flask_admin.menu.MenuView) assert isinstance(menu_items['OneAndTwo'], flask_admin.menu.MenuCategory) assert menu_items['OneAndTwo'].is_category() assert not menu_items['Model Three'].is_category() submenu_items = { str(item.name): item for item in menu_items['OneAndTwo'].get_children() } assert 'Model One' in submenu_items assert 'Model Two' in submenu_items assert not submenu_items['Model One'].is_category() assert not submenu_items['Model Two'].is_category() assert isinstance(submenu_items['Model One'], flask_admin.menu.MenuView) assert isinstance(submenu_items['Model Two'], flask_admin.menu.MenuView)
app.jinja_loader ]) InvenioDB(app) InvenioTheme(app) InvenioJSONSchemas(app) InvenioAccounts(app) InvenioRecords(app) InvenioRecordsUI(app) search = InvenioSearch(app) # search.register_mappings('testrecords', 'data') InvenioSearchUI(app) InvenioREST(app) InvenioIndexer(app) InvenioPIDStore(app) InvenioAdmin(app) InvenioOAuth2Server(app) InvenioRecordsREST(app) InvenioFilesREST(app) assets = InvenioAssets(app) assets.env.register('invenio_search_ui_search_js', js) InvenioDeposit(app) InvenioDepositREST(app) app.register_blueprint(accounts_blueprint) app.register_blueprint(settings_blueprint) app.register_blueprint(server_blueprint)
from invenio_records import InvenioRecords from invenio_pidstore import InvenioPIDStore # Create Flask application app = Flask(__name__) app.config.update( DB_VERSIONING_USER_MODEL=None, SECRET_KEY='test_key', SECURITY_PASSWORD_HASH='pbkdf2_sha512', SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO", SECURITY_PASSWORD_SCHEMES=[ 'pbkdf2_sha512', 'sha512_crypt', 'invenio_aes_encrypted_email' ], SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), WTF_CSRF_ENABLED=False, ) Babel(app) Menu(app) InvenioDB(app) admin = InvenioAdmin(app) InvenioPIDStore(app) accounts = InvenioAccounts(app) InvenioAccess(app) InvenioRecords(app) # register blueprints app.register_blueprint(accounts_blueprint)
MAIL_SUPPRESS_SEND=True, SECRET_KEY='CHANGE_ME', SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO', SQLALCHEMY_TRACK_MODIFICATIONS=False, WTF_CSRF_ENABLED=False, ) Babel(app) Mail(app) InvenioDB(app) InvenioAccounts(app) InvenioAccess(app) InvenioAssets(app) InvenioI18N(app) InvenioTheme(app) admin_app = InvenioAdmin(app) app.register_blueprint(accounts_blueprint) app.register_blueprint(admin_blueprint) @app.route('/') def index(): """Basic test view.""" flash( Markup('Login with username <strong>[email protected]</strong> ' 'and password <strong>123456</strong>.')) return redirect('/admin/') class TestModel(db.Model): """Simple model with just one column."""
os.environ['RECAPTCHA_PUBLIC_KEY']) app.config.setdefault('RECAPTCHA_PRIVATE_KEY', os.environ['RECAPTCHA_PRIVATE_KEY']) Babel(app) Mail(app) InvenioDB(app) InvenioAccounts(app) InvenioI18N(app) if INVENIO_ASSETS_AVAILABLE: InvenioAssets(app) if INVENIO_THEME_AVAILABLE: InvenioTheme(app) else: Menu(app) if INVENIO_ADMIN_AVAILABLE: InvenioAdmin(app, permission_factory=lambda x: x, view_class_factory=lambda x: x) app.register_blueprint(blueprint) @app.route("/") def index(): """Basic test view.""" if current_user.is_authenticated: return render_template("authenticated.html") else: return render_template("anonymous.html")
MAIL_SUPPRESS_SEND=True, SECRET_KEY='CHANGE_ME', SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO', SQLALCHEMY_TRACK_MODIFICATIONS=False, WTF_CSRF_ENABLED=False, ) Babel(app) Mail(app) InvenioDB(app) InvenioAccounts(app) InvenioAccess(app) InvenioAssets(app) InvenioI18N(app) InvenioTheme(app) admin_app = InvenioAdmin( app, permission_factory=lambda x: Permission(action_admin_access)) app.register_blueprint(accounts_blueprint) app.register_blueprint(admin_blueprint) @app.route('/') def index(): """Basic test view.""" flash( Markup('Login with username <strong>[email protected]</strong> ' 'and password <strong>123456</strong>.')) return redirect('/admin/') class TestModel(db.Model): """Simple model with just one column."""