Skip to content

Commit dcf8966

Browse files
committed
fix: make teacher test exit without teacher auth
Consider adding an 'official' sa teacher account :\
1 parent f8b4e6e commit dcf8966

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

scratchattach/site/activity.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import html
55
import warnings
66

7-
from typing import Optional
7+
from typing import Optional, Any
88

99
from bs4 import Tag
1010
from dataclasses import dataclass
@@ -20,7 +20,7 @@ class Activity(BaseSiteComponent):
2020
Represents a Scratch activity (message or other user page activity)
2121
"""
2222
_session: Optional[session.Session] = None
23-
raw = None
23+
raw: Any = None
2424

2525
id: Optional[int] = None
2626
actor_username: Optional[str] = None
@@ -51,8 +51,8 @@ class Activity(BaseSiteComponent):
5151
is_reshare: Optional[bool] = None
5252

5353
datetime_created: Optional[str] = None
54-
time = None
55-
type = None
54+
time: Any = None
55+
type: str = None
5656

5757
def __repr__(self):
5858
return f"Activity({repr(self.raw)})"

scratchattach/site/user.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,11 @@ def post_comment(self, content, *, parent_id="", commentee_id=""):
609609
Posts a comment on the user's profile. You can only use this function if this object was created using :meth:`scratchattach.session.Session.connect_user`
610610
611611
Args:
612-
content: Content of the comment that should be posted
612+
:param content: Content of the comment that should be posted
613613
614614
Keyword Arguments:
615-
parent_id: ID of the comment you want to reply to. If you don't want to mention a user, don't put the argument.
616-
commentee_id: ID of the user that will be mentioned in your comment and will receive a message about your comment. If you don't want to mention a user, don't put the argument.
615+
:param commentee_id: ID of the comment you want to reply to. If you don't want to mention a user, don't put the argument.
616+
:param parent_id: ID of the user that will be mentioned in your comment and will receive a message about your comment. If you don't want to mention a user, don't put the argument.
617617
618618
Returns:
619619
scratchattach.comment.Comment: An object representing the created comment.
@@ -636,8 +636,8 @@ def post_comment(self, content, *, parent_id="", commentee_id=""):
636636
else:
637637
raise exceptions.CommentPostFailure(r.text)
638638

639+
text = r.text
639640
try:
640-
text = r.text
641641
data = {
642642
'id': text.split('<div id="comments-')[1].split('" class="comment')[0],
643643
'author': {"username": text.split('" data-comment-user="')[1].split('"><img class')[0]},
@@ -648,18 +648,18 @@ def post_comment(self, content, *, parent_id="", commentee_id=""):
648648
_comment = comment.Comment(source=comment.CommentSource.USER_PROFILE, parent_id=None if parent_id=="" else parent_id, commentee_id=commentee_id, source_id=self.username, id=data["id"], _session = self._session, datetime = datetime.now())
649649
_comment._update_from_dict(data)
650650
return _comment
651-
except Exception:
651+
except Exception as e:
652652
if '{"error": "isFlood"}' in text:
653653
raise(exceptions.CommentPostFailure(
654-
"You are being rate-limited for running this operation too often. Implement a cooldown of about 10 seconds."))
654+
"You are being rate-limited for running this operation too often. Implement a cooldown of about 10 seconds.")) from e
655655
elif '<script id="error-data" type="application/json">' in text:
656656
raw_error_data = text.split('<script id="error-data" type="application/json">')[1].split('</script>')[0]
657657
error_data = json.loads(raw_error_data)
658658
expires = error_data['mute_status']['muteExpiresAt']
659659
expires = datetime.fromtimestamp(expires, timezone.utc)
660-
raise(exceptions.CommentPostFailure(f"You have been muted. Mute expires on {expires}"))
660+
raise(exceptions.CommentPostFailure(f"You have been muted. Mute expires on {expires}")) from e
661661
else:
662-
raise(exceptions.FetchError(f"Couldn't parse API response: {r.text!r}"))
662+
raise(exceptions.FetchError(f"Couldn't parse API response: {r.text!r}")) from e
663663

664664
def reply_comment(self, content, *, parent_id, commentee_id=""):
665665
"""
@@ -671,11 +671,11 @@ def reply_comment(self, content, *, parent_id, commentee_id=""):
671671
Therefore, parent_id should be the comment id of a top level comment.
672672
673673
Args:
674-
content: Content of the comment that should be posted
674+
:param content: Content of the comment that should be posted
675675
676676
Keyword Arguments:
677-
parent_id: ID of the comment you want to reply to
678-
commentee_id: ID of the user that will be mentioned in your comment and will receive a message about your comment. If you don't want to mention a user, don't put the argument.
677+
:param parent_id: ID of the comment you want to reply to
678+
:param commentee_id: ID of the user that will be mentioned in your comment and will receive a message about your comment. If you don't want to mention a user, don't put the argument.
679679
"""
680680
return self.post_comment(content, parent_id=parent_id, commentee_id=commentee_id)
681681

tests/test_teacher_activity.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,29 @@
22
from datetime import datetime, timedelta, timezone
33

44

5-
65
def test_teacher_activity():
76
sys.path.insert(0, ".")
87
import scratchattach as sa
98
from scratchattach.utils import exceptions
109
import util
11-
sess = util.teacher_session()
10+
11+
if not (sess := util.teacher_session()):
12+
return
1213

1314
# we cannot do assertions, but we can probe for any errors.
1415
cls = sess.mystuff_classes()[0]
1516

1617
messages = [cls.activity(page=page) for page in range(1, 3)]
1718
for page in messages:
1819
for msg in page:
19-
print(msg, end=' ')
20+
_ = str(msg)
2021

2122
try:
2223
target = msg.target()
2324
except exceptions.CommentNotFound:
2425
target = None
2526

26-
print(target)
27+
_ = target
2728

2829

2930
if __name__ == "__main__":

tests/util/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def session() -> _Session:
2121
return _session
2222

2323
_teacher_session: Optional[_Session] = None
24-
def teacher_session() -> _Session:
24+
def teacher_session() -> Optional[_Session]:
2525
global _teacher_session
2626

2727
if not _teacher_session:
2828
if "teacher_auth" not in __AUTH:
2929
warnings.warn(f"Could not test for teacher session")
30-
exit(0)
30+
return None
3131

3232
data = __AUTH["teacher_auth"]
3333
_teacher_session = login(data["username"], data["password"])

0 commit comments

Comments
 (0)