Merge remote-tracking branch 'origin/develop' into features/mastoapi/2.6.0-conversations
[akkoma] / test / web / twitter_api / twitter_api_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Object
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.UserInviteToken
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.TwitterAPI.ActivityView
14 alias Pleroma.Web.TwitterAPI.TwitterAPI
15 alias Pleroma.Web.TwitterAPI.UserView
16
17 import Pleroma.Factory
18
19 test "create a status" do
20 user = insert(:user)
21 mentioned_user = insert(:user, %{nickname: "shp", ap_id: "shp"})
22
23 object_data = %{
24 "type" => "Image",
25 "url" => [
26 %{
27 "type" => "Link",
28 "mediaType" => "image/jpg",
29 "href" => "http://example.org/image.jpg"
30 }
31 ],
32 "uuid" => 1
33 }
34
35 object = Repo.insert!(%Object{data: object_data})
36
37 input = %{
38 "status" =>
39 "Hello again, @shp.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric",
40 "media_ids" => [object.id]
41 }
42
43 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
44
45 expected_text =
46 "Hello again, <span class='h-card'><a data-user='#{mentioned_user.id}' class='u-url mention' href='shp'>@<span>shp</span></a></span>.&lt;script&gt;&lt;/script&gt;<br>This is on another :moominmamma: line. <a class='hashtag' data-tag='2hu' href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a> <a class='hashtag' data-tag='epic' href='http://localhost:4001/tag/epic' rel='tag'>#epic</a> <a class='hashtag' data-tag='phantasmagoric' href='http://localhost:4001/tag/phantasmagoric' rel='tag'>#phantasmagoric</a><br><a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>"
47
48 assert get_in(activity.data, ["object", "content"]) == expected_text
49 assert get_in(activity.data, ["object", "type"]) == "Note"
50 assert get_in(activity.data, ["object", "actor"]) == user.ap_id
51 assert get_in(activity.data, ["actor"]) == user.ap_id
52 assert Enum.member?(get_in(activity.data, ["cc"]), User.ap_followers(user))
53
54 assert Enum.member?(
55 get_in(activity.data, ["to"]),
56 "https://www.w3.org/ns/activitystreams#Public"
57 )
58
59 assert Enum.member?(get_in(activity.data, ["to"]), "shp")
60 assert activity.local == true
61
62 assert %{"moominmamma" => "http://localhost:4001/finmoji/128px/moominmamma-128.png"} =
63 activity.data["object"]["emoji"]
64
65 # hashtags
66 assert activity.data["object"]["tag"] == ["2hu", "epic", "phantasmagoric"]
67
68 # Add a context
69 assert is_binary(get_in(activity.data, ["context"]))
70 assert is_binary(get_in(activity.data, ["object", "context"]))
71
72 assert is_list(activity.data["object"]["attachment"])
73
74 assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
75
76 user = User.get_by_ap_id(user.ap_id)
77
78 assert user.info.note_count == 1
79 end
80
81 test "create a status that is a reply" do
82 user = insert(:user)
83
84 input = %{
85 "status" => "Hello again."
86 }
87
88 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
89
90 input = %{
91 "status" => "Here's your (you).",
92 "in_reply_to_status_id" => activity.id
93 }
94
95 {:ok, reply = %Activity{}} = TwitterAPI.create_status(user, input)
96
97 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
98
99 assert get_in(reply.data, ["object", "context"]) ==
100 get_in(activity.data, ["object", "context"])
101
102 assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
103 assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
104 end
105
106 test "Follow another user using user_id" do
107 user = insert(:user)
108 followed = insert(:user)
109
110 {:ok, user, followed, _activity} = TwitterAPI.follow(user, %{"user_id" => followed.id})
111 assert User.ap_followers(followed) in user.following
112
113 {:error, msg} = TwitterAPI.follow(user, %{"user_id" => followed.id})
114 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
115 end
116
117 test "Follow another user using screen_name" do
118 user = insert(:user)
119 followed = insert(:user)
120
121 {:ok, user, followed, _activity} =
122 TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
123
124 assert User.ap_followers(followed) in user.following
125
126 followed = User.get_by_ap_id(followed.ap_id)
127 assert followed.info.follower_count == 1
128
129 {:error, msg} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
130 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
131 end
132
133 test "Unfollow another user using user_id" do
134 unfollowed = insert(:user)
135 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
136 ActivityPub.follow(user, unfollowed)
137
138 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
139 assert user.following == []
140
141 {:error, msg} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
142 assert msg == "Not subscribed!"
143 end
144
145 test "Unfollow another user using screen_name" do
146 unfollowed = insert(:user)
147 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
148
149 ActivityPub.follow(user, unfollowed)
150
151 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
152 assert user.following == []
153
154 {:error, msg} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
155 assert msg == "Not subscribed!"
156 end
157
158 test "Block another user using user_id" do
159 user = insert(:user)
160 blocked = insert(:user)
161
162 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
163 assert User.blocks?(user, blocked)
164 end
165
166 test "Block another user using screen_name" do
167 user = insert(:user)
168 blocked = insert(:user)
169
170 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
171 assert User.blocks?(user, blocked)
172 end
173
174 test "Unblock another user using user_id" do
175 unblocked = insert(:user)
176 user = insert(:user)
177 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id})
178
179 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
180 assert user.info.blocks == []
181 end
182
183 test "Unblock another user using screen_name" do
184 unblocked = insert(:user)
185 user = insert(:user)
186 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname})
187
188 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
189 assert user.info.blocks == []
190 end
191
192 test "upload a file" do
193 user = insert(:user)
194
195 file = %Plug.Upload{
196 content_type: "image/jpg",
197 path: Path.absname("test/fixtures/image.jpg"),
198 filename: "an_image.jpg"
199 }
200
201 response = TwitterAPI.upload(file, user)
202
203 assert is_binary(response)
204 end
205
206 test "it favorites a status, returns the updated activity" do
207 user = insert(:user)
208 other_user = insert(:user)
209 note_activity = insert(:note_activity)
210
211 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
212 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
213 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 1
214
215 object = Object.normalize(note_activity.data["object"])
216
217 assert object.data["like_count"] == 1
218
219 assert status == updated_activity
220
221 {:ok, _status} = TwitterAPI.fav(other_user, note_activity.id)
222
223 object = Object.normalize(note_activity.data["object"])
224
225 assert object.data["like_count"] == 2
226
227 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
228 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 2
229 end
230
231 test "it unfavorites a status, returns the updated activity" do
232 user = insert(:user)
233 note_activity = insert(:note_activity)
234 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
235
236 {:ok, _like_activity, _object} = ActivityPub.like(user, object)
237 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
238
239 assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1
240
241 {:ok, activity} = TwitterAPI.unfav(user, note_activity.id)
242
243 assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 0
244 end
245
246 test "it retweets a status and returns the retweet" do
247 user = insert(:user)
248 note_activity = insert(:note_activity)
249
250 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
251 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
252
253 assert status == updated_activity
254 end
255
256 test "it unretweets an already retweeted status" do
257 user = insert(:user)
258 note_activity = insert(:note_activity)
259
260 {:ok, _status} = TwitterAPI.repeat(user, note_activity.id)
261 {:ok, status} = TwitterAPI.unrepeat(user, note_activity.id)
262 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
263
264 assert status == updated_activity
265 end
266
267 test "it registers a new user and returns the user." do
268 data = %{
269 "nickname" => "lain",
270 "email" => "lain@wired.jp",
271 "fullname" => "lain iwakura",
272 "password" => "bear",
273 "confirm" => "bear"
274 }
275
276 {:ok, user} = TwitterAPI.register_user(data)
277
278 fetched_user = Repo.get_by(User, nickname: "lain")
279
280 assert UserView.render("show.json", %{user: user}) ==
281 UserView.render("show.json", %{user: fetched_user})
282 end
283
284 test "it registers a new user with empty string in bio and returns the user." do
285 data = %{
286 "nickname" => "lain",
287 "email" => "lain@wired.jp",
288 "fullname" => "lain iwakura",
289 "bio" => "",
290 "password" => "bear",
291 "confirm" => "bear"
292 }
293
294 {:ok, user} = TwitterAPI.register_user(data)
295
296 fetched_user = Repo.get_by(User, nickname: "lain")
297
298 assert UserView.render("show.json", %{user: user}) ==
299 UserView.render("show.json", %{user: fetched_user})
300 end
301
302 @moduletag skip: "needs 'account_activation_required: true' in config"
303 test "it sends confirmation email if :account_activation_required is specified in instance config" do
304 setting = Pleroma.Config.get([:instance, :account_activation_required])
305
306 unless setting do
307 Pleroma.Config.put([:instance, :account_activation_required], true)
308 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
309 end
310
311 data = %{
312 "nickname" => "lain",
313 "email" => "lain@wired.jp",
314 "fullname" => "lain iwakura",
315 "bio" => "",
316 "password" => "bear",
317 "confirm" => "bear"
318 }
319
320 {:ok, user} = TwitterAPI.register_user(data)
321
322 assert user.info.confirmation_pending
323
324 Swoosh.TestAssertions.assert_email_sent(Pleroma.UserEmail.account_confirmation_email(user))
325 end
326
327 test "it registers a new user and parses mentions in the bio" do
328 data1 = %{
329 "nickname" => "john",
330 "email" => "john@gmail.com",
331 "fullname" => "John Doe",
332 "bio" => "test",
333 "password" => "bear",
334 "confirm" => "bear"
335 }
336
337 {:ok, user1} = TwitterAPI.register_user(data1)
338
339 data2 = %{
340 "nickname" => "lain",
341 "email" => "lain@wired.jp",
342 "fullname" => "lain iwakura",
343 "bio" => "@john test",
344 "password" => "bear",
345 "confirm" => "bear"
346 }
347
348 {:ok, user2} = TwitterAPI.register_user(data2)
349
350 expected_text =
351 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
352
353 assert user2.bio == expected_text
354 end
355
356 @moduletag skip: "needs 'registrations_open: false' in config"
357 test "it registers a new user via invite token and returns the user." do
358 {:ok, token} = UserInviteToken.create_token()
359
360 data = %{
361 "nickname" => "vinny",
362 "email" => "pasta@pizza.vs",
363 "fullname" => "Vinny Vinesauce",
364 "bio" => "streamer",
365 "password" => "hiptofbees",
366 "confirm" => "hiptofbees",
367 "token" => token.token
368 }
369
370 {:ok, user} = TwitterAPI.register_user(data)
371
372 fetched_user = Repo.get_by(User, nickname: "vinny")
373 token = Repo.get_by(UserInviteToken, token: token.token)
374
375 assert token.used == true
376
377 assert UserView.render("show.json", %{user: user}) ==
378 UserView.render("show.json", %{user: fetched_user})
379 end
380
381 @moduletag skip: "needs 'registrations_open: false' in config"
382 test "it returns an error if invalid token submitted" do
383 data = %{
384 "nickname" => "GrimReaper",
385 "email" => "death@reapers.afterlife",
386 "fullname" => "Reaper Grim",
387 "bio" => "Your time has come",
388 "password" => "scythe",
389 "confirm" => "scythe",
390 "token" => "DudeLetMeInImAFairy"
391 }
392
393 {:error, msg} = TwitterAPI.register_user(data)
394
395 assert msg == "Invalid token"
396 refute Repo.get_by(User, nickname: "GrimReaper")
397 end
398
399 @moduletag skip: "needs 'registrations_open: false' in config"
400 test "it returns an error if expired token submitted" do
401 {:ok, token} = UserInviteToken.create_token()
402 UserInviteToken.mark_as_used(token.token)
403
404 data = %{
405 "nickname" => "GrimReaper",
406 "email" => "death@reapers.afterlife",
407 "fullname" => "Reaper Grim",
408 "bio" => "Your time has come",
409 "password" => "scythe",
410 "confirm" => "scythe",
411 "token" => token.token
412 }
413
414 {:error, msg} = TwitterAPI.register_user(data)
415
416 assert msg == "Expired token"
417 refute Repo.get_by(User, nickname: "GrimReaper")
418 end
419
420 test "it returns the error on registration problems" do
421 data = %{
422 "nickname" => "lain",
423 "email" => "lain@wired.jp",
424 "fullname" => "lain iwakura",
425 "bio" => "close the world.",
426 "password" => "bear"
427 }
428
429 {:error, error_object} = TwitterAPI.register_user(data)
430
431 assert is_binary(error_object[:error])
432 refute Repo.get_by(User, nickname: "lain")
433 end
434
435 test "it assigns an integer conversation_id" do
436 note_activity = insert(:note_activity)
437 status = ActivityView.render("activity.json", activity: note_activity)
438
439 assert is_number(status["statusnet_conversation_id"])
440 end
441
442 setup do
443 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
444 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
445 :ok
446 end
447
448 describe "fetching a user by uri" do
449 test "fetches a user by uri" do
450 id = "https://mastodon.social/users/lambadalambda"
451 user = insert(:user)
452 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
453 remote = User.get_by_ap_id(id)
454
455 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
456
457 # Also fetches the feed.
458 # assert Activity.get_create_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
459 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
460 end
461 end
462 end