2018-05-15 19:59:29 +00:00
|
|
|
import os
|
2018-06-10 08:55:16 +00:00
|
|
|
|
2018-05-15 19:59:29 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-02-26 17:27:41 +00:00
|
|
|
from funkwhale_api.music import utils
|
|
|
|
|
2018-05-15 19:59:29 +00:00
|
|
|
DATA_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2018-02-26 17:27:41 +00:00
|
|
|
|
|
|
|
def test_guess_mimetype_try_using_extension(factories, mocker):
|
2018-06-09 13:36:16 +00:00
|
|
|
mocker.patch("magic.from_buffer", return_value="audio/mpeg")
|
2018-09-22 12:29:30 +00:00
|
|
|
f = factories["music.Upload"].build(audio_file__filename="test.ogg")
|
2018-02-26 17:27:41 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
assert utils.guess_mimetype(f.audio_file) == "audio/mpeg"
|
2018-02-26 17:27:41 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@pytest.mark.parametrize("wrong", ["application/octet-stream", "application/x-empty"])
|
2018-06-03 16:07:59 +00:00
|
|
|
def test_guess_mimetype_try_using_extension_if_fail(wrong, factories, mocker):
|
2018-06-09 13:36:16 +00:00
|
|
|
mocker.patch("magic.from_buffer", return_value=wrong)
|
2018-09-22 12:29:30 +00:00
|
|
|
f = factories["music.Upload"].build(audio_file__filename="test.mp3")
|
2018-02-26 17:27:41 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
assert utils.guess_mimetype(f.audio_file) == "audio/mpeg"
|
2018-05-15 19:59:29 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"name, expected",
|
|
|
|
[
|
|
|
|
("sample.flac", {"bitrate": 1608000, "length": 0.001}),
|
|
|
|
("test.mp3", {"bitrate": 8000, "length": 267.70285714285717}),
|
2019-01-04 11:23:45 +00:00
|
|
|
("test.ogg", {"bitrate": 112000, "length": 1}),
|
2018-06-09 13:36:16 +00:00
|
|
|
],
|
|
|
|
)
|
2018-05-15 19:59:29 +00:00
|
|
|
def test_get_audio_file_data(name, expected):
|
|
|
|
path = os.path.join(DATA_DIR, name)
|
2018-06-09 13:36:16 +00:00
|
|
|
with open(path, "rb") as f:
|
2018-05-15 19:59:29 +00:00
|
|
|
result = utils.get_audio_file_data(f)
|
|
|
|
|
|
|
|
assert result == expected
|
2019-06-13 09:05:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_guess_mimetype_dont_crash_with_s3(factories, mocker, settings):
|
|
|
|
"""See #857"""
|
|
|
|
settings.DEFAULT_FILE_STORAGE = "funkwhale_api.common.storage.ASCIIS3Boto3Storage"
|
|
|
|
mocker.patch("magic.from_buffer", return_value="none")
|
|
|
|
f = factories["music.Upload"].build(audio_file__filename="test.mp3")
|
|
|
|
|
|
|
|
assert utils.guess_mimetype(f.audio_file) == "audio/mpeg"
|