1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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
17 import Pleroma.Factory
19 test "create a status" do
21 mentioned_user = insert(:user, %{nickname: "shp", ap_id: "shp"})
28 "mediaType" => "image/jpg",
29 "href" => "http://example.org/image.jpg"
35 object = Repo.insert!(%Object{data: object_data})
39 "Hello again, @shp.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric",
40 "media_ids" => [object.id]
43 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
46 "Hello again, <span class='h-card'><a data-user='#{mentioned_user.id}' class='u-url mention' href='shp'>@<span>shp</span></a></span>.<script></script><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>"
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))
55 get_in(activity.data, ["to"]),
56 "https://www.w3.org/ns/activitystreams#Public"
59 assert Enum.member?(get_in(activity.data, ["to"]), "shp")
60 assert activity.local == true
62 assert %{"moominmamma" => "http://localhost:4001/finmoji/128px/moominmamma-128.png"} =
63 activity.data["object"]["emoji"]
66 assert activity.data["object"]["tag"] == ["2hu", "epic", "phantasmagoric"]
69 assert is_binary(get_in(activity.data, ["context"]))
70 assert is_binary(get_in(activity.data, ["object", "context"]))
72 assert is_list(activity.data["object"]["attachment"])
74 assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
76 user = User.get_by_ap_id(user.ap_id)
78 assert user.info.note_count == 1
81 test "create a status that is a reply" do
85 "status" => "Hello again."
88 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
91 "status" => "Here's your (you).",
92 "in_reply_to_status_id" => activity.id
95 {:ok, reply = %Activity{}} = TwitterAPI.create_status(user, input)
97 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
99 assert get_in(reply.data, ["object", "context"]) ==
100 get_in(activity.data, ["object", "context"])
102 assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
103 assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
106 test "Follow another user using user_id" do
108 followed = insert(:user)
110 {:ok, user, followed, _activity} = TwitterAPI.follow(user, %{"user_id" => followed.id})
111 assert User.ap_followers(followed) in user.following
113 {:error, msg} = TwitterAPI.follow(user, %{"user_id" => followed.id})
114 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
117 test "Follow another user using screen_name" do
119 followed = insert(:user)
121 {:ok, user, followed, _activity} =
122 TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
124 assert User.ap_followers(followed) in user.following
126 followed = User.get_by_ap_id(followed.ap_id)
127 assert followed.info.follower_count == 1
129 {:error, msg} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
130 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
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)
138 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
139 assert user.following == []
141 {:error, msg} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
142 assert msg == "Not subscribed!"
145 test "Unfollow another user using screen_name" do
146 unfollowed = insert(:user)
147 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
149 ActivityPub.follow(user, unfollowed)
151 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
152 assert user.following == []
154 {:error, msg} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
155 assert msg == "Not subscribed!"
158 test "Block another user using user_id" do
160 blocked = insert(:user)
162 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
163 assert User.blocks?(user, blocked)
166 test "Block another user using screen_name" do
168 blocked = insert(:user)
170 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
171 assert User.blocks?(user, blocked)
174 test "Unblock another user using user_id" do
175 unblocked = insert(:user)
177 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id})
179 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
180 assert user.info.blocks == []
183 test "Unblock another user using screen_name" do
184 unblocked = insert(:user)
186 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname})
188 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
189 assert user.info.blocks == []
192 test "upload a file" do
196 content_type: "image/jpg",
197 path: Path.absname("test/fixtures/image.jpg"),
198 filename: "an_image.jpg"
201 response = TwitterAPI.upload(file, user)
203 assert is_binary(response)
206 test "it favorites a status, returns the updated activity" do
208 other_user = insert(:user)
209 note_activity = insert(:note_activity)
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
215 object = Object.normalize(note_activity.data["object"])
217 assert object.data["like_count"] == 1
219 assert status == updated_activity
221 {:ok, _status} = TwitterAPI.fav(other_user, note_activity.id)
223 object = Object.normalize(note_activity.data["object"])
225 assert object.data["like_count"] == 2
227 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
228 assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 2
231 test "it unfavorites a status, returns the updated activity" do
233 note_activity = insert(:note_activity)
234 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
236 {:ok, _like_activity, _object} = ActivityPub.like(user, object)
237 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
239 assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1
241 {:ok, activity} = TwitterAPI.unfav(user, note_activity.id)
243 assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 0
246 test "it retweets a status and returns the retweet" do
248 note_activity = insert(:note_activity)
250 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
251 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
253 assert status == updated_activity
256 test "it unretweets an already retweeted status" do
258 note_activity = insert(:note_activity)
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"])
264 assert status == updated_activity
267 test "it registers a new user and returns the user." do
269 "nickname" => "lain",
270 "email" => "lain@wired.jp",
271 "fullname" => "lain iwakura",
272 "password" => "bear",
276 {:ok, user} = TwitterAPI.register_user(data)
278 fetched_user = User.get_by_nickname("lain")
280 assert UserView.render("show.json", %{user: user}) ==
281 UserView.render("show.json", %{user: fetched_user})
284 test "it registers a new user with empty string in bio and returns the user." do
286 "nickname" => "lain",
287 "email" => "lain@wired.jp",
288 "fullname" => "lain iwakura",
290 "password" => "bear",
294 {:ok, user} = TwitterAPI.register_user(data)
296 fetched_user = User.get_by_nickname("lain")
298 assert UserView.render("show.json", %{user: user}) ==
299 UserView.render("show.json", %{user: fetched_user})
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])
307 Pleroma.Config.put([:instance, :account_activation_required], true)
308 on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
312 "nickname" => "lain",
313 "email" => "lain@wired.jp",
314 "fullname" => "lain iwakura",
316 "password" => "bear",
320 {:ok, user} = TwitterAPI.register_user(data)
322 assert user.info.confirmation_pending
324 Swoosh.TestAssertions.assert_email_sent(Pleroma.UserEmail.account_confirmation_email(user))
327 test "it registers a new user and parses mentions in the bio" do
329 "nickname" => "john",
330 "email" => "john@gmail.com",
331 "fullname" => "John Doe",
333 "password" => "bear",
337 {:ok, user1} = TwitterAPI.register_user(data1)
340 "nickname" => "lain",
341 "email" => "lain@wired.jp",
342 "fullname" => "lain iwakura",
343 "bio" => "@john test",
344 "password" => "bear",
348 {:ok, user2} = TwitterAPI.register_user(data2)
351 "<span class='h-card'><a data-user='#{user1.id}' class='u-url mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
353 assert user2.bio == expected_text
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()
361 "nickname" => "vinny",
362 "email" => "pasta@pizza.vs",
363 "fullname" => "Vinny Vinesauce",
365 "password" => "hiptofbees",
366 "confirm" => "hiptofbees",
367 "token" => token.token
370 {:ok, user} = TwitterAPI.register_user(data)
372 fetched_user = User.get_by_nickname("vinny")
373 token = Repo.get_by(UserInviteToken, token: token.token)
375 assert token.used == true
377 assert UserView.render("show.json", %{user: user}) ==
378 UserView.render("show.json", %{user: fetched_user})
381 @moduletag skip: "needs 'registrations_open: false' in config"
382 test "it returns an error if invalid token submitted" do
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"
393 {:error, msg} = TwitterAPI.register_user(data)
395 assert msg == "Invalid token"
396 refute User.get_by_nickname("GrimReaper")
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)
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
414 {:error, msg} = TwitterAPI.register_user(data)
416 assert msg == "Expired token"
417 refute User.get_by_nickname("GrimReaper")
420 test "it returns the error on registration problems" do
422 "nickname" => "lain",
423 "email" => "lain@wired.jp",
424 "fullname" => "lain iwakura",
425 "bio" => "close the world.",
429 {:error, error_object} = TwitterAPI.register_user(data)
431 assert is_binary(error_object[:error])
432 refute User.get_by_nickname("lain")
435 test "it assigns an integer conversation_id" do
436 note_activity = insert(:note_activity)
437 status = ActivityView.render("activity.json", activity: note_activity)
439 assert is_number(status["statusnet_conversation_id"])
443 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
444 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
448 describe "fetching a user by uri" do
449 test "fetches a user by uri" do
450 id = "https://mastodon.social/users/lambadalambda"
452 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
453 remote = User.get_by_ap_id(id)
455 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
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