Resolve merge conflict
[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 test "decodes a salmon", %{conn: conn} do
18 user = insert(:user)
19 salmon = File.read!("test/fixtures/salmon.xml")
20
21 conn =
22 conn
23 |> put_req_header("content-type", "application/atom+xml")
24 |> post("/users/#{user.nickname}/salmon", salmon)
25
26 assert response(conn, 200)
27 end
28
29 test "decodes a salmon with a changed magic key", %{conn: conn} do
30 user = insert(:user)
31 salmon = File.read!("test/fixtures/salmon.xml")
32
33 conn =
34 conn
35 |> put_req_header("content-type", "application/atom+xml")
36 |> post("/users/#{user.nickname}/salmon", salmon)
37
38 assert response(conn, 200)
39
40 # Set a wrong magic-key for a user so it has to refetch
41 salmon_user = User.get_by_ap_id("http://gs.example.org:4040/index.php/user/1")
42 # Wrong key
43 info_cng =
44 User.Info.remote_user_creation(salmon_user.info, %{
45 magic_key:
46 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
47 })
48
49 salmon_user
50 |> Ecto.Changeset.change()
51 |> Ecto.Changeset.put_embed(:info, info_cng)
52 |> Repo.update()
53
54 conn =
55 build_conn()
56 |> put_req_header("content-type", "application/atom+xml")
57 |> post("/users/#{user.nickname}/salmon", salmon)
58
59 assert response(conn, 200)
60 end
61
62 test "gets a feed", %{conn: conn} do
63 note_activity = insert(:note_activity)
64 user = User.get_cached_by_ap_id(note_activity.data["actor"])
65
66 conn =
67 conn
68 |> put_req_header("content-type", "application/atom+xml")
69 |> get("/users/#{user.nickname}/feed.atom")
70
71 assert response(conn, 200) =~ note_activity.data["object"]["content"]
72 end
73
74 test "returns 404 for a missing feed", %{conn: conn} do
75 conn =
76 conn
77 |> put_req_header("content-type", "application/atom+xml")
78 |> get("/users/nonexisting/feed.atom")
79
80 assert response(conn, 404)
81 end
82
83 test "gets an object", %{conn: conn} do
84 note_activity = insert(:note_activity)
85 user = User.get_by_ap_id(note_activity.data["actor"])
86 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
87 url = "/objects/#{uuid}"
88
89 conn =
90 conn
91 |> put_req_header("accept", "application/xml")
92 |> get(url)
93
94 expected =
95 ActivityRepresenter.to_simple_form(note_activity, user, true)
96 |> ActivityRepresenter.wrap_with_entry()
97 |> :xmerl.export_simple(:xmerl_xml)
98 |> to_string
99
100 assert response(conn, 200) == expected
101 end
102
103 test "404s on private objects", %{conn: conn} do
104 note_activity = insert(:direct_note_activity)
105 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
106
107 conn
108 |> get("/objects/#{uuid}")
109 |> response(404)
110 end
111
112 test "404s on nonexisting objects", %{conn: conn} do
113 conn
114 |> get("/objects/123")
115 |> response(404)
116 end
117
118 test "gets an activity in xml format", %{conn: conn} do
119 note_activity = insert(:note_activity)
120 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
121
122 conn
123 |> put_req_header("accept", "application/xml")
124 |> get("/activities/#{uuid}")
125 |> response(200)
126 end
127
128 test "404s on deleted objects", %{conn: conn} do
129 note_activity = insert(:note_activity)
130 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
131 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
132
133 conn
134 |> get("/objects/#{uuid}")
135 |> response(200)
136
137 Object.delete(object)
138
139 conn
140 |> get("/objects/#{uuid}")
141 |> response(404)
142 end
143
144 test "404s on private activities", %{conn: conn} do
145 note_activity = insert(:direct_note_activity)
146 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
147
148 conn
149 |> get("/activities/#{uuid}")
150 |> response(404)
151 end
152
153 test "404s on nonexistent activities", %{conn: conn} do
154 conn
155 |> get("/activities/123")
156 |> response(404)
157 end
158
159 test "renders notice metatags in html format", %{conn: conn} do
160 note_activity = insert(:note_activity)
161 conn = get(conn, "/notice/#{note_activity.id}")
162 body = html_response(conn, 200)
163 twitter_card_summary = "<meta content=\"summary\" property=\"twitter:card\">"
164
165 description_content =
166 "<meta content=\"#{note_activity.data["object"]["content"]}\" property=\"og:description\">"
167
168 assert body =~ twitter_card_summary
169 assert body =~ description_content
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