summaryrefslogtreecommitdiff
path: root/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs
blob: d9aa8ce5500aac8541414aff15b4adc42a284339 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

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

  alias Pleroma.Config
  alias Pleroma.Tests.ObanHelpers
  alias Pleroma.User
  alias Pleroma.Web.CommonAPI

  import Pleroma.Factory
  import Swoosh.TestAssertions

  describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
    setup do
      {:ok, user} =
        insert(:user)
        |> User.confirmation_changeset(set_confirmation: false)
        |> User.update_and_set_cache()

      refute user.is_confirmed

      [user: user]
    end

    setup do: clear_config([:instance, :account_activation_required], true)

    test "resend account confirmation email", %{conn: conn, user: user} do
      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
      |> json_response_and_validate_schema(:no_content)

      ObanHelpers.perform_all()

      email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
      notify_email = Config.get([:instance, :notify_email])
      instance_name = Config.get([:instance, :name])

      assert_email_sent(
        from: {instance_name, notify_email},
        to: {user.name, user.email},
        html_body: email.html_body
      )
    end

    test "resend account confirmation email (with nickname)", %{conn: conn, user: user} do
      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/v1/pleroma/accounts/confirmation_resend?nickname=#{user.nickname}")
      |> json_response_and_validate_schema(:no_content)

      ObanHelpers.perform_all()

      email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
      notify_email = Config.get([:instance, :notify_email])
      instance_name = Config.get([:instance, :name])

      assert_email_sent(
        from: {instance_name, notify_email},
        to: {user.name, user.email},
        html_body: email.html_body
      )
    end
  end

  describe "getting favorites timeline of specified user" do
    setup do
      [current_user, user] = insert_pair(:user, hide_favorites: false)
      %{user: current_user, conn: conn} = oauth_access(["read:favourites"], user: current_user)
      [current_user: current_user, user: user, conn: conn]
    end

    test "returns list of statuses favorited by specified user", %{
      conn: conn,
      user: user
    } do
      [activity | _] = insert_pair(:note_activity)
      CommonAPI.favorite(user, activity.id)

      response =
        conn
        |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
        |> json_response_and_validate_schema(:ok)

      [like] = response

      assert length(response) == 1
      assert like["id"] == activity.id
    end

    test "returns favorites for specified user_id when requester is not logged in", %{
      user: user
    } do
      activity = insert(:note_activity)
      CommonAPI.favorite(user, activity.id)

      response =
        build_conn()
        |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
        |> json_response_and_validate_schema(200)

      assert length(response) == 1
    end

    test "returns favorited DM only when user is logged in and he is one of recipients", %{
      current_user: current_user,
      user: user
    } do
      {:ok, direct} =
        CommonAPI.post(current_user, %{
          status: "Hi @#{user.nickname}!",
          visibility: "direct"
        })

      CommonAPI.favorite(user, direct.id)

      for u <- [user, current_user] do
        response =
          build_conn()
          |> assign(:user, u)
          |> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
          |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
          |> json_response_and_validate_schema(:ok)

        assert length(response) == 1
      end

      response =
        build_conn()
        |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
        |> json_response_and_validate_schema(200)

      assert length(response) == 0
    end

    test "does not return others' favorited DM when user is not one of recipients", %{
      conn: conn,
      user: user
    } do
      user_two = insert(:user)

      {:ok, direct} =
        CommonAPI.post(user_two, %{
          status: "Hi @#{user.nickname}!",
          visibility: "direct"
        })

      CommonAPI.favorite(user, direct.id)

      response =
        conn
        |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
        |> json_response_and_validate_schema(:ok)

      assert Enum.empty?(response)
    end

    test "paginates favorites using since_id and max_id", %{
      conn: conn,
      user: user
    } do
      activities = insert_list(10, :note_activity)

      Enum.each(activities, fn activity ->
        CommonAPI.favorite(user, activity.id)
      end)

      third_activity = Enum.at(activities, 2)
      seventh_activity = Enum.at(activities, 6)

      response =
        conn
        |> get(
          "/api/v1/pleroma/accounts/#{user.id}/favourites?since_id=#{third_activity.id}&max_id=#{seventh_activity.id}"
        )
        |> json_response_and_validate_schema(:ok)

      assert length(response) == 3
      refute third_activity in response
      refute seventh_activity in response
    end

    test "limits favorites using limit parameter", %{
      conn: conn,
      user: user
    } do
      7
      |> insert_list(:note_activity)
      |> Enum.each(fn activity ->
        CommonAPI.favorite(user, activity.id)
      end)

      response =
        conn
        |> get("/api/v1/pleroma/accounts/#{user.id}/favourites?limit=3")
        |> json_response_and_validate_schema(:ok)

      assert length(response) == 3
    end

    test "returns empty response when user does not have any favorited statuses", %{
      conn: conn,
      user: user
    } do
      response =
        conn
        |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
        |> json_response_and_validate_schema(:ok)

      assert Enum.empty?(response)
    end

    test "returns 404 error when specified user is not exist", %{conn: conn} do
      conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")

      assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
    end

    test "returns 403 error when user has hidden own favorites", %{conn: conn} do
      user = insert(:user, hide_favorites: true)
      activity = insert(:note_activity)
      CommonAPI.favorite(user, activity.id)

      conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")

      assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
    end

    test "hides favorites for new users by default", %{conn: conn} do
      user = insert(:user)
      activity = insert(:note_activity)
      CommonAPI.favorite(user, activity.id)

      assert user.hide_favorites
      conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")

      assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
    end
  end

  describe "subscribing / unsubscribing" do
    test "subscribing / unsubscribing to a user" do
      %{user: user, conn: conn} = oauth_access(["follow"])
      subscription_target = insert(:user)

      ret_conn =
        conn
        |> assign(:user, user)
        |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")

      assert %{"id" => _id, "subscribing" => true} =
               json_response_and_validate_schema(ret_conn, 200)

      conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")

      assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(conn, 200)
    end
  end

  describe "subscribing" do
    test "returns 404 when subscription_target not found" do
      %{conn: conn} = oauth_access(["write:follows"])

      conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")

      assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
    end
  end

  describe "unsubscribing" do
    test "returns 404 when subscription_target not found" do
      %{conn: conn} = oauth_access(["follow"])

      conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")

      assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
    end
  end

  describe "account endorsements" do
    test "returns a list of pinned accounts", %{conn: conn} do
      %{id: id1} = user1 = insert(:user)
      %{id: id2} = user2 = insert(:user)
      %{id: id3} = user3 = insert(:user)

      CommonAPI.follow(user1, user2)
      CommonAPI.follow(user1, user3)

      User.endorse(user1, user2)
      User.endorse(user1, user3)

      [%{"id" => ^id2}, %{"id" => ^id3}] =
        conn
        |> get("/api/v1/pleroma/accounts/#{id1}/endorsements")
        |> json_response_and_validate_schema(200)
    end

    test "returns 404 error when specified user is not exist", %{conn: conn} do
      conn = get(conn, "/api/v1/pleroma/accounts/test/endorsements")

      assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
    end
  end
end