activity_view_test.exs 11.6 KB
Newer Older
kaniini's avatar
kaniini committed
1
2
3
4
# Pleroma: A lightweight social networking server
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5
6
7
8
9
10
11
12
defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
  use Pleroma.DataCase

  alias Pleroma.Web.CommonAPI
  alias Pleroma.Web.CommonAPI.Utils
  alias Pleroma.Web.TwitterAPI.ActivityView
  alias Pleroma.Web.TwitterAPI.UserView
  alias Pleroma.Web.TwitterAPI.TwitterAPI
lain's avatar
lain committed
13
14
  alias Pleroma.Repo
  alias Pleroma.Activity
lain's avatar
lain committed
15
16
  alias Pleroma.User
  alias Pleroma.Web.ActivityPub.ActivityPub
lain's avatar
lain committed
17

lain's avatar
lain committed
18
  import Pleroma.Factory
19
20
21
22
23
24
25
  import Tesla.Mock

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

lain's avatar
lain committed
26
  import Mock
lain's avatar
lain committed
27

28
  test "returns a temporary ap_id based user for activities missing db users" do
29
30
31
32
33
34
35
    user = insert(:user)

    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})

    Repo.delete(user)
    Cachex.clear(:user_cache)

36
37
38
39
40
    %{"user" => tw_user} = ActivityView.render("activity.json", activity: activity)

    assert tw_user["screen_name"] == "erroruser@example.com"
    assert tw_user["name"] == user.ap_id
    assert tw_user["statusnet_profile_url"] == user.ap_id
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  end

  test "tries to get a user by nickname if fetching by ap_id doesn't work" do
    user = insert(:user)

    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})

    {:ok, user} =
      user
      |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
      |> Repo.update()

    Cachex.clear(:user_cache)

    result = ActivityView.render("activity.json", activity: activity)
    assert result["user"]["id"] == user.id
  end

Maksim's avatar
Maksim committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  test "a create activity with a html status" do
    text = """
    #Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
    """

    {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})

    result = ActivityView.render("activity.json", activity: activity)

    assert result["statusnet_html"] ==
             "<a data-tag=\"bike\" href=\"http://localhost:4001/tag/bike\">#Bike</a> log - Commute Tuesday<br /><a href=\"https://pla.bike/posts/20181211/\">https://pla.bike/posts/20181211/</a><br /><a data-tag=\"cycling\" href=\"http://localhost:4001/tag/cycling\">#cycling</a> <a data-tag=\"chscycling\" href=\"http://localhost:4001/tag/chscycling\">#CHScycling</a> <a data-tag=\"commute\" href=\"http://localhost:4001/tag/commute\">#commute</a><br />MVIMG_20181211_054020.jpg"

    assert result["text"] ==
             "#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
  end

75
76
77
78
79
80
81
82
83
  test "a create activity with a summary containing emoji" do
    {:ok, activity} =
      CommonAPI.post(insert(:user), %{
        "spoiler_text" => ":woollysocks: meow",
        "status" => "."
      })

    result = ActivityView.render("activity.json", activity: activity)

84
85
86
    expected = ":woollysocks: meow"

    expected_html =
87
88
89
      "<img height=\"32px\" width=\"32px\" alt=\"woollysocks\" title=\"woollysocks\" src=\"http://localhost:4001/finmoji/128px/woollysocks-128.png\" /> meow"

    assert result["summary"] == expected
90
    assert result["summary_html"] == expected_html
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  end

  test "a create activity with a summary containing invalid HTML" do
    {:ok, activity} =
      CommonAPI.post(insert(:user), %{
        "spoiler_text" => "<span style=\"color: magenta; font-size: 32px;\">meow</span>",
        "status" => "."
      })

    result = ActivityView.render("activity.json", activity: activity)

    expected = "meow"

    assert result["summary"] == expected
105
    assert result["summary_html"] == expected
106
107
  end

lain's avatar
lain committed
108
109
110
111
  test "a create activity with a note" do
    user = insert(:user)
    other_user = insert(:user, %{nickname: "shp"})

lain's avatar
lain committed
112
    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
lain's avatar
lain committed
113
114
115
116
117
118
119
120
121
122
123

    result = ActivityView.render("activity.json", activity: activity)

    convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])

    expected = %{
      "activity_type" => "post",
      "attachments" => [],
      "attentions" => [
        UserView.render("show.json", %{user: other_user})
      ],
lain's avatar
lain committed
124
      "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
lain's avatar
lain committed
125
126
127
128
129
      "external_url" => activity.data["object"]["id"],
      "fave_num" => 0,
      "favorited" => false,
      "id" => activity.id,
      "in_reply_to_status_id" => nil,
130
      "in_reply_to_screen_name" => nil,
131
132
133
      "in_reply_to_user_id" => nil,
      "in_reply_to_profileurl" => nil,
      "in_reply_to_ostatus_uri" => nil,
lain's avatar
lain committed
134
135
136
137
138
139
      "is_local" => true,
      "is_post_verb" => true,
      "possibly_sensitive" => false,
      "repeat_num" => 0,
      "repeated" => false,
      "statusnet_conversation_id" => convo_id,
scarlett's avatar
scarlett committed
140
      "summary" => "",
141
      "summary_html" => "",
lain's avatar
lain committed
142
      "statusnet_html" =>
ValD's avatar
ValD committed
143
        "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
lain's avatar
lain committed
144
145
146
      "tags" => [],
      "text" => "Hey @shp!",
      "uri" => activity.data["object"]["id"],
lain's avatar
lain committed
147
      "user" => UserView.render("show.json", %{user: user}),
scarlett's avatar
scarlett committed
148
      "visibility" => "direct"
lain's avatar
lain committed
149
150
151
152
    }

    assert result == expected
  end
lain's avatar
lain committed
153

lain's avatar
lain committed
154
155
156
157
158
159
160
161
162
163
164
  test "a list of activities" do
    user = insert(:user)
    other_user = insert(:user, %{nickname: "shp"})
    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})

    convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])

    mocks = [
      {
        TwitterAPI,
        [],
lain's avatar
lain committed
165
166
167
168
169
170
        [context_to_conversation_id: fn _ -> false end]
      },
      {
        User,
        [:passthrough],
        [get_cached_by_ap_id: fn _ -> nil end]
lain's avatar
lain committed
171
172
173
174
175
176
177
178
      }
    ]

    with_mocks mocks do
      [result] = ActivityView.render("index.json", activities: [activity])

      assert result["statusnet_conversation_id"] == convo_id
      assert result["user"]
lain's avatar
lain committed
179
180
181
      refute called(TwitterAPI.context_to_conversation_id(:_))
      refute called(User.get_cached_by_ap_id(user.ap_id))
      refute called(User.get_cached_by_ap_id(other_user.ap_id))
lain's avatar
lain committed
182
183
184
    end
  end

lain's avatar
lain committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  test "an activity that is a reply" do
    user = insert(:user)
    other_user = insert(:user, %{nickname: "shp"})

    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})

    {:ok, answer} =
      CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})

    result = ActivityView.render("activity.json", %{activity: answer})

    assert result["in_reply_to_status_id"] == activity.id
  end

  test "a like activity" do
    user = insert(:user)
    other_user = insert(:user, %{nickname: "shp"})

    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
    {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)

    result = ActivityView.render("activity.json", activity: like)
207
    activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
lain's avatar
lain committed
208
209
210
211
212
213
214
215

    expected = %{
      "activity_type" => "like",
      "created_at" => like.data["published"] |> Utils.date_to_asctime(),
      "external_url" => like.data["id"],
      "id" => like.id,
      "in_reply_to_status_id" => activity.id,
      "is_local" => true,
HJ's avatar
HJ committed
216
      "is_post_verb" => false,
217
      "favorited_status" => ActivityView.render("activity.json", activity: activity),
HJ's avatar
HJ committed
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
      "statusnet_html" => "shp favorited a status.",
      "text" => "shp favorited a status.",
      "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
      "user" => UserView.render("show.json", user: other_user)
    }

    assert result == expected
  end

  test "a like activity for deleted post" do
    user = insert(:user)
    other_user = insert(:user, %{nickname: "shp"})

    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
    {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
    CommonAPI.delete(activity.id, user)

    result = ActivityView.render("activity.json", activity: like)

    expected = %{
      "activity_type" => "like",
      "created_at" => like.data["published"] |> Utils.date_to_asctime(),
      "external_url" => like.data["id"],
      "id" => like.id,
      "in_reply_to_status_id" => nil,
      "is_local" => true,
lain's avatar
lain committed
244
      "is_post_verb" => false,
245
      "favorited_status" => nil,
lain's avatar
lain committed
246
247
248
249
250
251
252
253
      "statusnet_html" => "shp favorited a status.",
      "text" => "shp favorited a status.",
      "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
      "user" => UserView.render("show.json", user: other_user)
    }

    assert result == expected
  end
lain's avatar
lain committed
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

  test "an announce activity" do
    user = insert(:user)
    other_user = insert(:user, %{nickname: "shp"})

    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
    {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)

    convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])

    activity = Repo.get(Activity, activity.id)

    result = ActivityView.render("activity.json", activity: announce)

    expected = %{
      "activity_type" => "repeat",
      "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
      "external_url" => announce.data["id"],
      "id" => announce.id,
      "is_local" => true,
      "is_post_verb" => false,
      "statusnet_html" => "shp retweeted a status.",
      "text" => "shp retweeted a status.",
      "uri" => "tag:#{announce.data["id"]}:objectType=note",
      "user" => UserView.render("show.json", user: other_user),
      "retweeted_status" => ActivityView.render("activity.json", activity: activity),
      "statusnet_conversation_id" => convo_id
    }

    assert result == expected
  end
lain's avatar
lain committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310

  test "A follow activity" do
    user = insert(:user)
    other_user = insert(:user, %{nickname: "shp"})

    {:ok, follower} = User.follow(user, other_user)
    {:ok, follow} = ActivityPub.follow(follower, other_user)

    result = ActivityView.render("activity.json", activity: follow)

    expected = %{
      "activity_type" => "follow",
      "attentions" => [],
      "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
      "external_url" => follow.data["id"],
      "id" => follow.id,
      "in_reply_to_status_id" => nil,
      "is_local" => true,
      "is_post_verb" => false,
      "statusnet_html" => "#{user.nickname} started following shp",
      "text" => "#{user.nickname} started following shp",
      "user" => UserView.render("show.json", user: user)
    }

    assert result == expected
  end
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

  test "a delete activity" do
    user = insert(:user)

    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
    {:ok, delete} = CommonAPI.delete(activity.id, user)

    result = ActivityView.render("activity.json", activity: delete)

    expected = %{
      "activity_type" => "delete",
      "attentions" => [],
      "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
      "external_url" => delete.data["id"],
      "id" => delete.id,
      "in_reply_to_status_id" => nil,
      "is_local" => true,
      "is_post_verb" => false,
      "statusnet_html" => "deleted notice {{tag",
      "text" => "deleted notice {{tag",
      "uri" => delete.data["object"],
      "user" => UserView.render("show.json", user: user)
    }

    assert result == expected
  end
337
338
339
340
341
342
343
344
345
346
347
348
349
350

  test "a peertube video" do
    {:ok, object} =
      ActivityPub.fetch_object_from_id(
        "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
      )

    %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])

    result = ActivityView.render("activity.json", activity: activity)

    assert length(result["attachments"]) == 1
    assert result["summary"] == "Friday Night"
  end
lain's avatar
lain committed
351
end