def test_connect_from_settings_or_403(self, mock_connect): user_settings = AddonDataverseUserSettings() user_settings.dataverse_username = '******' user_settings.dataverse_password = '******' connection = connect_from_settings_or_403(None) assert_is_none(connection) connection = connect_from_settings_or_403(user_settings) assert_true(connection) mock_connect.assert_called_once_with( user_settings.dataverse_username, user_settings.dataverse_password, )
def fail_if_unauthorized(node_addon, auth, file_id): node = node_addon.owner user_settings = node_addon.user_settings if file_id is None: raise HTTPError(httplib.NOT_FOUND) try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == 403: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) released_file_ids = [f.id for f in get_files(study, released=True)] all_file_ids = [f.id for f in get_files(study)] + released_file_ids if file_id not in all_file_ids: raise HTTPError(httplib.FORBIDDEN) elif not node.can_edit(auth) and file_id not in released_file_ids: raise HTTPError(httplib.UNAUTHORIZED)
def dataverse_get_widget_contents(node_addon, **kwargs): data = { 'connected': False, } if not node_addon.is_fully_configured: return {'data': data}, http.OK doi = node_addon.study_hdl alias = node_addon.dataverse_alias connection = connect_from_settings_or_403(node_addon.user_settings) dataverse = get_dataverse(connection, alias) study = get_study(dataverse, doi) if study is None: return {'data': data}, http.BAD_REQUEST dataverse_url = 'http://{0}/dvn/dv/'.format(HOST) + alias study_url = 'http://dx.doi.org/' + doi data.update({ 'connected': True, 'dataverse': node_addon.dataverse, 'dataverseUrl': dataverse_url, 'study': node_addon.study, 'doi': doi, 'studyUrl': study_url, 'citation': study.citation, }) return {'data': data}, http.OK
def fail_if_unauthorized(node_addon, auth, file_id): node = node_addon.owner user_settings = node_addon.user_settings if file_id is None: raise HTTPError(http.NOT_FOUND) try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == 403: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) released_file_ids = [f.id for f in get_files(study, released=True)] all_file_ids = [f.id for f in get_files(study)] + released_file_ids if file_id not in all_file_ids: raise HTTPError(http.FORBIDDEN) elif not node.can_edit(auth) and file_id not in released_file_ids: raise HTTPError(http.UNAUTHORIZED)
def dataverse_get_widget_contents(node_addon, **kwargs): data = { 'connected': False, } if not node_addon.is_fully_configured: return {'data': data}, http.OK doi = node_addon.study_hdl alias = node_addon.dataverse_alias connection = connect_from_settings_or_403(node_addon.user_settings) dataverse = get_dataverse(connection, alias) study = get_study(dataverse, doi) if study is None: return {'data': data}, http.BAD_REQUEST dataverse_url = 'http://{0}/dvn/dv/'.format(HOST) + alias study_url = 'http://dx.doi.org/' + doi data.update({ 'connected': True, 'dataverse': node_addon.dataverse, 'dataverseUrl': dataverse_url, 'study': node_addon.study, 'doi': doi, 'studyUrl': study_url, 'citation': study.citation, }) return {'data': data}, http.OK
def test_connect_from_settings_or_403_forbidden(self, mock_dvn_connection): mock_obj = mock.create_autospec(Connection) mock_obj.connected = False mock_obj.status = 403 mock_dvn_connection.return_value = mock_obj user_settings = AddonDataverseUserSettings() user_settings.dataverse_username = '******' user_settings.dataverse_password = '******' with assert_raises(HTTPError) as cm: connect_from_settings_or_403(user_settings) mock_dvn_connection.assert_called_once_with( username=user_settings.dataverse_username, password=user_settings.dataverse_password, host=settings.HOST, disable_ssl=not settings.VERIFY_SSL, ) assert_equal(cm.exception.code, 403)
def dataverse_delete_file(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings now = datetime.datetime.utcnow() file_id = kwargs.get('path') if file_id is None: raise HTTPError(httplib.NOT_FOUND) try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == httplib.FORBIDDEN: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) file = get_file_by_id(study, file_id) delete_file(file) # Check if file was deleted if get_file_by_id(study, file_id) is not None: raise HTTPError(httplib.BAD_REQUEST) node.add_log( action='dataverse_file_removed', params={ 'project': node.parent_id, 'node': node._primary_key, 'filename': file.name, 'study': study.title, }, auth=auth, log_date=now, ) return {}
def dataverse_delete_file(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings now = datetime.datetime.utcnow() file_id = kwargs.get('path') if file_id is None: raise HTTPError(http.NOT_FOUND) try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == 403: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) file = get_file_by_id(study, file_id) delete_file(file) # Check if file was deleted if get_file_by_id(study, file_id) is not None: raise HTTPError(http.BAD_REQUEST) node.add_log( action='dataverse_file_removed', params={ 'project': node.parent_id, 'node': node._primary_key, 'filename': file.name, 'study': study.title, }, auth=auth, log_date=now, ) return {}
def dataverse_release_study(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings now = datetime.datetime.utcnow() try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == 403: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) if study.get_state() == 'RELEASED': raise HTTPError(httplib.CONFLICT) release_study(study) # Add a log node.add_log( action='dataverse_study_released', params={ 'project': node.parent_id, 'node': node._primary_key, 'study': study.title, }, auth=auth, log_date=now, ) return {'study': study.title}, httplib.OK
def dataverse_release_study(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings now = datetime.datetime.utcnow() try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == 403: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) if study.get_state() == 'RELEASED': raise HTTPError(http.CONFLICT) release_study(study) # Add a log node.add_log( action='dataverse_study_released', params={ 'project': node.parent_id, 'node': node._primary_key, 'study': study.title, }, auth=auth, log_date=now, ) return {'study': study.title}, http.OK
def dataverse_user_config_get(user_addon, auth, **kwargs): """View for getting a JSON representation of the logged-in user's Dataverse user settings. """ try: connection = connect_from_settings_or_403(user_addon) except HTTPError as error: if error.code == 403: connection = None else: raise urls = { 'create': api_url_for('dataverse_set_user_config'), 'delete': api_url_for('dataverse_delete_user'), } return { 'result': { 'connected': connection is not None, 'userHasAuth': user_addon.has_auth, 'dataverseUsername': user_addon.dataverse_username, 'urls': urls }, }, http.OK
def dataverse_upload_file(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings try: name = request.args['name'] except KeyError: raise HTTPError(httplib.BAD_REQUEST) now = datetime.datetime.utcnow() can_edit = node.can_edit(auth) and not node.is_registration can_view = node.can_view(auth) try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == httplib.FORBIDDEN: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) filename = secure_filename(name) status_code = httplib.CREATED old_id = None # Fail if file is too small (Dataverse issue) content = request.data if len(content) < 5: raise HTTPError(httplib.UNSUPPORTED_MEDIA_TYPE) # Replace file if old version exists old_file = get_file(study, filename) if old_file is not None: status_code = httplib.OK old_id = old_file.id delete_file(old_file) # Check if file was deleted if get_file_by_id(study, old_id) is not None: raise HTTPError(httplib.BAD_REQUEST) upload_file(study, filename, content) file = get_file(study, filename) if file is None: raise HTTPError(httplib.BAD_REQUEST) node.add_log( action='dataverse_file_added', params={ 'project': node.parent_id, 'node': node._primary_key, 'filename': filename, 'path': node.web_url_for('dataverse_view_file', path=file.id), 'study': study.title, }, auth=auth, log_date=now, ) info = { 'addon': 'dataverse', 'file_id': file.id, 'old_id': old_id, 'name': filename, 'path': filename, 'size': [ len(content), rubeus.format_filesize(len(content)) ], rubeus.KIND: rubeus.FILE, 'urls': { 'view': node.web_url_for('dataverse_view_file', path=file.id), 'download': node.web_url_for('dataverse_download_file', path=file.id), 'delete': node.api_url_for('dataverse_delete_file', path=file.id), }, 'permissions': { 'view': can_view, 'edit': can_edit, }, } return info, status_code
def dataverse_upload_file(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings try: name = request.args['name'] except KeyError: raise HTTPError(http.BAD_REQUEST) now = datetime.datetime.utcnow() can_edit = node.can_edit(auth) and not node.is_registration can_view = node.can_view(auth) try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == 403: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) filename = secure_filename(name) action = 'file_uploaded' old_id = None # Fail if file is too small (Dataverse issue) content = request.data if len(content) < 5: raise HTTPError(http.UNSUPPORTED_MEDIA_TYPE) # Replace file if old version exists old_file = get_file(study, filename) if old_file is not None: action = 'file_updated' old_id = old_file.id delete_file(old_file) # Check if file was deleted if get_file_by_id(study, old_id) is not None: raise HTTPError(http.BAD_REQUEST) upload_file(study, filename, content) file = get_file(study, filename) if file is None: raise HTTPError(http.BAD_REQUEST) node.add_log( action='dataverse_file_added', params={ 'project': node.parent_id, 'node': node._primary_key, 'filename': filename, 'path': node.web_url_for('dataverse_view_file', path=file.id), 'study': study.title, }, auth=auth, log_date=now, ) info = { 'addon': 'dataverse', 'file_id': file.id, 'old_id': old_id, 'name': filename, 'path': filename, 'size': [len(content), rubeus.format_filesize(len(content))], rubeus.KIND: rubeus.FILE, 'urls': { 'view': node.web_url_for('dataverse_view_file', path=file.id), 'download': node.web_url_for('dataverse_download_file', path=file.id), 'delete': node.api_url_for('dataverse_delete_file', path=file.id), }, 'permissions': { 'view': can_view, 'edit': can_edit, }, 'actionTaken': action, } return info, 201