2017-07-25 14:02:53 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2017 Mike Fährmann
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import tempfile
|
|
|
|
import http.cookiejar
|
|
|
|
from os.path import join
|
|
|
|
|
|
|
|
import gallery_dl.config as config
|
|
|
|
import gallery_dl.extractor as extractor
|
|
|
|
|
|
|
|
CKEY = ("cookies",)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
.example.org\tTRUE\t/\tFALSE\t253402210800\tNAME\tVALUE
|
|
|
|
""")
|
|
|
|
|
|
|
|
@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):
|
|
|
|
config.set(CKEY, self.cookiefile)
|
|
|
|
|
|
|
|
cookies = extractor.find("test:").session.cookies
|
|
|
|
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):
|
|
|
|
self._test_warning(self.invalid_cookiefile, http.cookiejar.LoadError)
|
|
|
|
|
|
|
|
def test_invalid_filename(self):
|
|
|
|
self._test_warning(join(self.path.name, "nothing"), FileNotFoundError)
|
|
|
|
|
|
|
|
def _test_warning(self, filename, exc):
|
|
|
|
config.set(CKEY, filename)
|
|
|
|
log = logging.getLogger("test")
|
|
|
|
with mock.patch.object(log, "warning") as mock_warning:
|
|
|
|
cookies = extractor.find("test:").session.cookies
|
|
|
|
self.assertEqual(len(cookies), 0)
|
2017-07-26 10:33:15 +02:00
|
|
|
self.assertEqual(mock_warning.call_count, 1)
|
2017-07-25 14:02:53 +02:00
|
|
|
self.assertEqual(mock_warning.call_args[0][0], "cookies: %s")
|
|
|
|
self.assertIsInstance(mock_warning.call_args[0][1], exc)
|
|
|
|
|
|
|
|
|
|
|
|
class TestCookiedict(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.cdict = {"NAME1": "VALUE1", "NAME2": "VALUE2"}
|
|
|
|
config.set(CKEY, self.cdict)
|
|
|
|
|
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):
|
|
|
|
cookies = extractor.find("test:").session.cookies
|
|
|
|
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):
|
2018-01-23 15:45:30 +01:00
|
|
|
for category in ["exhentai", "nijie", "sankaku", "seiga"]:
|
2017-07-25 14:02:53 +02:00
|
|
|
extr = _get_extractor(category)
|
|
|
|
cookies = extr.session.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:
|
|
|
|
self.assertEqual(c.domain, extr.cookiedomain)
|
|
|
|
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
"exhentai": ("ipb_member_id", "ipb_pass_hash"),
|
2019-05-13 11:48:20 +02:00
|
|
|
"nijie" : ("nemail", "nlogin"),
|
|
|
|
"sankaku" : ("login", "pass_hash"),
|
|
|
|
"seiga" : ("user_session",),
|
2017-07-25 14:02:53 +02:00
|
|
|
}
|
|
|
|
for category, cookienames in extr_cookies.items():
|
|
|
|
cookies = {name: "value" for name in cookienames}
|
|
|
|
config.set(CKEY, cookies)
|
|
|
|
extr = _get_extractor(category)
|
|
|
|
with mock.patch.object(extr, "_login_impl") as mock_login:
|
|
|
|
extr.login()
|
|
|
|
mock_login.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
def _get_extractor(category):
|
2019-05-26 20:22:04 +02:00
|
|
|
for extr in extractor.extractors():
|
|
|
|
if extr.category == category and hasattr(extr, "_login_impl"):
|
|
|
|
url = next(extr._get_tests())[0]
|
|
|
|
return extr.from_url(url)
|
2017-07-25 14:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|