Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #501 #502

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ on:
push:
branches:
- master
# pull_request:
# branches:
# - master
pull_request:
branches:
- master

jobs:
build:
Expand Down
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