forked from tamland/python-tidal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
170 lines (137 loc) · 4.96 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019-2022 morguldir
# Copyright (C) 2014 Thomas Amland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import logging
from abc import ABC
from contextlib import suppress
from json import dumps, loads
from os import getenv
from pathlib import Path
from typing import List, Optional
import keyring
import keyring.backends
import keyring.errors
import pytest
import tidalapi
@pytest.fixture(scope="session")
def session(request):
logging.basicConfig(level=logging.DEBUG)
return login(request)
class Credentials(ABC):
def load(self, key: str) -> Optional[dict]:
"""Load secret."""
def save(self, key: str, val: dict) -> None:
"""Save secret."""
class EnvCredentials(Credentials):
def load(self, key: str) -> Optional[dict]:
credentials = {
"token_type": getenv("TIDAL_TOKEN_TYPE", "Bearer"),
"access_token": getenv("TIDAL_ACCESS_TOKEN"),
"refresh_token": getenv("TIDAL_REFRESH_TOKEN"),
}
return (
credentials
if credentials["access_token"] and credentials["refresh_token"]
else None
)
def save(self, key: str, val: dict) -> None:
print("Credentials are:")
print(val)
class KeyringCredentials(Credentials):
def load(self, key: str) -> Optional[dict]:
with suppress(Exception):
credentials = keyring.get_password(key, key)
return loads(credentials)
return None
def save(self, key: str, val: dict) -> None:
keyring.set_password(key, key, dumps(val))
class CachedCredentials(Credentials):
def __init__(self):
base_dir = Path("~/.local/cache").expanduser()
if not base_dir.exists():
raise FileNotFoundError(f"No cache directory {base_dir}")
self.cache_dir = base_dir / "python-tidal"
self.cache_dir.mkdir(exist_ok=True)
def load(self, key: str) -> Optional[dict]:
cachef = self.cache_dir / f"{key}.json"
try:
return loads(cachef.read_text())
except Exception:
return None
def save(self, key: str, val: dict) -> None:
cachef = self.cache_dir / f"{key}.json"
cachef.write_text(dumps(val))
KEY = "python-tidal"
def get_credential_store() -> tuple[List[Credentials], Optional[dict]]:
stores = []
for store in (EnvCredentials, CachedCredentials, KeyringCredentials):
with suppress(Exception):
stores.append(store())
for store in stores:
data = store.load(KEY)
if data:
return [store], data
stores = [s for s in stores if not isinstance(s, EnvCredentials)]
return stores, None
def login(request):
stores, credentials = get_credential_store()
config = tidalapi.Config()
tidal_session = tidalapi.Session(config)
if credentials and tidal_session.load_oauth_session(**credentials):
return tidal_session
else:
credentials = _oauth_login(request, tidal_session)
if stores:
for store in stores:
with suppress(Exception):
store.save(
KEY,
{
"token_type": tidal_session.token_type,
"access_token": tidal_session.access_token,
"refresh_token": tidal_session.refresh_token,
},
)
break
return tidal_session
def _oauth_login(request, tidal_session):
login, future = tidal_session.login_oauth()
# https://github.com/pytest-dev/pytest/issues/2704
capmanager = request.config.pluginmanager.getplugin("capturemanager")
with capmanager.global_and_fixture_disabled():
print(
"Visit",
"https://" + login.verification_uri_complete,
"to log in, the link expires in",
login.expires_in,
"seconds",
)
future.result()
def pytest_collection_modifyitems(config, items):
if config.getoption("--interactive"):
return
for item in items:
if "interactive" in item.keywords:
item.add_marker(pytest.mark.skip(reason="Skipping interactive tests"))
def pytest_addoption(parser):
parser.addoption(
"--interactive",
action="store_true",
default=False,
help="Run tests that require user input",
)