Fix specs.
[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 = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user})
20
21 assert status["id"] == announce_activity.id
22 assert status["user"] == UserView.render("show.json", %{user: user, for: user})
23
24 retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
25 assert retweeted_status["repeated"] == true
26 assert retweeted_status["id"] == note_activity.id
27 assert status["statusnet_conversation_id"] == retweeted_status["statusnet_conversation_id"]
28
29 assert status["retweeted_status"] == retweeted_status
30 assert status["activity_type"] == "repeat"
31 end
32
33 test "a like activity" do
34 user = insert(:user)
35 note_activity = insert(:note_activity)
36 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
37
38 {:ok, like_activity, _object} = ActivityPub.like(user, object)
39 status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
40
41 assert status["id"] == like_activity.id
42 assert status["in_reply_to_status_id"] == note_activity.id
43
44 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
45 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
46 liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
47 assert liked_status["favorited"] == true
48 assert status["activity_type"] == "like"
49 end
50
51 test "an activity" do
52 {:ok, user} = UserBuilder.insert
53 # {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
54 mentioned_user = insert(:user, %{nickname: "shp"})
55
56 # {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
57 follower = insert(:user, %{following: [User.ap_followers(user)]})
58
59 object = %Object{
60 data: %{
61 "type" => "Image",
62 "url" => [
63 %{
64 "type" => "Link",
65 "mediaType" => "image/jpg",
66 "href" => "http://example.org/image.jpg"
67 }
68 ],
69 "uuid" => 1
70 }
71 }
72
73 content_html = "<script>alert('YAY')</script>Some content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
74 content = HtmlSanitizeEx.strip_tags(content_html)
75 date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601
76
77 {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert
78
79 activity = %Activity{
80 id: 1,
81 data: %{
82 "type" => "Create",
83 "id" => "id",
84 "to" => [
85 User.ap_followers(user),
86 "https://www.w3.org/ns/activitystreams#Public",
87 mentioned_user.ap_id
88 ],
89 "actor" => User.ap_id(user),
90 "object" => %{
91 "published" => date,
92 "type" => "Note",
93 "content" => content_html,
94 "inReplyToStatusId" => 213123,
95 "attachment" => [
96 object
97 ],
98 "external_url" => "some url",
99 "like_count" => 5,
100 "announcement_count" => 3,
101 "context" => "2hu",
102 "tag" => ["content", "mentioning", "nsfw"]
103 },
104 "published" => date,
105 "context" => "2hu"
106 },
107 local: false
108 }
109
110
111 expected_status = %{
112 "id" => activity.id,
113 "user" => UserView.render("show.json", %{user: user, for: follower}),
114 "is_local" => false,
115 "statusnet_html" => HtmlSanitizeEx.basic_html(content_html),
116 "text" => content,
117 "is_post_verb" => true,
118 "created_at" => "Tue May 24 13:26:08 +0000 2016",
119 "in_reply_to_status_id" => 213123,
120 "statusnet_conversation_id" => convo_object.id,
121 "attachments" => [
122 ObjectRepresenter.to_map(object)
123 ],
124 "attentions" => [
125 UserView.render("show.json", %{user: mentioned_user, for: follower})
126 ],
127 "fave_num" => 5,
128 "repeat_num" => 3,
129 "favorited" => false,
130 "repeated" => false,
131 "external_url" => "some url",
132 "tags" => ["content", "mentioning", "nsfw"],
133 "activity_type" => "post",
134 "possibly_sensitive" => true,
135 "uri" => activity.data["object"]["id"]
136 }
137
138 assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
139 end
140
141 test "an undo for a follow" do
142 follower = insert(:user)
143 followed = insert(:user)
144
145 {:ok, follow} = ActivityPub.follow(follower, followed)
146 {:ok, unfollow} = ActivityPub.unfollow(follower, followed)
147
148 map = ActivityRepresenter.to_map(unfollow, %{user: follower})
149 assert map["is_post_verb"] == false
150 assert map["activity_type"] == "undo"
151 end
152
153 test "a delete activity" do
154 object = insert(:note)
155 user = User.get_by_ap_id(object.data["actor"])
156
157 {:ok, delete} = ActivityPub.delete(object)
158
159 map = ActivityRepresenter.to_map(delete, %{user: user})
160
161 assert map["is_post_verb"] == false
162 assert map["activity_type"] == "delete"
163 assert map["uri"] == object.data["id"]
164 end
165 end