Move notification actions to PleromaAPI.NotificationController
[akkoma] / test / web / pleroma_api / controllers / notification_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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 |> post("/api/v1/pleroma/notifications/read?id=#{notification1.id}")
27 |> json_response_and_validate_schema(:ok)
28
29 assert %{"pleroma" => %{"is_seen" => true}} = response
30 assert Repo.get(Notification, notification1.id).seen
31 refute Repo.get(Notification, notification2.id).seen
32 end
33
34 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
35 user2 = insert(:user)
36 {:ok, _activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
37 {:ok, _activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
38 {:ok, _activity3} = CommonAPI.post(user2, %{status: "HIE @#{user1.nickname}"})
39
40 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
41
42 [response1, response2] =
43 conn
44 |> post("/api/v1/pleroma/notifications/read?max_id=#{notification2.id}")
45 |> json_response_and_validate_schema(:ok)
46
47 assert %{"pleroma" => %{"is_seen" => true}} = response1
48 assert %{"pleroma" => %{"is_seen" => true}} = response2
49 assert Repo.get(Notification, notification1.id).seen
50 assert Repo.get(Notification, notification2.id).seen
51 refute Repo.get(Notification, notification3.id).seen
52 end
53
54 test "it returns error when notification not found", %{conn: conn} do
55 response =
56 conn
57 |> post("/api/v1/pleroma/notifications/read?id=22222222222222")
58 |> json_response_and_validate_schema(:bad_request)
59
60 assert response == %{"error" => "Cannot get notification"}
61 end
62 end
63 end