1 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
3 alias Pleroma.Builders.UserBuilder
4 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
5 alias Pleroma.{Activity, User, Object, Repo, UserInviteToken}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.TwitterAPI.ActivityView
11 test "create a status" do
13 _mentioned_user = insert(:user, %{nickname: "shp", ap_id: "shp"})
20 "mediaType" => "image/jpg",
21 "href" => "http://example.org/image.jpg"
27 object = Repo.insert!(%Object{data: object_data})
31 "Hello again, @shp.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric",
32 "media_ids" => [object.id]
35 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
38 "Hello again, <span><a class='mention' href='shp'>@<span>shp</span></a></span>.<script></script><br>This is on another :moominmamma: line. <a href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a> <a href='http://localhost:4001/tag/epic' rel='tag'>#epic</a> <a href='http://localhost:4001/tag/phantasmagoric' rel='tag'>#phantasmagoric</a><br><a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>"
40 assert get_in(activity.data, ["object", "content"]) == expected_text
41 assert get_in(activity.data, ["object", "type"]) == "Note"
42 assert get_in(activity.data, ["object", "actor"]) == user.ap_id
43 assert get_in(activity.data, ["actor"]) == user.ap_id
44 assert Enum.member?(get_in(activity.data, ["cc"]), User.ap_followers(user))
47 get_in(activity.data, ["to"]),
48 "https://www.w3.org/ns/activitystreams#Public"
51 assert Enum.member?(get_in(activity.data, ["to"]), "shp")
52 assert activity.local == true
54 assert %{"moominmamma" => "http://localhost:4001/finmoji/128px/moominmamma-128.png"} =
55 activity.data["object"]["emoji"]
58 assert activity.data["object"]["tag"] == ["2hu", "epic", "phantasmagoric"]
61 assert is_binary(get_in(activity.data, ["context"]))
62 assert is_binary(get_in(activity.data, ["object", "context"]))
64 assert is_list(activity.data["object"]["attachment"])
66 assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
68 user = User.get_by_ap_id(user.ap_id)
70 assert user.info.note_count == 1
73 test "create a status that is a reply" do
77 "status" => "Hello again."
80 {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input)
83 "status" => "Here's your (you).",
84 "in_reply_to_status_id" => activity.id
87 {:ok, reply = %Activity{}} = TwitterAPI.create_status(user, input)
89 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
91 assert get_in(reply.data, ["object", "context"]) ==
92 get_in(activity.data, ["object", "context"])
94 assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
95 assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
98 test "Follow another user using user_id" do
100 followed = insert(:user)
102 {:ok, user, followed, _activity} = TwitterAPI.follow(user, %{"user_id" => followed.id})
103 assert User.ap_followers(followed) in user.following
105 {:error, msg} = TwitterAPI.follow(user, %{"user_id" => followed.id})
106 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
109 test "Follow another user using screen_name" do
111 followed = insert(:user)
113 {:ok, user, followed, _activity} =
114 TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
116 assert User.ap_followers(followed) in user.following
118 followed = User.get_by_ap_id(followed.ap_id)
119 assert followed.info.follower_count == 1
121 {:error, msg} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
122 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
125 test "Unfollow another user using user_id" do
126 unfollowed = insert(:user)
127 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
128 ActivityPub.follow(user, unfollowed)
130 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
131 assert user.following == []
133 {:error, msg} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
134 assert msg == "Not subscribed!"
137 test "Unfollow another user using screen_name" do
138 unfollowed = insert(:user)
139 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
141 ActivityPub.follow(user, unfollowed)
143 {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
144 assert user.following == []
146 {:error, msg} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
147 assert msg == "Not subscribed!"
150 test "Block another user using user_id" do
152 blocked = insert(:user)
154 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
155 assert User.blocks?(user, blocked)
158 test "Block another user using screen_name" do
160 blocked = insert(:user)
162 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
163 assert User.blocks?(user, blocked)
166 test "Unblock another user using user_id" do
167 unblocked = insert(:user)
169 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id})
171 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
172 assert user.info.blocks == []
175 test "Unblock another user using screen_name" do
176 unblocked = insert(:user)
178 {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname})
180 {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
181 assert user.info.blocks == []
184 test "upload a file" do
186 content_type: "image/jpg",
187 path: Path.absname("test/fixtures/image.jpg"),
188 filename: "an_image.jpg"
191 response = TwitterAPI.upload(file)
193 assert is_binary(response)
196 test "it favorites a status, returns the updated activity" do
198 note_activity = insert(:note_activity)
200 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
201 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
203 assert status == updated_activity
206 test "it unfavorites a status, returns the updated activity" do
208 note_activity = insert(:note_activity)
209 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
211 {:ok, _like_activity, _object} = ActivityPub.like(user, object)
212 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
214 assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1
216 {:ok, activity} = TwitterAPI.unfav(user, note_activity.id)
218 assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 0
221 test "it retweets a status and returns the retweet" do
223 note_activity = insert(:note_activity)
225 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
226 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
228 assert status == updated_activity
231 test "it unretweets an already retweeted status" do
233 note_activity = insert(:note_activity)
235 {:ok, _status} = TwitterAPI.repeat(user, note_activity.id)
236 {:ok, status} = TwitterAPI.unrepeat(user, note_activity.id)
237 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
239 assert status == updated_activity
242 test "it registers a new user and returns the user." do
244 "nickname" => "lain",
245 "email" => "lain@wired.jp",
246 "fullname" => "lain iwakura",
247 "bio" => "close the world.",
248 "password" => "bear",
252 {:ok, user} = TwitterAPI.register_user(data)
254 fetched_user = Repo.get_by(User, nickname: "lain")
256 assert UserView.render("show.json", %{user: user}) ==
257 UserView.render("show.json", %{user: fetched_user})
260 test "it registers a new user and parses mentions in the bio" do
262 "nickname" => "john",
263 "email" => "john@gmail.com",
264 "fullname" => "John Doe",
266 "password" => "bear",
270 {:ok, user1} = TwitterAPI.register_user(data1)
273 "nickname" => "lain",
274 "email" => "lain@wired.jp",
275 "fullname" => "lain iwakura",
276 "bio" => "@john test",
277 "password" => "bear",
281 {:ok, user2} = TwitterAPI.register_user(data2)
284 "<span><a class='mention' href='#{user1.ap_id}'>@<span>john</span></a></span> test"
286 assert user2.bio == expected_text
289 @moduletag skip: "needs 'registrations_open: false' in config"
290 test "it registers a new user via invite token and returns the user." do
291 {:ok, token} = UserInviteToken.create_token()
294 "nickname" => "vinny",
295 "email" => "pasta@pizza.vs",
296 "fullname" => "Vinny Vinesauce",
298 "password" => "hiptofbees",
299 "confirm" => "hiptofbees",
300 "token" => token.token
303 {:ok, user} = TwitterAPI.register_user(data)
305 fetched_user = Repo.get_by(User, nickname: "vinny")
306 token = Repo.get_by(UserInviteToken, token: token.token)
308 assert token.used == true
310 assert UserView.render("show.json", %{user: user}) ==
311 UserView.render("show.json", %{user: fetched_user})
314 @moduletag skip: "needs 'registrations_open: false' in config"
315 test "it returns an error if invalid token submitted" do
317 "nickname" => "GrimReaper",
318 "email" => "death@reapers.afterlife",
319 "fullname" => "Reaper Grim",
320 "bio" => "Your time has come",
321 "password" => "scythe",
322 "confirm" => "scythe",
323 "token" => "DudeLetMeInImAFairy"
326 {:error, msg} = TwitterAPI.register_user(data)
328 assert msg == "Invalid token"
329 refute Repo.get_by(User, nickname: "GrimReaper")
332 @moduletag skip: "needs 'registrations_open: false' in config"
333 test "it returns an error if expired token submitted" do
334 {:ok, token} = UserInviteToken.create_token()
335 UserInviteToken.mark_as_used(token.token)
338 "nickname" => "GrimReaper",
339 "email" => "death@reapers.afterlife",
340 "fullname" => "Reaper Grim",
341 "bio" => "Your time has come",
342 "password" => "scythe",
343 "confirm" => "scythe",
344 "token" => token.token
347 {:error, msg} = TwitterAPI.register_user(data)
349 assert msg == "Expired token"
350 refute Repo.get_by(User, nickname: "GrimReaper")
353 test "it returns the error on registration problems" do
355 "nickname" => "lain",
356 "email" => "lain@wired.jp",
357 "fullname" => "lain iwakura",
358 "bio" => "close the world.",
362 {:error, error_object} = TwitterAPI.register_user(data)
364 assert is_binary(error_object[:error])
365 refute Repo.get_by(User, nickname: "lain")
368 test "it assigns an integer conversation_id" do
369 note_activity = insert(:note_activity)
370 status = ActivityView.render("activity.json", activity: note_activity)
372 assert is_number(status["statusnet_conversation_id"])
376 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
377 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
381 describe "context_to_conversation_id" do
382 test "creates a mapping object" do
383 conversation_id = TwitterAPI.context_to_conversation_id("random context")
384 object = Object.get_by_ap_id("random context")
386 assert conversation_id == object.id
389 test "returns an existing mapping for an existing object" do
390 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
391 conversation_id = TwitterAPI.context_to_conversation_id("random context")
393 assert conversation_id == object.id
397 describe "fetching a user by uri" do
398 test "fetches a user by uri" do
399 id = "https://mastodon.social/users/lambadalambda"
401 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
402 remote = User.get_by_ap_id(id)
404 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
406 # Also fetches the feed.
407 # assert Activity.get_create_activity_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")