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