1 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
3 alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
4 alias Pleroma.Web.TwitterAPI.{TwitterAPI,UserView,Utils}
5 alias Pleroma.{Activity, User, Object, Repo}
6 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
7 alias Pleroma.Web.ActivityPub.ActivityPub
11 test "create a status" do
13 _mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
20 "mediaType" => "image/jpg",
21 "href" => "http://example.org/image.jpg"
27 object = Repo.insert!(%Object{data: object_data})
30 "status" => "Hello again, @shp.<script></script>\nThis is on another line. #2hu #epic #phantasmagoric",
31 "media_ids" => [object.id]
34 { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
36 assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>.<br>\nThis is on another line. #2hu #epic #phantasmagoric<br>\n<a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>"
37 assert get_in(activity.data, ["object", "type"]) == "Note"
38 assert get_in(activity.data, ["object", "actor"]) == user.ap_id
39 assert get_in(activity.data, ["actor"]) == user.ap_id
40 assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
41 assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
42 assert Enum.member?(get_in(activity.data, ["to"]), "shp")
43 assert activity.local == true
46 assert activity.data["object"]["tag"] == ["2hu", "epic", "phantasmagoric"]
49 assert is_binary(get_in(activity.data, ["context"]))
50 assert is_binary(get_in(activity.data, ["object", "context"]))
52 assert is_list(activity.data["object"]["attachment"])
54 assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
56 user = User.get_by_ap_id(user.ap_id)
58 assert user.info["note_count"] == 1
61 test "create a status that is a reply" do
64 "status" => "Hello again."
67 { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
70 "status" => "Here's your (you).",
71 "in_reply_to_status_id" => activity.id
74 { :ok, reply = %Activity{} } = TwitterAPI.create_status(user, input)
76 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
77 assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
78 assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
79 assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
80 assert Enum.member?(get_in(reply.data, ["to"]), user.ap_id)
83 test "fetch public statuses, excluding remote ones." do
84 %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
85 insert(:note_activity, %{local: false})
87 follower = insert(:user, following: [User.ap_followers(user)])
89 statuses = TwitterAPI.fetch_public_statuses(follower)
91 assert length(statuses) == 1
92 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
95 test "fetch whole known network statuses" do
96 %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
97 insert(:note_activity, %{local: false})
99 follower = insert(:user, following: [User.ap_followers(user)])
101 statuses = TwitterAPI.fetch_public_and_external_statuses(follower)
103 assert length(statuses) == 2
104 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
107 test "fetch friends' statuses" do
108 user = insert(:user, %{following: ["someguy/followers"]})
109 {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]})
110 {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
112 statuses = TwitterAPI.fetch_friend_statuses(user)
114 activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
115 direct_activity_user = Repo.get_by(User, ap_id: direct_activity.data["actor"])
117 assert length(statuses) == 2
118 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user})
119 assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
122 test "fetch user's mentions" do
124 {:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
125 activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
127 statuses = TwitterAPI.fetch_mentions(user)
129 assert length(statuses) == 1
130 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]})
133 test "get a user by params" do
134 user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
135 {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
137 assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil)
138 assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
139 assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.nickname})
140 assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
141 assert user1_result == TwitterAPI.get_user(user1, nil)
142 assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
143 assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
144 assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
145 assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
148 test "fetch user's statuses" do
149 {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
150 {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
152 {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
153 {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
155 user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
157 assert length(user1_statuses) == 1
158 assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
160 user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
162 assert length(user2_statuses) == 1
163 assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
166 test "fetch a single status" do
167 {:ok, activity} = ActivityBuilder.insert()
168 {:ok, user} = UserBuilder.insert()
169 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
171 status = TwitterAPI.fetch_status(user, activity.id)
173 assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor})
176 test "Follow another user using user_id" do
178 followed = insert(:user)
180 {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"user_id" => followed.id})
181 assert user.following == [User.ap_followers(followed)]
183 { :error, msg } = TwitterAPI.follow(user, %{"user_id" => followed.id})
184 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
187 test "Follow another user using screen_name" do
189 followed = insert(:user)
191 {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
192 assert user.following == [User.ap_followers(followed)]
194 followed = User.get_by_ap_id(followed.ap_id)
195 assert followed.info["follower_count"] == 1
197 { :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
198 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
201 test "Unfollow another user using user_id" do
202 unfollowed = insert(:user)
203 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
204 ActivityPub.follow(user, unfollowed)
206 {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
207 assert user.following == []
209 { :error, msg } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id})
210 assert msg == "Not subscribed!"
213 test "Unfollow another user using screen_name" do
214 unfollowed = insert(:user)
215 user = insert(:user, %{following: [User.ap_followers(unfollowed)]})
217 ActivityPub.follow(user, unfollowed)
219 {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
220 assert user.following == []
222 { :error, msg } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname})
223 assert msg == "Not subscribed!"
226 test "fetch statuses in a context using the conversation id" do
227 {:ok, user} = UserBuilder.insert()
228 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
229 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
230 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
232 {:ok, object} = Object.context_mapping("2hu") |> Repo.insert
234 statuses = TwitterAPI.fetch_conversation(user, object.id)
236 assert length(statuses) == 2
237 assert Enum.at(statuses, 1)["id"] == activity.id
238 assert Enum.at(statuses, 0)["id"] == activity_two.id
241 test "upload a file" do
242 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
244 response = TwitterAPI.upload(file)
246 assert is_binary(response)
249 test "it adds user links to an existing text" do
250 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
252 gsimg = insert(:user, %{nickname: "gsimg"})
253 archaeme = insert(:user, %{nickname: "archaeme"})
254 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
256 mentions = Pleroma.Formatter.parse_mentions(text)
257 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>"
259 assert Utils.add_user_links(text, mentions) == expected_text
262 test "it favorites a status, returns the updated status" do
264 note_activity = insert(:note_activity)
265 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
267 {:ok, status} = TwitterAPI.fav(user, note_activity.id)
268 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
270 assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
273 test "it unfavorites a status, returns the updated status" do
275 note_activity = insert(:note_activity)
276 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
277 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
279 {:ok, _like_activity, _object } = ActivityPub.like(user, object)
280 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
281 assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1
283 {:ok, status} = TwitterAPI.unfavorite(user, note_activity)
285 assert status["fave_num"] == 0
288 test "it retweets a status and returns the retweet" do
290 note_activity = insert(:note_activity)
291 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
293 {:ok, status} = TwitterAPI.repeat(user, note_activity.id)
294 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
296 assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
299 test "it registers a new user and returns the user." do
301 "nickname" => "lain",
302 "email" => "lain@wired.jp",
303 "fullname" => "lain iwakura",
304 "bio" => "close the world.",
305 "password" => "bear",
309 {:ok, user} = TwitterAPI.register_user(data)
311 fetched_user = Repo.get_by(User, nickname: "lain")
312 assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user})
315 test "it returns the error on registration problems" do
317 "nickname" => "lain",
318 "email" => "lain@wired.jp",
319 "fullname" => "lain iwakura",
320 "bio" => "close the world.",
324 {:error, error_object} = TwitterAPI.register_user(data)
326 assert is_binary(error_object[:error])
327 refute Repo.get_by(User, nickname: "lain")
330 test "it assigns an integer conversation_id" do
331 note_activity = insert(:note_activity)
332 user = User.get_cached_by_ap_id(note_activity.data["actor"])
333 status = ActivityRepresenter.to_map(note_activity, %{user: user})
335 assert is_number(status["statusnet_conversation_id"])
339 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
340 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
344 describe "context_to_conversation_id" do
345 test "creates a mapping object" do
346 conversation_id = TwitterAPI.context_to_conversation_id("random context")
347 object = Object.get_by_ap_id("random context")
349 assert conversation_id == object.id
352 test "returns an existing mapping for an existing object" do
353 {:ok, object} = Object.context_mapping("random context") |> Repo.insert
354 conversation_id = TwitterAPI.context_to_conversation_id("random context")
356 assert conversation_id == object.id
360 describe "fetching a user by uri" do
361 test "fetches a user by uri" do
362 id = "https://mastodon.social/users/lambadalambda"
364 {:ok, represented} = TwitterAPI.get_external_profile(user, id)
365 remote = User.get_by_ap_id(id)
367 assert represented == UserView.render("show.json", %{user: remote, for: user})
369 # Also fetches the feed.
370 assert Activity.get_create_activity_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")