Skip to content

Commit

Permalink
increase coverage of api/dao/hierarchy.py with uid-upload edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrussimon committed Apr 28, 2017
1 parent d2b6b5c commit 5488cc0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
7 changes: 4 additions & 3 deletions api/dao/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def upsert_file(self, fileinfo):
self.update_file(fileinfo)
else:
self.add_file(fileinfo)
def update_file(self, fileinfo):

def update_file(self, fileinfo):
update_set = {self.file_prefix + '.$.modified': datetime.datetime.utcnow()}
# in this method, we are overriding an existing file.
# update_set allows to update all the fileinfo like size, hash, etc.
Expand Down Expand Up @@ -313,7 +313,7 @@ def _find_or_create_destination_project(group_id, project_label, timestamp, user
group_id, project_label = _group_id_fuzzy_match(group_id, project_label)
group = config.db.groups.find_one({'_id': group_id})

project = config.db.projects.find_one({'group': group['_id'],'label': {'$regex': re.escape(project_label), '$options': 'i'}})
project = config.db.projects.find_one({'group': group['_id'], 'label': {'$regex': re.escape(project_label), '$options': 'i'}})

if project:
# If the project already exists, check the user's access
Expand Down Expand Up @@ -572,7 +572,8 @@ def _update_container_nulls(base_query, update, container_type):
return config.db[coll_name].find_one(base_query)


def merge_fileinfos(parsed_files, infos):
# NOTE skip coverage since this function is currently not used
def merge_fileinfos(parsed_files, infos): # pragma: no cover
"""it takes a dictionary of "hard_infos" (file size, hash)
merging them with infos derived from a list of infos on the same or on other files
"""
Expand Down
33 changes: 31 additions & 2 deletions test/integration_tests/python/test_uploads.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import json

import dateutil.parser
Expand All @@ -8,7 +9,7 @@ def test_upload_without_login(as_public):
assert r.status_code == 403


def test_uid_upload(data_builder, file_form, as_admin, as_user):
def test_uid_upload(data_builder, file_form, as_admin, as_user, api_db):
group = data_builder.create_group()

# try to uid-upload w/o metadata
Expand Down Expand Up @@ -37,10 +38,24 @@ def test_uid_upload(data_builder, file_form, as_admin, as_user):
}
}

# try to uid-upload to new project w/o group rw perms
r = as_user.post('/upload/uid', files=file_form(*uid_files, meta=uid_meta))
assert r.status_code == 403

# uid-upload files
r = as_admin.post('/upload/uid', files=file_form(*uid_files, meta=uid_meta))
assert r.ok

# try to uid-upload to existing project w/o project rw perms
uid_meta_2 = copy.deepcopy(uid_meta)
uid_meta_2['session']['uid'] = uid_meta_2['acquisition']['uid'] = 'uid_upload_2'
r = as_user.post('/upload/uid', files=file_form(*uid_files, meta=uid_meta_2))
assert r.status_code == 403

# uid-upload to existing project but new session uid
r = as_admin.post('/upload/uid', files=file_form(*uid_files, meta=uid_meta_2))
assert r.ok

# uid-upload files to existing session uid
r = as_admin.post('/upload/uid', files=file_form(*uid_files, meta=uid_meta))
assert r.ok
Expand All @@ -49,8 +64,22 @@ def test_uid_upload(data_builder, file_form, as_admin, as_user):
r = as_user.post('/upload/uid', files=file_form(*uid_files, meta=uid_meta))
assert r.status_code == 403

# TODO figure out why api.dao.hierarchy._group_id_fuzzy_match is NOT called below

# # uid-upload to fat-fingered group id (should end up in group)
# uid_meta_fuzzy = copy.deepcopy(uid_meta)
# uid_meta_fuzzy['group']['_id'] = 'c' + group
# r = as_admin.post('/upload/uid', files=file_form(*uid_files, meta=uid_meta_fuzzy))
# assert r.ok

# # uid-upload to utterly non-existent group id (should end up in unknown group)
# uid_meta_unknown = copy.deepcopy(uid_meta)
# uid_meta_unknown['group']['_id'] = '0000000000000000000000000'
# r = as_admin.post('/upload/uid', files=file_form(*uid_files, meta=uid_meta_unknown))
# assert r.ok

# uid-match-upload files (to the same session and acquisition uid's as above)
uid_match_meta = uid_meta.copy()
uid_match_meta = copy.deepcopy(uid_meta)
del uid_match_meta['group']
r = as_admin.post('/upload/uid-match', files=file_form(*uid_files, meta=uid_match_meta))
assert r.ok
Expand Down

0 comments on commit 5488cc0

Please sign in to comment.