Add registration to the TwAPI controller.
[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.{UserRepresenter, ActivityRepresenter, ObjectRepresenter}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Builders.UserBuilder
7 import Pleroma.Factory
8
9 test "an announce activity" do
10 user = insert(:user)
11 note_activity = insert(:note_activity)
12 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
13 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
14
15 {:ok, announce_activity, _object} = ActivityPub.announce(user, object)
16 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
17
18 status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user})
19
20 assert status["id"] == announce_activity.id
21 assert status["user"] == UserRepresenter.to_map(user, %{for: user})
22
23 retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
24 assert retweeted_status["repeated"] == true
25
26 assert status["retweeted_status"] == retweeted_status
27 end
28
29 test "a like activity" do
30 user = insert(:user)
31 note_activity = insert(:note_activity)
32 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
33
34 {:ok, like_activity, _object} = ActivityPub.like(user, object)
35 status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
36
37 assert status["id"] == like_activity.id
38 assert status["in_reply_to_status_id"] == note_activity.id
39
40 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
41 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
42 liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
43 assert liked_status["favorited"] == true
44 end
45
46 test "an activity" do
47 {:ok, user} = UserBuilder.insert
48 {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
49 {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
50
51 object = %Object{
52 data: %{
53 "type" => "Image",
54 "url" => [
55 %{
56 "type" => "Link",
57 "mediaType" => "image/jpg",
58 "href" => "http://example.org/image.jpg"
59 }
60 ],
61 "uuid" => 1
62 }
63 }
64
65 content_html = "Some content mentioning <a href='shp'>@shp</shp>"
66 content = HtmlSanitizeEx.strip_tags(content_html)
67 date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601
68
69 activity = %Activity{
70 id: 1,
71 data: %{
72 "type" => "Create",
73 "to" => [
74 User.ap_followers(user),
75 "https://www.w3.org/ns/activitystreams#Public",
76 mentioned_user.ap_id
77 ],
78 "actor" => User.ap_id(user),
79 "object" => %{
80 "published" => date,
81 "type" => "Note",
82 "content" => content_html,
83 "inReplyToStatusId" => 213123,
84 "statusnetConversationId" => 4711,
85 "attachment" => [
86 object
87 ],
88 "like_count" => 5,
89 "announcement_count" => 3
90 },
91 "published" => date
92 }
93 }
94
95
96 expected_status = %{
97 "id" => activity.id,
98 "user" => UserRepresenter.to_map(user, %{for: follower}),
99 "is_local" => true,
100 "attentions" => [],
101 "statusnet_html" => content_html,
102 "text" => content,
103 "is_post_verb" => true,
104 "created_at" => "Tue May 24 13:26:08 +0000 2016",
105 "in_reply_to_status_id" => 213123,
106 "statusnet_conversation_id" => 4711,
107 "attachments" => [
108 ObjectRepresenter.to_map(object)
109 ],
110 "attentions" => [
111 UserRepresenter.to_map(mentioned_user, %{for: follower})
112 ],
113 "fave_num" => 5,
114 "repeat_num" => 3,
115 "favorited" => false,
116 "repeated" => false
117 }
118
119 assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
120 end
121 end