""" Integration tests for feed statistics in admin dashboard Tests the feed statistics features in /admin/metrics-dashboard and /admin/metrics per v1.1.2 Phase 3. """ import pytest from starpunk.auth import create_session @pytest.fixture def authenticated_client(app, client): """Client with authenticated session""" with app.test_request_context(): # Create a session for the test user session_token = create_session(app.config["ADMIN_ME"]) # Set session cookie client.set_cookie("starpunk_session", session_token) return client def test_feed_statistics_dashboard_endpoint(authenticated_client): """Test metrics dashboard includes feed statistics section""" response = authenticated_client.get("/admin/metrics-dashboard") assert response.status_code == 200 # Should contain feed statistics section assert b"Feed Statistics" in response.data assert b"Feed Requests by Format" in response.data assert b"Feed Cache Statistics" in response.data assert b"Feed Generation Performance" in response.data # Should have chart canvases assert b'id="feedFormatChart"' in response.data assert b'id="feedCacheChart"' in response.data def test_feed_statistics_metrics_endpoint(authenticated_client): """Test /admin/metrics endpoint includes feed statistics""" response = authenticated_client.get("/admin/metrics") assert response.status_code == 200 data = response.get_json() # Should have feeds key assert "feeds" in data # Should have expected structure feeds = data["feeds"] if "error" not in feeds: assert "by_format" in feeds assert "cache" in feeds assert "total_requests" in feeds assert "format_percentages" in feeds # Check format structure for format_name in ["rss", "atom", "json"]: assert format_name in feeds["by_format"] fmt = feeds["by_format"][format_name] assert "generated" in fmt assert "cached" in fmt assert "total" in fmt assert "avg_duration_ms" in fmt # Check cache structure assert "hits" in feeds["cache"] assert "misses" in feeds["cache"] assert "hit_rate" in feeds["cache"] def test_feed_statistics_after_feed_request(authenticated_client): """Test feed statistics track actual feed requests""" # Make a feed request response = authenticated_client.get("/feed.rss") assert response.status_code == 200 # Check metrics endpoint now has data response = authenticated_client.get("/admin/metrics") assert response.status_code == 200 data = response.get_json() # Should have feeds data assert "feeds" in data feeds = data["feeds"] # May have requests tracked (depends on metrics buffer timing) # Just verify structure is correct assert "total_requests" in feeds assert feeds["total_requests"] >= 0 def test_dashboard_requires_auth_for_feed_stats(client): """Test dashboard requires authentication (even for feed stats)""" response = client.get("/admin/metrics-dashboard") # Should redirect to auth or return 401/403 assert response.status_code in [302, 401, 403] def test_metrics_endpoint_requires_auth_for_feed_stats(client): """Test metrics endpoint requires authentication""" response = client.get("/admin/metrics") # Should redirect to auth or return 401/403 assert response.status_code in [302, 401, 403]