refactor YTMusicBase, improve oauth doc (#727)

* refactor YTMusicBase, improve oauth doc

* fix recursion issue

* fix bugs, fix coverage, add session test
This commit is contained in:
sigma67 2025-01-19 14:37:31 +01:00 • committed by GitHub
commit a004bed436
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 135 additions and 99 deletions

View file

@ -10,6 +10,7 @@ from requests import Response
from ytmusicapi.auth.oauth import OAuthToken
from ytmusicapi.auth.types import AuthType
from ytmusicapi.exceptions import YTMusicUserError
from ytmusicapi.setup import main
from ytmusicapi.ytmusic import OAuthCredentials, YTMusic
@ -114,9 +115,19 @@ class TestOAuth:
assert yt_alt_oauth.auth_type != AuthType.OAUTH_CUSTOM_CLIENT
with open(oauth_filepath) as f:
token_dict = json.load(f)
# oauth token dict entry and alt
yt_alt_oauth = YTMusic(token_dict, oauth_credentials=alt_oauth_credentials)
assert yt_alt_oauth.auth_type == AuthType.OAUTH_CUSTOM_CLIENT
# forgot to pass OAuth credentials - should raise
with pytest.raises(YTMusicUserError):
YTMusic(token_dict)
# oauth custom full
token_dict["authorization"] = "Bearer DKLEK23"
yt_alt_oauth = YTMusic(token_dict, oauth_credentials=alt_oauth_credentials)
assert yt_alt_oauth.auth_type == AuthType.OAUTH_CUSTOM_FULL
def test_alt_oauth_request(self, yt_alt_oauth: YTMusic, sample_video):
yt_alt_oauth.get_watch_playlist(sample_video)

View file

@ -1,4 +1,7 @@
from functools import partial
import pytest
import requests
from ytmusicapi import YTMusic
from ytmusicapi.exceptions import YTMusicUserError
@ -12,3 +15,14 @@ def test_ytmusic_context():
def test_ytmusic_auth_error():
with pytest.raises(YTMusicUserError, match="Invalid auth"):
YTMusic(auth="def")
def test_ytmusic_session():
test_session = requests.Session()
test_session.request = partial(test_session.request, timeout=60)
ytmusic = YTMusic(requests_session=test_session)
assert ytmusic._session == test_session
ytmusic = YTMusic()
assert isinstance(ytmusic._session, requests.Session)
assert ytmusic._session != test_session