55# Exhibit some not-so-intuitive behavior of /my_files and associated upload
66#
77
8- import logging
98import shutil
109from typing import Dict
1110
1211import pytest
1312from starlette import status
1413
1514from tests .credentials import CREATOR_AUTH , CREATOR_USER_ID
16- from tests .jobs import (
17- api_check_job_ok ,
18- )
1915from tests .api_wrappers import (
2016 api_file_import ,
2117 api_wait_for_stable_job ,
2218 api_check_job_errors ,
23- UPLOAD_FILE_URL ,
19+ MY_FILES_URL ,
20+ upload_file ,
21+ api_check_job_ok ,
2422)
2523from tests .test_import import (
2624 SHARED_DIR ,
@@ -41,20 +39,33 @@ def test_my_files(fastapi, tstlogs, title):
4139
4240 DEST_FILE_NAME = "LOKI_46-24hours_01.zip"
4341 DEST_DIR_NAME = "LOKI_46-24hours_01"
42+
4443 # Copy an existing test file into current dir, simulating client side
4544 shutil .copyfile (SHARED_DIR / V6_FILE , tstlogs / DEST_FILE_NAME )
4645 # Upload this file
47- upload_file (fastapi , DEST_FILE_NAME , DIRPATH + "/" + DEST_FILE_NAME , tstlogs )
46+ remote_path = upload_file (
47+ fastapi ,
48+ tstlogs / DEST_FILE_NAME ,
49+ DIRPATH + SEPARATOR + DEST_FILE_NAME ,
50+ CREATOR_AUTH ,
51+ )
52+ assert DIRPATH in remote_path # The subdirectory was created
4853
4954 # And another
5055 DEST_FILE_NAME2 = "readme.txt"
5156 # Copy an existing test file into current dir, simulating client side
5257 shutil .copyfile (SHARED_DIR / "HOWTO.txt" , tstlogs / DEST_FILE_NAME2 )
5358 # Upload this file
54- upload_file (fastapi , DEST_FILE_NAME2 , DIRPATH + "/" + DEST_FILE_NAME2 , tstlogs )
59+ upload_file (
60+ fastapi ,
61+ tstlogs / DEST_FILE_NAME2 ,
62+ DIRPATH + SEPARATOR + DEST_FILE_NAME2 ,
63+ CREATOR_AUTH ,
64+ )
65+ assert DIRPATH in remote_path # The subdirectory was created
5566
5667 # The pathparam becomes a top-level directory
57- list_rsp = fastapi .get (UPLOAD_FILE_URL + SEPARATOR , headers = CREATOR_AUTH )
68+ list_rsp = fastapi .get (MY_FILES_URL + SEPARATOR , headers = CREATOR_AUTH )
5869 assert list_rsp .status_code == 200
5970 my_files_root : Dict = list_rsp .json ()
6071 assert my_files_root ["path" ] == ""
@@ -73,17 +84,13 @@ def test_my_files(fastapi, tstlogs, title):
7384
7485 # The files are stored in the subdirectory
7586 DIRDEST = DIRPATH + SEPARATOR + DEST_DIR_NAME
76- list_rsp = fastapi .get (UPLOAD_FILE_URL + SEPARATOR + DIRDEST , headers = CREATOR_AUTH )
87+ list_rsp = fastapi .get (MY_FILES_URL + SEPARATOR + DIRDEST , headers = CREATOR_AUTH )
7788 assert list_rsp .status_code == 200
7889 my_files_subdir : Dict = list_rsp .json ()
7990 assert my_files_subdir ["path" ] == DIRDEST
8091 assert (
8192 len (my_files_subdir ["entries" ]) == 1
8293 ) # The second file being .txt will have size 0 on re-read
83- # test no longer valid as the zip is deleted after successful unzip
84- # the_file = [fil for fil in my_files_subdir["entries"] if fil["size"] == 22654][0]
85- # del the_file["mtime"] # Unpredictable
86- # assert the_file == {"name": DEST_FILE_NAME, "size": 22654, "type": "F"}
8794
8895 # Import the file without the right path -> Error
8996 req = {"source_path" : DEST_FILE_NAME }
@@ -94,33 +101,9 @@ def test_my_files(fastapi, tstlogs, title):
94101 errors = api_check_job_errors (fastapi , job_id )
95102 assert "FileNotFoundError" in "" .join (errors )
96103
97- # Import the file with the right path
98- # The below (unfortunately) hard-coded path is valid on the current configuration of EcoTaxa
99- file_path = "/tmp/ecotaxa_user.{}/{}/{}" .format (
100- CREATOR_USER_ID , # Should come from /api/users/me
101- DIRPATH , # existing tag, created the on the first file creation with it
102- DEST_FILE_NAME , # can come from an entry in GET /my_files/DIRPATH
103- )
104- req = {"source_path" : file_path }
104+ req = {"source_path" : DIRDEST }
105105 rsp = api_file_import (fastapi , prj_id , req , auth = CREATOR_AUTH )
106106 assert rsp .status_code == status .HTTP_200_OK
107- job_id = rsp .json ()["job_id" ]
108- api_wait_for_stable_job (fastapi , job_id )
109- # api_check_job_ok(fastapi, job_id) # TODO: It's _not_ anymore the right path
110-
111-
112- def upload_file (fastapi , dest_file_name , pathparam , tstlogs ):
113- print ("uploadfile----------" + str (dest_file_name ), pathparam )
114- with open (tstlogs / dest_file_name , "rb" ) as fin :
115- upload_rsp = fastapi .post (
116- UPLOAD_FILE_URL ,
117- headers = CREATOR_AUTH ,
118- data = {
119- "path" : pathparam
120- }, # /!\ If no pathparam error-> random use-once directory!
121- files = {"file" : fin },
122- )
123- assert upload_rsp .status_code == 200
124- srv_file_path = upload_rsp .json ()
125- print ("srv_file" , srv_file_path )
126- assert DIRPATH in srv_file_path
107+ job = api_wait_for_stable_job (fastapi , rsp .json ()["job_id" ])
108+ api_wait_for_stable_job (fastapi , job .id )
109+ api_check_job_ok (fastapi , job .id )
0 commit comments