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.OStatusTest do
8 alias Pleroma.Instances
12 alias Pleroma.Web.OStatus
14 import Pleroma.Factory
15 import ExUnit.CaptureLog
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 test "don't insert create notes twice" do
23 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
24 {:ok, [activity]} = OStatus.handle_incoming(incoming)
25 assert {:ok, [activity]} == OStatus.handle_incoming(incoming)
28 test "handle incoming note - GS, Salmon" do
29 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
30 {:ok, [activity]} = OStatus.handle_incoming(incoming)
31 object = Object.normalize(activity)
33 user = User.get_cached_by_ap_id(activity.data["actor"])
34 assert user.info.note_count == 1
35 assert activity.data["type"] == "Create"
36 assert object.data["type"] == "Note"
38 assert object.data["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note"
40 assert activity.data["published"] == "2017-04-23T14:51:03+00:00"
41 assert object.data["published"] == "2017-04-23T14:51:03+00:00"
43 assert activity.data["context"] ==
44 "tag:gs.example.org:4040,2017-04-23:objectType=thread:nonce=f09e22f58abd5c7b"
46 assert "http://pleroma.example.org:4000/users/lain3" in activity.data["to"]
47 assert object.data["emoji"] == %{"marko" => "marko.png", "reimu" => "reimu.png"}
48 assert activity.local == false
51 test "handle incoming notes - GS, subscription" do
52 incoming = File.read!("test/fixtures/ostatus_incoming_post.xml")
53 {:ok, [activity]} = OStatus.handle_incoming(incoming)
54 object = Object.normalize(activity)
56 assert activity.data["type"] == "Create"
57 assert object.data["type"] == "Note"
58 assert object.data["actor"] == "https://social.heldscal.la/user/23211"
59 assert object.data["content"] == "Will it blend?"
60 user = User.get_cached_by_ap_id(activity.data["actor"])
61 assert User.ap_followers(user) in activity.data["to"]
62 assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
65 test "handle incoming notes with attachments - GS, subscription" do
66 incoming = File.read!("test/fixtures/incoming_websub_gnusocial_attachments.xml")
67 {:ok, [activity]} = OStatus.handle_incoming(incoming)
68 object = Object.normalize(activity)
70 assert activity.data["type"] == "Create"
71 assert object.data["type"] == "Note"
72 assert object.data["actor"] == "https://social.heldscal.la/user/23211"
73 assert object.data["attachment"] |> length == 2
74 assert object.data["external_url"] == "https://social.heldscal.la/notice/2020923"
75 assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
78 test "handle incoming notes with tags" do
79 incoming = File.read!("test/fixtures/ostatus_incoming_post_tag.xml")
80 {:ok, [activity]} = OStatus.handle_incoming(incoming)
81 object = Object.normalize(activity)
83 assert object.data["tag"] == ["nsfw"]
84 assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
87 test "handle incoming notes - Mastodon, salmon, reply" do
88 # It uses the context of the replied to object
91 "id" => "https://pleroma.soykaf.com/objects/c237d966-ac75-4fe3-a87a-d89d71a3a7a4",
96 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
97 {:ok, [activity]} = OStatus.handle_incoming(incoming)
98 object = Object.normalize(activity)
100 assert activity.data["type"] == "Create"
101 assert object.data["type"] == "Note"
102 assert object.data["actor"] == "https://mastodon.social/users/lambadalambda"
103 assert activity.data["context"] == "2hu"
104 assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
107 test "handle incoming notes - Mastodon, with CW" do
108 incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
109 {:ok, [activity]} = OStatus.handle_incoming(incoming)
110 object = Object.normalize(activity)
112 assert activity.data["type"] == "Create"
113 assert object.data["type"] == "Note"
114 assert object.data["actor"] == "https://mastodon.social/users/lambadalambda"
115 assert object.data["summary"] == "technologic"
116 assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
119 test "handle incoming unlisted messages, put public into cc" do
120 incoming = File.read!("test/fixtures/mastodon-note-unlisted.xml")
121 {:ok, [activity]} = OStatus.handle_incoming(incoming)
122 object = Object.normalize(activity)
124 refute "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
125 assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["cc"]
126 refute "https://www.w3.org/ns/activitystreams#Public" in object.data["to"]
127 assert "https://www.w3.org/ns/activitystreams#Public" in object.data["cc"]
130 test "handle incoming retweets - Mastodon, with CW" do
131 incoming = File.read!("test/fixtures/cw_retweet.xml")
132 {:ok, [[_activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
133 retweeted_object = Object.normalize(retweeted_activity)
135 assert retweeted_object.data["summary"] == "Hey."
138 test "handle incoming notes - GS, subscription, reply" do
139 incoming = File.read!("test/fixtures/ostatus_incoming_reply.xml")
140 {:ok, [activity]} = OStatus.handle_incoming(incoming)
141 object = Object.normalize(activity)
143 assert activity.data["type"] == "Create"
144 assert object.data["type"] == "Note"
145 assert object.data["actor"] == "https://social.heldscal.la/user/23211"
147 assert object.data["content"] ==
148 "@<a href=\"https://gs.archae.me/user/4687\" class=\"h-card u-url p-nickname mention\" title=\"shpbot\">shpbot</a> why not indeed."
150 assert object.data["inReplyTo"] ==
151 "tag:gs.archae.me,2017-04-30:noticeId=778260:objectType=note"
153 assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
156 test "handle incoming retweets - GS, subscription" do
157 incoming = File.read!("test/fixtures/share-gs.xml")
158 {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
160 assert activity.data["type"] == "Announce"
161 assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
162 assert activity.data["object"] == retweeted_activity.data["object"]
163 assert "https://pleroma.soykaf.com/users/lain" in activity.data["to"]
164 refute activity.local
166 retweeted_activity = Activity.get_by_id(retweeted_activity.id)
167 retweeted_object = Object.normalize(retweeted_activity)
168 assert retweeted_activity.data["type"] == "Create"
169 assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
170 refute retweeted_activity.local
171 assert retweeted_object.data["announcement_count"] == 1
172 assert String.contains?(retweeted_object.data["content"], "mastodon")
173 refute String.contains?(retweeted_object.data["content"], "Test account")
176 test "handle incoming retweets - GS, subscription - local message" do
177 incoming = File.read!("test/fixtures/share-gs-local.xml")
178 note_activity = insert(:note_activity)
179 object = Object.normalize(note_activity)
180 user = User.get_cached_by_ap_id(note_activity.data["actor"])
184 |> String.replace("LOCAL_ID", object.data["id"])
185 |> String.replace("LOCAL_USER", user.ap_id)
187 {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
189 assert activity.data["type"] == "Announce"
190 assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
191 assert activity.data["object"] == object.data["id"]
192 assert user.ap_id in activity.data["to"]
193 refute activity.local
195 retweeted_activity = Activity.get_by_id(retweeted_activity.id)
196 assert note_activity.id == retweeted_activity.id
197 assert retweeted_activity.data["type"] == "Create"
198 assert retweeted_activity.data["actor"] == user.ap_id
199 assert retweeted_activity.local
200 assert retweeted_activity.data["object"]["announcement_count"] == 1
203 test "handle incoming retweets - Mastodon, salmon" do
204 incoming = File.read!("test/fixtures/share.xml")
205 {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
206 retweeted_object = Object.normalize(retweeted_activity)
208 assert activity.data["type"] == "Announce"
209 assert activity.data["actor"] == "https://mastodon.social/users/lambadalambda"
210 assert activity.data["object"] == retweeted_activity.data["object"]
212 assert activity.data["id"] ==
213 "tag:mastodon.social,2017-05-03:objectId=4934452:objectType=Status"
215 refute activity.local
216 assert retweeted_activity.data["type"] == "Create"
217 assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
218 refute retweeted_activity.local
219 refute String.contains?(retweeted_object.data["content"], "Test account")
222 test "handle incoming favorites - GS, websub" do
224 incoming = File.read!("test/fixtures/favorite.xml")
225 {:ok, [[activity, favorited_activity]]} = OStatus.handle_incoming(incoming)
227 assert activity.data["type"] == "Like"
228 assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
229 assert activity.data["object"] == favorited_activity.data["object"]
231 assert activity.data["id"] ==
232 "tag:social.heldscal.la,2017-05-05:fave:23211:comment:2061643:2017-05-05T09:12:50+00:00"
234 refute activity.local
235 assert favorited_activity.data["type"] == "Create"
236 assert favorited_activity.data["actor"] == "https://shitposter.club/user/1"
238 assert favorited_activity.data["object"] ==
239 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
241 refute favorited_activity.local
245 test "handle conversation references" do
246 incoming = File.read!("test/fixtures/mastodon_conversation.xml")
247 {:ok, [activity]} = OStatus.handle_incoming(incoming)
249 assert activity.data["context"] ==
250 "tag:mastodon.social,2017-08-28:objectId=7876885:objectType=Conversation"
253 test "handle incoming favorites with locally available object - GS, websub" do
254 note_activity = insert(:note_activity)
255 object = Object.normalize(note_activity)
258 File.read!("test/fixtures/favorite_with_local_note.xml")
259 |> String.replace("localid", object.data["id"])
261 {:ok, [[activity, favorited_activity]]} = OStatus.handle_incoming(incoming)
263 assert activity.data["type"] == "Like"
264 assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
265 assert activity.data["object"] == object.data["id"]
266 refute activity.local
267 assert note_activity.id == favorited_activity.id
268 assert favorited_activity.local
271 test "handle incoming replies" do
272 incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml")
273 {:ok, [activity]} = OStatus.handle_incoming(incoming)
274 object = Object.normalize(activity)
276 assert activity.data["type"] == "Create"
277 assert object.data["type"] == "Note"
279 assert object.data["inReplyTo"] ==
280 "http://pleroma.example.org:4000/objects/55bce8fc-b423-46b1-af71-3759ab4670bc"
282 assert "http://pleroma.example.org:4000/users/lain5" in activity.data["to"]
284 assert object.data["id"] == "tag:gs.example.org:4040,2017-04-25:noticeId=55:objectType=note"
286 assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
289 test "handle incoming follows" do
290 incoming = File.read!("test/fixtures/follow.xml")
291 {:ok, [activity]} = OStatus.handle_incoming(incoming)
292 assert activity.data["type"] == "Follow"
294 assert activity.data["id"] ==
295 "tag:social.heldscal.la,2017-05-07:subscription:23211:person:44803:2017-05-07T09:54:48+00:00"
297 assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
298 assert activity.data["object"] == "https://pawoo.net/users/pekorino"
299 refute activity.local
301 follower = User.get_cached_by_ap_id(activity.data["actor"])
302 followed = User.get_cached_by_ap_id(activity.data["object"])
304 assert User.following?(follower, followed)
307 test "handle incoming unfollows with existing follow" do
308 incoming_follow = File.read!("test/fixtures/follow.xml")
309 {:ok, [_activity]} = OStatus.handle_incoming(incoming_follow)
311 incoming = File.read!("test/fixtures/unfollow.xml")
312 {:ok, [activity]} = OStatus.handle_incoming(incoming)
314 assert activity.data["type"] == "Undo"
316 assert activity.data["id"] ==
317 "undo:tag:social.heldscal.la,2017-05-07:subscription:23211:person:44803:2017-05-07T09:54:48+00:00"
319 assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
320 embedded_object = activity.data["object"]
321 assert is_map(embedded_object)
322 assert embedded_object["type"] == "Follow"
323 assert embedded_object["object"] == "https://pawoo.net/users/pekorino"
324 refute activity.local
326 follower = User.get_cached_by_ap_id(activity.data["actor"])
327 followed = User.get_cached_by_ap_id(embedded_object["object"])
329 refute User.following?(follower, followed)
332 test "it clears `unreachable` federation status of the sender" do
333 incoming_reaction_xml = File.read!("test/fixtures/share-gs.xml")
334 doc = XML.parse_document(incoming_reaction_xml)
335 actor_uri = XML.string_from_xpath("//author/uri[1]", doc)
336 reacted_to_author_uri = XML.string_from_xpath("//author/uri[2]", doc)
338 Instances.set_consistently_unreachable(actor_uri)
339 Instances.set_consistently_unreachable(reacted_to_author_uri)
340 refute Instances.reachable?(actor_uri)
341 refute Instances.reachable?(reacted_to_author_uri)
343 {:ok, _} = OStatus.handle_incoming(incoming_reaction_xml)
344 assert Instances.reachable?(actor_uri)
345 refute Instances.reachable?(reacted_to_author_uri)
348 describe "new remote user creation" do
349 test "returns local users" do
350 local_user = insert(:user)
351 {:ok, user} = OStatus.find_or_make_user(local_user.ap_id)
353 assert user == local_user
356 test "tries to use the information in poco fields" do
357 uri = "https://social.heldscal.la/user/23211"
359 {:ok, user} = OStatus.find_or_make_user(uri)
361 user = User.get_cached_by_id(user.id)
362 assert user.name == "Constance Variable"
363 assert user.nickname == "lambadalambda@social.heldscal.la"
364 assert user.local == false
365 assert user.info.uri == uri
366 assert user.ap_id == uri
367 assert user.bio == "Call me Deacon Blues."
368 assert user.avatar["type"] == "Image"
370 {:ok, user_again} = OStatus.find_or_make_user(uri)
372 assert user == user_again
375 test "find_or_make_user sets all the nessary input fields" do
376 uri = "https://social.heldscal.la/user/23211"
377 {:ok, user} = OStatus.find_or_make_user(uri)
387 default_scope: "public",
398 hub: "https://social.heldscal.la/main/push/hub",
400 "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB",
401 salmon: "https://social.heldscal.la/main/salmon/user/23211",
402 topic: "https://social.heldscal.la/api/statuses/user_timeline/23211.atom",
403 uri: "https://social.heldscal.la/user/23211"
407 test "find_make_or_update_user takes an author element and returns an updated user" do
408 uri = "https://social.heldscal.la/user/23211"
410 {:ok, user} = OStatus.find_or_make_user(uri)
413 change = Ecto.Changeset.change(user, %{avatar: nil, bio: nil, name: nil})
415 {:ok, user} = Repo.update(change)
418 doc = XML.parse_document(File.read!("test/fixtures/23211.atom"))
419 [author] = :xmerl_xpath.string('//author[1]', doc)
420 {:ok, user} = OStatus.find_make_or_update_user(author)
421 assert user.avatar["type"] == "Image"
422 assert user.name == old_name
423 assert user.bio == old_bio
425 {:ok, user_again} = OStatus.find_make_or_update_user(author)
426 assert user_again == user
430 describe "gathering user info from a user id" do
431 test "it returns user info in a hash" do
432 user = "shp@social.heldscal.la"
434 # TODO: make test local
435 {:ok, data} = OStatus.gather_user_info(user)
438 "hub" => "https://social.heldscal.la/main/push/hub",
440 "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
443 "salmon" => "https://social.heldscal.la/main/salmon/user/29191",
444 "subject" => "acct:shp@social.heldscal.la",
445 "topic" => "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
446 "uri" => "https://social.heldscal.la/user/29191",
447 "host" => "social.heldscal.la",
454 "href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg",
455 "mediaType" => "image/jpeg",
460 "subscribe_address" => "https://social.heldscal.la/main/ostatussub?profile={uri}",
464 assert data == expected
467 test "it works with the uri" do
468 user = "https://social.heldscal.la/user/29191"
470 # TODO: make test local
471 {:ok, data} = OStatus.gather_user_info(user)
474 "hub" => "https://social.heldscal.la/main/push/hub",
476 "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
479 "salmon" => "https://social.heldscal.la/main/salmon/user/29191",
480 "subject" => "https://social.heldscal.la/user/29191",
481 "topic" => "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
482 "uri" => "https://social.heldscal.la/user/29191",
483 "host" => "social.heldscal.la",
490 "href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg",
491 "mediaType" => "image/jpeg",
496 "subscribe_address" => "https://social.heldscal.la/main/ostatussub?profile={uri}",
500 assert data == expected
504 describe "fetching a status by it's HTML url" do
505 test "it builds a missing status from an html url" do
507 url = "https://shitposter.club/notice/2827873"
508 {:ok, [activity]} = OStatus.fetch_activity_from_url(url)
510 assert activity.data["actor"] == "https://shitposter.club/user/1"
512 assert activity.data["object"] ==
513 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
517 test "it works for atom notes, too" do
518 url = "https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056"
519 {:ok, [activity]} = OStatus.fetch_activity_from_url(url)
520 assert activity.data["actor"] == "https://social.sakamoto.gq/users/eal"
521 assert activity.data["object"] == url
525 test "it doesn't add nil in the to field" do
526 incoming = File.read!("test/fixtures/nil_mention_entry.xml")
527 {:ok, [activity]} = OStatus.handle_incoming(incoming)
529 assert activity.data["to"] == [
530 "http://localhost:4001/users/atarifrosch@social.stopwatchingus-heidelberg.de/followers",
531 "https://www.w3.org/ns/activitystreams#Public"
535 describe "is_representable?" do
536 test "Note objects are representable" do
537 note_activity = insert(:note_activity)
539 assert OStatus.is_representable?(note_activity)
542 test "Article objects are not representable" do
543 note_activity = insert(:note_activity)
544 note_object = Object.normalize(note_activity)
548 |> Map.put("type", "Article")
550 Cachex.clear(:object_cache)
552 cs = Object.change(note_object, %{data: note_data})
553 {:ok, _article_object} = Repo.update(cs)
555 # the underlying object is now an Article instead of a note, so this should fail
556 refute OStatus.is_representable?(note_activity)