import contextlib
import shutil
from pathlib import Path

import pytest
from aiohttp import CookieJar
from clarabot_nano_common.common.auth.handler import CookieJarStorage

from clarabot_nano_tests.testing.file import temp_path

_LEGACY_COOKS = (
    b"\x80\x05\x95\x14\x02\x00\x00\x00\x00\x00\x00\x8c\x0bcollections\x94\x8c\x0bdefaultdict\x94\x93\x94\x8c\x0c"
    b"http.cookies\x94\x8c\x0cSimpleCookie\x94\x93\x94\x85\x94R\x94\x8c\x11nano.clarabot.com\x94h\x05)\x81\x94("
    b"\x8c\r__Secure-csrf\x94h\x03\x8c\x06Morsel\x94\x93\x94)\x81\x94(\x8c\x07expires\x94\x8c\x00\x94\x8c\x04path"
    b"\x94\x8c\x01/\x94\x8c\x07comment\x94h\x0f\x8c\x06domain\x94h\x08\x8c\x07max-age\x94\x8c\x0586400\x94\x8c\x06"
    b"secure\x94\x88\x8c\x08httponly\x94h\x0f\x8c\x07version\x94h\x0f\x8c\x08samesite\x94\x8c\x06strict\x94u}\x94("
    b"\x8c\x03key\x94h\n\x8c\x05value\x94\x8c\t5nX9_jBpv\x94\x8c\x0bcoded_value\x94h\x1eub\x8c\n__Secure-s\x94h\x0c"
    b")\x81\x94(h\x0eh\x0fh\x10h\x11h\x12h\x0fh\x13\x8c\x11nano.clarabot.com\x94h\x14h\x0fh\x16\x88h\x17\x88h\x18h"
    b"\x0fh\x19\x8c\x06strict\x94u}\x94(h\x1ch h\x1d\x8c\x19gq5q_8G-g4.ONe_8WMiCAE_5U\x94h\x1fh%ub\x8c\x01k\x94h\x0c"
    b")\x81\x94(h\x0eh\x0fh\x10h\x11h\x12h\x0fh\x13\x8c\x11nano.clarabot.com\x94h\x14h\x0fh\x16\x88h\x17h\x0fh\x18h"
    b'\x0fh\x19\x8c\x06strict\x94u}\x94(h\x1ch&h\x1d\x8c\nTM+i2HBl==\x94h\x1f\x8c\x0c"TM+i2HBl=="\x94ubus.'
)
_COOKS = """__Secure-csrf=5nX9_jBpv; Domain=nano.clarabot.com; Max-Age=86400; Path=/; SameSite=strict; Secure
__Secure-s=gq5q_8G-g4.ONe_8WMiCAE_5U; Domain=nano.clarabot.com; HttpOnly; Path=/; SameSite=strict; Secure
k="TM+i2HBl=="; Domain=nano.clarabot.com; Path=/; SameSite=strict; Secure""".replace(
    "\n", "\r\n"
)


@pytest.mark.asyncio
async def test_server_authenticator_cookie_jar_storage(tmpdir):
    store_dir = Path(str(temp_path(tmpdir)))

    @contextlib.contextmanager
    def _case():
        if store_dir.is_dir():
            shutil.rmtree(store_dir)
        cjs_ = CookieJarStorage(CookieJar(), store_dir)
        assert cjs_.jar_dumps() == ""
        cjs_.load_session_cookies()
        assert cjs_.jar_dumps() == ""
        assert cjs_.could_try_load() is False
        yield cjs_
        cjs_.remove_session_cookies()
        assert cjs_.jar_dumps() == ""
        assert cjs_.could_try_load() is False
        cjs_.load_session_cookies()
        assert cjs_.jar_dumps() == ""

    with _case() as cjs:
        cjs._legacy_cookie_storage_path().write_bytes(_LEGACY_COOKS)
        assert cjs.could_try_load() is True
        cjs.load_session_cookies()
        assert cjs.jar_dumps() == _COOKS

    with _case() as cjs:
        cjs._cookie_storage_path().write_text(_COOKS)
        assert cjs.could_try_load() is True
        cjs.load_session_cookies()
        assert cjs.jar_dumps() == _COOKS

    with _case() as cjs:
        cjs._legacy_cookie_storage_path().write_bytes(_LEGACY_COOKS)
        assert cjs.could_try_load() is True
        cjs.load_session_cookies()
        assert cjs.jar_dumps() == _COOKS
        cjs.save_session_cookies()
        assert cjs._cookie_storage_path().read_bytes().decode("utf-8") == _COOKS
