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

from nomad import config, api, files

12
from tests.test_processing import example_files
13
14
from tests.test_files import assert_exists
# import fixtures
15
from tests.test_files import clear_files, archive_id  # pylint: disable=unused-import
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

@pytest.fixture
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


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)


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

    return data


def test_no_uploads(client):
    rv = client.get('/uploads')

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


def test_bad_upload_id(client):
    rv = client.get('/uploads/bad_id')
    assert rv.status_code == 400


def test_not_existing_upload(client):
    rv = client.get('/uploads/123456789012123456789012')
    assert rv.status_code == 404


def test_create_upload(client):
    rv = client.post('/uploads')

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

    rv = client.get('/uploads/%s' % upload_id)
    assert rv.status_code == 200
    assert_upload(rv.data, id=upload_id)

    rv = client.get('/uploads')
    assert rv.status_code == 200
    assert_uploads(rv.data, count=1, id=upload_id)


81
@pytest.mark.parametrize("file", example_files)
82
@pytest.mark.timeout(10)
83
def test_upload_to_upload(client, file):
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    rv = client.post('/uploads')
    assert rv.status_code == 200
    upload = assert_upload(rv.data)

    @files.upload_put_handler
    def handle_upload_put(received_upload_id: str):
        assert upload['id'] == received_upload_id
        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']
101
    cmd = files.create_curl_upload_cmd(upload_url).replace('<ZIPFILE>', file)
102
103
104
105
    subprocess.call(shlex.split(cmd))

    handle_uploads_thread.join()

106
    assert_exists(config.files.uploads_bucket, upload['id'])
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136


@pytest.mark.parametrize("file", example_files)
@pytest.mark.timeout(10)
def test_processing(client, file):
    handle_uploads_thread = api.start_upload_handler(quit=True)

    rv = client.post('/uploads')
    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))

    handle_uploads_thread.join()

    while True:
        time.sleep(1)

        rv = client.get('/uploads/%s' % upload['id'])
        assert rv.status_code == 200
        upload = assert_upload(rv.data)
        assert 'upload_time' in upload
        assert 'processing' in upload

        if upload['processing']['status'] in ['SUCCESS', 'FAILURE']:
            break

137
138
139
140
141
    processing = upload['processing']
    assert processing['status'] == 'SUCCESS'
    assert 'results' in processing
    assert processing['results'] is not None
    assert processing['current_task'] == 'nomad.processing.close_upload'
142
    assert_exists(config.files.uploads_bucket, upload['id'])
143
144
145
146
147
148
149
150
151
152


def test_get_archive(client, archive_id):
    rv = client.get('/archive/%s' % archive_id)
    assert rv.status_code == 200


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