* fix #501

* enable tests on PR
This commit is contained in:
sigma67 2024-01-03 21:15:00 +01:00 • committed by GitHub
commit 9506bfd6d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 11 deletions

View file

@ -4,9 +4,9 @@ on:
push: push:
branches: branches:
- master - master
# pull_request: pull_request:
# branches: branches:
# - master - master
jobs: jobs:
build: build:

View file

@ -11,4 +11,3 @@ repos:
rev: v1.8.0 rev: v1.8.0
hooks: hooks:
- id: mypy - id: mypy
args: [--install-types, --non-interactive]

View file

@ -1,4 +1,6 @@
import json import json
import os
import tempfile
import time import time
from pathlib import Path from pathlib import Path
from typing import Any, Dict from typing import Any, Dict
@ -7,6 +9,7 @@ from unittest import mock
import pytest import pytest
from requests import Response from requests import Response
from ytmusicapi.auth.oauth import OAuthToken
from ytmusicapi.auth.types import AuthType from ytmusicapi.auth.types import AuthType
from ytmusicapi.constants import OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET from ytmusicapi.constants import OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET
from ytmusicapi.setup import main from ytmusicapi.setup import main
@ -37,21 +40,27 @@ def fixture_yt_alt_oauth(browser_filepath: str, alt_oauth_credentials: OAuthCred
class TestOAuth: class TestOAuth:
@mock.patch("requests.Response.json") @mock.patch("requests.Response.json")
@mock.patch("requests.Session.post") @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() session_mock.return_value = Response()
fresh_token = yt_oauth._token.as_dict() token_code = json.loads(config["auth"]["oauth_token"])
json_mock.side_effect = [blank_code, fresh_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( with mock.patch("builtins.input", return_value="y"), mock.patch(
"sys.argv", ["ytmusicapi", "oauth", "--file", oauth_filepath] "sys.argv", ["ytmusicapi", "oauth", "--file", oauth_filepath]
): ), mock.patch("webbrowser.open"):
main() main()
assert Path(oauth_filepath).exists() assert Path(oauth_filepath).exists()
json_mock.side_effect = None json_mock.side_effect = None
with open(oauth_filepath, mode="r", encoding="utf8") as oauth_file: 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): def test_oauth_tokens(self, oauth_filepath: str, yt_oauth: YTMusic):
# ensure instance initialized token # ensure instance initialized token

View file

@ -20,7 +20,7 @@ class Token:
access_token: str access_token: str
refresh_token: str refresh_token: str
expires_at: int expires_at: int = 0
expires_in: int = 0 expires_in: int = 0
@staticmethod @staticmethod
@ -128,6 +128,7 @@ class RefreshingToken(OAuthToken):
input(f"Go to {url}, finish the login flow and press Enter when done, Ctrl-C to abort") 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"]) raw_token = credentials.token_from_code(code["device_code"])
ref_token = cls(credentials=credentials, **raw_token) ref_token = cls(credentials=credentials, **raw_token)
ref_token.update(ref_token.as_dict())
if to_file: if to_file:
ref_token.local_cache = to_file ref_token.local_cache = to_file
return ref_token return ref_token