Don't insert newlines to generated HTML.
[akkoma] / test / web / twitter_api / twitter_api_test.exs
1 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
2 use Pleroma.DataCase
3 alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
4 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.{Activity, User, Object, Repo}
7 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
8 alias Pleroma.Web.ActivityPub.ActivityPub
9
10 import Pleroma.Factory
11
12 test "create a status" do
13 user = insert(:user)
14 _mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
15
16 object_data = %{
17 "type" => "Image",
18 "url" => [
19 %{
20 "type" => "Link",
21 "mediaType" => "image/jpg",
22 "href" => "http://example.org/image.jpg"
23 }
24 ],
25 "uuid" => 1
26 }
27
28 object = Repo.insert!(%Object{data: object_data})
29
30 input = %{
31 "status" => "Hello again, @shp.<script></script>\nThis is on another line. #2hu #epic #phantasmagoric",
32 "media_ids" => [object.id]
33 }
34
35 { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
36
37 assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>.&lt;script&gt;&lt;/script&gt;<br>This is on another line. #2hu #epic #phantasmagoric<br><a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>"
38 assert get_in(activity.data, ["object", "type"]) == "Note"
39 assert get_in(activity.data, ["object", "actor"]) == user.ap_id
40 assert get_in(activity.data, ["actor"]) == user.ap_id
41 assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
42 assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
43 assert Enum.member?(get_in(activity.data, ["to"]), "shp")
44 assert activity.local == true
45
46 # hashtags
47 assert activity.data["object"]["tag"] == ["2hu", "epic", "phantasmagoric"]
48
49 # Add a context
50 assert is_binary(get_in(activity.data, ["context"]))
51 assert is_binary(get_in(activity.data, ["object", "context"]))
52
53 assert is_list(activity.data["object"]["attachment"])
54
55 assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
56
57 user = User.get_by_ap_id(user.ap_id)
58
59 assert user.info["note_count"] == 1
60 end
61
62 test "create a status that is a reply" do
63 user = insert(:user)
64 input = %{
65 "status" => "Hello again."
66 }
67
68 { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
69
70 input = %{
71 "status" => "Here's your (you).",
72 "in_reply_to_status_id" => activity.id
73 }
74
75 { :ok, reply = %Activity{} } = TwitterAPI.create_status(user, input)
76
77 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
78 assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
79 assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
80 assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
81 assert Enum.member?(get_in(reply.data, ["to"]), user.ap_id)
82 end
83
84 test "fetch public statuses, excluding remote ones." do
85 %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
86 insert(:note_activity, %{local: false})
87
88 follower = insert(:user, following: [User.ap_followers(user)])
89
90 statuses = TwitterAPI.fetch_public_statuses(follower)
91
92 assert length(statuses) == 1
93 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
94 end
95
96 test "fetch whole known network statuses" do
97 %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
98 insert(:note_activity, %{local: false})
99
100 follower = insert(:user, following: [User.ap_followers(user)])
101
102 statuses = TwitterAPI.fetch_public_and_external_statuses(follower)
103
104 assert length(statuses) == 2
105 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
106 end
107
108 test "fetch friends' statuses" do
109 user = insert(:user, %{following: ["someguy/followers"]})
110 {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]})
111 {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
112
113 statuses = TwitterAPI.fetch_friend_statuses(user)
114
115 activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
116 direct_activity_user = Repo.get_by(User, ap_id: direct_activity.data["actor"])
117
118 assert length(statuses) == 2
119 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user})
120 assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
121 end
122
123 test "fetch user's mentions" do
124 user = insert(:user)
125 {:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
126 activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
127
128 statuses = TwitterAPI.fetch_mentions(user)
129
130 assert length(statuses) == 1
131 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]})
132 end
133
134 test "get a user by params" do
135 user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
136 {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
137
138 assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil)
139 assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
140 assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.nickname})
141 assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
142 assert user1_result == TwitterAPI.get_user(user1, nil)
143 assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
144 assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
145 assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
146 assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
147 end
148
149 test "fetch user's statuses" do
150 {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
151 {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
152
153 {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
154 {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
155
156 user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
157
158 assert length(user1_statuses) == 1
159 assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
160
161 user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
162
163 assert length(user2_statuses) == 1
164 assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
165 end
166
167 test "fetch a single status" do
168 {:ok, activity} = ActivityBuilder.insert()
169 {:ok, user} = UserBuilder.insert()
170 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
171
172 status = TwitterAPI.fetch_status(user, activity.id)
173
174 assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor})
175 end
176
177 test "Follow another user using user_id" do
178 user = insert(:user)
179 followed = insert(:user)
180
181 {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"user_id" => followed.id})
182 assert user.following == [User.ap_followers(followed)]
183
184 { :error, msg } = TwitterAPI.follow(user, %{"user_id" => followed.id})
185 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
186 end
187
188 test "Follow another user using screen_name" do
189 user = insert(:user)
190 followed = insert(:user)
191
192 {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
193 assert user.following == [User.ap_followers(followed)]
194
195 followed = User.get_by_ap_id(followed.ap_id)
196 assert followed.info["follower_count"] == 1
197
198 { :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
199 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
200 end
201
202 test "Unfollow another user using user_id" do
203 unfollowed = insert(:user)
204 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
205 ActivityPub.follow(user, unfollowed)
206
207 {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
208 assert user.following == []
209
210 { :error, msg } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
211 assert msg == "Not subscribed!"
212 end
213
214 test "Unfollow another user using screen_name" do
215 unfollowed = insert(:user)
216 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
217
218 ActivityPub.follow(user, unfollowed)
219
220 {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
221 assert user.following == []
222
223 { :error, msg } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
224 assert msg == "Not subscribed!"
225 end
226
227 test "Block another user using user_id" do
228 user = insert(:user)
229 blocked = insert(:user)
230
231 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
232 assert User.blocks?(user, blocked)
233 end
234
235 test "Block another user using screen_name" do
236 user = insert(:user)
237 blocked = insert(:user)
238
239 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
240 assert User.blocks?(user, blocked)
241 end
242
243 test "Unblock another user using user_id" do
244 unblocked = insert(:user)
245 user = insert(:user)
246 User.block(user, unblocked)
247
248 {:ok, user, unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
249 assert user.info["blocks"] == []
250 end
251
252 test "Unblock another user using screen_name" do
253 unblocked = insert(:user)
254 user = insert(:user)
255 User.block(user, unblocked)
256
257 {:ok, user, unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
258 assert user.info["blocks"] == []
259 end
260
261 test "fetch statuses in a context using the conversation id" do
262 {:ok, user} = UserBuilder.insert()
263 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
264 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
265 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
266
267 {:ok, object} = Object.context_mapping("2hu") |> Repo.insert
268
269 statuses = TwitterAPI.fetch_conversation(user, object.id)
270
271 assert length(statuses) == 2
272 assert Enum.at(statuses, 1)["id"] == activity.id
273 assert Enum.at(statuses, 0)["id"] == activity_two.id
274 end
275
276 test "upload a file" do
277 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
278
279 response = TwitterAPI.upload(file)
280
281 assert is_binary(response)
282 end
283
284 test "it adds user links to an existing text" do
285 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
286
287 gsimg = insert(:user, %{nickname: "gsimg"})
288 archaeme = insert(:user, %{nickname: "archaeme"})
289 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
290
291 mentions = Pleroma.Formatter.parse_mentions(text)
292 expected_text = "<a href='#{gsimg.ap_id}'>@gsimg</a> According to <a href='#{archaeme.ap_id}'>@archaeme</a>, that is @daggsy. Also hello <a href='#{archaeme_remote.ap_id}'>@archaeme</a>"
293
294 assert Utils.add_user_links(text, mentions) == expected_text
295 end
296
297 test "it favorites a status, returns the updated status" do
298 user = insert(:user)
299 note_activity = insert(:note_activity)
300 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
301
302 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
303 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
304
305 assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
306 end
307
308 test "it unfavorites a status, returns the updated status" do
309 user = insert(:user)
310 note_activity = insert(:note_activity)
311 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
312 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
313
314 {:ok, _like_activity, _object } = ActivityPub.like(user, object)
315 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
316 assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1
317
318 {:ok, status} = TwitterAPI.unfav(user, note_activity.id)
319
320 assert status["fave_num"] == 0
321 end
322
323 test "it retweets a status and returns the retweet" do
324 user = insert(:user)
325 note_activity = insert(:note_activity)
326 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
327
328 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
329 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
330
331 assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
332 end
333
334 test "it registers a new user and returns the user." do
335 data = %{
336 "nickname" => "lain",
337 "email" => "lain@wired.jp",
338 "fullname" => "lain iwakura",
339 "bio" => "close the world.",
340 "password" => "bear",
341 "confirm" => "bear"
342 }
343
344 {:ok, user} = TwitterAPI.register_user(data)
345
346 fetched_user = Repo.get_by(User, nickname: "lain")
347 assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user})
348 end
349
350 test "it returns the error on registration problems" do
351 data = %{
352 "nickname" => "lain",
353 "email" => "lain@wired.jp",
354 "fullname" => "lain iwakura",
355 "bio" => "close the world.",
356 "password" => "bear"
357 }
358
359 {:error, error_object} = TwitterAPI.register_user(data)
360
361 assert is_binary(error_object[:error])
362 refute Repo.get_by(User, nickname: "lain")
363 end
364
365 test "it assigns an integer conversation_id" do
366 note_activity = insert(:note_activity)
367 user = User.get_cached_by_ap_id(note_activity.data["actor"])
368 status = ActivityRepresenter.to_map(note_activity, %{user: user})
369
370 assert is_number(status["statusnet_conversation_id"])
371 end
372
373 setup do
374 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
375 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
376 :ok
377 end
378
379 describe "context_to_conversation_id" do
380 test "creates a mapping object" do
381 conversation_id = TwitterAPI.context_to_conversation_id("random context")
382 object = Object.get_by_ap_id("random context")
383
384 assert conversation_id == object.id
385 end
386
387 test "returns an existing mapping for an existing object" do
388 {:ok, object} = Object.context_mapping("random context") |> Repo.insert
389 conversation_id = TwitterAPI.context_to_conversation_id("random context")
390
391 assert conversation_id == object.id
392 end
393 end
394
395 describe "fetching a user by uri" do
396 test "fetches a user by uri" do
397 id = "https://mastodon.social/users/lambadalambda"
398 user = insert(:user)
399 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
400 remote = User.get_by_ap_id(id)
401
402 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
403
404 # Also fetches the feed.
405 assert Activity.get_create_activity_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
406 end
407 end
408 end