Commit f9e8c8cc authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Improved admin uploads cli test, added proc reset function.

parent cba76e53
......@@ -121,15 +121,15 @@ def ls(ctx, uploads, calculations, ids, json):
@uploads.command(help='Change the owner of the upload and all its calcs.')
@click.argument('EMAIL', nargs=1)
@click.argument('USERNAME', nargs=1)
@click.argument('UPLOADS', nargs=-1)
@click.pass_context
def chown(ctx, email, uploads):
def chown(ctx, username, uploads):
_, uploads = query_uploads(ctx, uploads)
print('%d uploads selected, changing its owner ...' % uploads.count())
user = datamodel.User.get(email=email)
user = datamodel.User.get(username=username)
for upload in uploads:
upload.user_id = user.user_id
......@@ -139,14 +139,38 @@ def chown(ctx, email, uploads):
def create_update(calc):
return UpdateOne(
{'_id': calc.calc_id},
{'$set': {'metadata.uploader': user.to_popo()}})
{'$set': {'metadata.uploader': user.user_id}})
proc.Calc._get_collection().bulk_write([create_update(calc) for calc in calcs])
upload.save()
upload_with_metadata = upload.to_upload_with_metadata()
calcs = upload_with_metadata.calcs
search.publish(calcs)
search.index_all(calcs, do_refresh=False)
search.refresh()
@uploads.command(help='Change the owner of the upload and all its calcs.')
@click.argument('UPLOADS', nargs=-1)
@click.option('--with-calcs', is_flag=True, help='Also reset all calculations.')
@click.pass_context
def reset(ctx, uploads, with_calcs):
_, uploads = query_uploads(ctx, uploads)
uploads_count = uploads.count()
print('%d uploads selected, resetting their processing ...' % uploads_count)
i = 0
for upload in uploads:
if with_calcs:
for calc in proc.Calc.objects(upload_id=upload.upload_id):
calc.reset()
calc.save()
upload.reset()
upload.save()
i += 1
print('resetted %d of %d uploads' % (i, uploads_count))
@uploads.command(help='(Re-)index all calcs of the given uploads.')
......
......@@ -236,7 +236,7 @@ class KeycloakMock:
for user_id, user_values in self.users.items():
if user_values['username'] == username:
return User(**user_values)
raise KeyError('Only test user emails are recognized')
raise KeyError('Only test user usernames are recognized')
else:
assert False, 'no token based get_user during tests'
......
......@@ -181,6 +181,37 @@ class TestAdminUploads:
with upload_files.archive_file(calc.calc_id) as f:
f.read()
def test_chown(self, published, test_user, other_test_user):
upload_id = published.upload_id
calc = Calc.objects(upload_id=upload_id).first()
assert calc.metadata['uploader'] == other_test_user.user_id
result = click.testing.CliRunner().invoke(
cli, ['admin', 'uploads', 'chown', test_user.username, upload_id], catch_exceptions=False, obj=utils.POPO())
assert result.exit_code == 0
assert 'changing' in result.stdout
upload = Upload.objects(upload_id=upload_id).first()
calc.reload()
assert upload.user_id == test_user.user_id
assert calc.metadata['uploader'] == test_user.user_id
def test_reset(self, non_empty_processed):
upload_id = non_empty_processed.upload_id
result = click.testing.CliRunner().invoke(
cli, ['admin', 'uploads', 'reset', '--with-calcs', upload_id], catch_exceptions=False, obj=utils.POPO())
assert result.exit_code == 0
assert 'reset' in result.stdout
upload = Upload.objects(upload_id=upload_id).first()
calc = Calc.objects(upload_id=upload_id).first()
assert upload.tasks_status == proc.PENDING
assert calc.tasks_status == proc.PENDING
@pytest.mark.usefixtures('reset_config')
class TestClient:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment