ostatus controller: respond with AS2 objects instead of activities to notice URIs
[akkoma] / test / web / ostatus / ostatus_controller_test.exs
1 defmodule Pleroma.Web.OStatus.OStatusControllerTest do
2 use Pleroma.Web.ConnCase
3 import Pleroma.Factory
4 alias Pleroma.{User, Repo}
5 alias Pleroma.Web.CommonAPI
6 alias Pleroma.Web.OStatus.ActivityRepresenter
7
8 test "decodes a salmon", %{conn: conn} do
9 user = insert(:user)
10 salmon = File.read!("test/fixtures/salmon.xml")
11
12 conn =
13 conn
14 |> put_req_header("content-type", "application/atom+xml")
15 |> post("/users/#{user.nickname}/salmon", salmon)
16
17 assert response(conn, 200)
18 end
19
20 test "decodes a salmon with a changed magic key", %{conn: conn} do
21 user = insert(:user)
22 salmon = File.read!("test/fixtures/salmon.xml")
23
24 conn =
25 conn
26 |> put_req_header("content-type", "application/atom+xml")
27 |> post("/users/#{user.nickname}/salmon", salmon)
28
29 assert response(conn, 200)
30
31 # Set a wrong magic-key for a user so it has to refetch
32 salmon_user = User.get_by_ap_id("http://gs.example.org:4040/index.php/user/1")
33 # Wrong key
34 info =
35 salmon_user.info
36 |> Map.put(
37 "magic_key",
38 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
39 )
40
41 Repo.update(User.info_changeset(salmon_user, %{info: info}))
42
43 conn =
44 build_conn()
45 |> put_req_header("content-type", "application/atom+xml")
46 |> post("/users/#{user.nickname}/salmon", salmon)
47
48 assert response(conn, 200)
49 end
50
51 test "gets a feed", %{conn: conn} do
52 note_activity = insert(:note_activity)
53 user = User.get_cached_by_ap_id(note_activity.data["actor"])
54
55 conn =
56 conn
57 |> put_req_header("content-type", "application/atom+xml")
58 |> get("/users/#{user.nickname}/feed.atom")
59
60 assert response(conn, 200) =~ note_activity.data["object"]["content"]
61 end
62
63 test "returns 404 for a missing feed", %{conn: conn} do
64 conn =
65 conn
66 |> put_req_header("content-type", "application/atom+xml")
67 |> get("/users/nonexisting/feed.atom")
68
69 assert response(conn, 404)
70 end
71
72 test "gets an object", %{conn: conn} do
73 note_activity = insert(:note_activity)
74 user = User.get_by_ap_id(note_activity.data["actor"])
75 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
76 url = "/objects/#{uuid}"
77
78 conn =
79 conn
80 |> get(url)
81
82 expected =
83 ActivityRepresenter.to_simple_form(note_activity, user, true)
84 |> ActivityRepresenter.wrap_with_entry()
85 |> :xmerl.export_simple(:xmerl_xml)
86 |> to_string
87
88 assert response(conn, 200) == expected
89 end
90
91 test "404s on private objects", %{conn: conn} do
92 note_activity = insert(:direct_note_activity)
93 user = User.get_by_ap_id(note_activity.data["actor"])
94 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
95 url = "/objects/#{uuid}"
96
97 conn =
98 conn
99 |> get(url)
100
101 assert response(conn, 404)
102 end
103
104 test "404s on nonexisting objects", %{conn: conn} do
105 url = "/objects/123"
106
107 conn =
108 conn
109 |> get(url)
110
111 assert response(conn, 404)
112 end
113
114 test "gets an activity", %{conn: conn} do
115 note_activity = insert(:note_activity)
116 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
117 url = "/activities/#{uuid}"
118
119 conn =
120 conn
121 |> get(url)
122
123 assert response(conn, 200)
124 end
125
126 test "404s on private activities", %{conn: conn} do
127 note_activity = insert(:direct_note_activity)
128 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
129 url = "/activities/#{uuid}"
130
131 conn =
132 conn
133 |> get(url)
134
135 assert response(conn, 404)
136 end
137
138 test "404s on nonexistent activities", %{conn: conn} do
139 url = "/activities/123"
140
141 conn =
142 conn
143 |> get(url)
144
145 assert response(conn, 404)
146 end
147
148 test "gets a notice", %{conn: conn} do
149 note_activity = insert(:note_activity)
150 url = "/notice/#{note_activity.id}"
151
152 conn =
153 conn
154 |> get(url)
155
156 assert response(conn, 200)
157 end
158
159 test "gets a notice in AS2 format", %{conn: conn} do
160 note_activity = insert(:note_activity)
161 url = "/notice/#{note_activity.id}"
162
163 conn =
164 conn
165 |> put_req_header("accept", "application/activity+json")
166 |> get(url)
167
168 assert json_response(conn, 200)
169 end
170
171 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
172 note_activity = insert(:note_activity)
173 url = "/notice/#{note_activity.id}"
174
175 conn =
176 conn
177 |> put_req_header("accept", "application/activity+json")
178 |> get(url)
179
180 assert json_response(conn, 200)
181
182 user = insert(:user)
183
184 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
185 url = "/notice/#{like_activity.id}"
186
187 assert like_activity.data["type"] == "Like"
188
189 conn =
190 build_conn()
191 |> put_req_header("accept", "application/activity+json")
192 |> get(url)
193
194 assert response(conn, 404)
195 end
196
197 test "gets an activity in AS2 format", %{conn: conn} do
198 note_activity = insert(:note_activity)
199 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
200 url = "/activities/#{uuid}"
201
202 conn =
203 conn
204 |> put_req_header("accept", "application/activity+json")
205 |> get(url)
206
207 assert json_response(conn, 200)
208 end
209
210 test "404s a private notice", %{conn: conn} do
211 note_activity = insert(:direct_note_activity)
212 url = "/notice/#{note_activity.id}"
213
214 conn =
215 conn
216 |> get(url)
217
218 assert response(conn, 404)
219 end
220
221 test "404s a nonexisting notice", %{conn: conn} do
222 url = "/notice/123"
223
224 conn =
225 conn
226 |> get(url)
227
228 assert response(conn, 404)
229 end
230 end