test_api.py 48.6 KB
Newer Older
Markus Scheidgen's avatar
Markus Scheidgen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
from typing import Any
16
17
18
import pytest
import time
import json
19
20
import zipfile
import io
21
import inspect
Markus Scheidgen's avatar
Markus Scheidgen committed
22
import datetime
23
import os.path
24
from urllib.parse import urlencode
25
import base64
26

Markus Scheidgen's avatar
Markus Scheidgen committed
27
from nomad.app.utils import rfc3339DateTime
Markus Scheidgen's avatar
Markus Scheidgen committed
28
from nomad.app.api.auth import generate_upload_token
29
from nomad import search, parsing, files, config, utils, infrastructure
30
31
from nomad.files import UploadFiles, PublicUploadFiles
from nomad.processing import Upload, Calc, SUCCESS
32
from nomad.datamodel import UploadWithMetadata, CalcWithMetadata, User
33

34
from tests.conftest import create_auth_headers, clear_elastic
35
from tests.test_files import example_file, example_file_mainfile, example_file_contents
36
from tests.test_files import create_staging_upload, create_public_upload, assert_upload_files
37
from tests.test_search import assert_search_upload
Markus Scheidgen's avatar
Markus Scheidgen committed
38
from tests.processing import test_data as test_processing
39

Markus Scheidgen's avatar
Markus Scheidgen committed
40
from tests.app.test_app import BlueprintClient
41

42
43
logger = utils.get_logger(__name__)

Markus Scheidgen's avatar
Markus Scheidgen committed
44

Markus Scheidgen's avatar
Markus Scheidgen committed
45
46
47
@pytest.fixture(scope='function')
def api(client):
    return BlueprintClient(client, '/api')
48
49


50
@pytest.fixture(scope='function')
Markus Scheidgen's avatar
Markus Scheidgen committed
51
def test_user_signature_token(api, test_user_auth):
Markus Scheidgen's avatar
Markus Scheidgen committed
52
    rv = api.get('/auth/', headers=test_user_auth)
53
    assert rv.status_code == 200
54
    return json.loads(rv.data)['signature_token']
55
56


57
58
59
60
61
62
63
64
def get_upload_with_metadata(upload: dict) -> UploadWithMetadata:
    """ Create a :class:`UploadWithMetadata` from a API upload json record. """
    return UploadWithMetadata(
        upload_id=upload['upload_id'], calcs=[
            CalcWithMetadata(calc_id=calc['calc_id'], mainfile=calc['mainfile'])
            for calc in upload['calcs']['results']])


65
class TestInfo:
Markus Scheidgen's avatar
Markus Scheidgen committed
66
67
    def test_info(self, api):
        rv = api.get('/info/')
68
69
70
71
        data = json.loads(rv.data)
        assert 'codes' in data
        assert 'parsers' in data
        assert len(data['parsers']) >= len(data['codes'])
72
73
        assert rv.status_code == 200

74

75
class TestKeycloak:
Markus Scheidgen's avatar
Markus Scheidgen committed
76
77
    def test_auth_wo_credentials(self, api, keycloak, no_warn):
        rv = api.get('/auth/')
78
79
        assert rv.status_code == 401

80
    @pytest.fixture(scope='function')
Markus Scheidgen's avatar
Markus Scheidgen committed
81
    def auth_headers(self, api, keycloak):
82
        basic_auth = base64.standard_b64encode(b'sheldon.cooper@nomad-coe.eu:password')
Markus Scheidgen's avatar
Markus Scheidgen committed
83
        rv = api.get('/auth/', headers=dict(Authorization='Basic %s' % basic_auth.decode('utf-8')))
84
        assert rv.status_code == 200
85
86
87
88
89
        auth = json.loads(rv.data)
        assert 'access_token' in auth
        assert auth['access_token'] is not None
        return dict(Authorization='Bearer %s' % auth['access_token'])

Markus Scheidgen's avatar
Markus Scheidgen committed
90
    def test_auth_with_password(self, api, auth_headers):
91
92
        pass

Markus Scheidgen's avatar
Markus Scheidgen committed
93
94
    def test_auth_with_access_token(self, api, auth_headers):
        rv = api.get('/auth/', headers=auth_headers)
95
96
        assert rv.status_code == 200

97
98
99
100
101
102
103
104
105
106
    def test_get_user(self, keycloak):
        user = infrastructure.keycloak.get_user(email='sheldon.cooper@nomad-coe.eu')
        assert user.email is not None
        assert user.name == 'Sheldon Cooper'
        assert user.first_name == 'Sheldon'
        assert user.last_name == 'Cooper'
        assert user.created is not None
        assert user.affiliation is not None
        assert user.affiliation_address is not None

107

108
class TestAuth:
Markus Scheidgen's avatar
Markus Scheidgen committed
109
110
    def test_auth_wo_credentials(self, api, no_warn):
        rv = api.get('/auth/')
111
        assert rv.status_code == 401
112

Markus Scheidgen's avatar
Markus Scheidgen committed
113
114
    def test_auth_with_token(self, api, test_user_auth):
        rv = api.get('/auth/', headers=test_user_auth)
115
        assert rv.status_code == 200
Markus Scheidgen's avatar
Markus Scheidgen committed
116
        self.assert_auth(api, json.loads(rv.data))
117

Markus Scheidgen's avatar
Markus Scheidgen committed
118
    def assert_auth(self, api, auth):
119
        assert 'user' not in auth
120
121
122
        assert 'access_token' in auth
        assert 'upload_token' in auth
        assert 'signature_token' in auth
123

124
125
126
    def test_signature_token(self, test_user_signature_token, no_warn):
        assert test_user_signature_token is not None

127
128
129
130
131

class TestUploads:

    def assert_uploads(self, upload_json_str, count=0, **kwargs):
        data = json.loads(upload_json_str)
132
133
134
135
        assert 'pagination' in data
        assert 'page' in data['pagination']

        data = data['results']
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
        assert isinstance(data, list)
        assert len(data) == count

        if count > 0:
            self.assert_upload(json.dumps(data[0]), **kwargs)

    def assert_upload(self, upload_json_str, id=None, **kwargs):
        data = json.loads(upload_json_str)
        assert 'upload_id' in data
        if id is not None:
            assert id == data['upload_id']
        assert 'create_time' in data

        for key, value in kwargs.items():
            assert data.get(key, None) == value

        return data

Markus Scheidgen's avatar
Markus Scheidgen committed
154
    def assert_processing(self, api, test_user_auth, upload_id):
155
156
157
        upload_endpoint = '/uploads/%s' % upload_id

        # poll until completed
Markus Scheidgen's avatar
Markus Scheidgen committed
158
        upload = self.block_until_completed(api, upload_id, test_user_auth)
159
160

        assert len(upload['tasks']) == 4
161
        assert upload['tasks_status'] == SUCCESS
162
        assert upload['current_task'] == 'cleanup'
163
        assert not upload['process_running']
164

165
166
        calcs = upload['calcs']['results']
        for calc in calcs:
167
            assert calc['tasks_status'] == SUCCESS
168
169
            assert calc['current_task'] == 'archiving'
            assert len(calc['tasks']) == 3
Markus Scheidgen's avatar
Markus Scheidgen committed
170
            assert api.get('/archive/logs/%s/%s' % (calc['upload_id'], calc['calc_id']), headers=test_user_auth).status_code == 200
171
172

        if upload['calcs']['pagination']['total'] > 1:
Markus Scheidgen's avatar
Markus Scheidgen committed
173
            rv = api.get('%s?page=2&per_page=1&order_by=tasks_status' % upload_endpoint, headers=test_user_auth)
174
175
176
177
            assert rv.status_code == 200
            upload = self.assert_upload(rv.data)
            assert len(upload['calcs']['results']) == 1

178
179
        upload_with_metadata = get_upload_with_metadata(upload)
        assert_upload_files(upload_with_metadata, files.StagingUploadFiles)
180
        assert_search_upload(upload_with_metadata, additional_keys=['atoms', 'system'])
181

Markus Scheidgen's avatar
Markus Scheidgen committed
182
    def assert_published(self, api, test_user_auth, upload_id, proc_infra, metadata={}):
Markus Scheidgen's avatar
Markus Scheidgen committed
183
        rv = api.get('/uploads/%s' % upload_id, headers=test_user_auth)
184
        upload = self.assert_upload(rv.data)
185
186

        upload_with_metadata = get_upload_with_metadata(upload)
187

Markus Scheidgen's avatar
Markus Scheidgen committed
188
        rv = api.post(
189
190
            '/uploads/%s' % upload_id,
            headers=test_user_auth,
191
            data=json.dumps(dict(operation='publish', metadata=metadata)),
192
            content_type='application/json')
193
        assert rv.status_code == 200
194
        upload = self.assert_upload(rv.data)
195
        assert upload['current_process'] == 'publish_upload'
196
        assert upload['process_running']
197

198
        additional_keys = ['with_embargo']
Markus Scheidgen's avatar
Markus Scheidgen committed
199
        if 'external_id' in metadata:
Markus Scheidgen's avatar
Markus Scheidgen committed
200
            additional_keys.append('external_id')
201

Markus Scheidgen's avatar
Markus Scheidgen committed
202
        self.block_until_completed(api, upload_id, test_user_auth)
203

204
205
206
        upload_proc = Upload.objects(upload_id=upload_id).first()
        assert upload_proc is not None
        assert upload_proc.published is True
207
        upload_with_metadata = upload_proc.to_upload_with_metadata()
208

209
        assert_upload_files(upload_with_metadata, files.PublicUploadFiles, published=True)
210
211
        assert_search_upload(upload_with_metadata, additional_keys=additional_keys, published=True)

Markus Scheidgen's avatar
Markus Scheidgen committed
212
    def block_until_completed(self, api, upload_id: str, test_user_auth):
213
214
        while True:
            time.sleep(0.1)
Markus Scheidgen's avatar
Markus Scheidgen committed
215
            rv = api.get('/uploads/%s' % upload_id, headers=test_user_auth)
216
217
            if rv.status_code == 200:
                upload = self.assert_upload(rv.data)
218
219
                if not upload['process_running'] and not upload['tasks_running']:
                    return upload
220
            elif rv.status_code == 404:
221
                return None
222
223
224
225
            else:
                raise Exception(
                    'unexpected status code while blocking for upload processing: %s' %
                    str(rv.status_code))
226

Markus Scheidgen's avatar
Markus Scheidgen committed
227
228
    def assert_upload_does_not_exist(self, api, upload_id: str, test_user_auth):
        self.block_until_completed(api, upload_id, test_user_auth)
229

Markus Scheidgen's avatar
Markus Scheidgen committed
230
        rv = api.get('/uploads/%s' % upload_id, headers=test_user_auth)
231
232
233
234
235
        assert rv.status_code == 404
        assert Upload.objects(upload_id=upload_id).first() is None
        assert Calc.objects(upload_id=upload_id).count() is 0
        upload_files = UploadFiles.get(upload_id)
        assert upload_files is None or isinstance(upload_files, PublicUploadFiles)
Markus Scheidgen's avatar
Markus Scheidgen committed
236

Markus Scheidgen's avatar
Markus Scheidgen committed
237
238
    def test_get_command(self, api, test_user_auth, no_warn):
        rv = api.get('/uploads/command', headers=test_user_auth)
239
240
241
        assert rv.status_code == 200
        data = json.loads(rv.data)
        assert 'upload_command' in data
242
        assert '/api/uploads' in data['upload_command']
243
244
        assert 'upload_url' in data

Markus Scheidgen's avatar
Markus Scheidgen committed
245
246
    def test_get_empty(self, api, test_user_auth, no_warn):
        rv = api.get('/uploads/', headers=test_user_auth)
Markus Scheidgen's avatar
Markus Scheidgen committed
247

248
249
        assert rv.status_code == 200
        self.assert_uploads(rv.data, count=0)
Markus Scheidgen's avatar
Markus Scheidgen committed
250

Markus Scheidgen's avatar
Markus Scheidgen committed
251
252
    def test_get_not_existing(self, api, test_user_auth, no_warn):
        rv = api.get('/uploads/123456789012123456789012', headers=test_user_auth)
253
        assert rv.status_code == 404
254

Markus Scheidgen's avatar
Markus Scheidgen committed
255
    def test_put_upload_token(self, api, non_empty_example_upload, test_user):
256
257
        url = '/uploads/?token=%s&local_path=%s&name=test_upload' % (
            generate_upload_token(test_user), non_empty_example_upload)
Markus Scheidgen's avatar
Markus Scheidgen committed
258
        rv = api.put(url)
259
        assert rv.status_code == 200
260
        assert 'Thanks for uploading' in rv.data.decode('utf-8')
261

262
263
    @pytest.mark.parametrize('mode', ['multipart', 'stream', 'local_path'])
    @pytest.mark.parametrize('name', [None, 'test_name'])
Markus Scheidgen's avatar
Markus Scheidgen committed
264
    def test_put(self, api, test_user_auth, proc_infra, example_upload, mode, name, no_warn):
265
        file = example_upload
266
267
268
269
270
271
        if name:
            url = '/uploads/?name=%s' % name
        else:
            url = '/uploads/'

        if mode == 'multipart':
Markus Scheidgen's avatar
Markus Scheidgen committed
272
            rv = api.put(
273
274
275
                url, data=dict(file=(open(file, 'rb'), 'the_name')), headers=test_user_auth)
            if not name:
                name = 'the_name'
276
277
        elif mode == 'stream':
            with open(file, 'rb') as f:
Markus Scheidgen's avatar
Markus Scheidgen committed
278
                rv = api.put(url, data=f.read(), headers=test_user_auth)
279
280
281
        elif mode == 'local_path':
            url += '&' if name else '?'
            url += 'local_path=%s' % file
Markus Scheidgen's avatar
Markus Scheidgen committed
282
            rv = api.put(url, headers=test_user_auth)
283
284
        else:
            assert False
285

286
287
        assert rv.status_code == 200
        if mode == 'local_path':
288
            upload = self.assert_upload(rv.data, upload_path=file, name=name)
289
290
        else:
            upload = self.assert_upload(rv.data, name=name)
291
        assert upload['tasks_running']
292

Markus Scheidgen's avatar
Markus Scheidgen committed
293
        self.assert_processing(api, test_user_auth, upload['upload_id'])
294

Markus Scheidgen's avatar
Markus Scheidgen committed
295
296
    @pytest.mark.timeout(config.tests.default_timeout)
    def test_upload_limit(self, api, mongo, test_user, test_user_auth, proc_infra):
297
298
299
        for _ in range(0, config.services.upload_limit):
            Upload.create(user=test_user)
        file = example_file
Markus Scheidgen's avatar
Markus Scheidgen committed
300
        rv = api.put('/uploads/?local_path=%s' % file, headers=test_user_auth)
301
302
303
        assert rv.status_code == 400
        assert Upload.user_uploads(test_user).count() == config.services.upload_limit

Markus Scheidgen's avatar
Markus Scheidgen committed
304
305
    def test_delete_not_existing(self, api, test_user_auth, no_warn):
        rv = api.delete('/uploads/123456789012123456789012', headers=test_user_auth)
306
        assert rv.status_code == 404
307

308
309
310
311
312
313
314
315
316
317
318
319
    @pytest.fixture(scope='function')
    def slow_processing(self, monkeypatch):
        old_cleanup = Upload.cleanup

        def slow_cleanup(self):
            time.sleep(0.5)
            old_cleanup(self)

        monkeypatch.setattr('nomad.processing.data.Upload.cleanup', slow_cleanup)
        yield True
        monkeypatch.setattr('nomad.processing.data.Upload.cleanup', old_cleanup)

Markus Scheidgen's avatar
Markus Scheidgen committed
320
    def test_delete_published(self, api, test_user_auth, proc_infra, no_warn):
Markus Scheidgen's avatar
Markus Scheidgen committed
321
        rv = api.put('/uploads/?local_path=%s' % example_file, headers=test_user_auth)
322
        upload = self.assert_upload(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
323
        self.assert_processing(api, test_user_auth, upload['upload_id'])
Markus Scheidgen's avatar
Markus Scheidgen committed
324
        self.assert_published(api, test_user_auth, upload['upload_id'], proc_infra)
Markus Scheidgen's avatar
Markus Scheidgen committed
325
        rv = api.delete('/uploads/%s' % upload['upload_id'], headers=test_user_auth)
326
        assert rv.status_code == 400
327

Markus Scheidgen's avatar
Markus Scheidgen committed
328
329
    def test_delete(self, api, test_user_auth, proc_infra, no_warn):
        rv = api.put('/uploads/?local_path=%s' % example_file, headers=test_user_auth)
330
        upload = self.assert_upload(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
331
332
        self.assert_processing(api, test_user_auth, upload['upload_id'])
        rv = api.delete('/uploads/%s' % upload['upload_id'], headers=test_user_auth)
333
        assert rv.status_code == 200
Markus Scheidgen's avatar
Markus Scheidgen committed
334
        self.assert_upload_does_not_exist(api, upload['upload_id'], test_user_auth)
335

Markus Scheidgen's avatar
Markus Scheidgen committed
336
337
    def test_post_empty(self, api, test_user_auth, empty_upload, proc_infra, no_warn):
        rv = api.put('/uploads/?local_path=%s' % empty_upload, headers=test_user_auth)
338
339
        assert rv.status_code == 200
        upload = self.assert_upload(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
340
341
        self.assert_processing(api, test_user_auth, upload['upload_id'])
        rv = api.post(
342
343
344
345
346
            '/uploads/%s' % upload['upload_id'], headers=test_user_auth,
            data=json.dumps(dict(operation='publish')),
            content_type='application/json')
        assert rv.status_code == 400

Markus Scheidgen's avatar
Markus Scheidgen committed
347
    def test_post(self, api, test_user_auth, non_empty_example_upload, proc_infra, no_warn):
Markus Scheidgen's avatar
Markus Scheidgen committed
348
        rv = api.put('/uploads/?local_path=%s' % non_empty_example_upload, headers=test_user_auth)
349
        assert rv.status_code == 200
350
        upload = self.assert_upload(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
351
        self.assert_processing(api, test_user_auth, upload['upload_id'])
Markus Scheidgen's avatar
Markus Scheidgen committed
352
        self.assert_published(api, test_user_auth, upload['upload_id'], proc_infra)
353

354
        # still visible
Markus Scheidgen's avatar
Markus Scheidgen committed
355
        assert api.get('/uploads/%s' % upload['upload_id'], headers=test_user_auth).status_code == 200
356
        # still listed with all=True
Markus Scheidgen's avatar
Markus Scheidgen committed
357
        rv = api.get('/uploads/?state=all', headers=test_user_auth)
358
        assert rv.status_code == 200
359
        data = json.loads(rv.data)['results']
360
361
362
        assert len(data) > 0
        assert any(item['upload_id'] == upload['upload_id'] for item in data)
        # not listed with all=False
Markus Scheidgen's avatar
Markus Scheidgen committed
363
        rv = api.get('/uploads/', headers=test_user_auth)
364
        assert rv.status_code == 200
365
        data = json.loads(rv.data)['results']
366
367
        assert not any(item['upload_id'] == upload['upload_id'] for item in data)

368
    def test_post_metadata(
Markus Scheidgen's avatar
Markus Scheidgen committed
369
            self, api, proc_infra, admin_user_auth, test_user_auth, test_user,
370
            other_test_user, no_warn, example_user_metadata):
Markus Scheidgen's avatar
Markus Scheidgen committed
371
        rv = api.put('/uploads/?local_path=%s' % example_file, headers=test_user_auth)
372
        upload = self.assert_upload(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
373
        self.assert_processing(api, test_user_auth, upload['upload_id'])
374
        metadata = dict(**example_user_metadata)
375
        metadata['_upload_time'] = datetime.datetime.utcnow().isoformat()
Markus Scheidgen's avatar
Markus Scheidgen committed
376
        self.assert_published(api, admin_user_auth, upload['upload_id'], proc_infra, metadata)
377

Markus Scheidgen's avatar
Markus Scheidgen committed
378
379
    def test_post_metadata_forbidden(self, api, proc_infra, test_user_auth, no_warn):
        rv = api.put('/uploads/?local_path=%s' % example_file, headers=test_user_auth)
380
        upload = self.assert_upload(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
381
382
        self.assert_processing(api, test_user_auth, upload['upload_id'])
        rv = api.post(
383
384
            '/uploads/%s' % upload['upload_id'],
            headers=test_user_auth,
385
            data=json.dumps(dict(operation='publish', metadata=dict(_pid=256))),
386
387
388
            content_type='application/json')
        assert rv.status_code == 401

389
    def test_post_metadata_and_republish(
Markus Scheidgen's avatar
Markus Scheidgen committed
390
            self, api, proc_infra, admin_user_auth, test_user_auth, test_user,
391
            other_test_user, no_warn, example_user_metadata):
Markus Scheidgen's avatar
Markus Scheidgen committed
392
        rv = api.put('/uploads/?local_path=%s' % example_file, headers=test_user_auth)
393
        upload = self.assert_upload(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
394
        self.assert_processing(api, test_user_auth, upload['upload_id'])
395
        metadata = dict(**example_user_metadata)
396
        metadata['_upload_time'] = datetime.datetime.utcnow().isoformat()
Markus Scheidgen's avatar
Markus Scheidgen committed
397
        self.assert_published(api, admin_user_auth, upload['upload_id'], proc_infra, metadata)
Markus Scheidgen's avatar
Markus Scheidgen committed
398
        self.assert_published(api, admin_user_auth, upload['upload_id'], proc_infra, {})
399

Markus Scheidgen's avatar
Markus Scheidgen committed
400
    def test_post_re_process(self, api, published, test_user_auth, monkeypatch):
401
402
403
404
        monkeypatch.setattr('nomad.config.version', 're_process_test_version')
        monkeypatch.setattr('nomad.config.commit', 're_process_test_commit')

        upload_id = published.upload_id
Markus Scheidgen's avatar
Markus Scheidgen committed
405
        rv = api.post(
406
407
408
409
410
411
            '/uploads/%s' % upload_id,
            headers=test_user_auth,
            data=json.dumps(dict(operation='re-process')),
            content_type='application/json')

        assert rv.status_code == 200
Markus Scheidgen's avatar
Markus Scheidgen committed
412
        assert self.block_until_completed(api, upload_id, test_user_auth) is not None
413

414
    # TODO validate metadata (or all input models in API for that matter)
Markus Scheidgen's avatar
Markus Scheidgen committed
415
    # def test_post_bad_metadata(self, api, proc_infra, test_user_auth):
Markus Scheidgen's avatar
Markus Scheidgen committed
416
    #     rv = api.put('/uploads/?local_path=%s' % example_file, headers=test_user_auth)
417
    #     upload = self.assert_upload(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
418
419
    #     self.assert_processing(api, test_user_auth, upload['upload_id'])
    #     rv = api.post(
420
421
    #         '/uploads/%s' % upload['upload_id'],
    #         headers=test_user_auth,
422
    #         data=json.dumps(dict(operation='publish', metadata=dict(doesnotexist='hi'))),
423
424
425
    #         content_type='application/json')
    #     assert rv.status_code == 400

Markus Scheidgen's avatar
Markus Scheidgen committed
426
    def test_potcar(self, api, proc_infra, test_user_auth):
427
        # only the owner, shared with people are supposed to download the original potcar file
428
        example_file = 'tests/data/proc/examples_potcar.zip'
Markus Scheidgen's avatar
Markus Scheidgen committed
429
        rv = api.put('/uploads/?local_path=%s' % example_file, headers=test_user_auth)
430
431
432

        upload = self.assert_upload(rv.data)
        upload_id = upload['upload_id']
Markus Scheidgen's avatar
Markus Scheidgen committed
433
        self.assert_processing(api, test_user_auth, upload_id)
Markus Scheidgen's avatar
Markus Scheidgen committed
434
        self.assert_published(api, test_user_auth, upload_id, proc_infra)
Markus Scheidgen's avatar
Markus Scheidgen committed
435
        rv = api.get('/raw/%s/examples_potcar/POTCAR' % upload_id)
436
        assert rv.status_code == 401
Markus Scheidgen's avatar
Markus Scheidgen committed
437
        rv = api.get('/raw/%s/examples_potcar/POTCAR' % upload_id, headers=test_user_auth)
438
        assert rv.status_code == 200
Markus Scheidgen's avatar
Markus Scheidgen committed
439
        rv = api.get('/raw/%s/examples_potcar/POTCAR.stripped' % upload_id)
440
441
        assert rv.status_code == 200

442

Markus Scheidgen's avatar
Markus Scheidgen committed
443
444
445
today = datetime.datetime.utcnow().date()


446
447
448
449
450
451
452
453
454
455
456
457
458
class UploadFilesBasedTests:

    @staticmethod
    def fix_signature(func, wrapper):
        additional_args = list(inspect.signature(func).parameters.values())[4:]
        wrapper_sig = inspect.signature(wrapper)
        wrapper_args = list(wrapper_sig.parameters.values())[:3] + additional_args
        wrapper_sig = wrapper_sig.replace(parameters=tuple(wrapper_args))
        wrapper.__signature__ = wrapper_sig

    @staticmethod
    def check_authorizaton(func):
        @pytest.mark.parametrize('test_data', [
459
460
461
462
463
464
465
466
467
468
469
            [True, None, True],      # in staging for upload
            [True, None, False],     # in staging for different user
            [True, None, None],      # in staging for guest
            [True, None, 'admin'],   # in staging, for admin
            [False, True, True],     # in public, restricted for uploader
            [False, True, False],    # in public, restricted for different user
            [False, True, None],     # in public, restricted for guest
            [False, True, 'admin'],  # in public, restricted for admin
            [False, False, True],    # in public, public, for uploader
            [False, False, False],   # in public, public, for different user
            [False, False, None]     # in public, public, for guest
470
        ], indirect=True)
Markus Scheidgen's avatar
Markus Scheidgen committed
471
        def wrapper(self, api, test_data, *args, **kwargs):
472
473
            upload, authorized, auth_headers = test_data
            try:
Markus Scheidgen's avatar
Markus Scheidgen committed
474
                func(self, api, upload, auth_headers, *args, **kwargs)
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
            except AssertionError as assertion:
                assertion_str = str(assertion)
                if not authorized:
                    if '0 == 5' in assertion_str and 'ZipFile' in assertion_str:
                        # the user is not authorized an gets an empty zip as expected
                        return
                    if '401' in assertion_str:
                        # the user is not authorized and gets a 401 as expected
                        return
                raise assertion

            if not authorized:
                assert False
        UploadFilesBasedTests.fix_signature(func, wrapper)
        return wrapper

    @staticmethod
    def ignore_authorization(func):
        @pytest.mark.parametrize('test_data', [
            [True, None, True],      # in staging
            [False, False, None],    # in public
        ], indirect=True)
Markus Scheidgen's avatar
Markus Scheidgen committed
497
        def wrapper(self, api, test_data, *args, **kwargs):
498
            upload, _, auth_headers = test_data
Markus Scheidgen's avatar
Markus Scheidgen committed
499
            func(self, api, upload, auth_headers, *args, **kwargs)
500
501
        UploadFilesBasedTests.fix_signature(func, wrapper)
        return wrapper
502

503
    @pytest.fixture(scope='function')
504
    def test_data(self, request, mongo, raw_files, no_warn, test_user, other_test_user, admin_user):
505
506
507
508
509
        # delete potential old test files
        for _ in [0, 1]:
            upload_files = UploadFiles.get('test_upload')
            if upload_files:
                upload_files.delete()
510

511
        in_staging, restricted, for_uploader = request.param
512

513
        if in_staging:
514
            authorized = for_uploader is True or for_uploader == 'admin'
515
        else:
516
            authorized = not restricted or for_uploader is True or for_uploader == 'admin'
517

518
        if for_uploader is True:
519
520
521
            auth_headers = create_auth_headers(test_user)
        elif for_uploader is False:
            auth_headers = create_auth_headers(other_test_user)
522
523
        elif for_uploader == 'admin':
            auth_headers = create_auth_headers(admin_user)
524
525
        else:
            auth_headers = None
526

527
        calc_specs = 'r' if restricted else 'p'
528
        Upload.create(user=test_user, upload_id='test_upload')
529
        if in_staging:
530
            _, upload_files = create_staging_upload('test_upload', calc_specs=calc_specs)
531
        else:
532
            _, upload_files = create_public_upload('test_upload', calc_specs=calc_specs)
533

534
        yield 'test_upload', authorized, auth_headers
535

536
        upload_files.delete()
537
538


539
540
class TestArchive(UploadFilesBasedTests):
    @UploadFilesBasedTests.check_authorizaton
Markus Scheidgen's avatar
Markus Scheidgen committed
541
542
    def test_get(self, api, upload, auth_headers):
        rv = api.get('/archive/%s/0' % upload, headers=auth_headers)
543
        assert rv.status_code == 200
544
        assert json.loads(rv.data) is not None
545

546
    @UploadFilesBasedTests.ignore_authorization
Markus Scheidgen's avatar
Markus Scheidgen committed
547
    def test_get_signed(self, api, upload, _, test_user_signature_token):
Markus Scheidgen's avatar
Markus Scheidgen committed
548
        rv = api.get('/archive/%s/0?signature_token=%s' % (upload, test_user_signature_token))
549
550
551
        assert rv.status_code == 200
        assert json.loads(rv.data) is not None

552
    @UploadFilesBasedTests.check_authorizaton
Markus Scheidgen's avatar
Markus Scheidgen committed
553
554
    def test_get_calc_proc_log(self, api, upload, auth_headers):
        rv = api.get('/archive/logs/%s/0' % upload, headers=auth_headers)
555
        assert rv.status_code == 200
556
        assert len(rv.data) > 0
557

558
    @UploadFilesBasedTests.ignore_authorization
Markus Scheidgen's avatar
Markus Scheidgen committed
559
    def test_get_calc_proc_log_signed(self, api, upload, _, test_user_signature_token):
Markus Scheidgen's avatar
Markus Scheidgen committed
560
        rv = api.get('/archive/logs/%s/0?signature_token=%s' % (upload, test_user_signature_token))
561
562
563
        assert rv.status_code == 200
        assert len(rv.data) > 0

564
    @UploadFilesBasedTests.ignore_authorization
Markus Scheidgen's avatar
Markus Scheidgen committed
565
566
    def test_get_non_existing_archive(self, api, upload, auth_headers):
        rv = api.get('/archive/%s' % 'doesnt/exist', headers=auth_headers)
567
        assert rv.status_code == 404
Markus Scheidgen's avatar
Markus Scheidgen committed
568

569
570
571
572
573
    @pytest.mark.parametrize('info', [
        'all.nomadmetainfo.json',
        'all.experimental.nomadmetainfo.json',
        'vasp.nomadmetainfo.json',
        'mpes.nomadmetainfo.json'])
Markus Scheidgen's avatar
Markus Scheidgen committed
574
575
    def test_get_metainfo(self, api, info):
        rv = api.get('/archive/metainfo/%s' % info)
576
        assert rv.status_code == 200
577
578
        metainfo = json.loads((rv.data))
        assert len(metainfo) > 0
579

Markus Scheidgen's avatar
Markus Scheidgen committed
580

581
class TestRepo():
582
583
584
    @pytest.fixture(scope='class')
    def example_elastic_calcs(
            self, elastic_infra, normalized: parsing.LocalBackend,
585
            test_user: User, other_test_user: User):
586
587
        clear_elastic(elastic_infra)

Markus Scheidgen's avatar
Markus Scheidgen committed
588
        calc_with_metadata = CalcWithMetadata(upload_id=0, calc_id=0, upload_time=today)
589
        calc_with_metadata.files = ['test/mainfile.txt']
590
        calc_with_metadata.apply_domain_metadata(normalized)
591

592
593
594
        calc_with_metadata.update(datasets=[
            utils.POPO(id='ds_id', doi=dict(value='ds_doi'), name='ds_name')])

Markus Scheidgen's avatar
Markus Scheidgen committed
595
        calc_with_metadata.update(
596
            calc_id='1', uploader=test_user.user_id, published=True, with_embargo=False)
597
598
        search.Entry.from_calc_with_metadata(calc_with_metadata).save(refresh=True)

Markus Scheidgen's avatar
Markus Scheidgen committed
599
        calc_with_metadata.update(
Markus Scheidgen's avatar
Markus Scheidgen committed
600
601
602
            calc_id='2', uploader=other_test_user.user_id, published=True,
            with_embargo=False, pid=2, upload_time=today - datetime.timedelta(days=5),
            external_id='external_id')
Markus Scheidgen's avatar
Markus Scheidgen committed
603
604
        calc_with_metadata.update(
            atoms=['Fe'], comment='this is a specific word', formula='AAA', basis_set='zzz')
605
606
        search.Entry.from_calc_with_metadata(calc_with_metadata).save(refresh=True)

Markus Scheidgen's avatar
Markus Scheidgen committed
607
        calc_with_metadata.update(
Markus Scheidgen's avatar
Markus Scheidgen committed
608
609
            calc_id='3', uploader=other_test_user.user_id, published=False,
            with_embargo=False, pid=3, external_id='external_id')
610
611
        search.Entry.from_calc_with_metadata(calc_with_metadata).save(refresh=True)

Markus Scheidgen's avatar
Markus Scheidgen committed
612
        calc_with_metadata.update(
Markus Scheidgen's avatar
Markus Scheidgen committed
613
614
            calc_id='4', uploader=other_test_user.user_id, published=True,
            with_embargo=True, pid=4, external_id='external_id')
615
616
        search.Entry.from_calc_with_metadata(calc_with_metadata).save(refresh=True)

617
    def assert_search(self, rv: Any, number_of_calcs: int) -> dict:
618
619
        if rv.status_code != 200:
            print(rv.data)
620
        assert rv.status_code == 200
621

622
623
624
625
626
627
628
629
630
        data = json.loads(rv.data)

        results = data.get('results', None)
        assert results is not None
        assert isinstance(results, list)
        assert len(results) == number_of_calcs

        return data

Markus Scheidgen's avatar
Markus Scheidgen committed
631
632
    def test_own_calc(self, api, example_elastic_calcs, no_warn, test_user_auth):
        rv = api.get('/repo/0/1', headers=test_user_auth)
633
634
        assert rv.status_code == 200

Markus Scheidgen's avatar
Markus Scheidgen committed
635
636
    def test_public_calc(self, api, example_elastic_calcs, no_warn, other_test_user_auth):
        rv = api.get('/repo/0/1', headers=other_test_user_auth)
637
638
        assert rv.status_code == 200

Markus Scheidgen's avatar
Markus Scheidgen committed
639
640
    def test_embargo_calc(self, api, example_elastic_calcs, no_warn, test_user_auth):
        rv = api.get('/repo/0/4', headers=test_user_auth)
641
642
        assert rv.status_code == 401

Markus Scheidgen's avatar
Markus Scheidgen committed
643
644
    def test_own_embargo_calc(self, api, example_elastic_calcs, no_warn, other_test_user_auth):
        rv = api.get('/repo/0/4', headers=other_test_user_auth)
645
646
        assert rv.status_code == 200

Markus Scheidgen's avatar
Markus Scheidgen committed
647
648
    def test_staging_calc(self, api, example_elastic_calcs, no_warn, test_user_auth):
        rv = api.get('/repo/0/3', headers=test_user_auth)
649
650
        assert rv.status_code == 401

Markus Scheidgen's avatar
Markus Scheidgen committed
651
652
    def test_own_staging_calc(self, api, example_elastic_calcs, no_warn, other_test_user_auth):
        rv = api.get('/repo/0/3', headers=other_test_user_auth)
653
654
        assert rv.status_code == 200

Markus Scheidgen's avatar
Markus Scheidgen committed
655
656
    def test_non_existing_calcs(self, api, example_elastic_calcs, test_user_auth):
        rv = api.get('/repo/0/10', headers=test_user_auth)
657
658
        assert rv.status_code == 404

Markus Scheidgen's avatar
Markus Scheidgen committed
659
660
    def test_search_datasets(self, api, example_elastic_calcs, no_warn, other_test_user_auth):
        rv = api.get('/repo/?owner=all&datasets=true', headers=other_test_user_auth)
661
662
663
664
665
666
667
668
669
        data = self.assert_search(rv, 4)

        datasets = data.get('datasets', None)
        assert datasets is not None
        values = datasets['values']
        assert values['ds_id']['total'] == 4
        assert values['ds_id']['examples'][0]['datasets'][0]['id'] == 'ds_id'
        assert 'after' in datasets

670
671
672
    @pytest.mark.parametrize('calcs, owner, auth', [
        (2, 'all', 'none'),
        (2, 'all', 'test_user'),
673
        (4, 'all', 'other_test_user'),
674
        (1, 'user', 'test_user'),
675
        (3, 'user', 'other_test_user'),
676
        (0, 'staging', 'test_user'),
677
        (1, 'staging', 'other_test_user')
678
    ])
Markus Scheidgen's avatar
Markus Scheidgen committed
679
    def test_search_owner(self, api, example_elastic_calcs, no_warn, test_user_auth, other_test_user_auth, calcs, owner, auth):
680
        auth = dict(none=None, test_user=test_user_auth, other_test_user=other_test_user_auth).get(auth)
Markus Scheidgen's avatar
Markus Scheidgen committed
681
        rv = api.get('/repo/?owner=%s' % owner, headers=auth)
682
        data = self.assert_search(rv, calcs)
683
684
685
686
687
        results = data.get('results', None)
        if calcs > 0:
            for key in ['uploader', 'calc_id', 'formula', 'upload_id']:
                assert key in results[0]

Markus Scheidgen's avatar
Markus Scheidgen committed
688
    @pytest.mark.parametrize('calcs, start, end', [
Markus Scheidgen's avatar
Markus Scheidgen committed
689
690
691
692
693
694
        (2, today - datetime.timedelta(days=6), today),
        (2, today - datetime.timedelta(days=5), today),
        (1, today - datetime.timedelta(days=4), today),
        (1, today, today),
        (1, today - datetime.timedelta(days=6), today - datetime.timedelta(days=5)),
        (0, today - datetime.timedelta(days=7), today - datetime.timedelta(days=6)),
Markus Scheidgen's avatar
Markus Scheidgen committed
695
        (2, None, None),
Markus Scheidgen's avatar
Markus Scheidgen committed
696
697
        (1, today, None),
        (2, None, today)
Markus Scheidgen's avatar
Markus Scheidgen committed
698
    ])
Markus Scheidgen's avatar
Markus Scheidgen committed
699
    def test_search_time(self, api, example_elastic_calcs, no_warn, calcs, start, end):
Markus Scheidgen's avatar
Markus Scheidgen committed
700
701
702
703
704
705
706
707
708
709
        query_string = ''
        if start is not None:
            query_string = 'from_time=%s' % rfc3339DateTime.format(start)
        if end is not None:
            if query_string != '':
                query_string += '&'
            query_string += 'until_time=%s' % rfc3339DateTime.format(end)
        if query_string != '':
            query_string = '?%s' % query_string

Markus Scheidgen's avatar
Markus Scheidgen committed
710
        rv = api.get('/repo/%s' % query_string)
711
        self.assert_search(rv, calcs)
Markus Scheidgen's avatar
Markus Scheidgen committed
712

713
    @pytest.mark.parametrize('calcs, quantity, value', [
714
715
        (2, 'system', 'bulk'),
        (0, 'system', 'atom'),
716
717
        (1, 'atoms', 'Br'),
        (1, 'atoms', 'Fe'),
718
        (0, 'atoms', ['Fe', 'Br', 'A', 'B']),
719
720
        (0, 'only_atoms', ['Br', 'Si']),
        (1, 'only_atoms', ['Fe']),
721
722
        (1, 'only_atoms', ['Br', 'K', 'Si']),
        (1, 'only_atoms', ['Br', 'Si', 'K']),
723
        (1, 'comment', 'specific'),
724
        (1, 'authors', 'Leonard Hofstadter'),
725
726
727
728
        (2, 'files', 'test/mainfile.txt'),
        (2, 'paths', 'mainfile.txt'),
        (2, 'paths', 'test'),
        (2, 'quantities', ['wyckoff_letters_primitive', 'hall_number']),
729
730
731
        (0, 'quantities', 'dos'),
        (1, 'external_id', 'external_id'),
        (0, 'external_id', 'external')
732
    ])
Markus Scheidgen's avatar
Markus Scheidgen committed
733
    def test_search_parameters(self, api, example_elastic_calcs, no_warn, test_user_auth, calcs, quantity, value):
Markus Scheidgen's avatar
Markus Scheidgen committed
734
        query_string = urlencode({quantity: value, 'statistics': True}, doseq=True)
735

Markus Scheidgen's avatar
Markus Scheidgen committed
736
        rv = api.get('/repo/?%s' % query_string, headers=test_user_auth)
737
        logger.debug('run search quantities test', query_string=query_string)
738
        data = self.assert_search(rv, calcs)
739

740
741
        statistics = data.get('statistics', None)
        assert statistics is not None
742
        if quantity == 'system' and calcs != 0:
743
            # for simplicity we only assert on quantities for this case
744
745
746
            assert 'system' in statistics
            assert len(statistics['system']) == 1
            assert value in statistics['system']
747

748
749
    metrics_permutations = [[], search.metrics_names] + [[metric] for metric in search.metrics_names]

Markus Scheidgen's avatar
Markus Scheidgen committed
750
751
    def test_search_admin(self, api, example_elastic_calcs, no_warn, admin_user_auth):
        rv = api.get('/repo/?owner=admin', headers=admin_user_auth)
752
753
        self.assert_search(rv, 4)

Markus Scheidgen's avatar
Markus Scheidgen committed
754
755
    def test_search_admin_auth(self, api, example_elastic_calcs, no_warn, test_user_auth):
        rv = api.get('/repo/?owner=admin', headers=test_user_auth)
756
757
        assert rv.status_code == 401

Markus Scheidgen's avatar
Markus Scheidgen committed
758
        rv = api.get('/repo/?owner=admin')
759
760
        assert rv.status_code == 401

761
    @pytest.mark.parametrize('metrics', metrics_permutations)
Markus Scheidgen's avatar
Markus Scheidgen committed
762
763
    def test_search_total_metrics(self, api, example_elastic_calcs, no_warn, metrics):
        rv = api.get('/repo/?%s' % urlencode(dict(metrics=metrics, statistics=True, datasets=True), doseq=True))
764
        assert rv.status_code == 200, str(rv.data)
Markus Scheidgen's avatar
Markus Scheidgen committed
765
        data = json.loads(rv.data)
766
        total_metrics = data.get('statistics', {}).get('total', {}).get('all', None)
767
768
        assert total_metrics is not None
        assert 'code_runs' in total_metrics
Markus Scheidgen's avatar
Markus Scheidgen committed
769
        for metric in metrics:
770
            assert metric in total_metrics
Markus Scheidgen's avatar
Markus Scheidgen committed
771

772
    @pytest.mark.parametrize('metrics', metrics_permutations)
Markus Scheidgen's avatar
Markus Scheidgen committed
773
774
    def test_search_aggregation_metrics(self, api, example_elastic_calcs, no_warn, metrics):
        rv = api.get('/repo/?%s' % urlencode(dict(metrics=metrics, statistics=True, datasets=True), doseq=True))
Markus Scheidgen's avatar
Markus Scheidgen committed
775
776
        assert rv.status_code == 200
        data = json.loads(rv.data)
777
        for name, quantity in data.get('statistics').items():
778
            for metrics_result in quantity.values():
Markus Scheidgen's avatar
Markus Scheidgen committed
779
                assert 'code_runs' in metrics_result
780
781
782
783
784
                if name != 'authors':
                    for metric in metrics:
                        assert metric in metrics_result
                else:
                    assert len(metrics_result) == 1  # code_runs is the only metric for authors