diff --git a/tests/mixins/test_browsing.py b/tests/mixins/test_browsing.py index f60f20fc..0c530a14 100644 --- a/tests/mixins/test_browsing.py +++ b/tests/mixins/test_browsing.py @@ -4,7 +4,8 @@ from unittest import mock import pytest -from test_helpers import is_ci + +from tests.test_helpers import is_ci class TestBrowsing: diff --git a/tests/mixins/test_uploads.py b/tests/mixins/test_uploads.py index a3856564..9e1d6ca5 100644 --- a/tests/mixins/test_uploads.py +++ b/tests/mixins/test_uploads.py @@ -1,10 +1,12 @@ import tempfile import time +from unittest import mock import pytest from tests.conftest import get_resource from ytmusicapi.enums import ResponseStatus +from ytmusicapi.exceptions import YTMusicUserError from ytmusicapi.ytmusic import YTMusic @@ -51,6 +53,12 @@ def test_upload_song(self, config, yt_auth): response = yt_auth.upload_song(get_resource(config["uploads"]["file"])) assert response.status_code == 409 + with ( + mock.patch("os.path.getsize", return_value=4 * 10**9), + pytest.raises(YTMusicUserError, match="larger than the limit of 300MB"), + ): + yt_auth.upload_song(get_resource(config["uploads"]["file"])) + def test_upload_song_and_verify(self, config, yt_auth: YTMusic): """Upload a song and verify it can be retrieved after it finishes processing.""" upload_response = yt_auth.upload_song(get_resource(config["uploads"]["file"])) diff --git a/ytmusicapi/mixins/uploads.py b/ytmusicapi/mixins/uploads.py index 7bb91f3b..2ccdbfd5 100644 --- a/ytmusicapi/mixins/uploads.py +++ b/ytmusicapi/mixins/uploads.py @@ -225,6 +225,10 @@ def upload_song(self, filepath: str) -> Union[ResponseStatus, requests.Response] headers = self.headers.copy() upload_url = f"https://upload.youtube.com/upload/usermusic/http?authuser={headers['x-goog-authuser']}" filesize = os.path.getsize(filepath) + if filesize >= 314572800: # 300MB in bytes + msg = f"File {filepath} has size {filesize} bytes, which is larger than the limit of 300MB" + raise YTMusicUserError(msg) + body = ("filename=" + ntpath.basename(filepath)).encode("utf-8") headers.pop("content-encoding", None) headers["content-type"] = "application/x-www-form-urlencoded;charset=utf-8"