Merge branch 'features/sec-websocket-protocol-header' 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 user = User.get_cached_by_ap_id(note_activity.data["actor"])
76
77 conn =
78 conn
79 |> put_req_header("content-type", "application/atom+xml")
80 |> get("/users/#{user.nickname}/feed.atom")
81
82 assert response(conn, 200) =~ note_activity.data["object"]["content"]
83 end
84
85 test "returns 404 for a missing feed", %{conn: conn} do
86 conn =
87 conn
88 |> put_req_header("content-type", "application/atom+xml")
89 |> get("/users/nonexisting/feed.atom")
90
91 assert response(conn, 404)
92 end
93
94 test "gets an object", %{conn: conn} do
95 note_activity = insert(:note_activity)
96 user = User.get_cached_by_ap_id(note_activity.data["actor"])
97 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
98 url = "/objects/#{uuid}"
99
100 conn =
101 conn
102 |> put_req_header("accept", "application/xml")
103 |> get(url)
104
105 expected =
106 ActivityRepresenter.to_simple_form(note_activity, user, true)
107 |> ActivityRepresenter.wrap_with_entry()
108 |> :xmerl.export_simple(:xmerl_xml)
109 |> to_string
110
111 assert response(conn, 200) == expected
112 end
113
114 test "404s on private objects", %{conn: conn} do
115 note_activity = insert(:direct_note_activity)
116 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
117
118 conn
119 |> get("/objects/#{uuid}")
120 |> response(404)
121 end
122
123 test "404s on nonexisting objects", %{conn: conn} do
124 conn
125 |> get("/objects/123")
126 |> response(404)
127 end
128
129 test "gets an activity in xml format", %{conn: conn} do
130 note_activity = insert(:note_activity)
131 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
132
133 conn
134 |> put_req_header("accept", "application/xml")
135 |> get("/activities/#{uuid}")
136 |> response(200)
137 end
138
139 test "404s on deleted objects", %{conn: conn} do
140 note_activity = insert(:note_activity)
141 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
142 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
143
144 conn
145 |> put_req_header("accept", "application/xml")
146 |> get("/objects/#{uuid}")
147 |> response(200)
148
149 Object.delete(object)
150
151 conn
152 |> put_req_header("accept", "application/xml")
153 |> get("/objects/#{uuid}")
154 |> response(404)
155 end
156
157 test "404s on private activities", %{conn: conn} do
158 note_activity = insert(:direct_note_activity)
159 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
160
161 conn
162 |> get("/activities/#{uuid}")
163 |> response(404)
164 end
165
166 test "404s on nonexistent activities", %{conn: conn} do
167 conn
168 |> get("/activities/123")
169 |> response(404)
170 end
171
172 test "gets a notice in xml format", %{conn: conn} do
173 note_activity = insert(:note_activity)
174
175 conn
176 |> get("/notice/#{note_activity.id}")
177 |> response(200)
178 end
179
180 test "gets a notice in AS2 format", %{conn: conn} do
181 note_activity = insert(:note_activity)
182
183 conn
184 |> put_req_header("accept", "application/activity+json")
185 |> get("/notice/#{note_activity.id}")
186 |> json_response(200)
187 end
188
189 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
190 note_activity = insert(:note_activity)
191 url = "/notice/#{note_activity.id}"
192
193 conn =
194 conn
195 |> put_req_header("accept", "application/activity+json")
196 |> get(url)
197
198 assert json_response(conn, 200)
199
200 user = insert(:user)
201
202 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
203 url = "/notice/#{like_activity.id}"
204
205 assert like_activity.data["type"] == "Like"
206
207 conn =
208 build_conn()
209 |> put_req_header("accept", "application/activity+json")
210 |> get(url)
211
212 assert response(conn, 404)
213 end
214
215 test "gets an activity in AS2 format", %{conn: conn} do
216 note_activity = insert(:note_activity)
217 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
218 url = "/activities/#{uuid}"
219
220 conn =
221 conn
222 |> put_req_header("accept", "application/activity+json")
223 |> get(url)
224
225 assert json_response(conn, 200)
226 end
227
228 test "404s a private notice", %{conn: conn} do
229 note_activity = insert(:direct_note_activity)
230 url = "/notice/#{note_activity.id}"
231
232 conn =
233 conn
234 |> get(url)
235
236 assert response(conn, 404)
237 end
238
239 test "404s a nonexisting notice", %{conn: conn} do
240 url = "/notice/123"
241
242 conn =
243 conn
244 |> get(url)
245
246 assert response(conn, 404)
247 end
248 end