2c999295aa9df75a7575200ed0f2a3ebcaf00b2b
[akkoma] / test / web / static_fe / static_fe_controller_test.exs
1 defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do
2 use Pleroma.Web.ConnCase
3 alias Pleroma.Activity
4 alias Pleroma.Web.ActivityPub.Transmogrifier
5 alias Pleroma.Web.CommonAPI
6
7 import Pleroma.Factory
8
9 clear_config_all([:static_fe, :enabled]) do
10 Pleroma.Config.put([:static_fe, :enabled], true)
11 end
12
13 describe "user profile page" do
14 test "just the profile as HTML", %{conn: conn} do
15 user = insert(:user)
16
17 conn =
18 conn
19 |> put_req_header("accept", "text/html")
20 |> get("/users/#{user.nickname}")
21
22 assert html_response(conn, 200) =~ user.nickname
23 end
24
25 test "renders json unless there's an html accept header", %{conn: conn} do
26 user = insert(:user)
27
28 conn =
29 conn
30 |> put_req_header("accept", "application/json")
31 |> get("/users/#{user.nickname}")
32
33 assert json_response(conn, 200)
34 end
35
36 test "404 when user not found", %{conn: conn} do
37 conn =
38 conn
39 |> put_req_header("accept", "text/html")
40 |> get("/users/limpopo")
41
42 assert html_response(conn, 404) =~ "not found"
43 end
44
45 test "profile does not include private messages", %{conn: conn} do
46 user = insert(:user)
47 CommonAPI.post(user, %{"status" => "public"})
48 CommonAPI.post(user, %{"status" => "private", "visibility" => "private"})
49
50 conn =
51 conn
52 |> put_req_header("accept", "text/html")
53 |> get("/users/#{user.nickname}")
54
55 html = html_response(conn, 200)
56
57 assert html =~ ">public<"
58 refute html =~ ">private<"
59 end
60
61 test "pagination", %{conn: conn} do
62 user = insert(:user)
63 Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end)
64
65 conn =
66 conn
67 |> put_req_header("accept", "text/html")
68 |> get("/users/#{user.nickname}")
69
70 html = html_response(conn, 200)
71
72 assert html =~ ">test30<"
73 assert html =~ ">test11<"
74 refute html =~ ">test10<"
75 refute html =~ ">test1<"
76 end
77
78 test "pagination, page 2", %{conn: conn} do
79 user = insert(:user)
80 activities = Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end)
81 {:ok, a11} = Enum.at(activities, 11)
82
83 conn =
84 conn
85 |> put_req_header("accept", "text/html")
86 |> get("/users/#{user.nickname}?max_id=#{a11.id}")
87
88 html = html_response(conn, 200)
89
90 assert html =~ ">test1<"
91 assert html =~ ">test10<"
92 refute html =~ ">test20<"
93 refute html =~ ">test29<"
94 end
95 end
96
97 describe "notice rendering" do
98 test "single notice page", %{conn: conn} do
99 user = insert(:user)
100 {:ok, activity} = CommonAPI.post(user, %{"status" => "testing a thing!"})
101
102 conn =
103 conn
104 |> put_req_header("accept", "text/html")
105 |> get("/notice/#{activity.id}")
106
107 html = html_response(conn, 200)
108 assert html =~ "<header>"
109 assert html =~ user.nickname
110 assert html =~ "testing a thing!"
111 end
112
113 test "filters HTML tags", %{conn: conn} do
114 user = insert(:user)
115 {:ok, activity} = CommonAPI.post(user, %{"status" => "<script>alert('xss')</script>"})
116
117 conn =
118 conn
119 |> put_req_header("accept", "text/html")
120 |> get("/notice/#{activity.id}")
121
122 html = html_response(conn, 200)
123 assert html =~ ~s[&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;]
124 end
125
126 test "shows the whole thread", %{conn: conn} do
127 user = insert(:user)
128 {:ok, activity} = CommonAPI.post(user, %{"status" => "space: the final frontier"})
129
130 CommonAPI.post(user, %{
131 "status" => "these are the voyages or something",
132 "in_reply_to_status_id" => activity.id
133 })
134
135 conn =
136 conn
137 |> put_req_header("accept", "text/html")
138 |> get("/notice/#{activity.id}")
139
140 html = html_response(conn, 200)
141 assert html =~ "the final frontier"
142 assert html =~ "voyages"
143 end
144
145 test "redirect by AP object ID", %{conn: conn} do
146 user = insert(:user)
147
148 {:ok, %Activity{data: %{"object" => object_url}}} =
149 CommonAPI.post(user, %{"status" => "beam me up"})
150
151 conn =
152 conn
153 |> put_req_header("accept", "text/html")
154 |> get(URI.parse(object_url).path)
155
156 assert html_response(conn, 302) =~ "redirected"
157 end
158
159 test "redirect by activity ID", %{conn: conn} do
160 user = insert(:user)
161
162 {:ok, %Activity{data: %{"id" => id}}} =
163 CommonAPI.post(user, %{"status" => "I'm a doctor, not a devops!"})
164
165 conn =
166 conn
167 |> put_req_header("accept", "text/html")
168 |> get(URI.parse(id).path)
169
170 assert html_response(conn, 302) =~ "redirected"
171 end
172
173 test "404 when notice not found", %{conn: conn} do
174 conn =
175 conn
176 |> put_req_header("accept", "text/html")
177 |> get("/notice/88c9c317")
178
179 assert html_response(conn, 404) =~ "not found"
180 end
181
182 test "404 for private status", %{conn: conn} do
183 user = insert(:user)
184
185 {:ok, activity} =
186 CommonAPI.post(user, %{"status" => "don't show me!", "visibility" => "private"})
187
188 conn =
189 conn
190 |> put_req_header("accept", "text/html")
191 |> get("/notice/#{activity.id}")
192
193 assert html_response(conn, 404) =~ "not found"
194 end
195
196 test "302 for remote cached status", %{conn: conn} do
197 user = insert(:user)
198
199 message = %{
200 "@context" => "https://www.w3.org/ns/activitystreams",
201 "to" => user.follower_address,
202 "cc" => "https://www.w3.org/ns/activitystreams#Public",
203 "type" => "Create",
204 "object" => %{
205 "content" => "blah blah blah",
206 "type" => "Note",
207 "attributedTo" => user.ap_id,
208 "inReplyTo" => nil
209 },
210 "actor" => user.ap_id
211 }
212
213 assert {:ok, activity} = Transmogrifier.handle_incoming(message)
214
215 conn =
216 conn
217 |> put_req_header("accept", "text/html")
218 |> get("/notice/#{activity.id}")
219
220 assert html_response(conn, 302) =~ "redirected"
221 end
222 end
223 end