Skip to content

Commit 5aff97b

Browse files
committed
Skip tests if there no required credentials available
1 parent 9438918 commit 5aff97b

File tree

10 files changed

+65
-22
lines changed

10 files changed

+65
-22
lines changed

tests/test_activity.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
from datetime import datetime, timedelta, timezone
3+
import warnings
34

45

56

@@ -8,6 +9,9 @@ def test_activity():
89
import scratchattach as sa
910
from scratchattach.utils import exceptions
1011
import util
12+
if not util.credentials_available():
13+
warnings.warn("Skipped test_activity because there were no credentials available.")
14+
return
1115
sess = util.session()
1216

1317
# we cannot do assertions, but we can probe for any errors.

tests/test_auth.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import sys
22
import os
3+
import warnings
34

4-
5-
def test_import():
5+
def test_auth():
66
sys.path.insert(0, ".")
77
import util
8+
if not util.credentials_available():
9+
warnings.warn("Skipped test_auth because there were no credentials available.")
10+
return
811
sess = util.session()
912

1013
assert "FERNET_KEY" in os.environ

tests/test_comment.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import sys
22
from datetime import datetime, timedelta, timezone
3-
3+
import warnings
44

55
def test_comment():
66
sys.path.insert(0, ".")
77
import scratchattach as sa
88
import util
9+
if not util.credentials_available():
10+
warnings.warn("Skipped test_comment because there were no credentials available.")
11+
return
912
sess = util.session()
1013

1114
user = sess.connect_linked_user()

tests/test_editor_integration.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import pprint
22
import sys
3-
3+
import warnings
44

55
def test_project():
66
sys.path.insert(0, ".")
77
import scratchattach as sa
8-
from util import session
9-
sess = session()
8+
import util
9+
if not util.credentials_available():
10+
warnings.warn("Skipped test_project because there were no credentials available.")
11+
return
12+
sess = util.session()
1013

1114

1215
project = sess.connect_project(104)

tests/test_project.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import pprint
22
import sys
3-
3+
import warnings
44

55
def test_project():
66
sys.path.insert(0, ".")
77
import scratchattach as sa
8-
from util import session
8+
from util import session, credentials_available
9+
if not credentials_available():
10+
warnings.warn("Skipped test_project because there were no credentials available.")
11+
return
912
sess = session()
1013

1114

tests/test_search.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import sys
2-
2+
import warnings
33

44
def test_search():
55
sys.path.insert(0, ".")
66
import scratchattach as sa
7-
from util import session
7+
from util import session, credentials_available
8+
if not credentials_available():
9+
warnings.warn("Skipped test_search because there were no credentials available.")
10+
return
811
sess = session()
912

1013
print(sess.search_projects())

tests/test_studio.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import sys
22
from datetime import datetime, timezone
3+
import warnings
34

4-
def test_import():
5+
def test_studio():
56
sys.path.insert(0, ".")
67
import scratchattach as sa
7-
from util import session
8+
from util import session, credentials_available
9+
if not credentials_available():
10+
warnings.warn("Skipped test_studio because there were no credentials available.")
11+
return
812
sess = session()
913

1014
studio = sess.connect_studio(50809872)

tests/test_user.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import pprint
22
import sys
3-
3+
import warnings
44

55
def test_user():
66
sys.path.insert(0, ".")
77
import scratchattach as sa
8-
from util import session
8+
from util import session, credentials_available
9+
if not credentials_available():
10+
warnings.warn("Skipped test_studio because there were no credentials available.")
11+
return
912
sess = session()
1013

1114
user = sess.connect_user("faretek1")

tests/util/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@
44

55
from typing import Optional
66

7-
from .keyhandler import AUTH as __AUTH
7+
from .keyhandler import get_auth
88
from scratchattach import login, Session as _Session, LoginDataWarning
99

1010
warnings.filterwarnings('ignore', category=LoginDataWarning)
1111

1212
_session: Optional[_Session] = None
1313

14+
def credentials_available() -> bool:
15+
auth = get_auth()
16+
if not auth:
17+
return False
18+
return auth.get("auth") is not None
1419

1520
def session() -> _Session:
1621
global _session
1722

1823
if not _session:
19-
_session = login("ScratchAttachV2", __AUTH["auth"]["scratchattachv2"])
24+
auth = get_auth().get("auth")
25+
scratchattachv2 = None if auth is None else auth.get("scratchattachv2")
26+
if scratchattachv2 is None:
27+
raise RuntimeError("Not enough data for login.")
28+
_session = login("ScratchAttachV2", scratchattachv2)
2029

2130
return _session
2231

@@ -25,11 +34,11 @@ def teacher_session() -> Optional[_Session]:
2534
global _teacher_session
2635

2736
if not _teacher_session:
28-
if "teacher_auth" not in __AUTH:
37+
if "teacher_auth" not in get_auth():
2938
warnings.warn(f"Could not test for teacher session")
3039
return None
3140

32-
data = __AUTH["teacher_auth"]
41+
data = get_auth()["teacher_auth"]
3342
_teacher_session = login(data["username"], data["password"])
3443

3544
return _teacher_session

tests/util/keyhandler.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import tomllib
44
from pathlib import Path
5-
from typing import TypeVar
5+
from typing import Any, Optional, TypeVar
66

77
from cryptography.fernet import Fernet
88
from base64 import urlsafe_b64encode
@@ -59,8 +59,16 @@ def _decrypt_dict(data: dict) -> dict:
5959
_auth_fp = __fp__ / "auth.toml"
6060
_local_auth_fp = __fp__ / "local_auth.toml"
6161

62-
_auth = _decrypt_dict(tomllib.load(_auth_fp.open("rb")))
62+
_cached_auth: Optional[dict[str, Any]] = None
6363

64-
_local_auth = tomllib.load(_local_auth_fp.open("rb")) if _local_auth_fp.exists() else {}
64+
def get_auth() -> dict[str, Any]:
65+
try:
66+
_auth = _decrypt_dict(tomllib.load(_auth_fp.open("rb")))
67+
except Exception:
68+
_auth = {}
6569

66-
AUTH = _auth | _local_auth
70+
_local_auth = tomllib.load(_local_auth_fp.open("rb")) if _local_auth_fp.exists() else {}
71+
72+
_cached_auth = _auth | _local_auth
73+
74+
return _cached_auth

0 commit comments

Comments
 (0)