2015-09-03 17:24:01 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2018-02-05 12:19:50 +01:00
|
|
|
# Copyright 2015-2018 Alexander Cogneau (acogneau) <alexander.cogneau@gmail.com>:
|
2015-09-03 17:24:01 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from PyQt5.QtNetwork import QNetworkCookie
|
|
|
|
from PyQt5.QtCore import QUrl
|
|
|
|
import pytest
|
|
|
|
|
2016-06-13 10:49:58 +02:00
|
|
|
from qutebrowser.browser.webkit import cookies
|
2017-09-19 11:07:11 +02:00
|
|
|
from qutebrowser.utils import usertypes
|
|
|
|
from qutebrowser.misc import lineparser, objects
|
2015-09-03 17:24:01 +02:00
|
|
|
|
2016-07-15 02:07:11 +02:00
|
|
|
pytestmark = pytest.mark.usefixtures('data_tmpdir')
|
|
|
|
|
2015-09-03 17:24:01 +02:00
|
|
|
|
2015-09-16 23:00:02 +02:00
|
|
|
COOKIE1 = b'foo1=bar; expires=Tue, 01-Jan-2036 08:00:01 GMT'
|
|
|
|
COOKIE2 = b'foo2=bar; expires=Tue, 01-Jan-2036 08:00:01 GMT'
|
|
|
|
SESSION_COOKIE = b'foo3=bar'
|
|
|
|
EXPIRED_COOKIE = b'foo4=bar; expires=Sat, 01-Jan-2000 08:00:01 GMT'
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
|
2015-09-04 11:44:59 +02:00
|
|
|
class LineparserSaveStub(lineparser.BaseLineParser):
|
2016-02-10 19:18:47 +01:00
|
|
|
|
|
|
|
"""A stub for LineParser's save().
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
Attributes:
|
2015-09-04 11:44:59 +02:00
|
|
|
data: The data before the write
|
2015-09-03 17:24:01 +02:00
|
|
|
saved: The .data before save()
|
|
|
|
"""
|
|
|
|
|
2015-09-12 22:11:14 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2015-09-04 11:44:59 +02:00
|
|
|
self.saved = []
|
|
|
|
self.data = []
|
|
|
|
|
2015-09-03 17:24:01 +02:00
|
|
|
def save(self):
|
|
|
|
self.saved = self.data
|
2015-09-04 11:44:59 +02:00
|
|
|
|
2016-06-08 09:43:54 +02:00
|
|
|
def clear(self):
|
|
|
|
pass
|
|
|
|
|
2015-09-04 11:44:59 +02:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.data)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.data[key]
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_set_cookies_accept(config_stub, qtbot, monkeypatch):
|
|
|
|
"""Test setCookiesFromUrl with cookies enabled."""
|
2017-09-19 11:07:11 +02:00
|
|
|
monkeypatch.setattr(objects, 'backend', usertypes.Backend.QtWebKit)
|
2017-07-03 18:42:41 +02:00
|
|
|
config_stub.val.content.cookies.accept = 'all'
|
|
|
|
|
2015-09-03 17:24:01 +02:00
|
|
|
ram_jar = cookies.RAMCookieJar()
|
|
|
|
cookie = QNetworkCookie(b'foo', b'bar')
|
|
|
|
url = QUrl('http://example.com/')
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(ram_jar.changed):
|
2015-09-03 17:24:01 +02:00
|
|
|
assert ram_jar.setCookiesFromUrl([cookie], url)
|
|
|
|
|
|
|
|
# assert the cookies are added correctly
|
|
|
|
all_cookies = ram_jar.cookiesForUrl(url)
|
|
|
|
assert len(all_cookies) == 1
|
|
|
|
saved_cookie = all_cookies[0]
|
2015-09-04 11:44:59 +02:00
|
|
|
expected = cookie.name(), cookie.value()
|
|
|
|
assert saved_cookie.name(), saved_cookie.value() == expected
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
|
2017-09-19 11:07:11 +02:00
|
|
|
def test_set_cookies_never_accept(qtbot, config_stub, monkeypatch):
|
2015-09-03 17:24:01 +02:00
|
|
|
"""Test setCookiesFromUrl when cookies are not accepted."""
|
2017-09-19 11:07:11 +02:00
|
|
|
monkeypatch.setattr(objects, 'backend', usertypes.Backend.QtWebKit)
|
2017-07-03 18:42:41 +02:00
|
|
|
config_stub.val.content.cookies.accept = 'never'
|
2015-09-03 17:24:01 +02:00
|
|
|
|
2017-07-03 18:42:41 +02:00
|
|
|
ram_jar = cookies.RAMCookieJar()
|
2015-09-12 22:11:14 +02:00
|
|
|
url = QUrl('http://example.com/')
|
2016-01-08 09:57:33 +01:00
|
|
|
|
|
|
|
with qtbot.assertNotEmitted(ram_jar.changed):
|
2017-07-03 18:42:41 +02:00
|
|
|
assert not ram_jar.setCookiesFromUrl('test', url)
|
2015-09-13 23:08:31 +02:00
|
|
|
assert not ram_jar.cookiesForUrl(url)
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_cookie_jar_init(config_stub, fake_save_manager):
|
|
|
|
"""Test the CookieJar constructor."""
|
2015-09-16 23:00:02 +02:00
|
|
|
line_parser_stub = [COOKIE1, COOKIE2]
|
2015-09-03 17:24:01 +02:00
|
|
|
jar = cookies.CookieJar(line_parser=line_parser_stub)
|
2016-06-09 13:02:10 +02:00
|
|
|
assert fake_save_manager.add_saveable.called
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
# Test that cookies are added to the jar
|
|
|
|
assert len(jar.allCookies()) == 2
|
2015-09-04 11:44:59 +02:00
|
|
|
raw_cookies = [c.toRawForm().data() for c in jar.allCookies()]
|
2015-09-16 23:00:02 +02:00
|
|
|
assert raw_cookies == [COOKIE1, COOKIE2]
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_purge_old_cookies(config_stub, fake_save_manager):
|
|
|
|
"""Test that expired cookies are deleted."""
|
2015-09-16 23:00:02 +02:00
|
|
|
line_parser_stub = [COOKIE1, COOKIE2, SESSION_COOKIE, EXPIRED_COOKIE]
|
2015-09-03 17:24:01 +02:00
|
|
|
jar = cookies.CookieJar(line_parser=line_parser_stub)
|
|
|
|
|
|
|
|
assert len(jar.allCookies()) == 4
|
2015-09-03 18:17:39 +02:00
|
|
|
|
2015-09-03 17:24:01 +02:00
|
|
|
jar.purge_old_cookies()
|
2015-09-03 18:17:39 +02:00
|
|
|
|
|
|
|
# Test that old cookies are gone
|
2015-09-12 22:11:14 +02:00
|
|
|
raw_cookies = [cookie.toRawForm().data() for cookie in jar.allCookies()]
|
2015-09-16 23:00:02 +02:00
|
|
|
assert raw_cookies == [COOKIE1, COOKIE2, SESSION_COOKIE]
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
|
2015-09-28 21:50:55 +02:00
|
|
|
def test_save(config_stub, fake_save_manager, monkeypatch, qapp):
|
2015-09-03 17:24:01 +02:00
|
|
|
"""Test that expired and session cookies are not saved."""
|
2016-04-27 18:30:54 +02:00
|
|
|
monkeypatch.setattr(lineparser, 'LineParser', LineparserSaveStub)
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
jar = cookies.CookieJar()
|
2015-09-16 23:00:02 +02:00
|
|
|
jar._lineparser.data = [COOKIE1, COOKIE2, SESSION_COOKIE, EXPIRED_COOKIE]
|
2015-09-03 17:24:01 +02:00
|
|
|
|
2015-09-03 18:17:39 +02:00
|
|
|
# Update the cookies on the jar itself
|
2015-09-03 17:24:01 +02:00
|
|
|
jar.parse_cookies()
|
|
|
|
jar.save()
|
2015-09-12 22:11:14 +02:00
|
|
|
saved_cookies = [cookie.data() for cookie in jar._lineparser.saved]
|
2015-09-16 23:00:02 +02:00
|
|
|
assert saved_cookies == [COOKIE1, COOKIE2]
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
|
2015-09-04 11:44:59 +02:00
|
|
|
def test_cookies_changed_emit(config_stub, fake_save_manager,
|
|
|
|
monkeypatch, qtbot):
|
|
|
|
"""Test that self.changed is emitted."""
|
2016-04-27 18:30:54 +02:00
|
|
|
monkeypatch.setattr(lineparser, 'LineParser', LineparserSaveStub)
|
2015-09-03 17:24:01 +02:00
|
|
|
jar = cookies.CookieJar()
|
|
|
|
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(jar.changed):
|
2017-07-03 18:42:41 +02:00
|
|
|
config_stub.val.content.cookies.store = False
|
2015-09-03 17:24:01 +02:00
|
|
|
|
2015-09-04 11:44:59 +02:00
|
|
|
|
2016-04-27 18:30:54 +02:00
|
|
|
@pytest.mark.parametrize('store_cookies,empty', [(True, False), (False, True)])
|
|
|
|
def test_cookies_changed(config_stub, fake_save_manager, monkeypatch, qtbot,
|
|
|
|
store_cookies, empty):
|
2015-09-04 11:44:59 +02:00
|
|
|
"""Test that cookies are saved correctly."""
|
2016-04-27 18:30:54 +02:00
|
|
|
monkeypatch.setattr(lineparser, 'LineParser', LineparserSaveStub)
|
2015-09-04 11:44:59 +02:00
|
|
|
jar = cookies.CookieJar()
|
2015-09-16 23:00:02 +02:00
|
|
|
jar._lineparser.data = [COOKIE1, COOKIE2]
|
2015-09-04 11:44:59 +02:00
|
|
|
jar.parse_cookies()
|
2017-07-03 18:42:41 +02:00
|
|
|
config_stub.val.content.cookies.store = store_cookies
|
2015-09-04 11:44:59 +02:00
|
|
|
|
|
|
|
if empty:
|
|
|
|
assert not jar._lineparser.data
|
|
|
|
assert not jar._lineparser.saved
|
|
|
|
else:
|
|
|
|
assert jar._lineparser.data
|