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