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