19c377ef65
- Test save_cookie creates env file - Test save_cookie updates existing line - Test save_cookie appends new line - Test save_cookie preserves other lines - Test save_cookie sets file permissions to 600 - Test load_cookie returns saved value - Test load_cookie returns empty if file not exists Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
137 lines
4.4 KiB
Python
137 lines
4.4 KiB
Python
"""Tests for Cookie persistence functionality
|
|
|
|
Tests CookieManager class for saving and loading cookies from ~/.kb_env file.
|
|
"""
|
|
|
|
import stat
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
from kb_workflow.auth.cookie_manager import CookieManager
|
|
|
|
|
|
class TestCookieManager:
|
|
"""Test suite for CookieManager class"""
|
|
|
|
def test_save_cookie_creates_file(self, tmp_path: Path):
|
|
"""Test that saving cookie creates the env file"""
|
|
# Arrange
|
|
env_file = tmp_path / ".kb_env"
|
|
manager = CookieManager(env_path=env_file)
|
|
cookie_str = "z_c0=token123; _zap=zap_value"
|
|
|
|
# Act
|
|
manager.save_cookie(cookie_str)
|
|
|
|
# Assert
|
|
assert env_file.exists()
|
|
content = env_file.read_text(encoding="utf-8")
|
|
assert 'ZHIHU_COOKIE="z_c0=token123; _zap=zap_value"' in content
|
|
|
|
def test_save_cookie_updates_existing(self, tmp_path: Path):
|
|
"""Test that saving cookie updates existing ZHIHU_COOKIE line"""
|
|
# Arrange
|
|
env_file = tmp_path / ".kb_env"
|
|
env_file.write_text('ZHIHU_COOKIE="old_cookie"\n', encoding="utf-8")
|
|
manager = CookieManager(env_path=env_file)
|
|
new_cookie = "z_c0=new_token"
|
|
|
|
# Act
|
|
manager.save_cookie(new_cookie)
|
|
|
|
# Assert
|
|
content = env_file.read_text(encoding="utf-8")
|
|
assert 'ZHIHU_COOKIE="z_c0=new_token"' in content
|
|
assert "old_cookie" not in content
|
|
|
|
def test_save_cookie_appends_new(self, tmp_path: Path):
|
|
"""Test that saving cookie appends new line when ZHIHU_COOKIE doesn't exist"""
|
|
# Arrange
|
|
env_file = tmp_path / ".kb_env"
|
|
env_file.write_text("OTHER_VAR=value\n", encoding="utf-8")
|
|
manager = CookieManager(env_path=env_file)
|
|
cookie_str = "z_c0=token"
|
|
|
|
# Act
|
|
manager.save_cookie(cookie_str)
|
|
|
|
# Assert
|
|
content = env_file.read_text(encoding="utf-8")
|
|
assert "OTHER_VAR=value" in content
|
|
assert 'ZHIHU_COOKIE="z_c0=token"' in content
|
|
|
|
def test_save_cookie_preserves_other_lines(self, tmp_path: Path):
|
|
"""Test that saving cookie preserves other configuration lines"""
|
|
# Arrange
|
|
env_file = tmp_path / ".kb_env"
|
|
env_file.write_text(
|
|
"KB_BASE_PATH=/path/to/kb\nOTHER_VAR=value\n",
|
|
encoding="utf-8"
|
|
)
|
|
manager = CookieManager(env_path=env_file)
|
|
cookie_str = "z_c0=token"
|
|
|
|
# Act
|
|
manager.save_cookie(cookie_str)
|
|
|
|
# Assert
|
|
content = env_file.read_text(encoding="utf-8")
|
|
assert "KB_BASE_PATH=/path/to/kb" in content
|
|
assert "OTHER_VAR=value" in content
|
|
assert 'ZHIHU_COOKIE="z_c0=token"' in content
|
|
|
|
def test_save_cookie_sets_file_permissions(self, tmp_path: Path):
|
|
"""Test that saved file has 600 permissions"""
|
|
# Arrange
|
|
env_file = tmp_path / ".kb_env"
|
|
manager = CookieManager(env_path=env_file)
|
|
cookie_str = "z_c0=token"
|
|
|
|
# Act
|
|
manager.save_cookie(cookie_str)
|
|
|
|
# Assert
|
|
file_stat = env_file.stat()
|
|
file_mode = stat.filemode(file_stat.st_mode)
|
|
# Check owner read/write only (600)
|
|
assert (file_stat.st_mode & 0o777) == 0o600, f"Expected 600, got {oct(file_stat.st_mode & 0o777)}"
|
|
|
|
def test_load_cookie_returns_saved(self, tmp_path: Path):
|
|
"""Test that loading cookie returns previously saved value"""
|
|
# Arrange
|
|
env_file = tmp_path / ".kb_env"
|
|
manager = CookieManager(env_path=env_file)
|
|
cookie_str = "z_c0=token123; _zap=zap_value"
|
|
|
|
# Act
|
|
manager.save_cookie(cookie_str)
|
|
loaded = manager.load_cookie()
|
|
|
|
# Assert
|
|
assert loaded == "z_c0=token123; _zap=zap_value"
|
|
|
|
def test_load_cookie_empty_if_not_exists(self, tmp_path: Path):
|
|
"""Test that loading cookie returns empty string if file doesn't exist"""
|
|
# Arrange
|
|
env_file = tmp_path / ".kb_env"
|
|
manager = CookieManager(env_path=env_file)
|
|
|
|
# Act
|
|
loaded = manager.load_cookie()
|
|
|
|
# Assert
|
|
assert loaded == ""
|
|
|
|
def test_load_cookie_empty_if_key_not_found(self, tmp_path: Path):
|
|
"""Test that loading cookie returns empty string if key not in file"""
|
|
# Arrange
|
|
env_file = tmp_path / ".kb_env"
|
|
env_file.write_text("OTHER_VAR=value\n", encoding="utf-8")
|
|
manager = CookieManager(env_path=env_file)
|
|
|
|
# Act
|
|
loaded = manager.load_cookie()
|
|
|
|
# Assert
|
|
assert loaded == ""
|