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