2017-07-25 14:02:53 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
# Copyright 2017-2023 Mike Fährmann
|
2017-07-25 14:02:53 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
2020-05-02 01:15:50 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2017-07-25 14:02:53 +02:00
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
2022-02-16 22:58:57 +01:00
|
|
|
import time
|
2017-07-25 14:02:53 +02:00
|
|
|
import logging
|
|
|
|
import tempfile
|
|
|
|
from os.path import join
|
|
|
|
|
2020-05-02 01:15:50 +02:00
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from gallery_dl import config, extractor # noqa E402
|
2017-07-25 14:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestCookiejar(unittest.TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.path = tempfile.TemporaryDirectory()
|
|
|
|
|
|
|
|
cls.cookiefile = join(cls.path.name, "cookies.txt")
|
|
|
|
with open(cls.cookiefile, "w") as file:
|
|
|
|
file.write("""# HTTP Cookie File
|
|
|
|
.example.org\tTRUE\t/\tFALSE\t253402210800\tNAME\tVALUE
|
|
|
|
""")
|
|
|
|
|
|
|
|
cls.invalid_cookiefile = join(cls.path.name, "invalid.txt")
|
|
|
|
with open(cls.invalid_cookiefile, "w") as file:
|
|
|
|
file.write("""# asd
|
2020-01-21 22:46:58 +01:00
|
|
|
.example.org\tTRUE/FALSE\t253402210800\tNAME\tVALUE
|
2017-07-25 14:02:53 +02:00
|
|
|
""")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.path.cleanup()
|
2017-07-25 14:59:41 +02:00
|
|
|
config.clear()
|
2017-07-25 14:02:53 +02:00
|
|
|
|
|
|
|
def test_cookiefile(self):
|
2019-11-23 23:50:16 +01:00
|
|
|
config.set((), "cookies", self.cookiefile)
|
2023-07-25 20:09:44 +02:00
|
|
|
cookies = _get_extractor("test").cookies
|
2017-07-25 14:02:53 +02:00
|
|
|
self.assertEqual(len(cookies), 1)
|
|
|
|
|
|
|
|
cookie = next(iter(cookies))
|
|
|
|
self.assertEqual(cookie.domain, ".example.org")
|
2019-05-13 11:48:20 +02:00
|
|
|
self.assertEqual(cookie.path , "/")
|
|
|
|
self.assertEqual(cookie.name , "NAME")
|
|
|
|
self.assertEqual(cookie.value , "VALUE")
|
2017-07-25 14:02:53 +02:00
|
|
|
|
|
|
|
def test_invalid_cookiefile(self):
|
2020-01-21 22:46:58 +01:00
|
|
|
self._test_warning(self.invalid_cookiefile, ValueError)
|
2017-07-25 14:02:53 +02:00
|
|
|
|
|
|
|
def test_invalid_filename(self):
|
|
|
|
self._test_warning(join(self.path.name, "nothing"), FileNotFoundError)
|
|
|
|
|
|
|
|
def _test_warning(self, filename, exc):
|
2019-11-23 23:50:16 +01:00
|
|
|
config.set((), "cookies", filename)
|
2024-02-27 01:37:57 +01:00
|
|
|
log = logging.getLogger("generic")
|
2023-07-25 20:09:44 +02:00
|
|
|
|
2017-07-25 14:02:53 +02:00
|
|
|
with mock.patch.object(log, "warning") as mock_warning:
|
2023-07-25 20:09:44 +02:00
|
|
|
cookies = _get_extractor("test").cookies
|
|
|
|
|
|
|
|
self.assertEqual(len(cookies), 0)
|
|
|
|
self.assertEqual(mock_warning.call_count, 1)
|
|
|
|
self.assertEqual(mock_warning.call_args[0][0], "cookies: %s")
|
|
|
|
self.assertIsInstance(mock_warning.call_args[0][1], exc)
|
2017-07-25 14:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestCookiedict(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.cdict = {"NAME1": "VALUE1", "NAME2": "VALUE2"}
|
2019-11-23 23:50:16 +01:00
|
|
|
config.set((), "cookies", self.cdict)
|
2017-07-25 14:02:53 +02:00
|
|
|
|
2017-07-25 14:59:41 +02:00
|
|
|
def tearDown(self):
|
|
|
|
config.clear()
|
|
|
|
|
2017-07-25 14:02:53 +02:00
|
|
|
def test_dict(self):
|
2023-07-25 20:09:44 +02:00
|
|
|
cookies = _get_extractor("test").cookies
|
|
|
|
|
2017-07-25 14:02:53 +02:00
|
|
|
self.assertEqual(len(cookies), len(self.cdict))
|
2019-05-14 22:32:40 +02:00
|
|
|
self.assertEqual(sorted(cookies.keys()), sorted(self.cdict.keys()))
|
2017-07-25 14:02:53 +02:00
|
|
|
self.assertEqual(sorted(cookies.values()), sorted(self.cdict.values()))
|
|
|
|
|
|
|
|
def test_domain(self):
|
2022-06-25 14:24:46 +02:00
|
|
|
for category in ["exhentai", "idolcomplex", "nijie", "horne"]:
|
2017-07-25 14:02:53 +02:00
|
|
|
extr = _get_extractor(category)
|
2023-07-21 22:38:39 +02:00
|
|
|
cookies = extr.cookies
|
2019-05-13 11:48:20 +02:00
|
|
|
for key in self.cdict:
|
2017-07-25 14:02:53 +02:00
|
|
|
self.assertTrue(key in cookies)
|
|
|
|
for c in cookies:
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertEqual(c.domain, extr.cookies_domain)
|
2017-07-25 14:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestCookieLogin(unittest.TestCase):
|
|
|
|
|
2017-07-25 14:59:41 +02:00
|
|
|
def tearDown(self):
|
|
|
|
config.clear()
|
|
|
|
|
2017-07-25 14:02:53 +02:00
|
|
|
def test_cookie_login(self):
|
|
|
|
extr_cookies = {
|
2020-12-08 21:05:47 +01:00
|
|
|
"exhentai" : ("ipb_member_id", "ipb_pass_hash"),
|
|
|
|
"idolcomplex": ("login", "pass_hash"),
|
2022-06-25 14:24:46 +02:00
|
|
|
"nijie" : ("nijie_tok",),
|
|
|
|
"horne" : ("horne_tok",),
|
2017-07-25 14:02:53 +02:00
|
|
|
}
|
|
|
|
for category, cookienames in extr_cookies.items():
|
|
|
|
cookies = {name: "value" for name in cookienames}
|
2019-11-23 23:50:16 +01:00
|
|
|
config.set((), "cookies", cookies)
|
2017-07-25 14:02:53 +02:00
|
|
|
extr = _get_extractor(category)
|
|
|
|
with mock.patch.object(extr, "_login_impl") as mock_login:
|
|
|
|
extr.login()
|
|
|
|
mock_login.assert_not_called()
|
|
|
|
|
|
|
|
|
2022-02-16 22:58:57 +01:00
|
|
|
class TestCookieUtils(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_check_cookies(self):
|
2023-07-25 20:09:44 +02:00
|
|
|
extr = _get_extractor("test")
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertFalse(extr.cookies, "empty")
|
|
|
|
self.assertFalse(extr.cookies_domain, "empty")
|
2022-02-16 22:58:57 +01:00
|
|
|
|
|
|
|
# always returns False when checking for empty cookie list
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertFalse(extr.cookies_check(()))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertFalse(extr.cookies_check(("a",)))
|
|
|
|
self.assertFalse(extr.cookies_check(("a", "b")))
|
|
|
|
self.assertFalse(extr.cookies_check(("a", "b", "c")))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("a", "1")
|
|
|
|
self.assertTrue(extr.cookies_check(("a",)))
|
|
|
|
self.assertFalse(extr.cookies_check(("a", "b")))
|
|
|
|
self.assertFalse(extr.cookies_check(("a", "b", "c")))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("b", "2")
|
|
|
|
self.assertTrue(extr.cookies_check(("a",)))
|
|
|
|
self.assertTrue(extr.cookies_check(("a", "b")))
|
|
|
|
self.assertFalse(extr.cookies_check(("a", "b", "c")))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
|
|
|
def test_check_cookies_domain(self):
|
2023-07-25 20:09:44 +02:00
|
|
|
extr = _get_extractor("test")
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertFalse(extr.cookies, "empty")
|
|
|
|
extr.cookies_domain = ".example.org"
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertFalse(extr.cookies_check(("a",)))
|
|
|
|
self.assertFalse(extr.cookies_check(("a", "b")))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("a", "1")
|
|
|
|
self.assertFalse(extr.cookies_check(("a",)))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("a", "1", domain=extr.cookies_domain)
|
|
|
|
self.assertTrue(extr.cookies_check(("a",)))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("a", "1", domain="www" + extr.cookies_domain)
|
|
|
|
self.assertEqual(len(extr.cookies), 3)
|
|
|
|
self.assertTrue(extr.cookies_check(("a",)))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("b", "2", domain=extr.cookies_domain)
|
|
|
|
extr.cookies.set("c", "3", domain=extr.cookies_domain)
|
|
|
|
self.assertTrue(extr.cookies_check(("a", "b", "c")))
|
2022-02-16 22:58:57 +01:00
|
|
|
|
|
|
|
def test_check_cookies_expires(self):
|
2023-07-25 20:09:44 +02:00
|
|
|
extr = _get_extractor("test")
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertFalse(extr.cookies, "empty")
|
|
|
|
self.assertFalse(extr.cookies_domain, "empty")
|
2022-02-16 22:58:57 +01:00
|
|
|
|
|
|
|
now = int(time.time())
|
2024-02-27 01:37:57 +01:00
|
|
|
log = logging.getLogger("generic")
|
2022-02-16 22:58:57 +01:00
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("a", "1", expires=now-100)
|
2022-02-16 22:58:57 +01:00
|
|
|
with mock.patch.object(log, "warning") as mw:
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertFalse(extr.cookies_check(("a",)))
|
2022-02-16 22:58:57 +01:00
|
|
|
self.assertEqual(mw.call_count, 1)
|
|
|
|
self.assertEqual(mw.call_args[0], ("Cookie '%s' has expired", "a"))
|
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("a", "1", expires=now+100)
|
2022-02-16 22:58:57 +01:00
|
|
|
with mock.patch.object(log, "warning") as mw:
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertTrue(extr.cookies_check(("a",)))
|
2022-02-16 22:58:57 +01:00
|
|
|
self.assertEqual(mw.call_count, 1)
|
|
|
|
self.assertEqual(mw.call_args[0], (
|
|
|
|
"Cookie '%s' will expire in less than %s hour%s", "a", 1, ""))
|
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("a", "1", expires=now+100+7200)
|
2022-02-16 22:58:57 +01:00
|
|
|
with mock.patch.object(log, "warning") as mw:
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertTrue(extr.cookies_check(("a",)))
|
2022-02-16 22:58:57 +01:00
|
|
|
self.assertEqual(mw.call_count, 1)
|
|
|
|
self.assertEqual(mw.call_args[0], (
|
|
|
|
"Cookie '%s' will expire in less than %s hour%s", "a", 3, "s"))
|
|
|
|
|
2023-07-21 22:38:39 +02:00
|
|
|
extr.cookies.set("a", "1", expires=now+100+24*3600)
|
2022-02-16 22:58:57 +01:00
|
|
|
with mock.patch.object(log, "warning") as mw:
|
2023-07-21 22:38:39 +02:00
|
|
|
self.assertTrue(extr.cookies_check(("a",)))
|
2022-02-16 22:58:57 +01:00
|
|
|
self.assertEqual(mw.call_count, 0)
|
|
|
|
|
|
|
|
|
2017-07-25 14:02:53 +02:00
|
|
|
def _get_extractor(category):
|
2023-07-25 20:09:44 +02:00
|
|
|
extr = extractor.find(URLS[category])
|
|
|
|
extr.initialize()
|
|
|
|
return extr
|
|
|
|
|
|
|
|
|
|
|
|
URLS = {
|
|
|
|
"exhentai" : "https://exhentai.org/g/1200119/d55c44d3d0/",
|
|
|
|
"idolcomplex": "https://idol.sankakucomplex.com/post/show/1",
|
|
|
|
"nijie" : "https://nijie.info/view.php?id=1",
|
|
|
|
"horne" : "https://horne.red/view.php?id=1",
|
2024-02-27 01:37:57 +01:00
|
|
|
"test" : "generic:https://example.org/",
|
2023-07-25 20:09:44 +02:00
|
|
|
}
|
2017-07-25 14:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|