1 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
3 alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
4 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.{Activity, User, Object, Repo}
7 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
8 alias Pleroma.Web.ActivityPub.ActivityPub
10 import Pleroma.Factory
12 test "create a status" do
14 _mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
21 "mediaType" => "image/jpg",
22 "href" => "http://example.org/image.jpg"
28 object = Repo.insert!(%Object{data: object_data})
31 "status" => "Hello again, @shp.<script></script>\nThis is on another line. #2hu #epic #phantasmagoric",
32 "media_ids" => [object.id]
35 { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
37 assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>.<script></script><br>This is on another line. #2hu #epic #phantasmagoric<br><a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>"
38 assert get_in(activity.data, ["object", "type"]) == "Note"
39 assert get_in(activity.data, ["object", "actor"]) == user.ap_id
40 assert get_in(activity.data, ["actor"]) == user.ap_id
41 assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
42 assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
43 assert Enum.member?(get_in(activity.data, ["to"]), "shp")
44 assert activity.local == true
47 assert activity.data["object"]["tag"] == ["2hu", "epic", "phantasmagoric"]
50 assert is_binary(get_in(activity.data, ["context"]))
51 assert is_binary(get_in(activity.data, ["object", "context"]))
53 assert is_list(activity.data["object"]["attachment"])
55 assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
57 user = User.get_by_ap_id(user.ap_id)
59 assert user.info["note_count"] == 1
62 test "create a status that is a reply" do
65 "status" => "Hello again."
68 { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
71 "status" => "Here's your (you).",
72 "in_reply_to_status_id" => activity.id
75 { :ok, reply = %Activity{} } = TwitterAPI.create_status(user, input)
77 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
78 assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
79 assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
80 assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
81 assert Enum.member?(get_in(reply.data, ["to"]), user.ap_id)
84 test "fetch public statuses, excluding remote ones." do
85 %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
86 insert(:note_activity, %{local: false})
88 follower = insert(:user, following: [User.ap_followers(user)])
90 statuses = TwitterAPI.fetch_public_statuses(follower)
92 assert length(statuses) == 1
93 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
96 test "fetch whole known network statuses" do
97 %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
98 insert(:note_activity, %{local: false})
100 follower = insert(:user, following: [User.ap_followers(user)])
102 statuses = TwitterAPI.fetch_public_and_external_statuses(follower)
104 assert length(statuses) == 2
105 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
108 test "fetch friends' statuses" do
109 user = insert(:user, %{following: ["someguy/followers"]})
110 {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]})
111 {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
113 statuses = TwitterAPI.fetch_friend_statuses(user)
115 activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
116 direct_activity_user = Repo.get_by(User, ap_id: direct_activity.data["actor"])
118 assert length(statuses) == 2
119 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user})
120 assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
123 test "fetch user's mentions" do
125 {:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
126 activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
128 statuses = TwitterAPI.fetch_mentions(user)
130 assert length(statuses) == 1
131 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]})
134 test "get a user by params" do
135 user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
136 {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
138 assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil)
139 assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
140 assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.nickname})
141 assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
142 assert user1_result == TwitterAPI.get_user(user1, nil)
143 assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
144 assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
145 assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
146 assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
149 test "fetch user's statuses" do
150 {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
151 {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
153 {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
154 {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
156 user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
158 assert length(user1_statuses) == 1
159 assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
161 user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
163 assert length(user2_statuses) == 1
164 assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
167 test "fetch a single status" do
168 {:ok, activity} = ActivityBuilder.insert()
169 {:ok, user} = UserBuilder.insert()
170 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
172 status = TwitterAPI.fetch_status(user, activity.id)
174 assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor})
177 test "Follow another user using user_id" do
179 followed = insert(:user)
181 {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"user_id" => followed.id})
182 assert user.following == [User.ap_followers(followed)]
184 { :error, msg } = TwitterAPI.follow(user, %{"user_id" => followed.id})
185 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
188 test "Follow another user using screen_name" do
190 followed = insert(:user)
192 {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
193 assert user.following == [User.ap_followers(followed)]
195 followed = User.get_by_ap_id(followed.ap_id)
196 assert followed.info["follower_count"] == 1
198 { :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
199 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
202 test "Unfollow another user using user_id" do
203 unfollowed = insert(:user)
204 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
205 ActivityPub.follow(user, unfollowed)
207 {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
208 assert user.following == []
210 { :error, msg } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
211 assert msg == "Not subscribed!"
214 test "Unfollow another user using screen_name" do
215 unfollowed = insert(:user)
216 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
218 ActivityPub.follow(user, unfollowed)
220 {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
221 assert user.following == []
223 { :error, msg } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
224 assert msg == "Not subscribed!"
227 test "Block another user using user_id" do
229 blocked = insert(:user)
231 {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
232 assert User.blocks?(user, blocked)
235 test "Block another user using screen_name" do
237 blocked = insert(:user)
239 {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
240 assert User.blocks?(user, blocked)
243 test "Unblock another user using user_id" do
244 unblocked = insert(:user)
246 User.block(user, unblocked)
248 {:ok, user, unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
249 assert user.info["blocks"] == []
252 test "Unblock another user using screen_name" do
253 unblocked = insert(:user)
255 User.block(user, unblocked)
257 {:ok, user, unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
258 assert user.info["blocks"] == []
261 test "fetch statuses in a context using the conversation id" do
262 {:ok, user} = UserBuilder.insert()
263 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
264 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
265 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
267 {:ok, object} = Object.context_mapping("2hu") |> Repo.insert
269 statuses = TwitterAPI.fetch_conversation(user, object.id)
271 assert length(statuses) == 2
272 assert Enum.at(statuses, 1)["id"] == activity.id
273 assert Enum.at(statuses, 0)["id"] == activity_two.id
276 test "upload a file" do
277 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
279 response = TwitterAPI.upload(file)
281 assert is_binary(response)
284 test "it adds user links to an existing text" do
285 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
287 gsimg = insert(:user, %{nickname: "gsimg"})
288 archaeme = insert(:user, %{nickname: "archaeme"})
289 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
291 mentions = Pleroma.Formatter.parse_mentions(text)
292 expected_text = "<a href='#{gsimg.ap_id}'>@gsimg</a> According to <a href='#{archaeme.ap_id}'>@archaeme</a>, that is @daggsy. Also hello <a href='#{archaeme_remote.ap_id}'>@archaeme</a>"
294 assert Utils.add_user_links(text, mentions) == expected_text
297 test "it favorites a status, returns the updated status" do
299 note_activity = insert(:note_activity)
300 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
302 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
303 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
305 assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
308 test "it unfavorites a status, returns the updated status" do
310 note_activity = insert(:note_activity)
311 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
312 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
314 {:ok, _like_activity, _object } = ActivityPub.like(user, object)
315 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
316 assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1
318 {:ok, status} = TwitterAPI.unfav(user, note_activity.id)
320 assert status["fave_num"] == 0
323 test "it retweets a status and returns the retweet" do
325 note_activity = insert(:note_activity)
326 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
328 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
329 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
331 assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
334 test "it registers a new user and returns the user." do
336 "nickname" => "lain",
337 "email" => "lain@wired.jp",
338 "fullname" => "lain iwakura",
339 "bio" => "close the world.",
340 "password" => "bear",
344 {:ok, user} = TwitterAPI.register_user(data)
346 fetched_user = Repo.get_by(User, nickname: "lain")
347 assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user})
350 test "it returns the error on registration problems" do
352 "nickname" => "lain",
353 "email" => "lain@wired.jp",
354 "fullname" => "lain iwakura",
355 "bio" => "close the world.",
359 {:error, error_object} = TwitterAPI.register_user(data)
361 assert is_binary(error_object[:error])
362 refute Repo.get_by(User, nickname: "lain")
365 test "it assigns an integer conversation_id" do
366 note_activity = insert(:note_activity)
367 user = User.get_cached_by_ap_id(note_activity.data["actor"])
368 status = ActivityRepresenter.to_map(note_activity, %{user: user})
370 assert is_number(status["statusnet_conversation_id"])
374 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
375 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
379 describe "context_to_conversation_id" do
380 test "creates a mapping object" do
381 conversation_id = TwitterAPI.context_to_conversation_id("random context")
382 object = Object.get_by_ap_id("random context")
384 assert conversation_id == object.id
387 test "returns an existing mapping for an existing object" do
388 {:ok, object} = Object.context_mapping("random context") |> Repo.insert
389 conversation_id = TwitterAPI.context_to_conversation_id("random context")
391 assert conversation_id == object.id
395 describe "fetching a user by uri" do
396 test "fetches a user by uri" do
397 id = "https://mastodon.social/users/lambadalambda"
399 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
400 remote = User.get_by_ap_id(id)
402 assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
404 # Also fetches the feed.
405 assert Activity.get_create_activity_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")