def test_clear_update(app): """Test Community.update(clear_fields=True).""" with app.app_context(): created_community = Community.create_community(**community_metadata) community_id = created_community.id db.session.commit() with app.app_context(): retrieved = Community.get(id=community_id) retrieved.update(community_update, clear_fields=True) # test invalid update with pytest.raises(InvalidCommunityError): retrieved.update({}, clear_fields=True) db.session.commit() with app.app_context(): updated = Community.get(id=community_id) assert community_id == updated.id assert updated.updated for field, value in updated_community_metadata.items(): if field not in community_update: if field == 'publication_workflow': assert updated.publication_workflow == \ 'review_and_publish' elif field == 'restricted_submission': assert updated.restricted_submission == False else: assert getattr(updated, field) is None else: assert getattr(updated, field) == value
def test_valid_delete(app, login_user, communities_permissions): """Test VALID community delete request (DELETE .../communities/<id>).""" with app.app_context(): # create community created_community = Community.create_community(**community_metadata) community_id = created_community.id # create allowed user allowed_user = create_user('allowed') communities_permissions(allowed_user.id).delete_permission( True, community_id) db.session.commit() with app.app_context(): with app.test_client() as client: login_user(allowed_user, client) headers = [('Content-Type', 'application/json'), ('Accept', 'application/json')] res = client.delete(url_for('b2share_communities.communities_item', community_id=community_id), headers=headers) assert res.status_code == 204 with app.app_context(): # check database state with pytest.raises(CommunityDeletedError): Community.get(community_id) community = Community.get(community_id, with_deleted=True) assert community.deleted # check that one community has been created assert len(CommunityModel.query.all()) == 1
def test_update(app): """Test Community.update().""" with app.app_context(): created_community = Community.create_community(**community_metadata) community_id = created_community.id db.session.commit() with app.app_context(): retrieved = Community.get(id=community_id) retrieved.update(community_update) db.session.commit() with app.app_context(): updated = Community.get(id=community_id) assert community_id == updated.id assert updated.updated for field, value in updated_community_metadata.items(): assert getattr(updated, field) == value # test invalid update with pytest.raises(InvalidCommunityError): updated.update({'name': None}) # check that the community was not modified for field, value in updated_community_metadata.items(): assert getattr(updated, field) == value
def community(self, community_id): """Set the ID of the community maintaining of this Block Schema. Raises: :class:`b2share.modules.communities.errors.CommunityDoesNotExistError`: if the community does not exist. :class:`b2share.modules.schemas.errors.InvalidBlockSchemaError`: if another conflict occured. """ # noqa try: with db.session.begin_nested(): self.model.community = community_id db.session.merge(self.model) except sqlalchemy.exc.IntegrityError as e: # this will raise CommunityDoesNotExistError if necessary Community.get(community_id) # else raise a generic exception raise InvalidBlockSchemaError() from e
def test_patch(app): """Test Community.patch().""" with app.app_context(): created_community = Community.create_community(**community_metadata) community_id = created_community.id db.session.commit() with app.app_context(): retrieved = Community.get(id=community_id) retrieved.patch(community_patch) db.session.commit() with app.app_context(): patched = Community.get(id=community_id) assert community_id == patched.id assert patched.updated for field, value in patched_community_metadata.items(): assert getattr(patched, field) == value # test invalid or conflicting patchs with pytest.raises(JsonPatchConflict): patched.patch([{ 'op': 'replace', 'path': '/name', 'value': 'this should not be applied' }, { 'op': 'replace', 'path': '/non-existing-field', 'value': 'random value' }]) with pytest.raises(InvalidJsonPatch): patched.patch({'whatever': 'key'}) with pytest.raises(InvalidCommunityError): patched.patch([{ 'op': 'replace', 'path': '/name', 'value': None }]) # check that the community was not modified for field, value in patched_community_metadata.items(): assert getattr(patched, field) == value
def test_delete(app): """Test Community.delete().""" with app.app_context(): created_community = Community.create_community(**community_metadata) community_id = created_community.id db.session.commit() # test deleting a community with app.app_context(): retrieved = Community.get(id=community_id) retrieved.delete() assert retrieved.deleted db.session.commit() # test getting a deleted community with app.app_context(): with pytest.raises(CommunityDeletedError): Community.get(id=community_id) # test force getting a deleted community with app.app_context(): deleted_community = Community.get(id=community_id, with_deleted=True) assert deleted_community.deleted for field, value in community_metadata.items(): assert getattr(deleted_community, field) == value
def test_patch(app): """Test Community.patch().""" with app.app_context(): created_community = Community.create_community(**community_metadata) community_id = created_community.id db.session.commit() with app.app_context(): retrieved = Community.get(id=community_id) retrieved.patch(community_patch) db.session.commit() with app.app_context(): patched = Community.get(id=community_id) assert community_id == patched.id assert patched.updated for field, value in patched_community_metadata.items(): assert getattr(patched, field) == value # test invalid or conflicting patchs with pytest.raises(JsonPatchConflict): patched.patch([{ 'op': 'replace', 'path': '/name', 'value': 'this should not be applied' }, { 'op': 'replace', 'path': '/non-existing-field', 'value': 'random value' }]) with pytest.raises(InvalidJsonPatch): patched.patch({'whatever': 'key'}) with pytest.raises(InvalidCommunityError): patched.patch([{'op': 'replace', 'path': '/name', 'value': None}]) # check that the community was not modified for field, value in patched_community_metadata.items(): assert getattr(patched, field) == value
def test_valid_create(app, login_user, communities_permissions): """Test VALID community creation request (POST .../communities/).""" with app.app_context(): allowed_user = create_user('allowed') communities_permissions(allowed_user.id).create_permission(True) communities_permissions(allowed_user.id).read_permission(True) db.session.commit() with app.test_client() as client: with app.app_context(): login_user(allowed_user, client) headers = [('Content-Type', 'application/json'), ('Accept', 'application/json')] res = client.post(url_for('b2share_communities.communities_list', _external=False), data=json.dumps(community_metadata), headers=headers) assert res.status_code == 201 # check that the returned community matches the given data response_data = json.loads(res.get_data(as_text=True)) for field, value in community_metadata.items(): assert response_data[field] == value # check that an internal community returned id and that it contains # the same data assert 'id' in response_data.keys() assert response_data['id'] == str( Community.get(name=community_metadata['name']).id) headers = res.headers community_id = response_data['id'] with app.app_context(): communities_permissions(allowed_user.id).read_permission( True, community_id) db.session.commit() with app.app_context(): with app.test_client() as client: login_user(allowed_user, client) # check that the returned self link returns the same data subtest_self_link(response_data, headers, client) assert headers['Location'] == response_data['links']['self'] # check that one community has been created assert len(CommunityModel.query.all()) == 1
def test_valid_put(app, login_user, communities_permissions): """Test VALID community put request (PUT .../communities/<id>).""" with app.app_context(): # create community created_community = Community.create_community(**community_metadata) community_id = created_community.id # create allowed user allowed_user = create_user('allowed') communities_permissions(allowed_user.id).update_permission( True, community_id) communities_permissions(allowed_user.id).read_permission( True, community_id) db.session.commit() with app.app_context(): with app.test_client() as client: login_user(allowed_user, client) headers = [('Content-Type', 'application/json'), ('Accept', 'application/json')] res = client.put(url_for('b2share_communities.communities_item', community_id=community_id), data=json.dumps(patched_community_metadata), headers=headers) assert res.status_code == 200 # check that the returned community matches the given data response_data = json.loads(res.get_data(as_text=True)) for field, value in patched_community_metadata.items(): assert response_data[field] == value # check that an internal community returned id and that it contains # the same data assert 'id' in response_data.keys() assert response_data['id'] == str( Community.get(name=patched_community_metadata['name']).id) headers = res.headers with app.app_context(): with app.test_client() as client: login_user(allowed_user, client) # check that the returned self link returns the same data subtest_self_link(response_data, headers, client)
def test_create_community_and_get(app): """Test Community.create_community() and Community.get().""" with app.app_context(): # check that getting a nonexistent community raises an exception with pytest.raises(CommunityDoesNotExistError): Community.get(id='e174feb1-5882-4c1e-bab6-6678f59b993f') # test misuse of community.get() with pytest.raises(ValueError): Community.get(id='e174feb1-5882-4c1e-bab6-6678f59b993f', name='myname') with pytest.raises(ValueError): Community.get() # test invalid community creation with app.app_context(): with pytest.raises(InvalidCommunityError): created_community = Community.create_community( name=None, description=None, ) # create a community with app.app_context(): created_community = Community.create_community(**community_metadata) community_id = created_community.id db.session.commit() for field, value in community_metadata.items(): assert getattr(created_community, field) == value assert not created_community.deleted assert created_community.created assert created_community.updated # get the created community with app.app_context(): retrieved_by_id = Community.get(id=community_id) retrieved_by_name = Community.get(name=community_metadata['name']) assert community_id == retrieved_by_id.id assert community_id == retrieved_by_name.id for field, value in community_metadata.items(): assert getattr(retrieved_by_id, field) == value assert getattr(retrieved_by_name, field) == value
def test_valid_get(app, login_user, communities_permissions): """Test VALID community get request (GET .../communities/<id>).""" with app.app_context(): # create community created_community = Community.create_community(**community_metadata) community_id = created_community.id # create allowed user allowed_user = create_user('allowed') communities_permissions( allowed_user.id).read_permission(True, community_id) db.session.commit() with app.app_context(): with app.test_client() as client: login_user(allowed_user, client) headers = [('Content-Type', 'application/json'), ('Accept', 'application/json')] res = client.get(url_for('b2share_communities.communities_item', community_id=community_id), headers=headers) assert res.status_code == 200 # check that the returned community matches the given data response_data = json.loads(res.get_data(as_text=True)) for field, value in community_metadata.items(): assert response_data[field] == value # check that an internal community returned id and that it contains # the same data assert 'id' in response_data.keys() assert response_data['id'] == str(Community.get( name=community_metadata['name']).id) headers = res.headers with app.app_context(): with app.test_client() as client: login_user(allowed_user, client) # check that the returned self link returns the same data subtest_self_link(response_data, headers, client)
def test_create_community_and_get(app): """Test Community.create_community() and Community.get().""" with app.app_context(): # check that getting a nonexistent community raises an exception with pytest.raises(CommunityDoesNotExistError): Community.get(id='e174feb1-5882-4c1e-bab6-6678f59b993f') # test misuse of community.get() with pytest.raises(ValueError): Community.get(id='e174feb1-5882-4c1e-bab6-6678f59b993f', name='myname') with pytest.raises(ValueError): Community.get() # test invalid community creation with app.app_context(): with pytest.raises(InvalidCommunityError): created_community = Community.create_community(name=None, description=None,) # create a community with app.app_context(): created_community = Community.create_community(**community_metadata) community_id = created_community.id db.session.commit() for field, value in community_metadata.items(): assert getattr(created_community, field) == value assert not created_community.deleted assert created_community.created assert created_community.updated # get the created community with app.app_context(): retrieved_by_id = Community.get(id=community_id) retrieved_by_name = Community.get(name=community_metadata['name']) assert community_id == retrieved_by_id.id assert community_id == retrieved_by_name.id for field, value in community_metadata.items(): assert getattr(retrieved_by_id, field) == value assert getattr(retrieved_by_name, field) == value
def test_valid_patch(app, login_user, communities_permissions, patch_community_function): """Test VALID community patch request (PATCH .../communities/<id>).""" with app.app_context(): # create community created_community = Community.create_community(**community_metadata) community_id = created_community.id # create allowed user allowed_user = create_user('allowed') communities_permissions(allowed_user.id).update_permission( True, community_id) communities_permissions(allowed_user.id).read_permission( True, community_id) db.session.commit() with app.app_context(): with app.test_client() as client: login_user(allowed_user, client) res = patch_community_function(community_id, client) assert res.status_code == 200 # check that the returned community matches the given data response_data = json.loads(res.get_data(as_text=True)) for field, value in patched_community_metadata.items(): assert response_data[field] == value # check that an internal community returned id and that it contains # the same data assert 'id' in response_data.keys() assert response_data['id'] == str( Community.get(name=patched_community_metadata['name']).id) headers = res.headers with app.app_context(): with app.test_client() as client: login_user(allowed_user, client) # check that the returned self link returns the same data subtest_self_link(response_data, headers, client)
def community_id_match(match): community_name = match.group(1) community = Community.get(name=community_name) return str(community.id)
def test_action_on_deleted(app, login_user, communities_permissions): """Test getting, deleting and updating a perviously deleted community.""" with app.app_context(): # create community created_community = Community.create_community(**community_metadata) community_id = created_community.id # create allowed user allowed_user = create_user('allowed') communities_permissions( allowed_user.id).delete_permission(True, community_id) db.session.commit() def assert_410_gone_result(res): """Check that the result is a 410 error with JSON message.""" assert res.status_code == 410 # check that the returned record matches the given data response_data = json.loads(res.get_data(as_text=True)) assert response_data['status'] == 410 with app.app_context(): # delete the record Community.get(community_id).delete() with app.test_client() as client: login_user(allowed_user, client) # # try DELETE # headers = [('Content-Type', 'application/json'), # ('Accept', 'application/json')] # res = client.delete(url_for('b2share_communities.communities_item', # community_id=community_id), # headers=headers) # assert_410_gone_result(res) # try GET headers = [('Content-Type', 'application/json'), ('Accept', 'application/json')] res = client.get(url_for('b2share_communities.communities_item', community_id=community_id), headers=headers) assert_410_gone_result(res) # # try PUT # headers = [('Content-Type', 'application/json'), # ('Accept', 'application/json')] # res = client.put(url_for('b2share_communities.communities_item', # community_id=community_id), # data=json.dumps(patched_community_metadata), # headers=headers) # assert_410_gone_result(res) # # try PATCH # headers = [('Content-Type', 'application/json-patch+json'), # ('Accept', 'application/json')] # res = client.patch(url_for('b2share_communities.communities_item', # community_id=community_id), # data=json.dumps(community_patch), # headers=headers) # assert_410_gone_result(res) # check that the record exist and is marked as deleted with app.app_context(): # check database state with pytest.raises(CommunityDeletedError): Community.get(community_id) community = Community.get(community_id, with_deleted=True) assert community.deleted # check that one community has been created assert len(CommunityModel.query.all()) == 1
def test_action_on_deleted(app, login_user, communities_permissions): """Test getting, deleting and updating a perviously deleted community.""" with app.app_context(): # create community created_community = Community.create_community(**community_metadata) community_id = created_community.id # create allowed user allowed_user = create_user('allowed') communities_permissions(allowed_user.id).delete_permission( True, community_id) db.session.commit() def assert_410_gone_result(res): """Check that the result is a 410 error with JSON message.""" assert res.status_code == 410 # check that the returned record matches the given data response_data = json.loads(res.get_data(as_text=True)) assert response_data['status'] == 410 with app.app_context(): # delete the record Community.get(community_id).delete() with app.test_client() as client: login_user(allowed_user, client) # # try DELETE # headers = [('Content-Type', 'application/json'), # ('Accept', 'application/json')] # res = client.delete(url_for('b2share_communities.communities_item', # community_id=community_id), # headers=headers) # assert_410_gone_result(res) # try GET headers = [('Content-Type', 'application/json'), ('Accept', 'application/json')] res = client.get(url_for('b2share_communities.communities_item', community_id=community_id), headers=headers) assert_410_gone_result(res) # # try PUT # headers = [('Content-Type', 'application/json'), # ('Accept', 'application/json')] # res = client.put(url_for('b2share_communities.communities_item', # community_id=community_id), # data=json.dumps(patched_community_metadata), # headers=headers) # assert_410_gone_result(res) # # try PATCH # headers = [('Content-Type', 'application/json-patch+json'), # ('Accept', 'application/json')] # res = client.patch(url_for('b2share_communities.communities_item', # community_id=community_id), # data=json.dumps(community_patch), # headers=headers) # assert_410_gone_result(res) # check that the record exist and is marked as deleted with app.app_context(): # check database state with pytest.raises(CommunityDeletedError): Community.get(community_id) community = Community.get(community_id, with_deleted=True) assert community.deleted # check that one community has been created assert len(CommunityModel.query.all()) == 1
def _process_record(rec): #rec is dict representing 1 record #from json donwloaded from b2share_v1 API result = {} generic_keys = ['open_access', 'contact_email', 'publication_date'] for k in generic_keys: result[k] = rec[k] result['license'] = {'license': rec['licence']} result['titles'] = [] result['titles'].append({'title':rec['title']}) result['descriptions'] = [] element = {} element['description_type'] = 'Abstract' element['description'] = rec['description'] result['descriptions'].append(element) result['contributors'] = [] contributors = unique(rec['contributors']) contributors = unique(rec['contributors']) for contributor in contributors: element = {} element['contributor_type'] = "Other" element['contributor_name'] = contributor result['contributors'].append(element) result['keywords'] = unique(rec['keywords']) creators = unique(rec['creator']) result['creators'] = [] for creator in creators: result['creators'].append({'creator_name':creator}) result['publisher'] = rec.get('publisher', "https://b2share.eudat.eu") if rec.get('discipline'): result['disciplines'] = unique(rec.get('discipline')) if rec.get('language'): result['language'] = rec.get('language') if rec.get('version'): result['version'] = rec.get('version') if rec.get('embargo_date'): result['embargo_date'] = rec.get('embargo_date') #fetch community rec['domain'] = rec['domain'].upper() #hardcoded Aalto exception if rec['domain'] == 'AALTO': rec['domain'] = 'Aalto' comms = Community.get_all(0, 1, name=rec['domain']) if comms: community = comms[0] result['community'] = str(community.id) elif rec['domain'] == 'GENERIC': community = Community.get(name='EUDAT') result['community'] = str(community.id) elif rec['domain'] == 'LINGUISTICS': community = Community.get(name='CLARIN') result['community'] = str(community.id) else: raise Exception("Community not found for domain: `{}`".format(rec['domain'])) result['alternate_identifiers'] = [{ 'alternate_identifier_type':'B2SHARE_V1_ID', 'alternate_identifier': str(rec['record_id']) }] if 'PID' in rec.keys(): result['alternate_identifiers'].append({ 'alternate_identifier_type':'ePIC_PID', 'alternate_identifier': rec['PID'] }) if 'resource_type' in rec.keys(): translate = { 'Audio': 'Audiovisual', 'Video': 'Audiovisual', 'Time-series': 'Dataset', 'Text': 'Text', 'Image': 'Image', 'Other': 'Other', 'treebank':'Other', 'Time-Series':'Dataset' } resource_types = unique([translate[r] for r in rec['resource_type']]) result['resource_types'] = [] for rt in resource_types: element = {'resource_type_general':rt} result['resource_types'].append(element) if not result['resource_types']: result['resource_types'] = [{'resource_type_general': "Other"}] result['community_specific'] = {} if 'domain_metadata' in rec.keys(): result.update( _match_community_specific_metadata(rec, community) ) return result