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