| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (c) 2008-2026 Andrew Ziem. |
| 3 |
# |
| 4 |
# This work is licensed under the terms of the GNU GPL, version 3 or |
| 5 |
# later. See the COPYING file in the top-level directory. |
| 6 |
|
| 7 |
""" |
| 8 |
Test case for module GuiCookie |
| 9 |
""" |
| 10 |
|
| 11 |
import json |
| 12 |
import os |
| 13 |
import types |
| 14 |
import unittest |
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from bleachbit.GtkShim import is_gtk_available |
| 18 |
|
| 19 |
from tests import common |
| 20 |
|
| 21 |
HAVE_GTK = is_gtk_available() |
| 22 |
if HAVE_GTK: |
| 23 |
from bleachbit.GuiCookie import CookieManagerPane |
| 24 |
|
| 25 |
|
| 26 |
@unittest.skipUnless(HAVE_GTK, 'requires GTK+ module and a display environment') |
| 27 |
class GuiCookieTestCase(common.BleachbitTestCase): |
| 28 |
"""Test case for CookieManagerPane""" |
| 29 |
|
| 30 |
def _fake_pane(self, keep_list_path, domains): |
| 31 |
"""A minimal stand-in for CookieManagerPane, avoiding a real Gtk widget.""" |
| 32 |
return types.SimpleNamespace( |
| 33 |
keep_list_path=keep_list_path, |
| 34 |
_iter_selected_domains=lambda: iter(domains)) |
| 35 |
|
| 36 |
def test_save_changes_refuses_symlink(self): |
| 37 |
"""save_changes() must not write the keep list through a symlink""" |
| 38 |
filename = self.write_file( |
| 39 |
'cookie_keep_list_target.json', text='["keepme.example"]') |
| 40 |
pane = self._fake_pane(filename, ['evil.example']) |
| 41 |
with mock.patch('bleachbit.FileUtilities.os.path.islink', |
| 42 |
side_effect=lambda p: p == filename): |
| 43 |
result = CookieManagerPane.save_changes(pane) |
| 44 |
self.assertFalse(result) |
| 45 |
with open(filename, encoding='utf-8') as f: |
| 46 |
self.assertEqual(json.load(f), ['keepme.example']) |
| 47 |
|
| 48 |
def test_save_changes_still_works(self): |
| 49 |
"""save_changes() still writes the sorted keep list normally""" |
| 50 |
filename = os.path.join(self.tempdir, 'cookie_keep_list.json') |
| 51 |
pane = self._fake_pane(filename, ['b.example', 'a.example']) |
| 52 |
result = CookieManagerPane.save_changes(pane) |
| 53 |
self.assertTrue(result) |
| 54 |
with open(filename, encoding='utf-8') as f: |
| 55 |
self.assertEqual(json.load(f), ['a.example', 'b.example']) |
| 56 |
self.assertEqual(pane.saved_domains, {'a.example', 'b.example'}) |