af3536aa92
- Test parse_json_cookies for Chrome Cookie Editor format - Test domain filtering for zhihu.com - Test empty array handling - Test malformed JSON error handling - Test file not found error handling Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
88 lines
3.2 KiB
Python
88 lines
3.2 KiB
Python
"""Tests for Cookie JSON parsing functionality
|
|
|
|
Tests parse_json_cookies function for Chrome Cookie Editor JSON format.
|
|
"""
|
|
|
|
import json
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
from kb_workflow.auth.cookie_manager import parse_json_cookies
|
|
|
|
|
|
class TestParseJsonCookies:
|
|
"""Test suite for parse_json_cookies function"""
|
|
|
|
def test_parse_json_cookies(self, tmp_path: Path):
|
|
"""Test parsing Chrome Cookie Editor JSON format"""
|
|
# Arrange: Create a valid JSON file with Chrome Cookie Editor format
|
|
cookie_data = [
|
|
{"name": "z_c0", "value": "token123", "domain": ".zhihu.com"},
|
|
{"name": "_zap", "value": "zap_value", "domain": ".zhihu.com"},
|
|
]
|
|
json_file = tmp_path / "cookies.json"
|
|
json_file.write_text(json.dumps(cookie_data), encoding="utf-8")
|
|
|
|
# Act
|
|
result = parse_json_cookies(str(json_file))
|
|
|
|
# Assert
|
|
assert result == "z_c0=token123; _zap=zap_value"
|
|
|
|
def test_parse_json_cookies_filters_domain(self, tmp_path: Path):
|
|
"""Test that only zhihu.com domain cookies are kept"""
|
|
# Arrange: Mix of domains
|
|
cookie_data = [
|
|
{"name": "z_c0", "value": "zhihu_token", "domain": ".zhihu.com"},
|
|
{"name": "other_cookie", "value": "other_value", "domain": ".other.com"},
|
|
{"name": "_zap", "value": "zap_value", "domain": "zhihu.com"},
|
|
]
|
|
json_file = tmp_path / "cookies.json"
|
|
json_file.write_text(json.dumps(cookie_data), encoding="utf-8")
|
|
|
|
# Act
|
|
result = parse_json_cookies(str(json_file))
|
|
|
|
# Assert: Only zhihu.com cookies included
|
|
assert "z_c0=zhihu_token" in result
|
|
assert "_zap=zap_value" in result
|
|
assert "other_cookie" not in result
|
|
|
|
def test_parse_json_cookies_empty(self, tmp_path: Path):
|
|
"""Test that empty array returns empty string"""
|
|
# Arrange
|
|
json_file = tmp_path / "cookies.json"
|
|
json_file.write_text("[]", encoding="utf-8")
|
|
|
|
# Act
|
|
result = parse_json_cookies(str(json_file))
|
|
|
|
# Assert
|
|
assert result == ""
|
|
|
|
def test_parse_json_cookies_malformed(self, tmp_path: Path):
|
|
"""Test that malformed JSON raises ValueError"""
|
|
# Arrange: Invalid JSON content
|
|
json_file = tmp_path / "cookies.json"
|
|
json_file.write_text("{not valid json}", encoding="utf-8")
|
|
|
|
# Act & Assert
|
|
with pytest.raises(ValueError, match="Invalid JSON format"):
|
|
parse_json_cookies(str(json_file))
|
|
|
|
def test_parse_json_cookies_file_not_found(self):
|
|
"""Test that non-existent file raises ValueError"""
|
|
# Act & Assert
|
|
with pytest.raises(ValueError, match="Cookie file not found"):
|
|
parse_json_cookies("/non/existent/path/cookies.json")
|
|
|
|
def test_parse_json_cookies_not_array(self, tmp_path: Path):
|
|
"""Test that non-array JSON raises ValueError"""
|
|
# Arrange: JSON object instead of array
|
|
json_file = tmp_path / "cookies.json"
|
|
json_file.write_text('{"name": "test"}', encoding="utf-8")
|
|
|
|
# Act & Assert
|
|
with pytest.raises(ValueError, match="Expected JSON array"):
|
|
parse_json_cookies(str(json_file))
|