1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Notification
10 alias Pleroma.Web.CommonAPI
12 import Pleroma.Factory
14 describe "POST /api/v1/pleroma/notifications/read" do
15 setup do: oauth_access(["write:notifications"])
17 test "it marks a single notification as read", %{user: user1, conn: conn} do
19 {:ok, activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
20 {:ok, activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
21 {:ok, [notification1]} = Notification.create_notifications(activity1)
22 {:ok, [notification2]} = Notification.create_notifications(activity2)
26 |> put_req_header("content-type", "application/json")
27 |> post("/api/v1/pleroma/notifications/read", %{id: notification1.id})
28 |> json_response_and_validate_schema(:ok)
30 assert %{"pleroma" => %{"is_seen" => true}} = response
31 assert Repo.get(Notification, notification1.id).seen
32 refute Repo.get(Notification, notification2.id).seen
35 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
37 {:ok, _activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
38 {:ok, _activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
39 {:ok, _activity3} = CommonAPI.post(user2, %{status: "HIE @#{user1.nickname}"})
41 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
43 [response1, response2] =
45 |> put_req_header("content-type", "application/json")
46 |> post("/api/v1/pleroma/notifications/read", %{max_id: notification2.id})
47 |> json_response_and_validate_schema(:ok)
49 assert %{"pleroma" => %{"is_seen" => true}} = response1
50 assert %{"pleroma" => %{"is_seen" => true}} = response2
51 assert Repo.get(Notification, notification1.id).seen
52 assert Repo.get(Notification, notification2.id).seen
53 refute Repo.get(Notification, notification3.id).seen
56 test "it returns error when notification not found", %{conn: conn} do
59 |> put_req_header("content-type", "application/json")
60 |> post("/api/v1/pleroma/notifications/read", %{
61 id: 22_222_222_222_222
63 |> json_response_and_validate_schema(:bad_request)
65 assert response == %{"error" => "Cannot get notification"}