11from  unittest  import  main 
2- from  os .path  import  exists , isfile 
2+ from  os .path  import  exists , basename 
33from  os  import  remove 
4- from   shutil   import  rmtree 
4+ import  filecmp 
55
66from  qiita_db .handlers .tests .oauthbase  import  OauthTestingBase 
77import  qiita_db  as  qdb 
@@ -11,33 +11,80 @@ class FetchFileFromCentralHandlerTests(OauthTestingBase):
1111    def  setUp (self ):
1212        super (FetchFileFromCentralHandlerTests , self ).setUp ()
1313
14-         self ._clean_up_files  =  []
15- 
16-     def  tearDown (self ):
17-         super (FetchFileFromCentralHandlerTests , self ).tearDown ()
18-         for  fp  in  self ._clean_up_files :
19-             if  exists (fp ):
20-                 if  isfile (fp ):
21-                     remove (fp )
22-                 else :
23-                     rmtree (fp )
24- 
2514    def  test_get (self ):
2615        endpoint  =  '/cloud/fetch_file_from_central/' 
27-         obs  =  self .get (endpoint  +  'nonexistingfile' , headers = self .header )
28-         self .assertEqual (obs .code , 403 )
16+         base_data_dir  =  qdb .util .get_db_files_base_dir ()
17+ 
18+         obs  =  self .get (endpoint  +  'nonexistingfile' )
19+         self .assertEqual (obs .status_code , 403 )
2920        self .assertIn ('outside of the BASE_DATA_DIR' , obs .reason )
3021
31-         base_data_dir  =  qdb .util .get_db_files_base_dir ()
32-         obs  =  self .get (endpoint  +  base_data_dir [1 :] +  '/nonexistingfile' ,
33-                        headers = self .header )
34-         self .assertEqual (obs .code , 403 )
22+         obs  =  self .get (endpoint  +  base_data_dir [1 :] +  '/nonexistingfile' )
23+         self .assertEqual (obs .status_code , 403 )
3524        self .assertIn ('The requested file is not present' , obs .reason )
3625
3726        obs  =  self .get (endpoint  +  base_data_dir [1 :] + 
38-                        '/raw_data/FASTA_QUAL_preprocessing.fna' ,
39-                        headers = self .header )
40-         print (obs .__dict__ )
27+                        '/raw_data/FASTA_QUAL_preprocessing.fna' )
28+         self .assertEqual (obs .status_code , 200 )
29+         self .assertIn ('FLP3FBN01ELBSX length=250 xy=1766_01' , str (obs .content ))
30+ 
31+ 
32+ class  PushFileToCentralHandlerTests (OauthTestingBase ):
33+     def  setUp (self ):
34+         super (PushFileToCentralHandlerTests , self ).setUp ()
35+ 
36+     def  test_post (self ):
37+         endpoint  =  '/cloud/push_file_to_central/' 
38+         base_data_dir  =  qdb .util .get_db_files_base_dir ()
39+ 
40+         # create a test file "locally", i.e. in current working directory 
41+         fp_source  =  'foo.bar' 
42+         with  open (fp_source , 'w' ) as  f :
43+             f .write ("this is a test\n " )
44+         self ._files_to_remove .append (fp_source )
45+ 
46+         # if successful, expected location of the file in BASE_DATA_DIR 
47+         fp_target  =  base_data_dir  +  '/bar/'  +  basename (fp_source )
48+         self ._files_to_remove .append (fp_target )
49+ 
50+         # create a second test file 
51+         fp_source2  =  'foo_two.bar' 
52+         with  open (fp_source2 , 'w' ) as  f :
53+             f .write ("this is another test\n " )
54+         self ._files_to_remove .append (fp_source2 )
55+         fp_target2  =  base_data_dir  +  '/barr/'  +  basename (fp_source2 )
56+         self ._files_to_remove .append (fp_target2 )
57+ 
58+         # test raise error if no file is given 
59+         obs  =  self .post (endpoint )
60+         self .assertEqual (obs .reason , "No files to upload defined!" )
61+ 
62+         # test correct mechanism 
63+         with  open (fp_source , 'rb' ) as  fh :
64+             obs  =  self .post (endpoint ,
65+                             files = {'bar/' : fh })
66+             self .assertIn ('Stored 1 files into BASE_DATA_DIR of Qiita' ,
67+                           str (obs .content ))
68+             self .assertTrue (filecmp .cmp (fp_source , fp_target , shallow = False ))
69+ 
70+         # check if error is raised, if file already exists 
71+         with  open (fp_source , 'rb' ) as  fh :
72+             obs  =  self .post (endpoint , files = {'bar/' : fh })
73+             self .assertIn ("already present in Qiita's BASE_DATA_DIR!" ,
74+                           obs .reason )
75+ 
76+         # test transfer of multiple files 
77+         if  exists (fp_target ):
78+             remove (fp_target )
79+         with  open (fp_source , 'rb' ) as  fh1 :
80+             with  open (fp_source2 , 'rb' ) as  fh2 :
81+                 obs  =  self .post (endpoint , files = {'bar/' : fh1 , 'barr/' : fh2 })
82+                 self .assertIn ('Stored 2 files into BASE_DATA_DIR of Qiita' ,
83+                               str (obs .content ))
84+                 self .assertTrue (filecmp .cmp (fp_source , fp_target ,
85+                                             shallow = False ))
86+                 self .assertTrue (filecmp .cmp (fp_source2 , fp_target2 ,
87+                                             shallow = False ))
4188
4289
4390if  __name__  ==  "__main__" :
0 commit comments