fd1b1598c1b246825ebab0387532f5e64f2fc213
[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}
6
7 import Pleroma.Factory
8
9 test "a note activity" do
10 note_activity = insert(:note_activity)
11 updated_at = note_activity.updated_at
12 |> NaiveDateTime.to_iso8601
13 inserted_at = note_activity.inserted_at
14 |> NaiveDateTime.to_iso8601
15
16 user = User.get_cached_by_ap_id(note_activity.data["actor"])
17
18 expected = """
19 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
20 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
21 <id>#{note_activity.data["id"]}</id>
22 <title>New note by #{user.nickname}</title>
23 <content type="html">#{note_activity.data["object"]["content"]}</content>
24 <published>#{inserted_at}</published>
25 <updated>#{updated_at}</updated>
26 <ostatus:conversation>#{note_activity.data["context"]}</ostatus:conversation>
27 <link href="#{note_activity.data["context"]}" rel="ostatus:conversation" />
28 """
29
30 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
31
32 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
33
34 assert clean(res) == clean(expected)
35 end
36
37 test "a reply note" do
38 note = insert(:note_activity)
39 answer = insert(:note_activity)
40 object = answer.data["object"]
41 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
42
43 data = %{answer.data | "object" => object}
44 answer = %{answer | data: data}
45
46 updated_at = answer.updated_at
47 |> NaiveDateTime.to_iso8601
48 inserted_at = answer.inserted_at
49 |> NaiveDateTime.to_iso8601
50
51 user = User.get_cached_by_ap_id(answer.data["actor"])
52
53 expected = """
54 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
55 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
56 <id>#{answer.data["id"]}</id>
57 <title>New note by #{user.nickname}</title>
58 <content type="html">#{answer.data["object"]["content"]}</content>
59 <published>#{inserted_at}</published>
60 <updated>#{updated_at}</updated>
61 <ostatus:conversation>#{answer.data["context"]}</ostatus:conversation>
62 <link href="#{answer.data["context"]}" rel="ostatus:conversation" />
63 <thr:in-reply-to ref="#{note.data["id"]}" />
64 """
65
66 tuple = ActivityRepresenter.to_simple_form(answer, user)
67
68 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
69
70 assert clean(res) == clean(expected)
71 end
72
73 test "an unknown activity" do
74 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
75 assert is_nil(tuple)
76 end
77
78 defp clean(string) do
79 String.replace(string, ~r/\s/, "")
80 end
81 end