Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions wp1/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import http.cookiejar
import logging
import os
from http.cookiejar import MozillaCookieJar
import tempfile

import mwclient
import requests
Expand Down Expand Up @@ -33,11 +35,15 @@ def login():
logger.warning("Not creating API site object, no credentials")
return False

cookie_path = "/tmp/cookies.txt"
cookie_path = os.path.join(tempfile.gettempdir(), "cookies.txt")
cookie_jar = MozillaCookieJar(cookie_path)
if os.path.exists(cookie_path):
# Load cookies from file, including session cookies (expirydate=0)
cookie_jar.load(ignore_discard=True, ignore_expires=True)
try:
cookie_jar.load(ignore_discard=True, ignore_expires=True)
except http.cookiejar.LoadError:
logger.warning("Cookie jar at %s is corrupted, deleting", cookie_path)
os.remove(cookie_path)
logger.info("Loaded %d cookies", len(cookie_jar))

connection = requests.Session()
Expand Down
57 changes: 54 additions & 3 deletions wp1/api_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import http.cookiejar
import tempfile
import os
import importlib
import unittest
from unittest.mock import MagicMock, patch
Expand All @@ -17,7 +20,7 @@ class ApiWithCredsTest(unittest.TestCase):
@patch("wp1.api.get_credentials", return_value=TEST_CREDS)
@patch("wp1.api.mwclient.Site")
@patch("wp1.api.site")
@patch("http.cookiejar.MozillaCookieJar")
@patch("wp1.api.MozillaCookieJar")
def test_login(
self, patched_cookies, patched_site, patched_mwsite, patched_credentials
):
Expand All @@ -29,7 +32,7 @@ def test_login(

@patch("wp1.api.get_credentials", return_value=TEST_CREDS)
@patch("wp1.api.mwclient.Site")
@patch("http.cookiejar.MozillaCookieJar")
@patch("wp1.api.MozillaCookieJar")
def test_login_already_logged_in(
self, patched_cookies, patched_mwsite, patched_credentials
):
Expand All @@ -42,7 +45,7 @@ def test_login_already_logged_in(
@patch("wp1.api.mwclient.Site")
@patch("wp1.api.site")
@patch("wp1.api.logger")
@patch("http.cookiejar.MozillaCookieJar")
@patch("wp1.api.MozillaCookieJar")
def test_login_exception(
self,
patched_cookies,
Expand All @@ -61,6 +64,54 @@ def test_login_exception(
self.assertFalse(actual)
self.assertEqual(1, patched_logger.exception.call_count)

@patch("wp1.api.get_credentials", return_value=TEST_CREDS)
@patch("wp1.api.mwclient.Site")
@patch("wp1.api.site")
@patch("wp1.api.os.remove")
@patch("wp1.api.os.path.exists", return_value=True)
@patch("wp1.api.MozillaCookieJar")
def test_login_corrupted_cookie_jar(
self,
patched_cookies,
patched_exists,
patched_remove,
patched_site,
patched_mwsite,
patched_credentials,
):
patched_site.logged_in = False
cookie_jar = patched_cookies.return_value
cookie_jar.load.side_effect = http.cookiejar.LoadError("corrupted cookie file")
site = patched_mwsite()
site.logged_in = False
wp1.api.login()
patched_remove.assert_called_once_with(
os.path.join(tempfile.gettempdir(), "cookies.txt")
)

@patch("wp1.api.get_credentials", return_value=TEST_CREDS)
@patch("wp1.api.mwclient.Site")
@patch("wp1.api.site")
@patch("wp1.api.os.remove")
@patch("wp1.api.os.path.exists", return_value=True)
@patch("wp1.api.MozillaCookieJar")
def test_login_corrupted_cookie_jar_still_logs_in(
self,
patched_cookies,
patched_exists,
patched_remove,
patched_site,
patched_mwsite,
patched_credentials,
):
patched_site.logged_in = False
cookie_jar = patched_cookies.return_value
cookie_jar.load.side_effect = http.cookiejar.LoadError("corrupted cookie file")
site = patched_mwsite()
site.logged_in = False
wp1.api.login()
self.assertEqual(1, site.login.call_count)


class ApiTest(unittest.TestCase):
Comment thread
arnchlmcodes marked this conversation as resolved.

Expand Down
Loading