twitter_api_test.exs 12.8 KB
Newer Older
lain's avatar
lain committed
1
2
3
4
defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
  use Pleroma.DataCase
  alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
  alias Pleroma.Web.TwitterAPI.TwitterAPI
lain's avatar
lain committed
5
  alias Pleroma.{Activity, User, Object, Repo}
lain's avatar
lain committed
6
  alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
lain's avatar
lain committed
7
  alias Pleroma.Web.ActivityPub.ActivityPub
lain's avatar
lain committed
8

lain's avatar
lain committed
9
10
  import Pleroma.Factory

lain's avatar
lain committed
11
  test "create a status" do
12
13
14
    user = UserBuilder.build(%{ap_id: "142344"})
    _mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})

lain's avatar
lain committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    object_data = %{
      "type" => "Image",
      "url" => [
        %{
          "type" => "Link",
          "mediaType" => "image/jpg",
          "href" => "http://example.org/image.jpg"
        }
      ],
      "uuid" => 1
    }

    object = Repo.insert!(%Object{data: object_data})

lain's avatar
lain committed
29
    input = %{
lain's avatar
lain committed
30
      "status" => "Hello again, @shp.<script></script>\nThis is on another line.",
lain's avatar
lain committed
31
      "media_ids" => [object.id]
lain's avatar
lain committed
32
33
34
35
    }

    { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)

lain's avatar
lain committed
36
    assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>.<br>This is on another line."
lain's avatar
lain committed
37
    assert get_in(activity.data, ["object", "type"]) == "Note"
lain's avatar
lain committed
38
    assert get_in(activity.data, ["object", "actor"]) == user.ap_id
39
    assert get_in(activity.data, ["actor"]) == user.ap_id
lain's avatar
lain committed
40
41
    assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
    assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
42
    assert Enum.member?(get_in(activity.data, ["to"]), "shp")
lain's avatar
lain committed
43

lain's avatar
lain committed
44
    # Add a context
lain's avatar
lain committed
45
46
    assert is_binary(get_in(activity.data, ["context"]))
    assert is_binary(get_in(activity.data, ["object", "context"]))
lain's avatar
lain committed
47

48
    assert is_list(activity.data["object"]["attachment"])
49
50

    assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
lain's avatar
lain committed
51
52
53
  end

  test "create a status that is a reply" do
54
    user = UserBuilder.build(%{ap_id: "some_cool_id"})
lain's avatar
lain committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    input = %{
      "status" => "Hello again."
    }

    { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)

    input = %{
      "status" => "Here's your (you).",
      "in_reply_to_status_id" => activity.id
    }

    { :ok, reply = %Activity{} } = TwitterAPI.create_status(user, input)

    assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
    assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
    assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
    assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
72
    assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id")
lain's avatar
lain committed
73
74
  end

lain's avatar
lain committed
75
  test "fetch public statuses" do
lain's avatar
lain committed
76
    %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
lain's avatar
lain committed
77
78

    follower = insert(:user, following: [User.ap_followers(user)])
79
80

    statuses = TwitterAPI.fetch_public_statuses(follower)
lain's avatar
lain committed
81
82

    assert length(statuses) == 1
83
    assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
lain's avatar
lain committed
84
  end
lain's avatar
lain committed
85
86

  test "fetch friends' statuses" do
lain's avatar
lain committed
87
    user = insert(:user, %{following: ["someguy/followers"]})
lain's avatar
lain committed
88
    {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]})
lain's avatar
lain committed
89
    {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
lain's avatar
lain committed
90
91
92
93

    statuses = TwitterAPI.fetch_friend_statuses(user)

    activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
lain's avatar
lain committed
94
    direct_activity_user = Repo.get_by(User, ap_id: direct_activity.data["actor"])
lain's avatar
lain committed
95

96
    assert length(statuses) == 2
lain's avatar
lain committed
97
    assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user})
lain's avatar
lain committed
98
    assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
lain's avatar
lain committed
99
  end
lain's avatar
lain committed
100

dtluna's avatar
dtluna committed
101
102
103
104
105
106
107
108
109
  test "fetch user's mentions" do
    user = insert(:user)
    {:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
    activity_user = Repo.get_by(User, ap_id: activity.data["actor"])

    statuses = TwitterAPI.fetch_mentions(user)

    assert length(statuses) == 1
    assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]})
lain's avatar
lain committed
110
  end
lain's avatar
lain committed
111

112
113
114
115
  test "get a user by params" do
    user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
    {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})

dtluna's avatar
dtluna committed
116
    assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil)
117
118
119
120
121
122
123
124
125
    assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
    assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
    assert user1_result == TwitterAPI.get_user(user1, nil)
    assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
    assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
    assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
    assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
  end

dtluna's avatar
dtluna committed
126
  test "fetch user's statuses" do
127
128
    {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
    {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
dtluna's avatar
dtluna committed
129
130
131
132

    {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
    {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})

133
    user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
dtluna's avatar
dtluna committed
134
135
136
137

    assert length(user1_statuses) == 1
    assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})

138
    user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
dtluna's avatar
dtluna committed
139
140
141
142
143

    assert length(user2_statuses) == 1
    assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
  end

lain's avatar
lain committed
144
145
146
147
148
149
150
151
152
153
  test "fetch a single status" do
    {:ok, activity} = ActivityBuilder.insert()
    {:ok, user} = UserBuilder.insert()
    actor = Repo.get_by!(User, ap_id: activity.data["actor"])

    status = TwitterAPI.fetch_status(user, activity.id)

    assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor})
  end

154
  test "Follow another user using user_id" do
lain's avatar
lain committed
155
    user = insert(:user)
156
    followed = insert(:user)
lain's avatar
lain committed
157

158
159
    {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"user_id" => followed.id})
    assert user.following == [User.ap_followers(followed)]
lain's avatar
lain committed
160

161
162
    { :error, msg } = TwitterAPI.follow(user, %{"user_id" => followed.id})
    assert msg == "Could not follow user: #{followed.nickname} is already on your list."
163
164
165
  end

  test "Follow another user using screen_name" do
166
    user = insert(:user)
167
    followed = insert(:user)
lain's avatar
lain committed
168

169
    {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
170
171
    assert user.following == [User.ap_followers(followed)]

172
    { :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
173
    assert msg == "Could not follow user: #{followed.nickname} is already on your list."
lain's avatar
lain committed
174
  end
lain's avatar
lain committed
175

176
177
178
  test "Unfollow another user using user_id" do
    unfollowed = insert(:user)
    user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
179

180
    {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
181
    assert user.following == []
lain's avatar
lain committed
182

183
184
    { :error, msg } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
    assert msg == "Not subscribed!"
185
  end
lain's avatar
lain committed
186

187
  test "Unfollow another user using screen_name" do
188
189
    unfollowed = insert(:user)
    user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
lain's avatar
lain committed
190

191
    {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
lain's avatar
lain committed
192
    assert user.following == []
193
194

    { :error, msg } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
195
    assert msg == "Not subscribed!"
lain's avatar
lain committed
196
  end
197
198
199

  test "fetch statuses in a context using the conversation id" do
    {:ok, user} = UserBuilder.insert()
200
201
    {:ok, activity} = ActivityBuilder.insert(%{"context" => "2hu"})
    {:ok, activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
202
203
    {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})

204
205
206
    {:ok, object} = Object.context_mapping("2hu") |> Repo.insert

    statuses = TwitterAPI.fetch_conversation(user, object.id)
207
208
209
210
211

    assert length(statuses) == 2
    assert Enum.at(statuses, 0)["id"] == activity.id
    assert Enum.at(statuses, 1)["id"] == activity_two.id
  end
lain's avatar
lain committed
212
213
214
215
216
217
218
219

  test "upload a file" do
    file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}

    response = TwitterAPI.upload(file)

    assert is_binary(response)
  end
220
221
222
223

  test "it can parse mentions and return the relevant users" do
    text = "@gsimg According to @archaeme , that is @daggsy."

lain's avatar
lain committed
224
225
    gsimg = insert(:user, %{nickname: "gsimg"})
    archaeme = insert(:user, %{nickname: "archaeme"})
226
227
228
229
230
231
232
233
234
235
236
237

    expected_result = [
      {"@gsimg", gsimg},
      {"@archaeme", archaeme}
    ]

    assert TwitterAPI.parse_mentions(text) == expected_result
  end

  test "it adds user links to an existing text" do
    text = "@gsimg According to @archaeme , that is @daggsy."

lain's avatar
lain committed
238
239
    gsimg = insert(:user, %{nickname: "gsimg"})
    archaeme = insert(:user, %{nickname: "archaeme"})
240
241

    mentions = TwitterAPI.parse_mentions(text)
lain's avatar
lain committed
242
    expected_text = "<a href='#{gsimg.ap_id}'>@gsimg</a> According to <a href='#{archaeme.ap_id}'>@archaeme</a> , that is @daggsy."
243
244
245

    assert TwitterAPI.add_user_links(text, mentions) == expected_text
  end
lain's avatar
lain committed
246
247
248
249
250
251
252
253
254
255
256

  test "it favorites a status, returns the updated status" do
    user = insert(:user)
    note_activity = insert(:note_activity)
    activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])

    {:ok, status} = TwitterAPI.favorite(user, note_activity)
    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])

    assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
  end
lain's avatar
lain committed
257

lain's avatar
lain committed
258
259
260
261
262
263
  test "it unfavorites a status, returns the updated status" do
    user = insert(:user)
    note_activity = insert(:note_activity)
    activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
    object = Object.get_by_ap_id(note_activity.data["object"]["id"])

lain's avatar
lain committed
264
    {:ok, _like_activity, _object } = ActivityPub.like(user, object)
lain's avatar
lain committed
265
266
267
268
269
270
271
272
    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
    assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1

    {:ok, status} = TwitterAPI.unfavorite(user, note_activity)

    assert status["fave_num"] == 0
  end

lain's avatar
lain committed
273
274
275
276
277
278
279
280
281
282
283
  test "it retweets a status and returns the retweet" do
    user = insert(:user)
    note_activity = insert(:note_activity)
    activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])

    {:ok, status} = TwitterAPI.retweet(user, note_activity)
    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])

    assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
  end

lain's avatar
lain committed
284
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
311
312
313
314
  test "it registers a new user and returns the user." do
    data = %{
      "nickname" => "lain",
      "email" => "lain@wired.jp",
      "fullname" => "lain iwakura",
      "bio" => "close the world.",
      "password" => "bear",
      "confirm" => "bear"
    }

    {:ok, user} = TwitterAPI.register_user(data)

    fetched_user = Repo.get_by(User, nickname: "lain")
    assert user == UserRepresenter.to_map(fetched_user)
  end

  test "it returns the error on registration problems" do
    data = %{
      "nickname" => "lain",
      "email" => "lain@wired.jp",
      "fullname" => "lain iwakura",
      "bio" => "close the world.",
      "password" => "bear"
    }

    {:error, error_object} = TwitterAPI.register_user(data)

    assert is_binary(error_object[:error])
    refute Repo.get_by(User, nickname: "lain")
  end

315
316
317
318
319
320
321
322
  test "it assigns an integer conversation_id" do
    note_activity = insert(:note_activity)
    user = User.get_cached_by_ap_id(note_activity.data["actor"])
    status = ActivityRepresenter.to_map(note_activity, %{user: user})

    assert is_number(status["statusnet_conversation_id"])
  end

lain's avatar
lain committed
323
  setup do
324
325
    Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
    Supervisor.restart_child(Pleroma.Supervisor, Cachex)
lain's avatar
lain committed
326
327
    :ok
  end
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

  describe "context_to_conversation_id" do
    test "creates a mapping object" do
      conversation_id = TwitterAPI.context_to_conversation_id("random context")
      object = Object.get_by_ap_id("random context")

      assert conversation_id == object.id
    end

    test "returns an existing mapping for an existing object" do
      {:ok, object} = Object.context_mapping("random context") |> Repo.insert
      conversation_id = TwitterAPI.context_to_conversation_id("random context")

      assert conversation_id == object.id
    end
  end
lain's avatar
lain committed
344
end