ytmusicapi/tests/conftest.py
fischcode fe95f5974e
Add optional parameters for OAuth client ID and secret to setup (#688)
* Add optional parameters for OAuth client ID and secret to setup

Use your own Google API client ID and client secret in setup by calling
ytmusicapi oauth --client_id <your Google API client ID> --client_secret <your Google API client secret> to avoid HTTP 400: Bad Request errors

* Improve setup parameter parsing and help texts

- Add subparsers for parameters for oauth and browser tokens
- Add help options
- Add a check for presence of both client_id and client_secret if oauth is chosen.

* ruff autofix

* wip: remove default oauth_client_id & secret, turn into mandatory params

* fix yt_oauth fixture

* fix format/lint

* fix cli args and oauth test

* remove skip

---------

Co-authored-by: sigma67 <sigma67.github@gmail.com>
2024-12-17 13:15:33 +01:00

74 lines
1.9 KiB
Python

import configparser
from pathlib import Path
import pytest
from ytmusicapi import YTMusic
from ytmusicapi.auth.oauth import OAuthCredentials
def get_resource(file: str) -> str:
data_dir = Path(__file__).parent
return data_dir.joinpath(file).as_posix()
def get_config() -> configparser.RawConfigParser:
config = configparser.RawConfigParser()
config.read(get_resource("test.cfg"), "utf-8")
return config
@pytest.fixture(name="config")
def fixture_config() -> configparser.RawConfigParser:
return get_config()
@pytest.fixture(name="sample_album")
def fixture_sample_album() -> str:
"""Eminem - Revival"""
return "MPREb_4pL8gzRtw1p"
@pytest.fixture(name="sample_video")
def fixture_sample_video() -> str:
"""Oasis - Wonderwall"""
return "hpSrLjc5SMs"
@pytest.fixture(name="browser_filepath")
def fixture_browser_filepath(config) -> str:
return get_resource(config["auth"]["browser_file"])
@pytest.fixture(name="oauth_filepath")
def fixture_oauth_filepath(config) -> str:
return get_resource(config["auth"]["oauth_file"])
@pytest.fixture(name="yt")
def fixture_yt() -> YTMusic:
return YTMusic()
@pytest.fixture(name="yt_auth")
def fixture_yt_auth(browser_filepath) -> YTMusic:
"""a non-brand account that is able to create uploads"""
return YTMusic(browser_filepath, location="GB")
@pytest.fixture(name="yt_oauth")
def fixture_yt_oauth(oauth_filepath, config) -> YTMusic:
credentials = OAuthCredentials(
client_id=config["auth"]["client_id"], client_secret=config["auth"]["client_secret"]
)
return YTMusic(oauth_filepath, oauth_credentials=credentials)
@pytest.fixture(name="yt_brand")
def fixture_yt_brand(config) -> YTMusic:
return YTMusic(config["auth"]["headers"], config["auth"]["brand_account"])
@pytest.fixture(name="yt_empty")
def fixture_yt_empty(config) -> YTMusic:
return YTMusic(config["auth"]["headers_empty"], config["auth"]["brand_account_empty"])