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