Merge branch 'fix/sign-in-with-toot' into 'develop'
[akkoma] / test / web / twitter_api / representers / activity_representer_test.exs
1 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
2 use Pleroma.DataCase
3 alias Pleroma.{User, Activity, Object}
4 alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, ObjectRepresenter}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Builders.UserBuilder
7 alias Pleroma.Web.TwitterAPI.UserView
8 import Pleroma.Factory
9
10 test "an announce activity" do
11 user = insert(:user)
12 note_activity = insert(:note_activity)
13 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
14 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
15
16 {:ok, announce_activity, _object} = ActivityPub.announce(user, object)
17 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
18
19 status =
20 ActivityRepresenter.to_map(announce_activity, %{
21 users: [user, activity_actor],
22 announced_activity: note_activity,
23 for: user
24 })
25
26 assert status["id"] == announce_activity.id
27 assert status["user"] == UserView.render("show.json", %{user: user, for: user})
28
29 retweeted_status =
30 ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
31
32 assert retweeted_status["repeated"] == true
33 assert retweeted_status["id"] == note_activity.id
34 assert status["statusnet_conversation_id"] == retweeted_status["statusnet_conversation_id"]
35
36 assert status["retweeted_status"] == retweeted_status
37 assert status["activity_type"] == "repeat"
38 end
39
40 test "a like activity" do
41 user = insert(:user)
42 note_activity = insert(:note_activity)
43 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
44
45 {:ok, like_activity, _object} = ActivityPub.like(user, object)
46
47 status =
48 ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
49
50 assert status["id"] == like_activity.id
51 assert status["in_reply_to_status_id"] == note_activity.id
52
53 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
54 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
55 liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
56 assert liked_status["favorited"] == true
57 assert status["activity_type"] == "like"
58 end
59
60 test "an activity" do
61 {:ok, user} = UserBuilder.insert()
62 # {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
63 mentioned_user = insert(:user, %{nickname: "shp"})
64
65 # {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
66 follower = insert(:user, %{following: [User.ap_followers(user)]})
67
68 object = %Object{
69 data: %{
70 "type" => "Image",
71 "url" => [
72 %{
73 "type" => "Link",
74 "mediaType" => "image/jpg",
75 "href" => "http://example.org/image.jpg"
76 }
77 ],
78 "uuid" => 1
79 }
80 }
81
82 content_html =
83 "<script>alert('YAY')</script>Some :2hu: content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
84
85 content = HtmlSanitizeEx.strip_tags(content_html)
86 date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601()
87
88 {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert()
89
90 to = [
91 User.ap_followers(user),
92 "https://www.w3.org/ns/activitystreams#Public",
93 mentioned_user.ap_id
94 ]
95
96 activity = %Activity{
97 id: 1,
98 data: %{
99 "type" => "Create",
100 "id" => "id",
101 "to" => to,
102 "actor" => User.ap_id(user),
103 "object" => %{
104 "published" => date,
105 "type" => "Note",
106 "content" => content_html,
107 "summary" => "2hu",
108 "inReplyToStatusId" => 213_123,
109 "attachment" => [
110 object
111 ],
112 "external_url" => "some url",
113 "like_count" => 5,
114 "announcement_count" => 3,
115 "context" => "2hu",
116 "tag" => ["content", "mentioning", "nsfw"],
117 "emoji" => %{
118 "2hu" => "corndog.png"
119 }
120 },
121 "published" => date,
122 "context" => "2hu"
123 },
124 local: false,
125 recipients: to
126 }
127
128 expected_html =
129 "<p>2hu</p>alert('YAY')Some <img height=\"32px\" width=\"32px\" alt=\"2hu\" title=\"2hu\" src=\"corndog.png\" /> content mentioning <a href=\"#{
130 mentioned_user.ap_id
131 }\">@shp</a>"
132
133 expected_status = %{
134 "id" => activity.id,
135 "user" => UserView.render("show.json", %{user: user, for: follower}),
136 "is_local" => false,
137 "statusnet_html" => expected_html,
138 "text" => "2hu" <> content,
139 "is_post_verb" => true,
140 "created_at" => "Tue May 24 13:26:08 +0000 2016",
141 "in_reply_to_status_id" => 213_123,
142 "statusnet_conversation_id" => convo_object.id,
143 "attachments" => [
144 ObjectRepresenter.to_map(object)
145 ],
146 "attentions" => [
147 UserView.render("show.json", %{user: mentioned_user, for: follower})
148 ],
149 "fave_num" => 5,
150 "repeat_num" => 3,
151 "favorited" => false,
152 "repeated" => false,
153 "external_url" => "some url",
154 "tags" => ["nsfw", "content", "mentioning"],
155 "activity_type" => "post",
156 "possibly_sensitive" => true,
157 "uri" => activity.data["object"]["id"],
158 "visibility" => "direct",
159 "summary" => "2hu"
160 }
161
162 assert ActivityRepresenter.to_map(activity, %{
163 user: user,
164 for: follower,
165 mentioned: [mentioned_user]
166 }) == expected_status
167 end
168
169 test "an undo for a follow" do
170 follower = insert(:user)
171 followed = insert(:user)
172
173 {:ok, _follow} = ActivityPub.follow(follower, followed)
174 {:ok, unfollow} = ActivityPub.unfollow(follower, followed)
175
176 map = ActivityRepresenter.to_map(unfollow, %{user: follower})
177 assert map["is_post_verb"] == false
178 assert map["activity_type"] == "undo"
179 end
180
181 test "a delete activity" do
182 object = insert(:note)
183 user = User.get_by_ap_id(object.data["actor"])
184
185 {:ok, delete} = ActivityPub.delete(object)
186
187 map = ActivityRepresenter.to_map(delete, %{user: user})
188
189 assert map["is_post_verb"] == false
190 assert map["activity_type"] == "delete"
191 assert map["uri"] == object.data["id"]
192 end
193 end