Skip to content

Commit

Permalink
fix #501
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Jan 3, 2024
1 parent d4bea93 commit 105d68e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ repos:
rev: v1.8.0
hooks:
- id: mypy
args: [--install-types, --non-interactive]
21 changes: 15 additions & 6 deletions tests/auth/test_oauth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import os
import tempfile
import time
from pathlib import Path
from typing import Any, Dict
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion ytmusicapi/auth/oauth/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Token:

access_token: str
refresh_token: str
expires_at: int
expires_at: int = 0
expires_in: int = 0

@staticmethod
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 105d68e

Please sign in to comment.