Skip to content
Snippets Groups Projects
Unverified Commit f6e0e4aa authored by Megan Henning's avatar Megan Henning Committed by GitHub
Browse files

Merge pull request #1039 from scitran/subject-info

Allow subject info editing in new style
parents f68bbf60 61afd806
No related branches found
No related tags found
No related merge requests found
......@@ -259,6 +259,7 @@ endpoints = [
prefix('/<cid:{cid}>', [
route( '/info', ContainerHandler, h='modify_info', m=['POST']),
route( '/<subject:subject>/info', ContainerHandler, h='modify_info', m=['POST']),
route('/<list_name:tags>', TagsListHandler, m=['POST']),
route('/<list_name:tags>/<value:{tag}>', TagsListHandler, m=['GET', 'PUT', 'DELETE']),
......
......@@ -230,7 +230,12 @@ class ContainerStorage(object):
f['info_exists'] = bool(f.pop('info', False))
return results
def modify_info(self, _id, payload):
def modify_info(self, _id, payload, modify_subject=False):
# Support modification of subject info
# Can be removed when subject becomes a standalone container
info_key = 'subject.info' if modify_subject else 'info'
update = {}
set_payload = payload.get('set')
delete_payload = payload.get('delete')
......@@ -242,7 +247,7 @@ class ContainerStorage(object):
if replace_payload is not None:
update = {
'$set': {
'info': util.mongo_sanitize_fields(replace_payload)
info_key: util.mongo_sanitize_fields(replace_payload)
}
}
......@@ -250,11 +255,11 @@ class ContainerStorage(object):
if set_payload:
update['$set'] = {}
for k,v in set_payload.items():
update['$set']['info.' + k] = util.mongo_sanitize_fields(v)
update['$set'][info_key + '.' + k] = util.mongo_sanitize_fields(v)
if delete_payload:
update['$unset'] = {}
for k in delete_payload:
update['$unset']['info.' + k] = ''
update['$unset'][info_key + '.' + k] = ''
if self.use_object_id:
_id = bson.objectid.ObjectId(_id)
......
......@@ -510,6 +510,13 @@ class ContainerHandler(base.RequestHandler):
def modify_info(self, cont_name, **kwargs):
_id = kwargs.pop('cid')
# Support subject info modification in new style
# Will be removed when subject becomes stand-alone container
modify_subject = True if 'subject' in kwargs else False
if modify_subject and cont_name != 'sessions':
self.abort(400, 'Subject info modification only allowed via session.')
self.config = self.container_handler_configurations[cont_name]
self.storage = self.config['storage']
container = self._get_container(_id)
......@@ -519,7 +526,7 @@ class ContainerHandler(base.RequestHandler):
validators.validate_data(payload, 'info_update.json', 'input', 'POST')
permchecker(noop)('PUT', _id=_id)
self.storage.modify_info(_id, payload)
self.storage.modify_info(_id, payload, modify_subject=modify_subject)
return
......
......@@ -883,9 +883,120 @@ def test_edit_file_info(data_builder, as_admin, file_form):
assert r.json()['info'] == {}
def test_edit_subject_info(data_builder, as_admin, as_user):
"""
These tests can be removed when subject becomes it's own container
"""
project = data_builder.create_project()
session = data_builder.create_session()
r = as_admin.get('/sessions/' + session + '/subject')
assert r.ok
assert not r.json().get('info')
# Attempt to set subject info at project level
r = as_admin.post('/projects/' + project + '/subject/info', json={
'replace': {'not_going': 'to_happen'}
})
assert r.status_code == 400
# Send improper payload
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'delete': ['map'],
'replace': {'not_going': 'to_happen'}
})
assert r.status_code == 400
# Send improper payload
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'delete': {'a': 'map'},
})
assert r.status_code == 400
# Send improper payload
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'set': 'cannot do this',
})
assert r.status_code == 400
# Attempt full replace of info
subject_info = {
'a': 'b',
'test': 123,
'map': {
'a': 'b'
},
'list': [1,2,3]
}
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'replace': subject_info
})
assert r.ok
r = as_admin.get('/sessions/' + session + '/subject')
assert r.ok
assert r.json()['info'] == subject_info
# Use 'set' to add new key
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'set': {'new': False}
})
assert r.ok
subject_info['new'] = False
r = as_admin.get('/sessions/' + session + '/subject')
assert r.ok
assert r.json()['info'] == subject_info
# Use 'set' to do full replace of "map" key
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'set': {'map': 'no longer a map'}
})
assert r.ok
subject_info['map'] = 'no longer a map'
r = as_admin.get('/sessions/' + session + '/subject')
assert r.ok
assert r.json()['info'] == subject_info
# Use 'delete' to unset "map" key
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'delete': ['map', 'a']
})
assert r.ok
subject_info.pop('map')
subject_info.pop('a')
r = as_admin.get('/sessions/' + session + '/subject')
assert r.ok
assert r.json()['info'] == subject_info
# Use 'delete' on keys that do not exist
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'delete': ['madeup', 'keys']
})
assert r.ok
r = as_admin.get('/sessions/' + session + '/subject')
assert r.ok
assert r.json()['info'] == subject_info
# Use 'replace' to set file info to {}
r = as_admin.post('/sessions/' + session + '/subject/info', json={
'replace': {}
})
assert r.ok
r = as_admin.get('/sessions/' + session + '/subject')
assert r.ok
assert r.json()['info'] == {}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment