Skip to content

Commit 4d923d1

Browse files
committed
Fix tests
1 parent fb9cbaa commit 4d923d1

1 file changed

Lines changed: 82 additions & 72 deletions

File tree

server/mergin/tests/test_private_project_api.py

Lines changed: 82 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -424,27 +424,102 @@ 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
427+
# test_download_proj_data = [
428+
# # project archive does not exist yet -> 202
429+
# (0, None, 202),
430+
# # specified version does not exist -> 404
431+
# (0, "v100", 404),
432+
# # zip is ready to download
433+
# (1, None, 200),
434+
# ]
435+
#
436+
#
437+
# @pytest.mark.parametrize(
438+
# "zipfile,version,exp_resp", test_download_proj_data
439+
# )
440+
# @patch("mergin.sync.tasks.create_project_version_zip.delay")
441+
def test_download_project(
442+
# mock_create_zip,
443+
client,
444+
# zipfile,
445+
# partial,
446+
# expired,
447+
# version,
448+
# exp_resp,
449+
# exp_call,
450+
diff_project,
451+
):
452+
"""Test download endpoint responses and celery task calling"""
453+
resp = client.head(
454+
url_for(
455+
"/app.mergin_sync_private_api_controller_download_project",
456+
id=diff_project.id,
457+
version="",
458+
)
459+
)
460+
# zip archive does not exist yet
461+
assert resp.status_code == 202
462+
project_version = diff_project.get_latest_version()
463+
# pretend archive has been created
464+
zip_path = Path(project_version.zip_path)
465+
if zip_path.parent.exists():
466+
shutil.rmtree(zip_path.parent, ignore_errors=True)
467+
zip_path.parent.mkdir(parents=True, exist_ok=True)
468+
zip_path.touch()
469+
resp = client.head(
470+
url_for(
471+
"/app.mergin_sync_private_api_controller_download_project",
472+
id=diff_project.id,
473+
version="",
474+
)
475+
)
476+
# zip archive is ready -> download can start
477+
assert resp.status_code == 200
478+
479+
480+
def test_large_project_download_fail(client, diff_project):
481+
"""Test downloading too large project is refused"""
482+
resp = client.get(
483+
url_for(
484+
"/app.mergin_sync_private_api_controller_download_project",
485+
id=diff_project.id,
486+
version="v1",
487+
)
488+
)
489+
assert resp.status_code == 202
490+
# pretend testing project to be too large by lowering limit
491+
client.application.config["MAX_DOWNLOAD_ARCHIVE_SIZE"] = 10
492+
resp = client.get(
493+
url_for(
494+
"/app.mergin_sync_private_api_controller_download_project",
495+
id=diff_project.id,
496+
version="v1",
497+
)
498+
)
499+
assert resp.status_code == 400
500+
501+
502+
test_prepare_proj_data = [
503+
# zips do not exist, version not specified -> trigger celery to create zip with latest version
429504
(0, 0, 0, None, 202, 1),
430505
# expired partial zip exists -> call celery task
431506
(0, 1, 1, None, 202, 1),
432-
# valid partial zip exists -> return, do not call celery
507+
# valid partial zip exists -> do not call celery
433508
(0, 1, 0, None, 202, 0),
434509
# zips do not exist, version specified -> call celery task with specified version
435510
(0, 0, 0, "v1", 202, 1),
436511
# specified version does not exist -> 404
437512
(0, 0, 0, "v100", 404, 0),
438-
# zip is ready to download
513+
# zip is already prepared to download -> do not call celery
439514
(1, 0, 0, None, 200, 0),
440515
]
441516

442517

443518
@pytest.mark.parametrize(
444-
"zipfile,partial,expired,version,exp_resp,exp_call", test_download_proj_data
519+
"zipfile,partial,expired,version,exp_resp,exp_call", test_prepare_proj_data
445520
)
446521
@patch("mergin.sync.tasks.create_project_version_zip.delay")
447-
def test_download_project(
522+
def test_prepare_archive(
448523
mock_create_zip,
449524
client,
450525
zipfile,
@@ -474,7 +549,7 @@ def test_download_project(
474549
seconds=current_app.config["PARTIAL_ZIP_EXPIRATION"] + 1
475550
)
476551
modify_file_times(temp_zip_path, new_time)
477-
resp = client.get(
552+
resp = client.post(
478553
url_for(
479554
"/app.mergin_sync_private_api_controller_download_project",
480555
id=diff_project.id,
@@ -487,68 +562,3 @@ def test_download_project(
487562
call_args, _ = mock_create_zip.call_args
488563
args = call_args[0]
489564
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

Comments
 (0)