Skip to content

Commit a0f96d9

Browse files
committed
#56: Example of my_files usage.
1 parent 0912f51 commit a0f96d9

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

QA/py/tests/test_my_files.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
# This file is part of Ecotaxa, see license.md in the application root directory for license informations.
3+
# Copyright (C) 2015-2023 Picheral, Colin, Irisson (UPMC-CNRS)
4+
#
5+
# Exhibit some not-so-intuitive behavior of /my_files and associated upload
6+
#
7+
8+
import logging
9+
import shutil
10+
11+
import pytest
12+
from starlette import status
13+
14+
from tests.credentials import ADMIN_USER_ID, CREATOR_AUTH, CREATOR_USER_ID
15+
from tests.test_import import FILE_IMPORT_URL, SHARED_DIR, PLAIN_DIR, PLAIN_FILE, V6_FILE, create_project, \
16+
PLAIN_FILE_PATH
17+
from tests.test_jobs import wait_for_stable, api_wait_for_stable_job, api_check_job_ok, api_check_job_errors
18+
from tests.test_import_simple import UPLOAD_FILE_URL
19+
20+
21+
@pytest.mark.parametrize("title", ["Try my files"])
22+
def test_my_files(config, database, fastapi, caplog, title):
23+
"""
24+
Simple import with no fixed values at all, but using the upload directory.
25+
"""
26+
caplog.set_level(logging.DEBUG)
27+
prj_id = create_project(CREATOR_USER_ID, title)
28+
29+
TAG = "XXX"
30+
DEST_FILE_NAME = "LOKI_46-24hours_01.zip"
31+
32+
# Copy an existing test file into current dir, simulating client side
33+
shutil.copyfile(SHARED_DIR / V6_FILE, DEST_FILE_NAME)
34+
35+
# Upload this file
36+
with open(DEST_FILE_NAME, "rb") as fin:
37+
upload_rsp = fastapi.post(UPLOAD_FILE_URL, headers=CREATOR_AUTH,
38+
data={"tag": TAG}, # /!\ If no tag -> random use-once directory!
39+
files={"file": fin})
40+
assert upload_rsp.status_code == 200
41+
srv_file_path = upload_rsp.json()
42+
assert TAG in srv_file_path
43+
44+
# The tag becomes a top-level directory
45+
list_rsp = fastapi.get(UPLOAD_FILE_URL, headers=CREATOR_AUTH)
46+
assert list_rsp.status_code == 200
47+
my_files_root: Dict = list_rsp.json()
48+
assert my_files_root["path"] == ""
49+
assert len(my_files_root["entries"]) == 1
50+
assert my_files_root["entries"][0] == {'mtime': '', 'name': TAG, 'size': 0, 'type': 'D'}
51+
52+
# The file is stored in the subdirectory
53+
list_rsp = fastapi.get(UPLOAD_FILE_URL + TAG, headers=CREATOR_AUTH)
54+
assert list_rsp.status_code == 200
55+
my_files_subdir: Dict = list_rsp.json()
56+
assert my_files_subdir["path"] == TAG
57+
assert len(my_files_subdir["entries"]) == 1
58+
the_file = my_files_subdir["entries"][0]
59+
del the_file["mtime"] # Unpredictable
60+
assert the_file == {'name': DEST_FILE_NAME, 'size': 22654, 'type': 'F'}
61+
62+
# Import the file without the right path -> Error
63+
url = FILE_IMPORT_URL.format(project_id=prj_id)
64+
req = {"source_path": DEST_FILE_NAME}
65+
rsp = fastapi.post(url, headers=CREATOR_AUTH, json=req)
66+
assert rsp.status_code == status.HTTP_200_OK
67+
job_id = rsp.json()["job_id"]
68+
api_wait_for_stable_job(fastapi, job_id)
69+
errors = api_check_job_errors(fastapi, job_id)
70+
assert "FileNotFoundError" in "".join(errors)
71+
72+
# Import the file with the right path
73+
# The below (unfortunately) hard-coded path is valid on current configuration of EcoTaxa
74+
file_path = "/tmp/ecotaxa_user.{}/{}/{}" \
75+
.format(CREATOR_USER_ID, # Should come from /api/users/me
76+
TAG, # existing tag, created the on the first file creation with it
77+
DEST_FILE_NAME # can come from an entry in GET /my_files/TAG
78+
)
79+
req = {"source_path": file_path}
80+
rsp = fastapi.post(url, headers=CREATOR_AUTH, json=req)
81+
assert rsp.status_code == status.HTTP_200_OK
82+
job_id = rsp.json()["job_id"]
83+
api_wait_for_stable_job(fastapi, job_id)
84+
api_check_job_ok(fastapi, job_id)

0 commit comments

Comments
 (0)