diff --git a/tests/test_auth/conftest.py b/tests/test_auth/conftest.py new file mode 100644 index 0000000..13b8f7a --- /dev/null +++ b/tests/test_auth/conftest.py @@ -0,0 +1,111 @@ +"""Pytest fixtures for auth module tests + +Provides mock objects and test fixtures for ZhihuLogin testing. +""" + +import os +from pathlib import Path +from unittest.mock import AsyncMock, MagicMock, patch +import pytest + + +@pytest.fixture +def mock_page(): + """Mock Playwright Page object.""" + page = MagicMock() + page.goto = MagicMock() + page.wait_for_selector = MagicMock() + page.query_selector = MagicMock() + page.set_default_timeout = MagicMock() + return page + + +@pytest.fixture +def mock_context(): + """Mock Playwright BrowserContext object.""" + context = MagicMock() + context.cookies = MagicMock(return_value=[]) + context.add_cookies = MagicMock() + context.new_page = MagicMock() + context.close = MagicMock() + return context + + +@pytest.fixture +def mock_canvas(): + """Mock Canvas element for QR code.""" + canvas = MagicMock() + canvas.screenshot = MagicMock(return_value=b"fake_png_bytes") + return canvas + + +@pytest.fixture +def mock_browser(mock_page, mock_context, mock_canvas): + """Mock PlaywrightBrowser instance. + + Provides a fully mocked browser that can be used for unit testing + without requiring actual browser interaction. + """ + browser = MagicMock() + + # Setup browser properties + browser.headless = True + browser.timeout = 30000 + browser.page = mock_page + browser.context = mock_context + + # Setup methods + browser.start = MagicMock() + browser.close = MagicMock() + browser.goto = MagicMock() + browser.wait_for_selector = MagicMock(return_value=mock_canvas) + browser.query_selector = MagicMock(return_value=mock_canvas) + + # Setup context.cookies to return cookie list + browser.context.cookies = MagicMock(return_value=[]) + + return browser + + +@pytest.fixture +def sample_cookies(): + """Sample cookie data for testing. + + Returns a list of cookies including z_c0 for login detection. + """ + return [ + {"name": "z_c0", "value": "test_z_c0_value", "domain": ".zhihu.com"}, + {"name": "_xsrf", "value": "test_xsrf_value", "domain": ".zhihu.com"}, + {"name": "d_c0", "value": "test_d_c0_value", "domain": ".zhihu.com"}, + ] + + +@pytest.fixture +def sample_cookies_no_login(): + """Sample cookies without login cookie (z_c0).""" + return [ + {"name": "_xsrf", "value": "test_xsrf_value", "domain": ".zhihu.com"}, + {"name": "d_c0", "value": "test_d_c0_value", "domain": ".zhihu.com"}, + ] + + +@pytest.fixture +def mock_env_file(tmp_path): + """Create a temporary .kb_env file for testing. + + Returns the path to the temporary env file. + """ + env_file = tmp_path / ".kb_env" + env_file.write_text("") + return env_file + + +@pytest.fixture +def intermediate_path(tmp_path): + """Create a temporary intermediate directory for testing. + + Returns the path to the intermediate directory. + """ + intermediate = tmp_path / "intermediate" + intermediate.mkdir(parents=True, exist_ok=True) + return str(intermediate)