Merge branch 'reserve-user-names' 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.{User, Repo}
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 |> get(url)
92
93 expected =
94 ActivityRepresenter.to_simple_form(note_activity, user, true)
95 |> ActivityRepresenter.wrap_with_entry()
96 |> :xmerl.export_simple(:xmerl_xml)
97 |> to_string
98
99 assert response(conn, 200) == expected
100 end
101
102 test "404s on private objects", %{conn: conn} do
103 note_activity = insert(:direct_note_activity)
104 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
105
106 conn
107 |> get("/objects/#{uuid}")
108 |> response(404)
109 end
110
111 test "404s on nonexisting objects", %{conn: conn} do
112 conn
113 |> get("/objects/123")
114 |> response(404)
115 end
116
117 test "gets an activity", %{conn: conn} do
118 note_activity = insert(:note_activity)
119 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
120
121 conn
122 |> get("/activities/#{uuid}")
123 |> response(200)
124 end
125
126 test "404s on private activities", %{conn: conn} do
127 note_activity = insert(:direct_note_activity)
128 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
129
130 conn
131 |> get("/activities/#{uuid}")
132 |> response(404)
133 end
134
135 test "404s on nonexistent activities", %{conn: conn} do
136 conn
137 |> get("/activities/123")
138 |> response(404)
139 end
140
141 test "gets a notice", %{conn: conn} do
142 note_activity = insert(:note_activity)
143
144 conn
145 |> get("/notice/#{note_activity.id}")
146 |> response(200)
147 end
148
149 test "gets a notice in AS2 format", %{conn: conn} do
150 note_activity = insert(:note_activity)
151
152 conn
153 |> put_req_header("accept", "application/activity+json")
154 |> get("/notice/#{note_activity.id}")
155 |> json_response(200)
156 end
157
158 test "only gets a notice in AS2 format for Create messages", %{conn: conn} do
159 note_activity = insert(:note_activity)
160 url = "/notice/#{note_activity.id}"
161
162 conn =
163 conn
164 |> put_req_header("accept", "application/activity+json")
165 |> get(url)
166
167 assert json_response(conn, 200)
168
169 user = insert(:user)
170
171 {:ok, like_activity, _} = CommonAPI.favorite(note_activity.id, user)
172 url = "/notice/#{like_activity.id}"
173
174 assert like_activity.data["type"] == "Like"
175
176 conn =
177 build_conn()
178 |> put_req_header("accept", "application/activity+json")
179 |> get(url)
180
181 assert response(conn, 404)
182 end
183
184 test "gets an activity in AS2 format", %{conn: conn} do
185 note_activity = insert(:note_activity)
186 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
187 url = "/activities/#{uuid}"
188
189 conn =
190 conn
191 |> put_req_header("accept", "application/activity+json")
192 |> get(url)
193
194 assert json_response(conn, 200)
195 end
196
197 test "404s a private notice", %{conn: conn} do
198 note_activity = insert(:direct_note_activity)
199 url = "/notice/#{note_activity.id}"
200
201 conn =
202 conn
203 |> get(url)
204
205 assert response(conn, 404)
206 end
207
208 test "404s a nonexisting notice", %{conn: conn} do
209 url = "/notice/123"
210
211 conn =
212 conn
213 |> get(url)
214
215 assert response(conn, 404)
216 end
217 end