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