summaryrefslogtreecommitdiff
path: root/test/web/static_fe/static_fe_controller_test.exs
blob: 2c999295aa9df75a7575200ed0f2a3ebcaf00b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do
  use Pleroma.Web.ConnCase
  alias Pleroma.Activity
  alias Pleroma.Web.ActivityPub.Transmogrifier
  alias Pleroma.Web.CommonAPI

  import Pleroma.Factory

  clear_config_all([:static_fe, :enabled]) do
    Pleroma.Config.put([:static_fe, :enabled], true)
  end

  describe "user profile page" do
    test "just the profile as HTML", %{conn: conn} do
      user = insert(:user)

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/users/#{user.nickname}")

      assert html_response(conn, 200) =~ user.nickname
    end

    test "renders json unless there's an html accept header", %{conn: conn} do
      user = insert(:user)

      conn =
        conn
        |> put_req_header("accept", "application/json")
        |> get("/users/#{user.nickname}")

      assert json_response(conn, 200)
    end

    test "404 when user not found", %{conn: conn} do
      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/users/limpopo")

      assert html_response(conn, 404) =~ "not found"
    end

    test "profile does not include private messages", %{conn: conn} do
      user = insert(:user)
      CommonAPI.post(user, %{"status" => "public"})
      CommonAPI.post(user, %{"status" => "private", "visibility" => "private"})

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/users/#{user.nickname}")

      html = html_response(conn, 200)

      assert html =~ ">public<"
      refute html =~ ">private<"
    end

    test "pagination", %{conn: conn} do
      user = insert(:user)
      Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end)

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/users/#{user.nickname}")

      html = html_response(conn, 200)

      assert html =~ ">test30<"
      assert html =~ ">test11<"
      refute html =~ ">test10<"
      refute html =~ ">test1<"
    end

    test "pagination, page 2", %{conn: conn} do
      user = insert(:user)
      activities = Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end)
      {:ok, a11} = Enum.at(activities, 11)

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/users/#{user.nickname}?max_id=#{a11.id}")

      html = html_response(conn, 200)

      assert html =~ ">test1<"
      assert html =~ ">test10<"
      refute html =~ ">test20<"
      refute html =~ ">test29<"
    end
  end

  describe "notice rendering" do
    test "single notice page", %{conn: conn} do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => "testing a thing!"})

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/notice/#{activity.id}")

      html = html_response(conn, 200)
      assert html =~ "<header>"
      assert html =~ user.nickname
      assert html =~ "testing a thing!"
    end

    test "filters HTML tags", %{conn: conn} do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => "<script>alert('xss')</script>"})

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/notice/#{activity.id}")

      html = html_response(conn, 200)
      assert html =~ ~s[&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;]
    end

    test "shows the whole thread", %{conn: conn} do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => "space: the final frontier"})

      CommonAPI.post(user, %{
        "status" => "these are the voyages or something",
        "in_reply_to_status_id" => activity.id
      })

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/notice/#{activity.id}")

      html = html_response(conn, 200)
      assert html =~ "the final frontier"
      assert html =~ "voyages"
    end

    test "redirect by AP object ID", %{conn: conn} do
      user = insert(:user)

      {:ok, %Activity{data: %{"object" => object_url}}} =
        CommonAPI.post(user, %{"status" => "beam me up"})

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get(URI.parse(object_url).path)

      assert html_response(conn, 302) =~ "redirected"
    end

    test "redirect by activity ID", %{conn: conn} do
      user = insert(:user)

      {:ok, %Activity{data: %{"id" => id}}} =
        CommonAPI.post(user, %{"status" => "I'm a doctor, not a devops!"})

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get(URI.parse(id).path)

      assert html_response(conn, 302) =~ "redirected"
    end

    test "404 when notice not found", %{conn: conn} do
      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/notice/88c9c317")

      assert html_response(conn, 404) =~ "not found"
    end

    test "404 for private status", %{conn: conn} do
      user = insert(:user)

      {:ok, activity} =
        CommonAPI.post(user, %{"status" => "don't show me!", "visibility" => "private"})

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/notice/#{activity.id}")

      assert html_response(conn, 404) =~ "not found"
    end

    test "302 for remote cached status", %{conn: conn} do
      user = insert(:user)

      message = %{
        "@context" => "https://www.w3.org/ns/activitystreams",
        "to" => user.follower_address,
        "cc" => "https://www.w3.org/ns/activitystreams#Public",
        "type" => "Create",
        "object" => %{
          "content" => "blah blah blah",
          "type" => "Note",
          "attributedTo" => user.ap_id,
          "inReplyTo" => nil
        },
        "actor" => user.ap_id
      }

      assert {:ok, activity} = Transmogrifier.handle_incoming(message)

      conn =
        conn
        |> put_req_header("accept", "text/html")
        |> get("/notice/#{activity.id}")

      assert html_response(conn, 302) =~ "redirected"
    end
  end
end