7f003226bed64b9a914a65d937c1cfc4bed84796
[akkoma] / test / web / ostatus / activity_representer_test.exs
1 defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.OStatus.ActivityRepresenter
5 alias Pleroma.{User, Activity, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7
8 import Pleroma.Factory
9
10 test "a note activity" do
11 note_activity = insert(:note_activity)
12 updated_at = note_activity.updated_at
13 |> NaiveDateTime.to_iso8601
14 inserted_at = note_activity.inserted_at
15 |> NaiveDateTime.to_iso8601
16
17 user = User.get_cached_by_ap_id(note_activity.data["actor"])
18
19 expected = """
20 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
21 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
22 <id>#{note_activity.data["object"]["id"]}</id>
23 <title>New note by #{user.nickname}</title>
24 <content type="html">#{note_activity.data["object"]["content"]}</content>
25 <published>#{inserted_at}</published>
26 <updated>#{updated_at}</updated>
27 <ostatus:conversation>#{note_activity.data["context"]}</ostatus:conversation>
28 <link href="#{note_activity.data["context"]}" rel="ostatus:conversation" />
29 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
30 """
31
32 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
33
34 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
35
36 assert clean(res) == clean(expected)
37 end
38
39 test "a reply note" do
40 note = insert(:note_activity)
41 answer = insert(:note_activity)
42 object = answer.data["object"]
43 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
44
45 data = %{answer.data | "object" => object}
46 answer = %{answer | data: data}
47
48 updated_at = answer.updated_at
49 |> NaiveDateTime.to_iso8601
50 inserted_at = answer.inserted_at
51 |> NaiveDateTime.to_iso8601
52
53 user = User.get_cached_by_ap_id(answer.data["actor"])
54
55 expected = """
56 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
57 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
58 <id>#{answer.data["object"]["id"]}</id>
59 <title>New note by #{user.nickname}</title>
60 <content type="html">#{answer.data["object"]["content"]}</content>
61 <published>#{inserted_at}</published>
62 <updated>#{updated_at}</updated>
63 <ostatus:conversation>#{answer.data["context"]}</ostatus:conversation>
64 <link href="#{answer.data["context"]}" rel="ostatus:conversation" />
65 <thr:in-reply-to ref="#{note.data["object"]["id"]}" />
66 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
67 """
68
69 tuple = ActivityRepresenter.to_simple_form(answer, user)
70
71 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
72
73 assert clean(res) == clean(expected)
74 end
75
76 test "an announce activity" do
77 note = insert(:note_activity)
78 user = insert(:user)
79 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
80
81 {:ok, announce, object} = ActivityPub.announce(user, object)
82
83 announce = Repo.get(Activity, announce.id)
84
85 note_user = User.get_cached_by_ap_id(note.data["actor"])
86 note = Repo.get(Activity, note.id)
87 note_xml = ActivityRepresenter.to_simple_form(note, note_user, true)
88 |> :xmerl.export_simple_content(:xmerl_xml)
89 |> to_string
90
91 updated_at = announce.updated_at
92 |> NaiveDateTime.to_iso8601
93 inserted_at = announce.inserted_at
94 |> NaiveDateTime.to_iso8601
95
96 expected = """
97 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
98 <id>#{announce.data["id"]}</id>
99 <title>#{user.nickname} repeated a notice</title>
100 <content type="html">RT #{note.data["object"]["content"]}</content>
101 <published>#{inserted_at}</published>
102 <updated>#{updated_at}</updated>
103 <ostatus:conversation>#{announce.data["context"]}</ostatus:conversation>
104 <link href="#{announce.data["context"]}" rel="ostatus:conversation" />
105 <thr:in-reply-to ref="#{note.data["object"]["id"]}" />
106 <activity:object>
107 #{note_xml}
108 </activity:object>
109 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
110 """
111
112 announce_xml = ActivityRepresenter.to_simple_form(announce, user)
113 |> :xmerl.export_simple_content(:xmerl_xml)
114 |> to_string
115
116 assert clean(expected) == clean(announce_xml)
117 end
118
119 test "a like activity" do
120 note = insert(:note)
121 user = insert(:user)
122 {:ok, like, _note} = ActivityPub.like(user, note)
123
124 updated_at = like.updated_at
125 |> NaiveDateTime.to_iso8601
126 inserted_at = like.inserted_at
127 |> NaiveDateTime.to_iso8601
128
129 tuple = ActivityRepresenter.to_simple_form(like, user)
130 refute is_nil(tuple)
131
132 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
133
134 expected = """
135 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
136 <id>#{like.data["id"]}</id>
137 <title>New favorite by #{user.nickname}</title>
138 <content type="html">#{user.nickname} favorited something</content>
139 <published>#{inserted_at}</published>
140 <updated>#{updated_at}</updated>
141 <activity:object>
142 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
143 <id>#{note.data["id"]}</id>
144 </activity:object>
145 <ostatus:conversation>#{like.data["context"]}</ostatus:conversation>
146 <link href="#{like.data["context"]}" rel="ostatus:conversation" />
147 <thr:in-reply-to ref="#{note.data["id"]}" />
148 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
149 """
150
151 assert clean(res) == clean(expected)
152 end
153
154 test "an unknown activity" do
155 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
156 assert is_nil(tuple)
157 end
158
159 defp clean(string) do
160 String.replace(string, ~r/\s/, "")
161 end
162 end