File: test_cookie_jar.py

package info (click to toggle)
pyicloud 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,012 kB
  • sloc: python: 14,028; sh: 29; makefile: 3
file content (105 lines) | stat: -rw-r--r-- 3,590 bytes parent folder | download
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
"""Tests for the PyiCloudCookieJar class and its handling of FMIP auth cookies."""

from io import StringIO
from unittest.mock import MagicMock, mock_open, patch

from pyicloud.cookie_jar import _FMIP_AUTH_COOKIE_NAME, PyiCloudCookieJar


def create_cookie_jar_with_cookie(
    filename, name, domain="example.com", path="/", value="test"
) -> PyiCloudCookieJar:
    """Create a PyiCloudCookieJar with a single cookie."""
    with (
        patch("builtins.open", new_callable=mock_open),
        patch("os.open", new_callable=mock_open),
    ):
        jar = PyiCloudCookieJar(filename=filename)
        jar.set(name, value, domain=domain, path=path)
        return jar


def test_load_no_filename() -> None:
    """Test that load is a no-op if no filename is set."""
    jar = PyiCloudCookieJar()
    # Should not raise or do anything if no filename is set
    # with patch("builtins.open", mock_open()):
    jar.load()  # No-op


def test_load_with_filename_removes_fmip_cookie() -> None:
    """Test that loading a jar with an FMIP cookie removes that cookie."""
    filename = "test_cookies.txt"
    buffer = StringIO()
    buffer.close = MagicMock()
    with (
        patch("builtins.open", new_callable=mock_open) as m,
        patch("os.open"),
        patch("os.fdopen") as os_fdopen,
    ):
        m.return_value = buffer
        os_fdopen.return_value = buffer

        jar: PyiCloudCookieJar = create_cookie_jar_with_cookie(
            filename, _FMIP_AUTH_COOKIE_NAME
        )
        # Add a non-FMIP cookie too
        jar.set("other_cookie", "value", domain="example.com", path="/")
        jar.save()
        # Reload and check FMIP cookie is removed
        jar2 = PyiCloudCookieJar(filename=filename)
        buffer.seek(0)
        jar2.load()
        names: list[str] = [cookie.name for cookie in jar2]
        assert _FMIP_AUTH_COOKIE_NAME not in names
        assert "other_cookie" in names


def test_load_with_custom_filename_argument_removes_fmip_cookie() -> None:
    """Test that loading a jar with an FMIP cookie removes that cookie."""
    filename = "test_cookies.txt"
    buffer = StringIO()
    buffer.close = MagicMock()
    with (
        patch("builtins.open", new_callable=mock_open) as m,
        patch("os.open"),
        patch("os.fdopen") as os_fdopen,
    ):
        m.return_value = buffer
        os_fdopen.return_value = buffer
        jar: PyiCloudCookieJar = create_cookie_jar_with_cookie(
            filename, _FMIP_AUTH_COOKIE_NAME
        )
        jar.save()

        jar2: PyiCloudCookieJar = PyiCloudCookieJar()
        names: list[str] = [cookie.name for cookie in jar2]
        assert _FMIP_AUTH_COOKIE_NAME not in names


def test_load_handles_keyerror_on_clear() -> None:
    """Test that load handles KeyError from clear gracefully."""
    filename = "test_cookies.txt"
    buffer = StringIO()
    buffer.close = MagicMock()
    with (
        patch("builtins.open", new_callable=mock_open) as m,
        patch("os.open"),
        patch("os.fdopen") as os_fdopen,
    ):
        m.return_value = buffer
        os_fdopen.return_value = buffer
        jar: PyiCloudCookieJar = create_cookie_jar_with_cookie(
            filename, _FMIP_AUTH_COOKIE_NAME
        )
        jar.save()
        buffer.seek(0)
        jar2: PyiCloudCookieJar = PyiCloudCookieJar(filename=filename)

        # Monkeypatch clear to raise KeyError
        def raise_keyerror(*args, **kwargs) -> None:
            raise KeyError

        with patch.object(jar2, "clear", side_effect=raise_keyerror):
            # Should not raise
            jar2.load()