Skip to content

Commit af29680

Browse files
committed
improved cookie handling logic
1 parent 27938c6 commit af29680

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

pwiki/wiki.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,21 @@ def _cookie_path(self, username: str = None) -> Path:
9292
Returns:
9393
Path: The file `Path` to save the cookies of this instance to. If no cookie_jar or username was set in the initializer, then return `None`.
9494
"""
95-
return (self.cookie_jar / f"{self.domain}_{username}.pickle") if self.cookie_jar and (username := username or self.username) else None
95+
if not (username := username or self.username):
96+
raise RuntimeError("No username specified or user is not logged in, not possible to determine a path to cookies")
9697

97-
def _load_cookies(self, username: str = None) -> bool:
98+
return self.cookie_jar / f"{self.domain}_{username}.pickle"
99+
100+
def _load_cookies(self, username: str) -> bool:
98101
"""Load saved cookies from a file into this pwiki instance.
99102
100103
Args:
101-
username (str, optional): The override username to use. If unset, then `self.username` will be used as the default. Defaults to None.
104+
username (str): The username to load cookies for.
102105
103106
Returns:
104-
bool: True if successful (confirmed with server that cookies are valid).
107+
bool: `True` if successful (confirmed with server that cookies are valid).
105108
"""
106-
if not (cookie_path := self._cookie_path(username)) or not cookie_path.is_file():
109+
if not (self.cookie_jar and (cookie_path := self._cookie_path(username)).is_file()):
107110
return False
108111

109112
with cookie_path.open('rb') as f:
@@ -125,29 +128,23 @@ def _load_cookies(self, username: str = None) -> bool:
125128

126129
def clear_cookies(self) -> None:
127130
"""Deletes any saved cookies from disk."""
131+
if not self.cookie_jar:
132+
return
133+
128134
(p := self._cookie_path()).unlink(True)
129135
log.info("%s: Removed cookies saved at '%s'", self, p)
130136

131137
def save_cookies(self) -> None:
132-
"""Write the cookies of the Wiki object to disk, so they can be used in the future.
133-
134-
Raises:
135-
ValueError: If a directory for cookies was not specified (i.e. set to `None`) when initializing this Wiki.
136-
"""
137-
if not (p := self._cookie_path()):
138-
log.warning("No cookie path is specified, unable to save cookies")
138+
"""Write the cookies of the Wiki object to disk, so they can be used in the future. Does nothing if `self.cookie_jar` is set to `None`. Raises `RuntimeError` if the `Wiki` is not logged in."""
139+
if not self.cookie_jar:
139140
return
140141

141-
if not self.is_logged_in:
142-
log.warning("%s: not logged in, no cookies to save", self)
143-
return
144-
145-
log.info("%s: Saving cookies to '%s'", self, p)
146-
147-
p.parent.mkdir(parents=True, exist_ok=True)
142+
(p := self._cookie_path()).parent.mkdir(parents=True, exist_ok=True)
148143
with p.open('wb') as f:
149144
pickle.dump(self.client.cookies, f)
150145

146+
log.info("%s: Saved cookies to '%s'", self, p)
147+
151148
##################################################################################################
152149
##################################### N A M E S P A C E S ########################################
153150
##################################################################################################

tests/test_wiki.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def test_save_load_cookies(self, fetch_token: mock.Mock, refresh_rights: mock.Mo
7777
wiki = new_wiki(username=u, password="hi", cookie_jar=tmp_dir)
7878
self.assertEqual("foobar", wiki.client.cookies.get("yolo"))
7979

80+
def test_no_auth_save_error(self):
81+
with self.assertRaises(RuntimeError):
82+
new_wiki().save_cookies()
83+
8084

8185
class TestWikiQuery(WikiTestCase):
8286
"""Tests wiki's query methods. These are basically smoke tests because the backing modules are more thoroughly tested."""

0 commit comments

Comments
 (0)