s/Pleroma.UserEmail/Pleroma.Emails.UserEmail/
[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 = User.get_by_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 = User.get_by_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(
325 Pleroma.Emails.UserEmail.account_confirmation_email(user)
326 )
327 end
328
329 test "it registers a new user and parses mentions in the bio" do
330 data1 = %{
331 "nickname" => "john",
332 "email" => "john@gmail.com",
333 "fullname" => "John Doe",
334 "bio" => "test",
335 "password" => "bear",
336 "confirm" => "bear"
337 }
338
339 {:ok, user1} = TwitterAPI.register_user(data1)
340
341 data2 = %{
342 "nickname" => "lain",
343 "email" => "lain@wired.jp",
344 "fullname" => "lain iwakura",
345 "bio" => "@john test",
346 "password" => "bear",
347 "confirm" => "bear"
348 }
349
350 {:ok, user2} = TwitterAPI.register_user(data2)
351
352 expected_text =
353 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
354
355 assert user2.bio == expected_text
356 end
357
358 @moduletag skip: "needs 'registrations_open: false' in config"
359 test "it registers a new user via invite token and returns the user." do
360 {:ok, token} = UserInviteToken.create_token()
361
362 data = %{
363 "nickname" => "vinny",
364 "email" => "pasta@pizza.vs",
365 "fullname" => "Vinny Vinesauce",
366 "bio" => "streamer",
367 "password" => "hiptofbees",
368 "confirm" => "hiptofbees",
369 "token" => token.token
370 }
371
372 {:ok, user} = TwitterAPI.register_user(data)
373
374 fetched_user = User.get_by_nickname("vinny")
375 token = Repo.get_by(UserInviteToken, token: token.token)
376
377 assert token.used == true
378
379 assert UserView.render("show.json", %{user: user}) ==
380 UserView.render("show.json", %{user: fetched_user})
381 end
382
383 @moduletag skip: "needs 'registrations_open: false' in config"
384 test "it returns an error if invalid token submitted" do
385 data = %{
386 "nickname" => "GrimReaper",
387 "email" => "death@reapers.afterlife",
388 "fullname" => "Reaper Grim",
389 "bio" => "Your time has come",
390 "password" => "scythe",
391 "confirm" => "scythe",
392 "token" => "DudeLetMeInImAFairy"
393 }
394
395 {:error, msg} = TwitterAPI.register_user(data)
396
397 assert msg == "Invalid token"
398 refute User.get_by_nickname("GrimReaper")
399 end
400
401 @moduletag skip: "needs 'registrations_open: false' in config"
402 test "it returns an error if expired token submitted" do
403 {:ok, token} = UserInviteToken.create_token()
404 UserInviteToken.mark_as_used(token.token)
405
406 data = %{
407 "nickname" => "GrimReaper",
408 "email" => "death@reapers.afterlife",
409 "fullname" => "Reaper Grim",
410 "bio" => "Your time has come",
411 "password" => "scythe",
412 "confirm" => "scythe",
413 "token" => token.token
414 }
415
416 {:error, msg} = TwitterAPI.register_user(data)
417
418 assert msg == "Expired token"
419 refute User.get_by_nickname("GrimReaper")
420 end
421
422 test "it returns the error on registration problems" do
423 data = %{
424 "nickname" => "lain",
425 "email" => "lain@wired.jp",
426 "fullname" => "lain iwakura",
427 "bio" => "close the world.",
428 "password" => "bear"
429 }
430
431 {:error, error_object} = TwitterAPI.register_user(data)
432
433 assert is_binary(error_object[:error])
434 refute User.get_by_nickname("lain")
435 end
436
437 test "it assigns an integer conversation_id" do
438 note_activity = insert(:note_activity)
439 status = ActivityView.render("activity.json", activity: note_activity)
440
441 assert is_number(status["statusnet_conversation_id"])
442 end
443
444 setup do
445 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
446 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
447 :ok
448 end
449
450 describe "fetching a user by uri" do
451 test "fetches a user by uri" do
452 id = "https://mastodon.social/users/lambadalambda"
453 user = insert(:user)
454 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
455 remote = User.get_by_ap_id(id)
456
457 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
458
459 # Also fetches the feed.
460 # assert Activity.get_create_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
461 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
462 end
463 end
464 end