1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
import unittest
import time
import datetime
import freezegun
import requests.cookies
from tests.mock import Mock, ANY, call
from streamlink.plugin import Plugin
class TestPlugin(unittest.TestCase):
def _create_cookie_dict(self, name, value, expires):
return {'version': 0, 'name': name, 'value': value,
'port': None, 'domain': "test.se", 'path': "/", 'secure': False,
'expires': expires, 'discard': True, 'comment': None,
'comment_url': None, 'rest': {"HttpOnly": None}, 'rfc2109': False}
def _cookie_to_dict(self, cookie):
r = {}
for name in ("version", "name", "value", "port", "domain", "path",
"secure", "expires", "discard", "comment", "comment_url"):
r[name] = getattr(cookie, name, None)
r["rest"] = getattr(cookie, "rest", getattr(cookie, "_rest", None))
return r
def tearDown(self):
Plugin.session = None
Plugin.cache = None
Plugin.module = None
Plugin.logger = None
def test_cookie_store_save(self):
session = Mock()
session.http.cookies = [
requests.cookies.create_cookie("test-name", "test-value", domain="test.se")
]
Plugin.bind(session, 'tests.test_plugin')
Plugin.cache = Mock()
Plugin.cache.get_all.return_value = {}
plugin = Plugin("http://test.se")
plugin.save_cookies(default_expires=3600)
Plugin.cache.set.assert_called_with("__cookie:test-name:test.se:80:/",
self._create_cookie_dict("test-name", "test-value", None),
3600)
def test_cookie_store_save_expires(self):
with freezegun.freeze_time(datetime.datetime(2018, 1, 1)):
session = Mock()
session.http.cookies = [
requests.cookies.create_cookie("test-name", "test-value", domain="test.se", expires=time.time() + 3600,
rest={'HttpOnly': None})
]
Plugin.bind(session, 'tests.test_plugin')
Plugin.cache = Mock()
Plugin.cache.get_all.return_value = {}
plugin = Plugin("http://test.se")
plugin.save_cookies(default_expires=60)
Plugin.cache.set.assert_called_with("__cookie:test-name:test.se:80:/",
self._create_cookie_dict("test-name", "test-value", 1514768400),
3600)
def test_cookie_store_load(self):
session = Mock()
session.http.cookies = requests.cookies.RequestsCookieJar()
Plugin.bind(session, 'tests.test_plugin')
Plugin.cache = Mock()
Plugin.cache.get_all.return_value = {
"__cookie:test-name:test.se:80:/": self._create_cookie_dict("test-name", "test-value", None)
}
plugin = Plugin("http://test.se")
self.assertSequenceEqual(
list(map(self._cookie_to_dict, session.http.cookies)),
[self._cookie_to_dict(requests.cookies.create_cookie("test-name", "test-value", domain="test.se"))]
)
def test_cookie_store_clear(self):
session = Mock()
session.http.cookies = requests.cookies.RequestsCookieJar()
Plugin.bind(session, 'tests.test_plugin')
Plugin.cache = Mock()
Plugin.cache.get_all.return_value = {
"__cookie:test-name:test.se:80:/": self._create_cookie_dict("test-name", "test-value", None),
"__cookie:test-name2:test.se:80:/": self._create_cookie_dict("test-name2", "test-value2", None)
}
plugin = Plugin("http://test.se")
# non-empty cookiejar
self.assertTrue(len(session.http.cookies.get_dict()) > 0)
plugin.clear_cookies()
self.assertSequenceEqual(
Plugin.cache.set.mock_calls,
[call("__cookie:test-name:test.se:80:/", None, 0),
call("__cookie:test-name2:test.se:80:/", None, 0)])
self.assertSequenceEqual(session.http.cookies, [])
def test_cookie_store_clear_filter(self):
session = Mock()
session.http.cookies = requests.cookies.RequestsCookieJar()
Plugin.bind(session, 'tests.test_plugin')
Plugin.cache = Mock()
Plugin.cache.get_all.return_value = {
"__cookie:test-name:test.se:80:/": self._create_cookie_dict("test-name", "test-value", None),
"__cookie:test-name2:test.se:80:/": self._create_cookie_dict("test-name2", "test-value2", None)
}
plugin = Plugin("http://test.se")
# non-empty cookiejar
self.assertTrue(len(session.http.cookies.get_dict()) > 0)
plugin.clear_cookies(lambda c: c.name.endswith("2"))
self.assertSequenceEqual(
Plugin.cache.set.mock_calls,
[call("__cookie:test-name2:test.se:80:/", None, 0)])
self.assertSequenceEqual(
list(map(self._cookie_to_dict, session.http.cookies)),
[self._cookie_to_dict(requests.cookies.create_cookie("test-name", "test-value", domain="test.se"))]
)
def test_cookie_load_unbound(self):
plugin = Plugin("http://test.se")
self.assertRaises(RuntimeError, plugin.load_cookies)
def test_cookie_save_unbound(self):
plugin = Plugin("http://test.se")
self.assertRaises(RuntimeError, plugin.save_cookies)
def test_cookie_clear_unbound(self):
plugin = Plugin("http://test.se")
self.assertRaises(RuntimeError, plugin.clear_cookies)
|