46ffdef4482b0cd912a752fa1eca7bd311891942
[akkoma] / test / web / twitter_api / views / activity_view_test.exs
1 defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.UserView
8 alias Pleroma.Web.TwitterAPI.TwitterAPI
9 alias Pleroma.Repo
10 alias Pleroma.Activity
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13
14 import Pleroma.Factory
15 import Mock
16
17 test "a create activity with a note" do
18 user = insert(:user)
19 other_user = insert(:user, %{nickname: "shp"})
20
21 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
22
23 result = ActivityView.render("activity.json", activity: activity)
24
25 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
26
27 expected = %{
28 "activity_type" => "post",
29 "attachments" => [],
30 "attentions" => [
31 UserView.render("show.json", %{user: other_user})
32 ],
33 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
34 "external_url" => activity.data["object"]["id"],
35 "fave_num" => 0,
36 "favorited" => false,
37 "id" => activity.id,
38 "in_reply_to_status_id" => nil,
39 "is_local" => true,
40 "is_post_verb" => true,
41 "possibly_sensitive" => false,
42 "repeat_num" => 0,
43 "repeated" => false,
44 "statusnet_conversation_id" => convo_id,
45 "statusnet_html" =>
46 "Hey <span><a href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
47 "tags" => [],
48 "text" => "Hey @shp!",
49 "uri" => activity.data["object"]["id"],
50 "user" => UserView.render("show.json", %{user: user})
51 }
52
53 assert result == expected
54 end
55
56 test "a list of activities" do
57 user = insert(:user)
58 other_user = insert(:user, %{nickname: "shp"})
59 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
60
61 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
62
63 mocks = [
64 {
65 TwitterAPI,
66 [],
67 [context_to_conversation_id: fn(_) -> false end]
68 }
69 ]
70
71 with_mocks mocks do
72 [result] = ActivityView.render("index.json", activities: [activity])
73
74 assert result["statusnet_conversation_id"] == convo_id
75 assert result["user"]
76 refute called TwitterAPI.context_to_conversation_id(:_)
77 end
78 end
79
80 test "an activity that is a reply" do
81 user = insert(:user)
82 other_user = insert(:user, %{nickname: "shp"})
83
84 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
85
86 {:ok, answer} =
87 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
88
89 result = ActivityView.render("activity.json", %{activity: answer})
90
91 assert result["in_reply_to_status_id"] == activity.id
92 end
93
94 test "a like activity" do
95 user = insert(:user)
96 other_user = insert(:user, %{nickname: "shp"})
97
98 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
99 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
100
101 result = ActivityView.render("activity.json", activity: like)
102
103 expected = %{
104 "activity_type" => "like",
105 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
106 "external_url" => like.data["id"],
107 "id" => like.id,
108 "in_reply_to_status_id" => activity.id,
109 "is_local" => true,
110 "is_post_verb" => false,
111 "statusnet_html" => "shp favorited a status.",
112 "text" => "shp favorited a status.",
113 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
114 "user" => UserView.render("show.json", user: other_user)
115 }
116
117 assert result == expected
118 end
119
120 test "an announce activity" do
121 user = insert(:user)
122 other_user = insert(:user, %{nickname: "shp"})
123
124 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
125 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
126
127 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
128
129 activity = Repo.get(Activity, activity.id)
130
131 result = ActivityView.render("activity.json", activity: announce)
132
133 expected = %{
134 "activity_type" => "repeat",
135 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
136 "external_url" => announce.data["id"],
137 "id" => announce.id,
138 "is_local" => true,
139 "is_post_verb" => false,
140 "statusnet_html" => "shp retweeted a status.",
141 "text" => "shp retweeted a status.",
142 "uri" => "tag:#{announce.data["id"]}:objectType=note",
143 "user" => UserView.render("show.json", user: other_user),
144 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
145 "statusnet_conversation_id" => convo_id
146 }
147
148 assert result == expected
149 end
150
151 test "A follow activity" do
152 user = insert(:user)
153 other_user = insert(:user, %{nickname: "shp"})
154
155 {:ok, follower} = User.follow(user, other_user)
156 {:ok, follow} = ActivityPub.follow(follower, other_user)
157
158 result = ActivityView.render("activity.json", activity: follow)
159
160 expected = %{
161 "activity_type" => "follow",
162 "attentions" => [],
163 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
164 "external_url" => follow.data["id"],
165 "id" => follow.id,
166 "in_reply_to_status_id" => nil,
167 "is_local" => true,
168 "is_post_verb" => false,
169 "statusnet_html" => "#{user.nickname} started following shp",
170 "text" => "#{user.nickname} started following shp",
171 "user" => UserView.render("show.json", user: user)
172 }
173
174 assert result == expected
175 end
176
177 test "a delete activity" do
178 user = insert(:user)
179
180 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
181 {:ok, delete} = CommonAPI.delete(activity.id, user)
182
183 result = ActivityView.render("activity.json", activity: delete)
184
185 expected = %{
186 "activity_type" => "delete",
187 "attentions" => [],
188 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
189 "external_url" => delete.data["id"],
190 "id" => delete.id,
191 "in_reply_to_status_id" => nil,
192 "is_local" => true,
193 "is_post_verb" => false,
194 "statusnet_html" => "deleted notice {{tag",
195 "text" => "deleted notice {{tag",
196 "uri" => delete.data["object"],
197 "user" => UserView.render("show.json", user: user)
198 }
199
200 assert result == expected
201 end
202 end