2017-12-27 19:29:26 +00:00
|
|
|
import pytest
|
2017-12-26 20:12:37 +00:00
|
|
|
import acoustid
|
2017-12-24 18:15:21 +00:00
|
|
|
import datetime
|
2017-12-26 20:12:37 +00:00
|
|
|
import os
|
2017-12-27 19:29:26 +00:00
|
|
|
from django.core.management import call_command
|
|
|
|
from django.core.management.base import CommandError
|
2017-12-24 18:15:21 +00:00
|
|
|
|
|
|
|
from funkwhale_api.providers.audiofile import tasks
|
2018-02-27 16:43:50 +00:00
|
|
|
from funkwhale_api.music import tasks as music_tasks
|
2017-12-24 18:15:21 +00:00
|
|
|
|
|
|
|
DATA_DIR = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'files'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-12-27 19:29:26 +00:00
|
|
|
def test_can_create_track_from_file_metadata(db, mocker):
|
2017-12-26 20:12:37 +00:00
|
|
|
mocker.patch('acoustid.match', side_effect=acoustid.WebServiceError('test'))
|
2017-12-24 18:15:21 +00:00
|
|
|
metadata = {
|
|
|
|
'artist': ['Test artist'],
|
|
|
|
'album': ['Test album'],
|
|
|
|
'title': ['Test track'],
|
|
|
|
'TRACKNUMBER': ['4'],
|
|
|
|
'date': ['2012-08-15'],
|
|
|
|
'musicbrainz_albumid': ['a766da8b-8336-47aa-a3ee-371cc41ccc75'],
|
|
|
|
'musicbrainz_trackid': ['bd21ac48-46d8-4e78-925f-d9cc2a294656'],
|
|
|
|
'musicbrainz_artistid': ['013c8e5b-d72a-4cd3-8dee-6c64d6125823'],
|
|
|
|
}
|
|
|
|
m1 = mocker.patch('mutagen.File', return_value=metadata)
|
|
|
|
m2 = mocker.patch(
|
|
|
|
'funkwhale_api.music.metadata.Metadata.get_file_type',
|
|
|
|
return_value='OggVorbis',
|
|
|
|
)
|
2017-12-27 19:29:26 +00:00
|
|
|
track = tasks.import_track_data_from_path(
|
|
|
|
os.path.join(DATA_DIR, 'dummy_file.ogg'))
|
2017-12-24 18:15:21 +00:00
|
|
|
|
|
|
|
assert track.title == metadata['title'][0]
|
|
|
|
assert track.mbid == metadata['musicbrainz_trackid'][0]
|
|
|
|
assert track.position == 4
|
|
|
|
assert track.album.title == metadata['album'][0]
|
|
|
|
assert track.album.mbid == metadata['musicbrainz_albumid'][0]
|
|
|
|
assert track.album.release_date == datetime.date(2012, 8, 15)
|
|
|
|
assert track.artist.name == metadata['artist'][0]
|
|
|
|
assert track.artist.mbid == metadata['musicbrainz_artistid'][0]
|
2017-12-27 19:29:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_management_command_requires_a_valid_username(factories, mocker):
|
|
|
|
path = os.path.join(DATA_DIR, 'dummy_file.ogg')
|
|
|
|
user = factories['users.User'](username='me')
|
|
|
|
mocker.patch('funkwhale_api.providers.audiofile.management.commands.import_files.Command.do_import') # NOQA
|
|
|
|
with pytest.raises(CommandError):
|
|
|
|
call_command('import_files', path, username='not_me', interactive=False)
|
|
|
|
call_command('import_files', path, username='me', interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_import_files_creates_a_batch_and_job(factories, mocker):
|
2018-03-06 19:27:28 +00:00
|
|
|
m = mocker.patch('funkwhale_api.common.utils.on_commit')
|
2017-12-27 19:29:26 +00:00
|
|
|
user = factories['users.User'](username='me')
|
|
|
|
path = os.path.join(DATA_DIR, 'dummy_file.ogg')
|
|
|
|
call_command(
|
|
|
|
'import_files',
|
|
|
|
path,
|
|
|
|
username='me',
|
|
|
|
async=True,
|
|
|
|
interactive=False)
|
|
|
|
|
|
|
|
batch = user.imports.latest('id')
|
|
|
|
assert batch.source == 'shell'
|
|
|
|
assert batch.jobs.count() == 1
|
|
|
|
|
|
|
|
job = batch.jobs.first()
|
|
|
|
|
|
|
|
assert job.status == 'pending'
|
|
|
|
with open(path, 'rb') as f:
|
|
|
|
assert job.audio_file.read() == f.read()
|
|
|
|
|
|
|
|
assert job.source == 'file://' + path
|
2018-02-27 16:43:50 +00:00
|
|
|
m.assert_called_once_with(
|
|
|
|
music_tasks.import_job_run.delay,
|
2018-03-06 19:27:28 +00:00
|
|
|
import_job_id=job.pk,
|
|
|
|
use_acoustid=True)
|
|
|
|
|
|
|
|
|
|
|
|
def test_import_files_skip_acoustid(factories, mocker):
|
|
|
|
m = mocker.patch('funkwhale_api.common.utils.on_commit')
|
|
|
|
user = factories['users.User'](username='me')
|
|
|
|
path = os.path.join(DATA_DIR, 'dummy_file.ogg')
|
|
|
|
call_command(
|
|
|
|
'import_files',
|
|
|
|
path,
|
|
|
|
username='me',
|
|
|
|
async=True,
|
|
|
|
no_acoustid=True,
|
|
|
|
interactive=False)
|
|
|
|
batch = user.imports.latest('id')
|
|
|
|
job = batch.jobs.first()
|
|
|
|
m.assert_called_once_with(
|
|
|
|
music_tasks.import_job_run.delay,
|
|
|
|
import_job_id=job.pk,
|
|
|
|
use_acoustid=False)
|
2018-03-25 13:40:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_import_files_works_with_utf8_file_name(factories, mocker):
|
|
|
|
m = mocker.patch('funkwhale_api.common.utils.on_commit')
|
|
|
|
user = factories['users.User'](username='me')
|
|
|
|
path = os.path.join(DATA_DIR, 'utf8-éà◌.ogg')
|
|
|
|
call_command(
|
|
|
|
'import_files',
|
|
|
|
path,
|
|
|
|
username='me',
|
|
|
|
async=True,
|
|
|
|
no_acoustid=True,
|
|
|
|
interactive=False)
|
|
|
|
batch = user.imports.latest('id')
|
|
|
|
job = batch.jobs.first()
|
|
|
|
m.assert_called_once_with(
|
|
|
|
music_tasks.import_job_run.delay,
|
|
|
|
import_job_id=job.pk,
|
|
|
|
use_acoustid=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_storage_rename_utf_8_files(factories):
|
|
|
|
tf = factories['music.TrackFile'](audio_file__filename='été.ogg')
|
|
|
|
assert tf.audio_file.name.endswith('ete.ogg')
|