File: cookie_tests.py

package info (click to toggle)
python-selenium 4.24.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,404 kB
  • sloc: python: 14,901; javascript: 2,347; makefile: 124; sh: 52
file content (164 lines) | stat: -rw-r--r-- 5,117 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

import calendar
import random
import time

import pytest


@pytest.fixture
def cookie(webserver):
    cookie = {"name": "foo", "value": "bar", "domain": webserver.host, "path": "/", "secure": False}
    return cookie


@pytest.fixture
def same_site_cookie_strict(webserver):
    same_site_cookie_strict = {
        "name": "foo",
        "value": "bar",
        "path": "/",
        "domain": webserver.host,
        "sameSite": "Strict",
        "secure": False,
    }
    return same_site_cookie_strict


@pytest.fixture
def same_site_cookie_lax(webserver):
    same_site_cookie_lax = {
        "name": "foo",
        "value": "bar",
        "path": "/",
        "domain": webserver.host,
        "sameSite": "Lax",
        "secure": False,
    }
    return same_site_cookie_lax


@pytest.fixture
def same_site_cookie_none(webserver):
    same_site_cookie_none = {
        "name": "foo",
        "value": "bar",
        "path": "/",
        "domain": webserver.host,
        "sameSite": "None",
        "secure": True,
    }
    return same_site_cookie_none


@pytest.fixture(autouse=True)
def pages(request, driver, pages):
    pages.load("simpleTest.html")
    yield pages
    driver.delete_all_cookies()


def test_add_cookie(cookie, driver):
    driver.add_cookie(cookie)
    returned = driver.execute_script("return document.cookie")
    assert cookie["name"] in returned


@pytest.mark.xfail_firefox(reason="sameSite cookie attribute not implemented")
@pytest.mark.xfail_remote(reason="sameSite cookie attribute not implemented")
@pytest.mark.xfail_safari
def test_add_cookie_same_site_strict(same_site_cookie_strict, driver):
    driver.add_cookie(same_site_cookie_strict)
    returned = driver.get_cookie("foo")
    assert "sameSite" in returned and returned["sameSite"] == "Strict"


@pytest.mark.xfail_firefox(reason="sameSite cookie attribute not implemented")
@pytest.mark.xfail_remote(reason="sameSite cookie attribute not implemented")
@pytest.mark.xfail_safari
def test_add_cookie_same_site_lax(same_site_cookie_lax, driver):
    driver.add_cookie(same_site_cookie_lax)
    returned = driver.get_cookie("foo")
    assert "sameSite" in returned and returned["sameSite"] == "Lax"


@pytest.mark.xfail_firefox(reason="sameSite cookie attribute not implemented")
@pytest.mark.xfail_remote(reason="sameSite cookie attribute not implemented")
@pytest.mark.xfail_safari
def test_add_cookie_same_site_none(same_site_cookie_none, driver):
    driver.add_cookie(same_site_cookie_none)
    # Note that insecure sites (http:) can't set cookies with the Secure directive.
    # driver.get_cookie would return None


@pytest.mark.xfail_ie
@pytest.mark.xfail_safari
def test_adding_acookie_that_expired_in_the_past(cookie, driver):
    expired = cookie.copy()
    expired["expiry"] = calendar.timegm(time.gmtime()) - 1
    driver.add_cookie(expired)
    assert 0 == len(driver.get_cookies())


def test_delete_all_cookie(cookie, driver):
    driver.add_cookie(cookie)
    driver.delete_all_cookies()
    assert not driver.get_cookies()


def test_delete_cookie(cookie, driver):
    driver.add_cookie(cookie)
    driver.delete_cookie("foo")
    assert not driver.get_cookies()


def test_should_get_cookie_by_name(driver):
    key = f"key_{int(random.random() * 10000000)}"
    driver.execute_script("document.cookie = arguments[0] + '=set';", key)
    cookie = driver.get_cookie(key)
    assert "set" == cookie["value"]


def test_should_return_none_when_cookie_does_not_exist(driver):
    key = f"key_{int(random.random() * 10000000)}"
    cookie = driver.get_cookie(key)
    assert cookie is None


def test_get_all_cookies(cookie, driver, pages, webserver):
    cookies = driver.get_cookies()
    count = len(cookies)

    for i in range(2):
        cookie["name"] = f"key_{int(random.random() * 10000000)}"
        driver.add_cookie(cookie)

    pages.load("simpleTest.html")
    assert count + 2 == len(driver.get_cookies())


def test_should_not_delete_cookies_with_asimilar_name(cookie, driver, webserver):
    cookie2 = cookie.copy()
    cookie2["name"] = "{}x".format(cookie["name"])
    driver.add_cookie(cookie)
    driver.add_cookie(cookie2)
    driver.delete_cookie(cookie["name"])
    cookies = driver.get_cookies()
    assert cookie["name"] != cookies[0]["name"]
    assert cookie2["name"] == cookies[0]["name"]