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