diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index b11da693..6d9a19c2 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -4,9 +4,9 @@ on: push: branches: - master -# pull_request: -# branches: -# - master + pull_request: + branches: + - master jobs: build: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a9699913..597e0363 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,4 +11,3 @@ repos: rev: v1.8.0 hooks: - id: mypy - args: [--install-types, --non-interactive] diff --git a/tests/auth/test_oauth.py b/tests/auth/test_oauth.py index d2309283..ea12e546 100644 --- a/tests/auth/test_oauth.py +++ b/tests/auth/test_oauth.py @@ -1,4 +1,6 @@ import json +import os +import tempfile import time from pathlib import Path from typing import Any, Dict @@ -7,6 +9,7 @@ import pytest from requests import Response +from ytmusicapi.auth.oauth import OAuthToken from ytmusicapi.auth.types import AuthType from ytmusicapi.constants import OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET from ytmusicapi.setup import main @@ -37,21 +40,27 @@ def fixture_yt_alt_oauth(browser_filepath: str, alt_oauth_credentials: OAuthCred class TestOAuth: @mock.patch("requests.Response.json") @mock.patch("requests.Session.post") - def test_setup_oauth(self, session_mock, json_mock, oauth_filepath, blank_code, yt_oauth): + def test_setup_oauth(self, session_mock, json_mock, blank_code, config): session_mock.return_value = Response() - fresh_token = yt_oauth._token.as_dict() - json_mock.side_effect = [blank_code, fresh_token] + token_code = json.loads(config["auth"]["oauth_token"]) + json_mock.side_effect = [blank_code, token_code] + oauth_file = tempfile.NamedTemporaryFile(delete=False) + oauth_filepath = oauth_file.name with mock.patch("builtins.input", return_value="y"), mock.patch( "sys.argv", ["ytmusicapi", "oauth", "--file", oauth_filepath] - ): + ), mock.patch("webbrowser.open"): main() assert Path(oauth_filepath).exists() json_mock.side_effect = None with open(oauth_filepath, mode="r", encoding="utf8") as oauth_file: - string_oauth_token = oauth_file.read() + oauth_token = json.loads(oauth_file.read()) - YTMusic(string_oauth_token) + assert oauth_token["expires_at"] != 0 + assert OAuthToken.is_oauth(oauth_token) + + oauth_file.close() + os.unlink(oauth_filepath) def test_oauth_tokens(self, oauth_filepath: str, yt_oauth: YTMusic): # ensure instance initialized token diff --git a/ytmusicapi/auth/oauth/token.py b/ytmusicapi/auth/oauth/token.py index 0df8cb82..a0071f66 100644 --- a/ytmusicapi/auth/oauth/token.py +++ b/ytmusicapi/auth/oauth/token.py @@ -20,7 +20,7 @@ class Token: access_token: str refresh_token: str - expires_at: int + expires_at: int = 0 expires_in: int = 0 @staticmethod @@ -128,6 +128,7 @@ def prompt_for_token( input(f"Go to {url}, finish the login flow and press Enter when done, Ctrl-C to abort") raw_token = credentials.token_from_code(code["device_code"]) ref_token = cls(credentials=credentials, **raw_token) + ref_token.update(ref_token.as_dict()) if to_file: ref_token.local_cache = to_file return ref_token