Skip to content
Snippets Groups Projects
test_utils.py 1.54 KiB
Newer Older
  • Learn to ignore specific revisions
  • from funkwhale_api.music import utils
    
    
    DATA_DIR = os.path.dirname(os.path.abspath(__file__))
    
    
    
    def test_guess_mimetype_try_using_extension(factories, mocker):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        mocker.patch("magic.from_buffer", return_value="audio/mpeg")
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        f = factories["music.Upload"].build(audio_file__filename="test.ogg")
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        assert utils.guess_mimetype(f.audio_file) == "audio/mpeg"
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    @pytest.mark.parametrize("wrong", ["application/octet-stream", "application/x-empty"])
    
    def test_guess_mimetype_try_using_extension_if_fail(wrong, factories, mocker):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        mocker.patch("magic.from_buffer", return_value=wrong)
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        f = factories["music.Upload"].build(audio_file__filename="test.mp3")
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        assert utils.guess_mimetype(f.audio_file) == "audio/mpeg"
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    @pytest.mark.parametrize(
        "name, expected",
        [
            ("sample.flac", {"bitrate": 1608000, "length": 0.001}),
            ("test.mp3", {"bitrate": 8000, "length": 267.70285714285717}),
    
            ("test.ogg", {"bitrate": 112000, "length": 1}),
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        ],
    )
    
    def test_get_audio_file_data(name, expected):
        path = os.path.join(DATA_DIR, name)
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        with open(path, "rb") as f:
    
            result = utils.get_audio_file_data(f)
    
        assert result == expected
    
    
    
    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"