First
[anni] / test / pleroma / web / pleroma_api / controllers / notification_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   alias Pleroma.Notification
9   alias Pleroma.Repo
10   alias Pleroma.Web.CommonAPI
11
12   import Pleroma.Factory
13
14   describe "POST /api/v1/pleroma/notifications/read" do
15     setup do: oauth_access(["write:notifications"])
16
17     test "it marks a single notification as read", %{user: user1, conn: conn} do
18       user2 = insert(:user)
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)
23
24       response =
25         conn
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)
29
30       assert %{"pleroma" => %{"is_seen" => true}} = response
31       assert Repo.get(Notification, notification1.id).seen
32       refute Repo.get(Notification, notification2.id).seen
33     end
34
35     test "it marks multiple notifications as read", %{user: user1, conn: conn} do
36       user2 = insert(:user)
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}"})
40
41       [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
42
43       [response1, response2] =
44         conn
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)
48
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
54     end
55
56     test "it returns error when notification not found", %{conn: conn} do
57       response =
58         conn
59         |> put_req_header("content-type", "application/json")
60         |> post("/api/v1/pleroma/notifications/read", %{
61           id: 22_222_222_222_222
62         })
63         |> json_response_and_validate_schema(:bad_request)
64
65       assert response == %{"error" => "Cannot get notification"}
66     end
67   end
68 end