2020-05-24 00:53:29 +08:00
|
|
|
from flask import url_for
|
|
|
|
|
2021-10-12 20:36:47 +08:00
|
|
|
from app.db import Session
|
2022-04-15 22:59:44 +08:00
|
|
|
from app.models import Notification
|
|
|
|
from tests.api.utils import get_new_user_and_api_key
|
2020-05-24 00:53:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_notifications(flask_client):
|
2022-04-15 22:59:44 +08:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-05-24 00:53:29 +08:00
|
|
|
|
|
|
|
# create some notifications
|
|
|
|
Notification.create(user_id=user.id, message="Test message 1")
|
|
|
|
Notification.create(user_id=user.id, message="Test message 2")
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-05-24 00:53:29 +08:00
|
|
|
|
|
|
|
r = flask_client.get(
|
2022-01-24 22:20:59 +08:00
|
|
|
"/api/notifications?page=0",
|
2020-05-24 00:53:29 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
2020-05-24 04:51:00 +08:00
|
|
|
assert r.json["more"] is False
|
|
|
|
assert len(r.json["notifications"]) == 2
|
|
|
|
for n in r.json["notifications"]:
|
2020-05-24 00:53:29 +08:00
|
|
|
assert n["id"] > 0
|
|
|
|
assert n["message"]
|
2022-01-24 22:20:59 +08:00
|
|
|
assert "title" in n
|
2020-05-24 00:53:29 +08:00
|
|
|
assert n["read"] is False
|
|
|
|
assert n["created_at"]
|
|
|
|
|
|
|
|
# no more post at the next page
|
|
|
|
r = flask_client.get(
|
|
|
|
url_for("api.get_notifications", page=1),
|
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
2020-05-24 04:51:00 +08:00
|
|
|
assert r.json["more"] is False
|
|
|
|
assert len(r.json["notifications"]) == 0
|
2020-05-24 00:53:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_mark_notification_as_read(flask_client):
|
2022-04-15 22:59:44 +08:00
|
|
|
user, api_key = get_new_user_and_api_key()
|
2020-05-24 00:53:29 +08:00
|
|
|
|
2024-02-15 22:48:02 +08:00
|
|
|
notif_id = Notification.create(
|
|
|
|
user_id=user.id, message="Test message 1", flush=True
|
|
|
|
).id
|
2021-10-12 20:36:47 +08:00
|
|
|
Session.commit()
|
2020-05-24 00:53:29 +08:00
|
|
|
|
|
|
|
r = flask_client.post(
|
2024-02-15 22:48:02 +08:00
|
|
|
url_for("api.mark_as_read", notification_id=notif_id),
|
2020-05-24 00:53:29 +08:00
|
|
|
headers={"Authentication": api_key.code},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
2024-02-15 22:48:02 +08:00
|
|
|
notification = Notification.filter_by(id=notif_id).first()
|
2020-05-24 00:53:29 +08:00
|
|
|
assert notification.read
|