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