Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ def prefix(path, routes):
prefix('/<cont_name:{cname}>', [
route( '/<cid:{cid}>', ContainerHandler, m=['GET','PUT','DELETE']),
prefix('/<cid:{cid}>', [

route( '/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']),

Expand Down
38 changes: 38 additions & 0 deletions api/dao/basecontainerstorage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bson
import datetime
import pymongo.errors

from . import consistencychecker
Expand Down Expand Up @@ -203,3 +204,40 @@ def get_all_el(self, query, user, projection, fill_defaults=False):
for f in cont.get('files', []):
f['info_exists'] = bool(f.pop('info', False))
return results

def modify_info(self, _id, payload):
update = {}
set_payload = payload.get('set')
delete_payload = payload.get('delete')
replace_payload = payload.get('replace')

if (set_payload or delete_payload) and replace_payload is not None:
raise APIStorageException('Cannot set or delete AND replace info fields.')

if replace_payload is not None:
update = {
'$set': {
'info': util.mongo_sanitize_fields(replace_payload)
}
}

else:
if set_payload:
update['$set'] = {}
for k,v in set_payload.items():
update['$set']['info.' + k] = util.mongo_sanitize_fields(v)
if delete_payload:
update['$unset'] = {}
for k in delete_payload:
update['$unset']['info.' + k] = ''

if self.use_object_id:
_id = bson.objectid.ObjectId(_id)
query = {'_id': _id }

if not update.get('$set'):
update['$set'] = {'modified': datetime.datetime.utcnow()}
else:
update['$set']['modified'] = datetime.datetime.utcnow()

return self.dbc.update_one(query, update)
15 changes: 15 additions & 0 deletions api/handlers/containerhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,21 @@ def put(self, cont_name, **kwargs):
else:
self.abort(404, 'Element not updated in container {} {}'.format(self.storage.cont_name, _id))

def modify_info(self, cont_name, **kwargs):
_id = kwargs.pop('cid')
self.config = self.container_handler_configurations[cont_name]
self.storage = self.config['storage']
container = self._get_container(_id)
permchecker = self._get_permchecker(container)
payload = self.request.json_body

validators.validate_data(payload, 'info_update.json', 'input', 'POST')

permchecker(noop)('PUT', _id=_id)
self.storage.modify_info(_id, payload)

return

def delete(self, cont_name, **kwargs):
_id = kwargs.pop('cid')
self.config = self.container_handler_configurations[cont_name]
Expand Down
111 changes: 111 additions & 0 deletions tests/integration_tests/python/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,117 @@ def test_edit_file_attributes(data_builder, as_admin, file_form):
assert r.status_code == 400


def test_edit_container_info(data_builder, as_admin, as_user):
"""
When files become their own collections in Mongo, consider combining
the file info and container info tests.
"""

project = data_builder.create_project()


r = as_admin.get('/projects/' + project)
assert r.ok
assert not r.json()['info']

# Send improper payload
r = as_admin.post('/projects/' + project + '/info', json={
'delete': ['map'],
'replace': {'not_going': 'to_happen'}
})
assert r.status_code == 400

# Send improper payload
r = as_admin.post('/projects/' + project + '/info', json={
'delete': {'a': 'map'},
})
assert r.status_code == 400

# Send improper payload
r = as_admin.post('/projects/' + project + '/info', json={
'set': 'cannot do this',
})
assert r.status_code == 400

# Attempt full replace of info
project_info = {
'a': 'b',
'test': 123,
'map': {
'a': 'b'
},
'list': [1,2,3]
}


r = as_admin.post('/projects/' + project + '/info', json={
'replace': project_info
})
assert r.ok

r = as_admin.get('/projects/' + project)
assert r.ok
assert r.json()['info'] == project_info


# Use 'set' to add new key
r = as_admin.post('/projects/' + project + '/info', json={
'set': {'new': False}
})
assert r.ok

project_info['new'] = False
r = as_admin.get('/projects/' + project)
assert r.ok
assert r.json()['info'] == project_info


# Use 'set' to do full replace of "map" key
r = as_admin.post('/projects/' + project + '/info', json={
'set': {'map': 'no longer a map'}
})
assert r.ok

project_info['map'] = 'no longer a map'
r = as_admin.get('/projects/' + project)
assert r.ok
assert r.json()['info'] == project_info


# Use 'delete' to unset "map" key
r = as_admin.post('/projects/' + project + '/info', json={
'delete': ['map', 'a']
})
assert r.ok

project_info.pop('map')
project_info.pop('a')
r = as_admin.get('/projects/' + project)
assert r.ok
assert r.json()['info'] == project_info


# Use 'delete' on keys that do not exist
r = as_admin.post('/projects/' + project + '/info', json={
'delete': ['madeup', 'keys']
})
assert r.ok

r = as_admin.get('/projects/' + project)
assert r.ok
assert r.json()['info'] == project_info


# Use 'replace' to set file info to {}
r = as_admin.post('/projects/' + project + '/info', json={
'replace': {}
})
assert r.ok

r = as_admin.get('/projects/' + project)
assert r.ok
assert r.json()['info'] == {}

def test_edit_file_info(data_builder, as_admin, file_form):
project = data_builder.create_project()
file_name = 'test_file.txt'
Expand Down