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