From fca57924f63147555d29f6b629c57d5c9168fe1b Mon Sep 17 00:00:00 2001
From: Markus Scheidgen <markus.scheidgen@gmail.com>
Date: Mon, 25 Jan 2021 13:14:07 +0100
Subject: [PATCH] Allow the admin user to provide uploader metadata via
 nomad.json.

---
 nomad/processing/data.py                      |   4 ++++
 tests/conftest.py                             |   1 +
 .../data/proc/examples_with_adminmetadata.zip | Bin 0 -> 1144 bytes
 tests/processing/test_data.py                 |  22 ++++++++++++++++++
 4 files changed, 27 insertions(+)
 create mode 100644 tests/data/proc/examples_with_adminmetadata.zip

diff --git a/nomad/processing/data.py b/nomad/processing/data.py
index bf46e6b609..8d10a9f8b8 100644
--- a/nomad/processing/data.py
+++ b/nomad/processing/data.py
@@ -586,6 +586,10 @@ class Calc(Proc):
             if definition is None and self.upload.from_oasis:
                 definition = _oasis_metadata.get(key, None)
 
+            if key == 'uploader':
+                if datamodel.User.get(self.upload.user_id).is_admin:
+                    definition = datamodel.EntryMetadata.uploader
+
             if definition is None:
                 logger.warn('Users cannot set metadata', quantity=key)
                 continue
diff --git a/tests/conftest.py b/tests/conftest.py
index 16fecf4119..288c741498 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -297,6 +297,7 @@ def keycloak(monkeypatch):
 @pytest.fixture(scope='function')
 def proc_infra(worker, elastic, mongo, raw_files):
     ''' Combines all fixtures necessary for processing (elastic, worker, files, mongo) '''
+    processing.Upload.metadata_file_cached.cache_clear()
     return dict(elastic=elastic)
 
 
diff --git a/tests/data/proc/examples_with_adminmetadata.zip b/tests/data/proc/examples_with_adminmetadata.zip
new file mode 100644
index 0000000000000000000000000000000000000000..de6f4503aaa28cad845af6765aa66d4ffdeef977
GIT binary patch
literal 1144
zcmWIWW@Zs#U|`^2NRGD*3Y?b63*zZ9Ffgbw$S|Z<B<2?6q!!1QXO?8d=cbk<rX-dm
z#;0ZGr0VD8=O(7;RVL=<gobc3Fjp?&OAubdmrz>4&A`a=m63q~Y_7KMiIe_5p1PZY
z3>uhX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1H+O=5R2Gs$qLDqXn6zKF3b#wY*z^b1KuJ4EjzL@Fk{Jz
W0p6@^AV)JXFfrU_U|?ux2Jrxxh{B!#

literal 0
HcmV?d00001

diff --git a/tests/processing/test_data.py b/tests/processing/test_data.py
index 1edf034a82..eeb53fee31 100644
--- a/tests/processing/test_data.py
+++ b/tests/processing/test_data.py
@@ -606,3 +606,25 @@ def test_read_metadata_from_file(proc_infra, test_user, other_test_user):
             assert entry_coauthors[j].email == coauthors[i][j].email
             assert entry_coauthors[j].first_name == coauthors[i][j].first_name
             assert entry_coauthors[j].last_name == coauthors[i][j].last_name
+
+
+@pytest.mark.parametrize('user,uploader', [
+    ('admin_user', 'other_test_user'),
+    ('test_user', 'test_user')
+])
+def test_read_adminmetadata_from_file(proc_infra, test_user, other_test_user, admin_user, user, uploader):
+    def user_from_name(user_name):
+        if user_name == 'test_user':
+            return test_user
+        if user_name == 'other_test_user':
+            return other_test_user
+        if user_name == 'admin_user':
+            return admin_user
+
+    user = user_from_name(user)
+    uploader = user_from_name(uploader)
+
+    upload = run_processing(
+        ('test_upload', 'tests/data/proc/examples_with_adminmetadata.zip'), user)
+    calc = Calc.objects(upload_id=upload.upload_id).first()
+    assert calc.metadata['uploader'] == uploader.user_id
-- 
GitLab