notyfikacje

This commit is contained in:
2026-11-01 20:27:10 +01:00
parent e55af30644
commit b77b348d54
11 changed files with 375 additions and 18 deletions
+66
View File
@@ -168,6 +168,7 @@ class MainAppTest(unittest.TestCase):
self.assertEqual(self.client.get("/home").status_code, 200)
self.assertEqual(self.client.get("/sensors").status_code, 200)
self.assertEqual(self.client.get("/notes").status_code, 200)
self.assertEqual(self.client.get("/notifications").status_code, 200)
self.assertEqual(self.client.get("/api/docs").status_code, 200)
def test_public_device_api_is_proxied_to_sensors(self):
@@ -275,6 +276,71 @@ class MainAppTest(unittest.TestCase):
["second item", "renamed first item"],
)
def test_notifications_badge_and_mark_read(self):
from user_models import Notification, User, user_db
self.login()
with self.main_app.app_context():
user = User.query.filter_by(username="tester").one()
other = User(username="other-user")
other.set_password("secret")
user_db.session.add(other)
user_db.session.flush()
first = Notification(user_id=user.id, message="first notification")
second = Notification(user_id=user.id, message="second notification")
foreign = Notification(user_id=other.id, message="other user notification")
user_db.session.add_all([first, second, foreign])
user_db.session.commit()
first_id = first.id
foreign_id = foreign.id
home = self.client.get("/home").get_data(as_text=True)
self.assertIn('nav-badge">2</span>', home)
page = self.client.get("/notifications")
html = page.get_data(as_text=True)
self.assertEqual(page.status_code, 200)
self.assertIn("first notification", html)
self.assertIn("second notification", html)
self.assertNotIn("other user notification", html)
self.assertEqual(html.count('class="notification-item new"'), 2)
read = self.client.get(f"/notifications/{first_id}/read")
self.assertEqual(read.status_code, 302)
with self.main_app.app_context():
user_db.session.expire_all()
notification = user_db.session.get(Notification, first_id)
self.assertTrue(notification.is_read)
self.assertIsNotNone(notification.read_at)
page = self.client.get("/notifications")
html = page.get_data(as_text=True)
self.assertIn("1 new notification.", html)
self.assertIn('nav-badge">1</span>', html)
self.assertEqual(html.count('class="notification-item read"'), 1)
self.assertEqual(html.count('class="notification-item new"'), 1)
self.assertEqual(
self.client.get(f"/notifications/{foreign_id}/read").status_code, 404
)
def test_navigation_links_are_present_on_all_pages(self):
self.login()
links = (
'href="/home"',
'href="/sensors"',
'href="/notes"',
'href="/notifications"',
'href="/api/docs"',
'href="/logout"',
)
for path in ("/home", "/sensors", "/notes", "/notifications", "/api/docs"):
html = self.client.get(path).get_data(as_text=True)
for link in links:
self.assertIn(link, html, f"{link} missing on {path}")
if __name__ == "__main__":
unittest.main()