summaryrefslogtreecommitdiff
path: root/Biz
diff options
context:
space:
mode:
authorOmni Worker <bot@omni.agent>2025-11-22 05:08:07 -0500
committerOmni Worker <bot@omni.agent>2025-11-22 05:16:30 -0500
commit6042e2145478dd966eea697a2286ff3f232acf95 (patch)
treed7513d1b12d05a2536880c0fa2be0234e290dbf0 /Biz
parent1fa573be7dde6f5a0c1719b0a3bc386222b96c0a (diff)
task: claim t-rWbMpxaBk
Diffstat (limited to 'Biz')
-rw-r--r--Biz/PodcastItLater/TestMetricsView.py101
-rw-r--r--Biz/PodcastItLater/UI.py4
2 files changed, 103 insertions, 2 deletions
diff --git a/Biz/PodcastItLater/TestMetricsView.py b/Biz/PodcastItLater/TestMetricsView.py
new file mode 100644
index 0000000..9ca30c2
--- /dev/null
+++ b/Biz/PodcastItLater/TestMetricsView.py
@@ -0,0 +1,101 @@
+# : 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('<h3 class="mb-0">3</h3>', response.text)
+ self.assertIn('<h3 class="mb-0">1</h3>', 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()
diff --git a/Biz/PodcastItLater/UI.py b/Biz/PodcastItLater/UI.py
index d315249..5102652 100644
--- a/Biz/PodcastItLater/UI.py
+++ b/Biz/PodcastItLater/UI.py
@@ -422,7 +422,7 @@ class PricingPage(Component[AnyChildren, PricingPageAttrs]):
current_tier = user.get("plan_tier", "free") if user else "free"
return PageLayout(
- html.div(
+ cast(Any, html.div(
html.h2("Simple Pricing", classes=["text-center", "mb-5"]),
html.div(
# Free Tier
@@ -501,7 +501,7 @@ class PricingPage(Component[AnyChildren, PricingPageAttrs]):
],
),
action="/upgrade",
- method="get",
+ method=cast(Any, "get"),
)
if user and current_tier == "free"
else (