funquail/api/tests/test_import_audio_file.py

126 lines
4.7 KiB
Python
Raw Normal View History

import os
2018-04-21 16:33:15 +02:00
2018-06-10 10:55:16 +02:00
import pytest
from django.core.management import call_command
from django.core.management.base import CommandError
2018-06-22 17:12:54 +02:00
from funkwhale_api.music.models import ImportJob
2018-09-22 12:29:30 +00:00
DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "uploads")
2018-09-22 12:29:30 +00:00
@pytest.mark.skip("XXX : wip")
def test_management_command_requires_a_valid_username(factories, mocker):
2018-06-09 15:36:16 +02:00
path = os.path.join(DATA_DIR, "dummy_file.ogg")
factories["users.User"](username="me")
2018-04-21 16:33:15 +02:00
mocker.patch(
2018-06-09 15:36:16 +02:00
"funkwhale_api.providers.audiofile.management.commands.import_files.Command.do_import", # noqa
return_value=(mocker.MagicMock(), []),
)
with pytest.raises(CommandError):
2018-06-09 15:36:16 +02:00
call_command("import_files", path, username="not_me", interactive=False)
call_command("import_files", path, username="me", interactive=False)
2018-04-21 18:16:43 +02:00
def test_in_place_import_only_from_music_dir(factories, settings):
factories["users.User"](username="me")
2018-06-09 15:36:16 +02:00
settings.MUSIC_DIRECTORY_PATH = "/nope"
path = os.path.join(DATA_DIR, "dummy_file.ogg")
2018-04-21 18:16:43 +02:00
with pytest.raises(CommandError):
call_command(
2018-06-09 15:36:16 +02:00
"import_files", path, in_place=True, username="me", interactive=False
2018-04-21 18:16:43 +02:00
)
2018-09-22 12:29:30 +00:00
@pytest.mark.skip("XXX : wip")
def test_import_with_multiple_argument(factories, mocker):
factories["users.User"](username="me")
path1 = os.path.join(DATA_DIR, "dummy_file.ogg")
path2 = os.path.join(DATA_DIR, "utf8-éà◌.ogg")
mocked_filter = mocker.patch(
"funkwhale_api.providers.audiofile.management.commands.import_files.Command.filter_matching",
return_value=({"new": [], "skipped": []}),
)
call_command("import_files", path1, path2, username="me", interactive=False)
mocked_filter.assert_called_once_with([path1, path2])
@pytest.mark.skip("Refactoring in progress")
2018-06-22 17:12:54 +02:00
def test_import_with_replace_flag(factories, mocker):
factories["users.User"](username="me")
path = os.path.join(DATA_DIR, "dummy_file.ogg")
mocked_job_run = mocker.patch("funkwhale_api.music.tasks.import_job_run")
call_command("import_files", path, username="me", replace=True, interactive=False)
created_job = ImportJob.objects.latest("id")
assert created_job.replace_if_duplicate is True
mocked_job_run.assert_called_once_with(
import_job_id=created_job.id, use_acoustid=False
)
@pytest.mark.skip("Refactoring in progress")
def test_import_files_creates_a_batch_and_job(factories, mocker):
2018-06-09 15:36:16 +02:00
m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
user = factories["users.User"](username="me")
path = os.path.join(DATA_DIR, "dummy_file.ogg")
call_command("import_files", path, username="me", async=False, interactive=False)
2018-06-09 15:36:16 +02:00
batch = user.imports.latest("id")
assert batch.source == "shell"
assert batch.jobs.count() == 1
job = batch.jobs.first()
2018-06-09 15:36:16 +02:00
assert job.status == "pending"
with open(path, "rb") as f:
assert job.audio_file.read() == f.read()
2018-06-09 15:36:16 +02:00
assert job.source == "file://" + path
m.assert_called_once_with(import_job_id=job.pk, use_acoustid=False)
2018-09-22 12:29:30 +00:00
@pytest.mark.skip("XXX : wip")
def test_import_files_skip_if_path_already_imported(factories, mocker):
2018-06-09 15:36:16 +02:00
user = factories["users.User"](username="me")
path = os.path.join(DATA_DIR, "dummy_file.ogg")
2018-09-22 12:29:30 +00:00
factories["music.Upload"](source="file://{}".format(path))
2018-06-09 15:36:16 +02:00
call_command("import_files", path, username="me", async=False, interactive=False)
assert user.imports.count() == 0
@pytest.mark.skip("Refactoring in progress")
def test_import_files_works_with_utf8_file_name(factories, mocker):
2018-06-09 15:36:16 +02:00
m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
user = factories["users.User"](username="me")
path = os.path.join(DATA_DIR, "utf8-éà◌.ogg")
call_command("import_files", path, username="me", async=False, interactive=False)
batch = user.imports.latest("id")
job = batch.jobs.first()
2018-06-09 15:36:16 +02:00
m.assert_called_once_with(import_job_id=job.pk, use_acoustid=False)
@pytest.mark.skip("Refactoring in progress")
2018-04-21 18:16:43 +02:00
def test_import_files_in_place(factories, mocker, settings):
settings.MUSIC_DIRECTORY_PATH = DATA_DIR
2018-06-09 15:36:16 +02:00
m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
user = factories["users.User"](username="me")
path = os.path.join(DATA_DIR, "utf8-éà◌.ogg")
2018-04-21 18:16:43 +02:00
call_command(
2018-06-09 15:36:16 +02:00
"import_files",
2018-04-21 18:16:43 +02:00
path,
2018-06-09 15:36:16 +02:00
username="me",
2018-04-21 18:16:43 +02:00
async=False,
in_place=True,
2018-06-09 15:36:16 +02:00
interactive=False,
)
batch = user.imports.latest("id")
2018-04-21 18:16:43 +02:00
job = batch.jobs.first()
assert bool(job.audio_file) is False
2018-06-09 15:36:16 +02:00
m.assert_called_once_with(import_job_id=job.pk, use_acoustid=False)
2018-04-21 18:16:43 +02:00
def test_storage_rename_utf_8_files(factories):
2018-09-22 12:29:30 +00:00
upload = factories["music.Upload"](audio_file__filename="été.ogg")
assert upload.audio_file.name.endswith("ete.ogg")