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