wohnbot/tests/test_session_validation.py

83 lines
2.9 KiB
Python
Raw Normal View History

"""Tests for session validation and re-login logic"""
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from pathlib import Path
import sys
import os
# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
from state_manager import StateManager
class TestSessionValidation:
"""Test session validation and automatic re-login"""
@pytest.fixture
def state_manager(self, tmp_path):
"""Create a StateManager with a temporary state file"""
state_file = tmp_path / "state.json"
return StateManager(state_file)
def test_logged_in_status_toggle(self, state_manager):
"""Test that logged_in status can be toggled"""
assert state_manager.is_logged_in() is False
state_manager.set_logged_in(True)
assert state_manager.is_logged_in() is True
state_manager.set_logged_in(False)
assert state_manager.is_logged_in() is False
def test_logged_in_persistence_is_memory_only(self, state_manager, tmp_path):
"""Test that logged_in status is memory-only (not persisted to disk)"""
state_manager.set_logged_in(True)
assert state_manager.is_logged_in() is True
# Create new state manager with same file - should start as False
new_state_manager = StateManager(tmp_path / "state.json")
assert new_state_manager.is_logged_in() is False
def test_zero_listings_counter_increments(self):
"""Test that zero_listings_count increments when no listings are fetched"""
# This would be tested in integration tests with the main loop
# For now, we test the logic conceptually
zero_listings_count = 0
max_zero = 3
# Simulate 3 consecutive zero fetches
for _ in range(3):
current_listings = [] # Empty result
if not current_listings:
zero_listings_count += 1
assert zero_listings_count == max_zero
def test_zero_listings_counter_resets_on_success(self):
"""Test that counter resets when listings are successfully fetched"""
zero_listings_count = 2
# Simulate successful fetch
current_listings = [{"id": "test123", "rooms": "2"}]
if current_listings and len(current_listings) > 0:
zero_listings_count = 0
assert zero_listings_count == 0
def test_session_validation_logic(self, state_manager):
"""Test the session validation workflow"""
# Start logged in
state_manager.set_logged_in(True)
assert state_manager.is_logged_in() is True
# Simulate session expiration detection (zero listings threshold reached)
zero_listings_count = 3
max_zero = 3
if zero_listings_count >= max_zero:
state_manager.set_logged_in(False)
# Verify session was reset
assert state_manager.is_logged_in() is False