From af3536aa92cfcd33c5a17b3fe2733d33359f9098 Mon Sep 17 00:00:00 2001 From: starmen1265 Date: Tue, 17 Mar 2026 17:48:23 +0800 Subject: [PATCH] test(07-02): add failing tests for Cookie JSON parsing - 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 --- kb_workflow/auth/__init__.py | 4 ++ tests/test_auth/__init__.py | 1 + tests/test_auth/test_cookie_import.py | 87 +++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 kb_workflow/auth/__init__.py create mode 100644 tests/test_auth/__init__.py create mode 100644 tests/test_auth/test_cookie_import.py diff --git a/kb_workflow/auth/__init__.py b/kb_workflow/auth/__init__.py new file mode 100644 index 0000000..9101c5b --- /dev/null +++ b/kb_workflow/auth/__init__.py @@ -0,0 +1,4 @@ +"""Auth module for KB Workflow + +Provides authentication and cookie management functionality. +""" diff --git a/tests/test_auth/__init__.py b/tests/test_auth/__init__.py new file mode 100644 index 0000000..d0c3099 --- /dev/null +++ b/tests/test_auth/__init__.py @@ -0,0 +1 @@ +"""Tests for auth module""" diff --git a/tests/test_auth/test_cookie_import.py b/tests/test_auth/test_cookie_import.py new file mode 100644 index 0000000..521e3f5 --- /dev/null +++ b/tests/test_auth/test_cookie_import.py @@ -0,0 +1,87 @@ +"""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))