@@ -263,5 +263,77 @@ def test_is_directory(self):
263263 self .assertTrue (obs )
264264
265265
266+ class DeleteFileFromCentralHandlerTests (OauthTestingBase ):
267+ def setUp (self ):
268+ super (DeleteFileFromCentralHandlerTests , self ).setUp ()
269+ self .endpoint = '/cloud/delete_file_from_central/'
270+ self .base_data_dir = qdb .util .get_db_files_base_dir ()
271+ self ._clean_up_files = []
272+
273+ def tearDown (self ):
274+ for fp in self ._clean_up_files :
275+ if exists (fp ):
276+ if isdir (fp ):
277+ rmtree (fp )
278+ else :
279+ remove (fp )
280+
281+ def test_post (self ):
282+ # check if error is raised when NOT providing a filepath
283+ obs = self .get_authed (self .endpoint )
284+ self .assertEqual (obs .status_code , 403 )
285+ self .assertIn ("You cannot delete file '/', which" , obs .reason )
286+
287+ # check if error is raised when deleting something in productive mode
288+ # we need to let qiita thinks for this test, to NOT be in test mode
289+ with TRN :
290+ TRN .add ("UPDATE settings SET test = False" )
291+ TRN .execute ()
292+ obs = self .get_authed (self .endpoint )
293+ with TRN :
294+ TRN .add ("UPDATE settings SET test = True" )
295+ TRN .execute ()
296+ self .assertEqual (obs .status_code , 403 )
297+ self .assertEqual ("You cannot delete files through this API endpoint"
298+ ", when Qiita is not in test-mode!" , obs .reason )
299+
300+ # check if error is raised when deleting existing file outside of base
301+ # dir
302+ obs = self .get_authed (self .endpoint + 'home' )
303+ self .assertEqual (obs .status_code , 403 )
304+ self .assertIn ("You cannot delete file '/home', which" , obs .reason )
305+
306+ # check if a file can be deleted
307+ # step 1: create file
308+ fp_file = join (self .base_data_dir , 'deleteme' )
309+ with open (fp_file , 'w' ) as f :
310+ f .write ("this file shall be deleted" )
311+ self ._clean_up_files .append (fp_file )
312+ # step 2: ensure file exists
313+ self .assertTrue (exists (fp_file ))
314+ # step 3: delete file via API
315+ obs = self .get_authed (self .endpoint + fp_file )
316+ self .assertEqual (obs .status_code , 200 )
317+ self .assertIn ("Deleted file %s from BASE_DATA_DIR" % fp_file ,
318+ str (obs .content ))
319+ # step 4: ensure file does not exist anymore
320+ self .assertFalse (exists (fp_file ))
321+
322+ # check if a directory can be deleted
323+ # step 1: create directory
324+ fp_dir = join (self .base_data_dir , 'deletemeDir' )
325+ makedirs (fp_dir )
326+ self ._clean_up_files .append (fp_dir )
327+ # step 2: ensure file exists
328+ self .assertTrue (exists (fp_dir ))
329+ # step 3: delete file via API
330+ obs = self .get_authed (self .endpoint + fp_dir )
331+ self .assertEqual (obs .status_code , 200 )
332+ self .assertIn ("Deleted directory %s from BASE_DATA_DIR" % fp_dir ,
333+ str (obs .content ))
334+ # step 4: ensure file does not exist anymore
335+ self .assertFalse (exists (fp_dir ))
336+
337+
266338if __name__ == "__main__" :
267339 main ()
0 commit comments