273093ebad5814f309799bf2b495502cd6df9bfc
[akkoma] / test / web / twitter_api / twitter_api_test.exs
1 defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
2 use Pleroma.DataCase
3 alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
4 alias Pleroma.Web.TwitterAPI.TwitterAPI
5 alias Pleroma.{Activity, User, Object, Repo}
6 alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
7 alias Pleroma.Web.ActivityPub.ActivityPub
8
9 import Pleroma.Factory
10
11 test "create a status" do
12 user = UserBuilder.build(%{ap_id: "142344"})
13 _mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
14
15 object_data = %{
16 "type" => "Image",
17 "url" => [
18 %{
19 "type" => "Link",
20 "mediaType" => "image/jpg",
21 "href" => "http://example.org/image.jpg"
22 }
23 ],
24 "uuid" => 1
25 }
26
27 object = Repo.insert!(%Object{data: object_data})
28
29 input = %{
30 "status" => "Hello again, @shp.<script></script>\nThis is on another line.",
31 "media_ids" => [object.id]
32 }
33
34 { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
35
36 assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>.<br>This is on another line."
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
44 # Add a context + 'statusnet_conversation_id'
45 assert is_binary(get_in(activity.data, ["context"]))
46 assert is_binary(get_in(activity.data, ["object", "context"]))
47 assert get_in(activity.data, ["object", "statusnetConversationId"]) == activity.id
48 assert get_in(activity.data, ["statusnetConversationId"]) == activity.id
49
50 assert is_list(activity.data["object"]["attachment"])
51
52 assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
53 end
54
55 test "create a status that is a reply" do
56 user = UserBuilder.build(%{ap_id: "some_cool_id"})
57 input = %{
58 "status" => "Hello again."
59 }
60
61 { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
62
63 input = %{
64 "status" => "Here's your (you).",
65 "in_reply_to_status_id" => activity.id
66 }
67
68 { :ok, reply = %Activity{} } = TwitterAPI.create_status(user, input)
69
70 assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
71 assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
72 assert get_in(reply.data, ["statusnetConversationId"]) == get_in(activity.data, ["statusnetConversationId"])
73 assert get_in(reply.data, ["object", "statusnetConversationId"]) == get_in(activity.data, ["object", "statusnetConversationId"])
74 assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
75 assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
76 assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id")
77 end
78
79 test "fetch public statuses" do
80 %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
81
82 follower = insert(:user, following: [User.ap_followers(user)])
83
84 statuses = TwitterAPI.fetch_public_statuses(follower)
85
86 assert length(statuses) == 1
87 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
88 end
89
90 test "fetch friends' statuses" do
91 user = insert(:user, %{following: ["someguy/followers"]})
92 {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]})
93 {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
94
95 statuses = TwitterAPI.fetch_friend_statuses(user)
96
97 activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
98 direct_activity_user = Repo.get_by(User, ap_id: direct_activity.data["actor"])
99
100 assert length(statuses) == 2
101 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user})
102 assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
103 end
104
105 test "fetch user's mentions" do
106 user = insert(:user)
107 {:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
108 activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
109
110 statuses = TwitterAPI.fetch_mentions(user)
111
112 assert length(statuses) == 1
113 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]})
114 end
115
116 test "get a user by params" do
117 user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
118 {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
119
120 assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil)
121 assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
122 assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
123 assert user1_result == TwitterAPI.get_user(user1, nil)
124 assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
125 assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
126 assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
127 assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
128 end
129
130 test "fetch user's statuses" do
131 {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
132 {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
133
134 {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
135 {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
136
137 user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
138
139 assert length(user1_statuses) == 1
140 assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
141
142 user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
143
144 assert length(user2_statuses) == 1
145 assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
146 end
147
148 test "fetch a single status" do
149 {:ok, activity} = ActivityBuilder.insert()
150 {:ok, user} = UserBuilder.insert()
151 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
152
153 status = TwitterAPI.fetch_status(user, activity.id)
154
155 assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor})
156 end
157
158 test "Follow another user" do
159 user = insert(:user)
160 followed = insert(:user)
161
162 { :ok, user, followed, activity } = TwitterAPI.follow(user, followed.id)
163
164 user = Repo.get(User, user.id)
165 follow = Repo.get(Activity, activity.id)
166
167 assert user.following == [User.ap_followers(followed)]
168 assert follow == activity
169
170 { :error, msg } = TwitterAPI.follow(user, followed.id)
171 assert msg == "Could not follow user: #{followed.nickname} is already on your list."
172 end
173
174 test "Unfollow another user" do
175 followed = insert(:user)
176 user = insert(:user, %{following: [User.ap_followers(followed)]})
177
178 { :ok, user, _followed } = TwitterAPI.unfollow(user, followed.id)
179
180 user = Repo.get(User, user.id)
181
182 assert user.following == []
183 { :error, msg } = TwitterAPI.unfollow(user, followed.id)
184 assert msg == "Not subscribed!"
185 end
186
187 test "fetch statuses in a context using the conversation id" do
188 {:ok, user} = UserBuilder.insert()
189 {:ok, activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"})
190 {:ok, activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
191 {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
192
193 statuses = TwitterAPI.fetch_conversation(user, 1)
194
195 assert length(statuses) == 2
196 assert Enum.at(statuses, 0)["id"] == activity.id
197 assert Enum.at(statuses, 1)["id"] == activity_two.id
198 end
199
200 test "upload a file" do
201 file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
202
203 response = TwitterAPI.upload(file)
204
205 assert is_binary(response)
206 end
207
208 test "it can parse mentions and return the relevant users" do
209 text = "@gsimg According to @archaeme , that is @daggsy."
210
211 gsimg = insert(:user, %{nickname: "gsimg"})
212 archaeme = insert(:user, %{nickname: "archaeme"})
213
214 expected_result = [
215 {"@gsimg", gsimg},
216 {"@archaeme", archaeme}
217 ]
218
219 assert TwitterAPI.parse_mentions(text) == expected_result
220 end
221
222 test "it adds user links to an existing text" do
223 text = "@gsimg According to @archaeme , that is @daggsy."
224
225 gsimg = insert(:user, %{nickname: "gsimg"})
226 archaeme = insert(:user, %{nickname: "archaeme"})
227
228 mentions = TwitterAPI.parse_mentions(text)
229 expected_text = "<a href='#{gsimg.ap_id}'>@gsimg</a> According to <a href='#{archaeme.ap_id}'>@archaeme</a> , that is @daggsy."
230
231 assert TwitterAPI.add_user_links(text, mentions) == expected_text
232 end
233
234 test "it favorites a status, returns the updated status" do
235 user = insert(:user)
236 note_activity = insert(:note_activity)
237 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
238
239 {:ok, status} = TwitterAPI.favorite(user, note_activity)
240 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
241
242 assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
243 end
244
245 test "it unfavorites a status, returns the updated status" do
246 user = insert(:user)
247 note_activity = insert(:note_activity)
248 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
249 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
250
251 {:ok, _like_activity, _object } = ActivityPub.like(user, object)
252 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
253 assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1
254
255 {:ok, status} = TwitterAPI.unfavorite(user, note_activity)
256
257 assert status["fave_num"] == 0
258 end
259
260 test "it retweets a status and returns the retweet" do
261 user = insert(:user)
262 note_activity = insert(:note_activity)
263 activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
264
265 {:ok, status} = TwitterAPI.retweet(user, note_activity)
266 updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
267
268 assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
269 end
270
271 test "it registers a new user and returns the user." do
272 data = %{
273 "nickname" => "lain",
274 "email" => "lain@wired.jp",
275 "fullname" => "lain iwakura",
276 "bio" => "close the world.",
277 "password" => "bear",
278 "confirm" => "bear"
279 }
280
281 {:ok, user} = TwitterAPI.register_user(data)
282
283 fetched_user = Repo.get_by(User, nickname: "lain")
284 assert user == UserRepresenter.to_map(fetched_user)
285 end
286
287 test "it returns the error on registration problems" do
288 data = %{
289 "nickname" => "lain",
290 "email" => "lain@wired.jp",
291 "fullname" => "lain iwakura",
292 "bio" => "close the world.",
293 "password" => "bear"
294 }
295
296 {:error, error_object} = TwitterAPI.register_user(data)
297
298 assert is_binary(error_object[:error])
299 refute Repo.get_by(User, nickname: "lain")
300 end
301
302 setup do
303 Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
304 Supervisor.restart_child(Pleroma.Supervisor, Cachex)
305 :ok
306 end
307 end