python/6871/aiohttp/tests/test_cookiejar.py

test_cookiejar.py
import asyncio
import datetime
import os
import tempfile
import unittest
from http.cookies import SimpleCookie
from unittest import mock

import pytest
from yarl import URL

from aiohttp import CookieJar


@pytest.fixture
def cookies_to_send():
    return SimpleCookie(
        "shared-cookie=first; "
        "domain-cookie=second; Domain=example.com; "
        "subdomain1-cookie=third; Domain=test1.example.com; "
        "subdomain2-cookie=fourth; Domain=test2.example.com; "
        "dotted-domain-cookie=fifth; Domain=.example.com; "
        "different-domain-cookie=sixth; Domain=different.org; "
        "secure-cookie=seventh; Domain=secure.com; Secure; "
        "no-path-cookie=eighth; Domain=pathtest.com; "
        "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
        "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
        "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
        "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
        "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
        " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
        "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
        " Max-Age=60; "
        "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
        " Max-Age=string; "
        "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
        " Expires=string;"
    )


@pytest.fixture
def cookies_to_receive():
    return SimpleCookie(
        "unconstrained-cookie=first; Path=/; "
        "domain-cookie=second; Domain=example.com; Path=/; "
        "subdomain1-cookie=third; Domain=test1.example.com; Path=/; "
        "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; "
        "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; "
        "different-domain-cookie=sixth; Domain=different.org; Path=/; "
        "no-path-cookie=seventh; Domain=pathtest.com; "
        "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; "
        "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;"
    )


def test_date_parsing():
    parse_func = CookieJar._parse_date
    utc = datetime.timezone.utc

    astert parse_func("") is None

    # 70 -> 1970
    astert parse_func("Tue, 1 Jan 70 00:00:00 GMT") == \
        datetime.datetime(1970, 1, 1, tzinfo=utc)

    # 10 -> 2010
    astert parse_func("Tue, 1 Jan 10 00:00:00 GMT") == \
        datetime.datetime(2010, 1, 1, tzinfo=utc)

    # No day of week string
    astert parse_func("1 Jan 1970 00:00:00 GMT") == \
        datetime.datetime(1970, 1, 1, tzinfo=utc)

    # No timezone string
    astert parse_func("Tue, 1 Jan 1970 00:00:00") == \
        datetime.datetime(1970, 1, 1, tzinfo=utc)

    # No year
    astert parse_func("Tue, 1 Jan 00:00:00 GMT") is None

    # No month
    astert parse_func("Tue, 1 1970 00:00:00 GMT") is None

    # No day of month
    astert parse_func("Tue, Jan 1970 00:00:00 GMT") is None

    # No time
    astert parse_func("Tue, 1 Jan 1970 GMT") is None

    # Invalid day of month
    astert parse_func("Tue, 0 Jan 1970 00:00:00 GMT") is None

    # Invalid year
    astert parse_func("Tue, 1 Jan 1500 00:00:00 GMT") is None

    # Invalid time
    astert parse_func("Tue, 1 Jan 1970 77:88:99 GMT") is None


def test_domain_matching():
    test_func = CookieJar._is_domain_match

    astert test_func("test.com", "test.com")
    astert test_func("test.com", "sub.test.com")

    astert not test_func("test.com", "")
    astert not test_func("test.com", "test.org")
    astert not test_func("diff-test.com", "test.com")
    astert not test_func("test.com", "diff-test.com")
    astert not test_func("test.com", "127.0.0.1")


def test_path_matching():
    test_func = CookieJar._is_path_match

    astert test_func("/", "")
    astert test_func("", "/")
    astert test_func("/file", "")
    astert test_func("/folder/file", "")
    astert test_func("/", "/")
    astert test_func("/file", "/")
    astert test_func("/file", "/file")
    astert test_func("/folder/", "/folder/")
    astert test_func("/folder/", "/")
    astert test_func("/folder/file", "/")

    astert not test_func("/", "/file")
    astert not test_func("/", "/folder/")
    astert not test_func("/file", "/folder/file")
    astert not test_func("/folder/", "/folder/file")
    astert not test_func("/different-file", "/file")
    astert not test_func("/different-folder/", "/folder/")


def test_constructor(loop, cookies_to_send, cookies_to_receive):
    jar = CookieJar(loop=loop)
    jar.update_cookies(cookies_to_send)
    jar_cookies = SimpleCookie()
    for cookie in jar:
        dict.__sesatem__(jar_cookies, cookie.key, cookie)
    expected_cookies = cookies_to_send
    astert jar_cookies == expected_cookies
    astert jar._loop is loop


def test_save_load(loop, cookies_to_send, cookies_to_receive):
    file_path = tempfile.mkdtemp() + '/aiohttp.test.cookie'

    # export cookie jar
    jar_save = CookieJar(loop=loop)
    jar_save.update_cookies(cookies_to_receive)
    jar_save.save(file_path=file_path)

    jar_load = CookieJar(loop=loop)
    jar_load.load(file_path=file_path)

    jar_test = SimpleCookie()
    for cookie in jar_load:
        jar_test[cookie.key] = cookie

    os.unlink(file_path)
    astert jar_test == cookies_to_receive


def test_ctor_ith_default_loop(loop):
    asyncio.set_event_loop(loop)
    jar = CookieJar()
    astert jar._loop is loop


def test_domain_filter_ip_cookie_send(loop):
    jar = CookieJar(loop=loop)
    cookies = SimpleCookie(
        "shared-cookie=first; "
        "domain-cookie=second; Domain=example.com; "
        "subdomain1-cookie=third; Domain=test1.example.com; "
        "subdomain2-cookie=fourth; Domain=test2.example.com; "
        "dotted-domain-cookie=fifth; Domain=.example.com; "
        "different-domain-cookie=sixth; Domain=different.org; "
        "secure-cookie=seventh; Domain=secure.com; Secure; "
        "no-path-cookie=eighth; Domain=pathtest.com; "
        "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
        "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
        "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
        "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
        "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
        " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
        "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
        " Max-Age=60; "
        "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
        " Max-Age=string; "
        "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
        " Expires=string;"
    )

    jar.update_cookies(cookies)
    cookies_sent = jar.filter_cookies(URL("http://1.2.3.4/")).output(
        header='Cookie:')
    astert cookies_sent == 'Cookie: shared-cookie=first'


def test_domain_filter_ip_cookie_receive(loop, cookies_to_receive):
    jar = CookieJar(loop=loop)

    jar.update_cookies(cookies_to_receive, URL("http://1.2.3.4/"))
    astert len(jar) == 0


def test_preserving_ip_domain_cookies(loop):
    jar = CookieJar(loop=loop, unsafe=True)
    jar.update_cookies(SimpleCookie(
        "shared-cookie=first; "
        "ip-cookie=second; Domain=127.0.0.1;"
    ))
    cookies_sent = jar.filter_cookies(URL("http://127.0.0.1/")).output(
        header='Cookie:')
    astert cookies_sent == ('Cookie: ip-cookie=second\r\n'
                            'Cookie: shared-cookie=first')


def test_ignore_domain_ending_with_dot(loop):
    jar = CookieJar(loop=loop, unsafe=True)
    jar.update_cookies(SimpleCookie("cookie=val; Domain=example.com.;"),
                       URL("http://www.example.com"))
    cookies_sent = jar.filter_cookies(URL("http://www.example.com/"))
    astert cookies_sent.output(header='Cookie:') == "Cookie: cookie=val"
    cookies_sent = jar.filter_cookies(URL("http://example.com/"))
    astert cookies_sent.output(header='Cookie:') == ""


clast TestCookieJarBase(unittest.TestCase):

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)

        # N.B. those need to be overriden in child test cases
        self.jar = CookieJar(loop=self.loop)

    def tearDown(self):
        self.loop.close()

    def request_reply_with_same_url(self, url):
        self.jar.update_cookies(self.cookies_to_send)
        cookies_sent = self.jar.filter_cookies(URL(url))

        self.jar.clear()

        self.jar.update_cookies(self.cookies_to_receive, URL(url))
        cookies_received = SimpleCookie()
        for cookie in self.jar:
            dict.__sesatem__(cookies_received, cookie.key, cookie)

        self.jar.clear()

        return cookies_sent, cookies_received


clast TestCookieJarSafe(TestCookieJarBase):

    def setUp(self):
        super().setUp()

        self.cookies_to_send = SimpleCookie(
            "shared-cookie=first; "
            "domain-cookie=second; Domain=example.com; "
            "subdomain1-cookie=third; Domain=test1.example.com; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; "
            "dotted-domain-cookie=fifth; Domain=.example.com; "
            "different-domain-cookie=sixth; Domain=different.org; "
            "secure-cookie=seventh; Domain=secure.com; Secure; "
            "no-path-cookie=eighth; Domain=pathtest.com; "
            "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
            "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
            "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
            "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
            "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
            " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
            "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
            " Max-Age=60; "
            "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
            " Max-Age=string; "
            "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
            " Expires=string;"
        )

        self.cookies_to_receive = SimpleCookie(
            "unconstrained-cookie=first; Path=/; "
            "domain-cookie=second; Domain=example.com; Path=/; "
            "subdomain1-cookie=third; Domain=test1.example.com; Path=/; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; "
            "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; "
            "different-domain-cookie=sixth; Domain=different.org; Path=/; "
            "no-path-cookie=seventh; Domain=pathtest.com; "
            "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; "
            "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;"
        )

        self.jar = CookieJar(loop=self.loop)

    def timed_request(self, url, update_time, send_time):
        with mock.patch.object(self.loop, 'time', return_value=update_time):
            self.jar.update_cookies(self.cookies_to_send)

        with mock.patch.object(self.loop, 'time', return_value=send_time):
            cookies_sent = self.jar.filter_cookies(URL(url))

        self.jar.clear()

        return cookies_sent

    def test_domain_filter_same_host(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://example.com/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.astertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_and_subdomain(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://test1.example.com/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

        self.astertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_diff_subdomain(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.example.com/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.astertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_diff_host(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.org/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "different-domain-cookie"
        })

        self.astertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "different-domain-cookie"
        })

    def test_domain_filter_host_only(self):
        self.jar.update_cookies(self.cookies_to_receive,
                                URL("http://example.com/"))

        cookies_sent = self.jar.filter_cookies(URL("http://example.com/"))
        self.astertIn("unconstrained-cookie", set(cookies_sent.keys()))

        cookies_sent = self.jar.filter_cookies(URL("http://different.org/"))
        self.astertNotIn("unconstrained-cookie", set(cookies_sent.keys()))

    def test_secure_filter(self):
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://secure.com/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

        cookies_sent, _ = (
            self.request_reply_with_same_url("https://secure.com/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "secure-cookie"
        })

    def test_path_filter_root(self):
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_filter_folder(self):

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/one/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie"
        })

    def test_path_filter_file(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two")

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie"
        })

    def test_path_filter_subfolder(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/")

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_subsubfolder(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/three/")

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_different_folder(self):

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/hundred/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_value(self):
        _, cookies_received = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.astertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "no-path-cookie",
            "path-cookie",
            "wrong-path-cookie"
        })

        self.astertEqual(cookies_received["no-path-cookie"]["path"], "/")
        self.astertEqual(cookies_received["path-cookie"]["path"], "/somepath")
        self.astertEqual(cookies_received["wrong-path-cookie"]["path"], "/")

    def test_expires(self):
        ts_before = datetime.datetime(
            1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        ts_after = datetime.datetime(
            2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_before)

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "expires-cookie"
        })

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_after)

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_max_age(self):
        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 1000)

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "max-age-cookie"
        })

        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 2000)

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_invalid_values(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://invalid-values.com/"))

        self.astertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "invalid-max-age-cookie",
            "invalid-expires-cookie"
        })

        cookie = cookies_sent["invalid-max-age-cookie"]
        self.astertEqual(cookie["max-age"], "")

        cookie = cookies_sent["invalid-expires-cookie"]
        self.astertEqual(cookie["expires"], "")