def test_init(): """Test extension initialization.""" app = Flask('testapp') ext = InvenioRecordsUI(app) app.register_blueprint(create_blueprint_from_app(app)) assert 'invenio-records-ui' in app.extensions app = Flask('testapp') ext = InvenioRecordsUI() assert 'invenio-records-ui' not in app.extensions ext.init_app(app) app.register_blueprint(create_blueprint_from_app(app)) assert 'invenio-records-ui' in app.extensions
def test_view(app): """Test view.""" InvenioRecordsUI(app) app.register_blueprint(create_blueprint_from_app(app)) setup_record_fixture(app) with app.test_client() as client: res = client.get("/records/1") assert res.status_code == 200 # Deleted PID res = client.get("/records/2") assert res.status_code == 410 res = client.get("/records/3") assert res.status_code == 410 # Missing object res = client.get("/records/4") assert res.status_code == 500 # Redirected PID res = client.get("/records/5") assert res.status_code == 302 # Non existing endpoint res = client.get("/records/6") assert res.status_code == 500 # Unregistered PID res = client.get("/records/7") assert res.status_code == 404
def test_changed_views(app): """Test view.""" app.config.update(dict( PIDSTORE_DATACITE_DOI_PREFIX="10.4321", RECORDS_UI_ENDPOINTS=dict( recid=dict( pid_type='recid', route='/records/<pid_value>', ), references=dict( pid_type='recid', route='/records/<pid_value>/references', ), doi=dict( pid_type='doi', route='/doi/<path:pid_value>', ) ) )) InvenioRecordsUI(app) app.register_blueprint(create_blueprint_from_app(app)) setup_record_fixture(app) with app.test_client() as client: res = client.get("/records/1") assert res.status_code == 200 res = client.get("/records/1/references") assert res.status_code == 200 res = client.get("/doi/10.1234/foo") assert res.status_code == 200
def app(request): """Flask application fixture.""" app_ = Flask('testapp') app_.config.update( TESTING=True, CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_RESULT_BACKEND="cache", SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite://'), SQLALCHEMY_TRACK_MODIFICATIONS=False, SECRET_KEY='mysecret', SUPPORT_EMAIL='*****@*****.**', WTF_CSRF_ENABLED=False, SERVER_NAME='test.it', RECORDS_UI_ENDPOINTS=dict( recid=dict( pid_type='recid', route='/records/<pid_value>', template='invenio_records_ui/detail.html', ), recid_access_request=dict( pid_type='recid', route='/records/<pid_value>/accessrequest', template='zenodo_accessrequests/access_request.html', view_imp='zenodo_accessrequests.views.requests.access_request', methods=['GET', 'POST'], ), recid_access_request_email_confirm=dict( pid_type='recid', route='/records/<pid_value>/accessrequest/<token>/confirm', # template='invenio_records_ui/detail.html', view_imp='zenodo_accessrequests.views.requests.confirm', ), ), ) InvenioFormatter(app_) Babel(app_) InvenioDB(app_) InvenioAccounts(app_) InvenioRecords(app_) FlaskMenu(app_) Mail(app_) InvenioRecordsUI(app_) InvenioAccess(app_) ZenodoAccessRequests(app_) InvenioPIDStore(app_) app_.register_blueprint(request_blueprint) app_.register_blueprint(settings_blueprint) app_.register_blueprint(blueprint_user) app_.register_blueprint(create_blueprint_from_app(app_)) with app_.app_context(): yield app_
def test_permission(app): """Test permission control to records.""" app.config.update( WTF_CSRF_ENABLED=False, SECRET_KEY='CHANGEME', SECURITY_PASSWORD_SALT='CHANGEME', # conftest switches off permission checking, so re-enable it for this # app. RECORDS_UI_DEFAULT_PERMISSION_FACTORY='helpers:' 'only_authenticated_users', ) Menu(app) InvenioRecordsUI(app) app.register_blueprint(create_blueprint_from_app(app)) accounts = InvenioAccounts(app) app.register_blueprint(accounts_blueprint) InvenioAccess(app) setup_record_fixture(app) # Create admin with app.app_context(): accounts.datastore.create_user( email='*****@*****.**', password=encrypt_password('123456'), active=True, ) # Get record 1 r = Resolver(pid_type='recid', object_type='rec', getter=Record.get_record) dummy_pid, record = r.resolve('1') db.session.commit() with app.test_request_context(): login_url = url_for('security.login') record_url = url_for('invenio_records_ui.recid', pid_value='1') # Access record 1 as admin with app.test_client() as client: res = client.get(record_url) assert res.status_code == 302 res = client.post(login_url, data={ 'email': '*****@*****.**', 'password': '******'}) assert res.status_code == 302 res = client.get(record_url) res.status_code == 200 # Access record 1 as anonymous with app.test_client() as client: res = client.get(record_url) res.status_code == 403
def test_custom_view_method(app): """Test view.""" app.config.update(dict( PIDSTORE_DATACITE_DOI_PREFIX='10.4321', RECORDS_UI_ENDPOINTS=dict( recid=dict( pid_type='recid', route='/records/<pid_value>', ), record_with_param=dict( pid_type='recid', route='/records/<pid_value>/export/<format>', ), recid_custom=dict( pid_type='recid', route='/records/<pid_value>/custom/<filename>', view_imp='test_invenio_records_ui:custom_view', ), recid_get_post=dict( pid_type='recid', route='/records/<pid_value>/custom_get_and_post', view_imp='test_invenio_records_ui:custom_get_and_post', methods=['GET', 'POST'], ), ) )) InvenioRecordsUI(app) app.register_blueprint(create_blueprint_from_app(app)) setup_record_fixture(app) with app.test_client() as client: res = client.get('/records/1') assert res.status_code == 200 # Test that filename parameter is passed to custom view function res = client.get('/records/1/custom/afilename') assert res.status_code == 200 assert res.get_data(as_text=True) == 'TEST:1:afilename' # Test that default view function can deal with multiple parameters. res = client.get('/records/1/export/bibtex') assert res.status_code == 200 # Test GET/POST res = client.get('/records/1/custom_get_and_post') assert res.status_code == 200 assert res.get_data(as_text=True) == 'TEST:1:GET' res = client.post('/records/1/custom_get_and_post') assert res.status_code == 200 assert res.get_data(as_text=True) == 'TEST:1:POST'
def app(): """Flask application fixture with database initialization.""" instance_path = tempfile.mkdtemp() app_ = Flask('testapp', static_folder=instance_path, instance_path=instance_path) app_.config.update( TESTING=True, SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///:memory:'), SQLALCHEMY_TRACK_MODIFICATIONS=True, RECORDS_UI_DEFAULT_PERMISSION_FACTORY=None, RECORDS_UI_ENDPOINTS=dict( recid=dict( pid_type='recid', route='/records/<pid_value>', template='invenio_records_ui/detail.html', ), recid_previewer=dict( pid_type='recid', route='/records/<pid_value>/preview', view_imp='invenio_previewer.views:preview', record_class='invenio_records_files.api:Record', ), recid_files=dict( pid_type='recid', route='/record/<pid_value>/files/<filename>', view_imp='invenio_records_files.utils.file_download_ui', record_class='invenio_records_files.api:Record', ), ), SERVER_NAME='localhost', APP_THEME=['semantic-ui']) Babel(app_) InvenioAssets(app_) InvenioDB(app_) InvenioRecords(app_) InvenioConfigDefault(app_) InvenioFormatter(app_) InvenioPreviewer(app_)._state InvenioRecordsUI(app_) app_.register_blueprint(create_blueprint_from_app(app_)) InvenioFilesREST(app_) with app_.app_context(): yield app_ shutil.rmtree(instance_path)
def test_signal(app): """Test view.""" InvenioRecordsUI(app) app.register_blueprint(create_blueprint_from_app(app)) setup_record_fixture(app) called = {'record-viewed': False} def _signal_sent(app, record=None, pid=None): assert request.path assert record assert pid called['record-viewed'] = record['recid'] with app.test_client() as client: with record_viewed.connected_to(_signal_sent): res = client.get("/records/1") assert res.status_code == 200 assert called['record-viewed'] == 1
def test_record_export(app, json_v1): """Test record export formats.""" app.config.update(dict( RECORDS_UI_EXPORT_FORMATS=dict( recid=dict( json=dict( title='JSON', serializer=json_v1, order=1, ) ) ) )) InvenioRecordsUI(app) app.register_blueprint(create_blueprint_from_app(app)) setup_record_fixture(app) with app.test_client() as client: res = client.get('/records/1/export/json') assert res.status_code == 200 res = client.get('/records/1/export/None') assert res.status_code == 404
), recid_previewer=dict( pid_type='recid', route='/records/<pid_value>/preview', view_imp='invenio_previewer.views:preview', record_class='invenio_records_files.api:Record', ))) Babel(app) InvenioI18N(app) InvenioDB(app) InvenioAssets(app) InvenioRecords(app) InvenioFilesREST(app) InvenioPreviewer(app) InvenioRecordsUI(app) app.register_blueprint(create_blueprint_from_app(app)) @app.cli.command() def fixtures(): """Command for working with test data.""" temp_path = os.path.join(os.path.dirname(__file__), 'temp') demo_files_path = os.path.join(os.path.dirname(__file__), 'demo_files') # Create location loc = Location(name='local', uri=temp_path, default=True) db.session.add(loc) db.session.commit() # Example files from the data folder demo_files = (