test_api.py 7.64 KB
Newer Older
1
2
3
4
5
6
7
8
import pytest
from threading import Thread
import subprocess
import shlex
import time
import json
from mongoengine import connect
from mongoengine.connection import disconnect
9
from datetime import datetime, timedelta
10
import base64
11

12
13
14
15
16
17
from nomad import config
# for convinience we test the api without path prefix
services_config = config.services._asdict()
services_config.update(api_base_path='')
config.services = config.NomadServicesConfig(**services_config)

Markus Scheidgen's avatar
Markus Scheidgen committed
18
19
from nomad import api, files  # noqa
from nomad.processing import Upload, handle_uploads_thread  # noqa
20

Markus Scheidgen's avatar
Markus Scheidgen committed
21
from tests.processing.test_data import example_files  # noqa
22
from tests.test_files import assert_exists  # noqa
23

24
# import fixtures
25
26
27
from tests.test_files import clear_files, archive_id  # noqa pylint: disable=unused-import
from tests.test_normalizing import normalized_vasp_example  # noqa pylint: disable=unused-import
from tests.test_parsing import parsed_vasp_example  # noqa pylint: disable=unused-import
Markus Scheidgen's avatar
Markus Scheidgen committed
28
from tests.test_repo import example_elastic_calc  # noqa pylint: disable=unused-import
29

30

31
@pytest.fixture(scope='function')
32
33
34
35
36
37
38
39
def client():
    disconnect()
    connect('users_test', host=config.mongo.host, is_mock=True)

    api.app.config['TESTING'] = True
    client = api.app.test_client()

    yield client
40
    Upload._get_collection().drop()
41
42


43
44
45
46
47
48
49
@pytest.fixture(scope='session')
def test_user_auth():
    return {
        'Authorization': 'Basic %s' % base64.b64encode(b'me@gmail.com:nomad').decode('utf-8')
    }


50
51
52
53
54
55
56
57
58
def assert_uploads(upload_json_str, count=0, **kwargs):
    data = json.loads(upload_json_str)
    assert isinstance(data, list)
    assert len(data) == count

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


59
def assert_upload(upload_json_str, id=None, **kwargs):
60
    data = json.loads(upload_json_str)
Markus Scheidgen's avatar
Markus Scheidgen committed
61
    assert 'upload_id' in data
62
    if id is not None:
Markus Scheidgen's avatar
Markus Scheidgen committed
63
        assert id == data['upload_id']
64
65
    assert 'create_time' in data
    assert 'presigned_url' in data
66
    assert 'upload_command' in data
67

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

71
72
73
    return data


74
75
def test_no_uploads(client, test_user_auth):
    rv = client.get('/uploads', headers=test_user_auth)
76
77
78
79
80

    assert rv.status_code == 200
    assert_uploads(rv.data, count=0)


81
82
def test_not_existing_upload(client, test_user_auth):
    rv = client.get('/uploads/123456789012123456789012', headers=test_user_auth)
83
84
85
    assert rv.status_code == 404


86
def test_stale_upload(client, test_user_auth):
87
88
    rv = client.post(
        '/uploads',
89
        headers=test_user_auth,
90
91
92
93
94
        data=json.dumps(dict(name='test_name')),
        content_type='application/json')
    assert rv.status_code == 200
    upload_id = assert_upload(rv.data)['upload_id']

Markus Scheidgen's avatar
Markus Scheidgen committed
95
    upload = Upload.get(upload_id)
96
97
98
    upload.create_time = datetime.now() - timedelta(days=2)
    upload.save()

99
    rv = client.get('/uploads/%s' % upload_id, headers=test_user_auth)
100
101
102
103
    assert rv.status_code == 200
    assert_upload(rv.data, is_stale=True)


104
105
def test_create_upload(client, test_user_auth):
    rv = client.post('/uploads', headers=test_user_auth)
106
107

    assert rv.status_code == 200
Markus Scheidgen's avatar
Markus Scheidgen committed
108
    upload_id = assert_upload(rv.data)['upload_id']
109

110
    rv = client.get('/uploads/%s' % upload_id, headers=test_user_auth)
111
    assert rv.status_code == 200
112
    assert_upload(rv.data, id=upload_id, is_stale=False)
113

114
    rv = client.get('/uploads', headers=test_user_auth)
115
116
117
118
    assert rv.status_code == 200
    assert_uploads(rv.data, count=1, id=upload_id)


119
def test_create_upload_with_name(client, test_user_auth):
120
    rv = client.post(
121
122
        '/uploads', headers=test_user_auth,
        data=json.dumps(dict(name='test_name')), content_type='application/json')
123
124
125
126
127
    assert rv.status_code == 200
    upload = assert_upload(rv.data)
    assert upload['name'] == 'test_name'


128
129
def test_delete_empty_upload(client, test_user_auth):
    rv = client.post('/uploads', headers=test_user_auth)
Markus Scheidgen's avatar
Markus Scheidgen committed
130
131
132
133

    assert rv.status_code == 200
    upload_id = assert_upload(rv.data)['upload_id']

134
    rv = client.delete('/uploads/%s' % upload_id, headers=test_user_auth)
Markus Scheidgen's avatar
Markus Scheidgen committed
135
136
    assert rv.status_code == 200

137
    rv = client.get('/uploads/%s' % upload_id, headers=test_user_auth)
Markus Scheidgen's avatar
Markus Scheidgen committed
138
139
140
    assert rv.status_code == 404


141
@pytest.mark.parametrize("file", example_files)
Markus Scheidgen's avatar
Markus Scheidgen committed
142
@pytest.mark.timeout(30)
143
144
def test_upload_to_upload(client, file, test_user_auth):
    rv = client.post('/uploads', headers=test_user_auth)
145
146
147
148
149
    assert rv.status_code == 200
    upload = assert_upload(rv.data)

    @files.upload_put_handler
    def handle_upload_put(received_upload_id: str):
Markus Scheidgen's avatar
Markus Scheidgen committed
150
        assert upload['upload_id'] == received_upload_id
151
152
153
154
155
156
157
158
159
160
        raise StopIteration

    def handle_uploads():
        handle_upload_put(received_upload_id='provided by decorator')

    handle_uploads_thread = Thread(target=handle_uploads)
    handle_uploads_thread.start()

    time.sleep(1)
    upload_url = upload['presigned_url']
161
    cmd = files.create_curl_upload_cmd(upload_url).replace('<ZIPFILE>', file)
162
163
164
165
    subprocess.call(shlex.split(cmd))

    handle_uploads_thread.join()

166
167
    upload_id = upload['upload_id']
    assert_exists(config.files.uploads_bucket, upload_id)
168
169
170


@pytest.mark.parametrize("file", example_files)
Markus Scheidgen's avatar
Markus Scheidgen committed
171
@pytest.mark.timeout(10)
172
def test_processing(client, file, worker, mocksearch, test_user_auth):
Markus Scheidgen's avatar
Markus Scheidgen committed
173
    handler = handle_uploads_thread(quit=True)
174

175
    rv = client.post('/uploads', headers=test_user_auth)
176
177
178
179
180
181
182
183
    assert rv.status_code == 200
    upload = assert_upload(rv.data)

    time.sleep(1)
    upload_url = upload['presigned_url']
    cmd = files.create_curl_upload_cmd(upload_url).replace('<ZIPFILE>', file)
    subprocess.call(shlex.split(cmd))

Markus Scheidgen's avatar
Markus Scheidgen committed
184
    handler.join()
185
186
187
188

    while True:
        time.sleep(1)

189
        rv = client.get('/uploads/%s' % upload['upload_id'], headers=test_user_auth)
190
191
192
        assert rv.status_code == 200
        upload = assert_upload(rv.data)
        assert 'upload_time' in upload
Markus Scheidgen's avatar
Markus Scheidgen committed
193
        if upload['completed']:
Markus Scheidgen's avatar
Markus Scheidgen committed
194
            break
195

Markus Scheidgen's avatar
Markus Scheidgen committed
196
197
198
    assert len(upload['tasks']) == 4
    assert upload['status'] == 'SUCCESS'
    assert upload['current_task'] == 'cleanup'
199
    calcs = upload['calcs']['results']
Markus Scheidgen's avatar
Markus Scheidgen committed
200
201
202
203
204
205
    for calc in calcs:
        assert calc['status'] == 'SUCCESS'
        assert calc['current_task'] == 'archiving'
        assert len(calc['tasks']) == 3
        assert_exists(config.files.uploads_bucket, upload['upload_id'])

206
207
208
209
210
211
    if upload['calcs']['pagination']['total'] > 1:
        rv = client.get('/uploads/%s?page=2&per_page=1&order_by=status' % upload['upload_id'])
        assert rv.status_code == 200
        upload = assert_upload(rv.data)
        assert len(upload['calcs']['results']) == 1

Markus Scheidgen's avatar
Markus Scheidgen committed
212
    time.sleep(1)
213
214


Markus Scheidgen's avatar
Markus Scheidgen committed
215
216
217
def test_repo_calc(client, example_elastic_calc):
    rv = client.get(
        '/repo/%s/%s' % (example_elastic_calc.upload_hash, example_elastic_calc.calc_hash))
218
219
220
    assert rv.status_code == 200


221
def test_non_existing_repo_cals(client):
222
223
224
225
    rv = client.get('/repo/doesnt/exist')
    assert rv.status_code == 404


Markus Scheidgen's avatar
Markus Scheidgen committed
226
def test_repo_calcs(client, example_elastic_calc):
227
228
229
230
231
232
233
234
235
    rv = client.get('/repo')
    assert rv.status_code == 200
    data = json.loads(rv.data)
    results = data.get('results', None)
    assert results is not None
    assert isinstance(results, list)
    assert len(results) >= 1


Markus Scheidgen's avatar
Markus Scheidgen committed
236
def test_repo_calcs_pagination(client, example_elastic_calc):
237
238
239
240
241
242
243
244
245
    rv = client.get('/repo?page=1&per_page=1')
    assert rv.status_code == 200
    data = json.loads(rv.data)
    results = data.get('results', None)
    assert results is not None
    assert isinstance(results, list)
    assert len(results) == 1


246
247
def test_get_archive(client, archive_id):
    rv = client.get('/archive/%s' % archive_id)
248
    assert rv.status_code == 302
249
250
251
252
253


def test_get_non_existing_archive(client):
    rv = client.get('/archive/%s' % 'doesnt/exist')
    assert rv.status_code == 404