aboutsummaryrefslogtreecommitdiff
path: root/test/web/pleroma_api/controllers/subscription_notification_controller_test.exs
blob: c6a71732d060f392b7f28eccdc4b4b43482903a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationControllerTest do
  use Pleroma.Web.ConnCase

  alias Pleroma.Repo
  alias Pleroma.SubscriptionNotification
  alias Pleroma.User
  alias Pleroma.Web.CommonAPI
  import Pleroma.Factory
  import Tesla.Mock

  setup do
    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end

  clear_config([:instance, :public])
  clear_config([:rich_media, :enabled])

  describe "subscription_notifications" do
    setup do
      user = insert(:user)
      subscriber = insert(:user)

      User.subscribe(subscriber, user)

      {:ok, %{user: user, subscriber: subscriber}}
    end

    test "list of notifications", %{conn: conn, user: user, subscriber: subscriber} do
      status_text = "Hello"
      {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text})
      path = subscription_notification_path(conn, :index)

      conn =
        conn
        |> assign(:user, subscriber)
        |> get(path)

      assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
      assert response == status_text
    end

    test "getting a single notification", %{conn: conn, user: user, subscriber: subscriber} do
      status_text = "Hello"

      {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text})
      [notification] = Repo.all(SubscriptionNotification)

      path = subscription_notification_path(conn, :show, notification)

      conn =
        conn
        |> assign(:user, subscriber)
        |> get(path)

      assert %{"status" => %{"content" => response}} = json_response(conn, 200)
      assert response == status_text
    end

    test "dismissing a single notification also deletes it", %{
      conn: conn,
      user: user,
      subscriber: subscriber
    } do
      status_text = "Hello"
      {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text})

      [notification] = Repo.all(SubscriptionNotification)

      conn =
        conn
        |> assign(:user, subscriber)
        |> post(subscription_notification_path(conn, :dismiss), %{"id" => notification.id})

      assert %{} = json_response(conn, 200)

      assert Repo.all(SubscriptionNotification) == []
    end

    test "clearing all notifications also deletes them", %{
      conn: conn,
      user: user,
      subscriber: subscriber
    } do
      status_text1 = "Hello"
      status_text2 = "Hello again"
      {:ok, _activity1} = CommonAPI.post(user, %{"status" => status_text1})
      {:ok, _activity2} = CommonAPI.post(user, %{"status" => status_text2})

      conn =
        conn
        |> assign(:user, subscriber)
        |> post(subscription_notification_path(conn, :clear))

      assert %{} = json_response(conn, 200)

      conn =
        build_conn()
        |> assign(:user, subscriber)
        |> get(subscription_notification_path(conn, :index))

      assert json_response(conn, 200) == []

      assert Repo.all(SubscriptionNotification) == []
    end

    test "paginates notifications using min_id, since_id, max_id, and limit", %{
      conn: conn,
      user: user,
      subscriber: subscriber
    } do
      {:ok, activity1} = CommonAPI.post(user, %{"status" => "Hello 1"})
      {:ok, activity2} = CommonAPI.post(user, %{"status" => "Hello 2"})
      {:ok, activity3} = CommonAPI.post(user, %{"status" => "Hello 3"})
      {:ok, activity4} = CommonAPI.post(user, %{"status" => "Hello 4"})

      notification1_id =
        Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string()

      notification2_id =
        Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string()

      notification3_id =
        Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string()

      notification4_id =
        Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string()

      conn = assign(conn, :user, subscriber)

      # min_id
      conn_res =
        get(
          conn,
          subscription_notification_path(conn, :index, %{
            "limit" => 2,
            "min_id" => notification1_id
          })
        )

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result

      # since_id
      conn_res =
        get(
          conn,
          subscription_notification_path(conn, :index, %{
            "limit" => 2,
            "since_id" => notification1_id
          })
        )

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result

      # max_id
      conn_res =
        get(
          conn,
          subscription_notification_path(conn, :index, %{
            "limit" => 2,
            "max_id" => notification4_id
          })
        )

      result = json_response(conn_res, 200)
      assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
    end

    test "destroy multiple", %{conn: conn, user: user1, subscriber: user2} do
      # mutual subscription
      User.subscribe(user1, user2)

      {:ok, activity1} = CommonAPI.post(user1, %{"status" => "Hello 1"})
      {:ok, activity2} = CommonAPI.post(user1, %{"status" => "World 1"})
      {:ok, activity3} = CommonAPI.post(user2, %{"status" => "Hello 2"})
      {:ok, activity4} = CommonAPI.post(user2, %{"status" => "World 2"})

      notification1_id =
        Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string()

      notification2_id =
        Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string()

      notification3_id =
        Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string()

      notification4_id =
        Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string()

      conn = assign(conn, :user, user1)

      conn_res = get(conn, subscription_notification_path(conn, :index))

      result = json_response(conn_res, 200)

      Enum.each(result, fn %{"id" => id} ->
        assert id in [notification3_id, notification4_id]
      end)

      conn2 = assign(conn, :user, user2)

      conn_res = get(conn2, subscription_notification_path(conn, :index))

      result = json_response(conn_res, 200)

      Enum.each(result, fn %{"id" => id} ->
        assert id in [notification1_id, notification2_id]
      end)

      conn_destroy =
        delete(conn, subscription_notification_path(conn, :destroy_multiple), %{
          "ids" => [notification3_id, notification4_id]
        })

      assert json_response(conn_destroy, 200) == %{}

      conn_res = get(conn2, subscription_notification_path(conn, :index))

      result = json_response(conn_res, 200)

      Enum.each(result, fn %{"id" => id} ->
        assert id in [notification1_id, notification2_id]
      end)

      assert length(Repo.all(SubscriptionNotification)) == 2
    end
  end
end