# : out podcastitlater-test-metrics # : dep pytest # : dep starlette # : dep httpx # : dep ludic # : dep feedgen # : dep itsdangerous # : dep uvicorn # : dep stripe # : dep sqids import Biz.PodcastItLater.Core as Core import Biz.PodcastItLater.Web as Web import Omni.Test as Test from starlette.testclient import TestClient class BaseWebTest(Test.TestCase): def setUp(self) -> None: Core.Database.init_db() self.client = TestClient(Web.app) @staticmethod def tearDown() -> None: Core.Database.teardown() class TestMetricsView(BaseWebTest): def test_admin_metrics_view_access(self): """Admin user should be able to access metrics view.""" # Create admin user admin_id, _ = Core.Database.create_user("ben@bensima.com") self.client.post("/login", data={"email": "ben@bensima.com"}) response = self.client.get("/admin/metrics") self.assertEqual(response.status_code, 200) self.assertIn("Growth & Usage", response.text) self.assertIn("Total Users", response.text) def test_admin_metrics_data(self): """Metrics view should show correct data.""" # Create admin user admin_id, _ = Core.Database.create_user("ben@bensima.com") self.client.post("/login", data={"email": "ben@bensima.com"}) # Create some data # 1. Users Core.Database.create_user("user1@example.com") user2_id, _ = Core.Database.create_user("user2@example.com") # 2. Subscriptions (simulate by setting subscription_status) with Core.Database.get_connection() as conn: conn.execute("UPDATE users SET subscription_status = 'active' WHERE id = ?", (user2_id,)) conn.commit() # 3. Submissions Core.Database.add_to_queue("http://example.com/1", "user1@example.com", admin_id) # Get metrics page response = self.client.get("/admin/metrics") self.assertEqual(response.status_code, 200) # Check labels self.assertIn("Total Users", response.text) self.assertIn("Active Subs", response.text) self.assertIn("Submissions (24h)", response.text) # Check values (metrics dict is passed to template, we check rendered HTML) # Total users: 3 (admin + user1 + user2) # Active subs: 1 (user2) # Submissions 24h: 1 # Check for values in HTML # Note: This is a bit brittle, but effective for quick verification self.assertIn('

3

', response.text) self.assertIn('

1

', response.text) def test_non_admin_access_denied(self): """Non-admin users should be denied access.""" # Create regular user Core.Database.create_user("regular@example.com") self.client.post("/login", data={"email": "regular@example.com"}) response = self.client.get("/admin/metrics") # Should redirect to /?error=forbidden self.assertEqual(response.status_code, 302) self.assertIn("error=forbidden", response.headers["Location"]) def test_anonymous_access_redirect(self): """Anonymous users should be redirected to login.""" response = self.client.get("/admin/metrics") self.assertEqual(response.status_code, 302) self.assertEqual(response.headers["Location"], "/") def test() -> None: """Run the tests.""" Test.run( Web.area, [TestMetricsView], ) if __name__ == "__main__": test()