File size: 4,416 Bytes
4021124 | 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 | from datetime import datetime
from aws_lambda_powertools.shared.cookies import Cookie, SameSite
def test_cookie_without_secure():
# GIVEN a cookie without secure
cookie = Cookie(name="powertools", value="test", path="/", secure=False)
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value
assert cookie.secure is False
assert str(cookie) == "powertools=test; Path=/"
def test_cookie_with_path():
# GIVEN a cookie with a path
cookie = Cookie(name="powertools", value="test", path="/")
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value
assert cookie.name == "powertools"
assert cookie.value == "test"
assert cookie.path == "/"
assert str(cookie) == "powertools=test; Path=/; Secure"
def test_cookie_with_domain():
# GIVEN a cookie with a domain
cookie = Cookie(name="powertools", value="test", path="/", domain="example.com")
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value
assert cookie.name == "powertools"
assert cookie.value == "test"
assert cookie.path == "/"
assert cookie.domain == "example.com"
assert str(cookie) == "powertools=test; Path=/; Domain=example.com; Secure"
def test_cookie_with_expires():
# GIVEN a cookie with a expires
time_to_expire = datetime(year=2022, month=12, day=31)
cookie = Cookie(name="powertools", value="test", path="/", expires=time_to_expire)
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value
assert cookie.name == "powertools"
assert cookie.value == "test"
assert cookie.path == "/"
assert cookie.expires == time_to_expire
assert str(cookie) == "powertools=test; Path=/; Expires=Sat, 31 Dec 2022 00:00:00 GMT; Secure"
def test_cookie_with_max_age_positive():
# GIVEN a cookie with a positive max age
cookie = Cookie(name="powertools", value="test", path="/", max_age=100)
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value
assert cookie.name == "powertools"
assert cookie.value == "test"
assert cookie.path == "/"
assert cookie.max_age == 100
assert str(cookie) == "powertools=test; Path=/; Max-Age=100; Secure"
def test_cookie_with_max_age_negative():
# GIVEN a cookie with a negative max age
cookie = Cookie(name="powertools", value="test", path="/", max_age=-100)
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value and Max-Age must be 0
assert cookie.name == "powertools"
assert cookie.value == "test"
assert cookie.path == "/"
assert str(cookie) == "powertools=test; Path=/; Max-Age=0; Secure"
def test_cookie_with_http_only():
# GIVEN a cookie with http_only
cookie = Cookie(name="powertools", value="test", path="/", http_only=True)
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value
assert cookie.name == "powertools"
assert cookie.value == "test"
assert cookie.path == "/"
assert cookie.http_only is True
assert str(cookie) == "powertools=test; Path=/; HttpOnly; Secure"
def test_cookie_with_same_site():
# GIVEN a cookie with same_site
cookie = Cookie(name="powertools", value="test", path="/", same_site=SameSite.STRICT_MODE)
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value
assert cookie.name == "powertools"
assert cookie.value == "test"
assert cookie.path == "/"
assert cookie.same_site == SameSite.STRICT_MODE
assert str(cookie) == "powertools=test; Path=/; Secure; SameSite=Strict"
def test_cookie_with_custom_attribute():
# GIVEN a cookie with custom_attributes
cookie = Cookie(name="powertools", value="test", path="/", custom_attributes=["extra1=value1", "extra2=value2"])
# WHEN getting the cookie's attributes
# THEN the path attribute should be set to the provided value
assert cookie.name == "powertools"
assert cookie.value == "test"
assert cookie.path == "/"
assert cookie.custom_attributes == ["extra1=value1", "extra2=value2"]
assert str(cookie) == "powertools=test; Path=/; Secure; extra1=value1; extra2=value2"
|