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