@@ -424,27 +424,81 @@ def test_admin_project_list(client):
424424 assert len (resp .json ["items" ]) == 14
425425
426426
427- test_download_proj_data = [
428- # zips do not exist, version not specified -> call celery task to create zip with latest version
429- (0 , 0 , 0 , None , 202 , 1 ),
427+ def test_download_project (
428+ client ,
429+ diff_project ,
430+ ):
431+ """Test download endpoint responses"""
432+ resp = client .head (
433+ url_for (
434+ "/app.mergin_sync_private_api_controller_download_project" ,
435+ id = diff_project .id ,
436+ version = "" ,
437+ )
438+ )
439+ # zip archive does not exist yet
440+ assert resp .status_code == 202
441+ project_version = diff_project .get_latest_version ()
442+ # pretend archive has been created
443+ zip_path = Path (project_version .zip_path )
444+ if zip_path .parent .exists ():
445+ shutil .rmtree (zip_path .parent , ignore_errors = True )
446+ zip_path .parent .mkdir (parents = True , exist_ok = True )
447+ zip_path .touch ()
448+ resp = client .head (
449+ url_for (
450+ "/app.mergin_sync_private_api_controller_download_project" ,
451+ id = diff_project .id ,
452+ version = "" ,
453+ )
454+ )
455+ # zip archive is ready -> download can start
456+ assert resp .status_code == 200
457+
458+
459+ def test_prepare_large_project_fail (client , diff_project ):
460+ """Test asking for too large project is refused"""
461+ resp = client .post (
462+ url_for (
463+ "/app.mergin_sync_private_api_controller_prepare_archive" ,
464+ id = diff_project .id ,
465+ version = "v1" ,
466+ )
467+ )
468+ assert resp .status_code == 201
469+ # pretend testing project to be too large by lowering limit
470+ client .application .config ["MAX_DOWNLOAD_ARCHIVE_SIZE" ] = 10
471+ resp = client .post (
472+ url_for (
473+ "/app.mergin_sync_private_api_controller_prepare_archive" ,
474+ id = diff_project .id ,
475+ version = "v1" ,
476+ )
477+ )
478+ assert resp .status_code == 400
479+
480+
481+ test_prepare_proj_data = [
482+ # zips do not exist, version not specified -> trigger celery to create zip with latest version
483+ (0 , 0 , 0 , None , 201 , 1 ),
430484 # expired partial zip exists -> call celery task
431- (0 , 1 , 1 , None , 202 , 1 ),
432- # valid partial zip exists -> return, do not call celery
433- (0 , 1 , 0 , None , 202 , 0 ),
485+ (0 , 1 , 1 , None , 201 , 1 ),
486+ # valid partial zip exists -> do not call celery
487+ (0 , 1 , 0 , None , 204 , 0 ),
434488 # zips do not exist, version specified -> call celery task with specified version
435- (0 , 0 , 0 , "v1" , 202 , 1 ),
489+ (0 , 0 , 0 , "v1" , 201 , 1 ),
436490 # specified version does not exist -> 404
437491 (0 , 0 , 0 , "v100" , 404 , 0 ),
438- # zip is ready to download
439- (1 , 0 , 0 , None , 200 , 0 ),
492+ # zip is already prepared to download -> do not call celery
493+ (1 , 0 , 0 , None , 204 , 0 ),
440494]
441495
442496
443497@pytest .mark .parametrize (
444- "zipfile,partial,expired,version,exp_resp,exp_call" , test_download_proj_data
498+ "zipfile,partial,expired,version,exp_resp,exp_call" , test_prepare_proj_data
445499)
446500@patch ("mergin.sync.tasks.create_project_version_zip.delay" )
447- def test_download_project (
501+ def test_prepare_archive (
448502 mock_create_zip ,
449503 client ,
450504 zipfile ,
@@ -455,7 +509,7 @@ def test_download_project(
455509 exp_call ,
456510 diff_project ,
457511):
458- """Test download endpoint responses and celery task calling"""
512+ """Test prepare archive endpoint responses and celery task calling"""
459513 # prepare initial state according to testcase
460514 project_version = diff_project .get_latest_version ()
461515 if zipfile :
@@ -474,7 +528,7 @@ def test_download_project(
474528 seconds = current_app .config ["PARTIAL_ZIP_EXPIRATION" ] + 1
475529 )
476530 modify_file_times (temp_zip_path , new_time )
477- resp = client .get (
531+ resp = client .post (
478532 url_for (
479533 "/app.mergin_sync_private_api_controller_download_project" ,
480534 id = diff_project .id ,
@@ -487,68 +541,3 @@ def test_download_project(
487541 call_args , _ = mock_create_zip .call_args
488542 args = call_args [0 ]
489543 assert args == diff_project .latest_version
490-
491-
492- def test_large_project_download_fail (client , diff_project ):
493- """Test downloading too large project is refused"""
494- resp = client .get (
495- url_for (
496- "/app.mergin_sync_private_api_controller_download_project" ,
497- id = diff_project .id ,
498- version = "v1" ,
499- )
500- )
501- assert resp .status_code == 202
502- # pretend testing project to be too large by lowering limit
503- client .application .config ["MAX_DOWNLOAD_ARCHIVE_SIZE" ] = 10
504- resp = client .get (
505- url_for (
506- "/app.mergin_sync_private_api_controller_download_project" ,
507- id = diff_project .id ,
508- version = "v1" ,
509- )
510- )
511- assert resp .status_code == 400
512-
513-
514- @patch ("mergin.sync.tasks.create_project_version_zip.delay" )
515- def test_remove_abandoned_zip (mock_prepare_zip , client , diff_project ):
516- """Test project download removes partial zip which is inactive for some time"""
517- latest_version = diff_project .get_latest_version ()
518- # pretend an incomplete zip remained
519- partial_zip_path = latest_version .zip_path + ".partial"
520- os .makedirs (os .path .dirname (partial_zip_path ), exist_ok = True )
521- os .mknod (partial_zip_path )
522- assert os .path .exists (partial_zip_path )
523- # pretend abandoned partial zip by lowering the expiration limit
524- client .application .config ["PARTIAL_ZIP_EXPIRATION" ] = 0
525- # download should remove it (move to temp folder) and call a celery task which will try to create a correct zip
526- resp = client .get (
527- url_for (
528- "/app.mergin_sync_private_api_controller_download_project" ,
529- id = diff_project .id ,
530- )
531- )
532- assert mock_prepare_zip .called
533- assert resp .status_code == 202
534-
535-
536- @patch ("mergin.sync.tasks.create_project_version_zip.delay" )
537- def test_download_project_request_method (mock_prepare_zip , client , diff_project ):
538- """Test head request does not create a celery job"""
539- resp = client .head (
540- url_for (
541- "/app.mergin_sync_private_api_controller_download_project" ,
542- id = diff_project .id ,
543- )
544- )
545- assert not mock_prepare_zip .called
546- assert resp .status_code == 202
547- resp = client .get (
548- url_for (
549- "/app.mergin_sync_private_api_controller_download_project" ,
550- id = diff_project .id ,
551- )
552- )
553- assert mock_prepare_zip .called
554- assert resp .status_code == 202
0 commit comments