9be0445c0e47af2fa3dd4d9d7a7b90eb48ac6e8f
[anni] / test / pleroma / integration / mastodon_websocket_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Integration.MastodonWebsocketTest do
6   # Needs a streamer, needs to stay synchronous
7   use Pleroma.DataCase
8
9   import ExUnit.CaptureLog
10   import Pleroma.Factory
11
12   alias Pleroma.Integration.WebsocketClient
13   alias Pleroma.Web.CommonAPI
14   alias Pleroma.Web.OAuth
15
16   @moduletag needs_streamer: true, capture_log: true
17
18   @path Pleroma.Web.Endpoint.url()
19         |> URI.parse()
20         |> Map.put(:scheme, "ws")
21         |> Map.put(:path, "/api/v1/streaming")
22         |> URI.to_string()
23
24   def start_socket(qs \\ nil, headers \\ []) do
25     path =
26       case qs do
27         nil -> @path
28         qs -> @path <> qs
29       end
30
31     WebsocketClient.start_link(self(), path, headers)
32   end
33
34   test "refuses invalid requests" do
35     capture_log(fn ->
36       assert {:error, %WebSockex.RequestError{code: 404}} = start_socket()
37       assert {:error, %WebSockex.RequestError{code: 404}} = start_socket("?stream=ncjdk")
38       Process.sleep(30)
39     end)
40   end
41
42   test "requires authentication and a valid token for protected streams" do
43     capture_log(fn ->
44       assert {:error, %WebSockex.RequestError{code: 401}} =
45                start_socket("?stream=user&access_token=aaaaaaaaaaaa")
46
47       assert {:error, %WebSockex.RequestError{code: 401}} = start_socket("?stream=user")
48       Process.sleep(30)
49     end)
50   end
51
52   test "allows public streams without authentication" do
53     assert {:ok, _} = start_socket("?stream=public")
54     assert {:ok, _} = start_socket("?stream=public:local")
55     assert {:ok, _} = start_socket("?stream=public:remote&instance=lain.com")
56     assert {:ok, _} = start_socket("?stream=hashtag&tag=lain")
57   end
58
59   test "receives well formatted events" do
60     user = insert(:user)
61     {:ok, _} = start_socket("?stream=public")
62     {:ok, activity} = CommonAPI.post(user, %{status: "nice echo chamber"})
63
64     assert_receive {:text, raw_json}, 1_000
65     assert {:ok, json} = Jason.decode(raw_json)
66
67     assert "update" == json["event"]
68     assert json["payload"]
69     assert {:ok, json} = Jason.decode(json["payload"])
70
71     view_json =
72       Pleroma.Web.MastodonAPI.StatusView.render("show.json", activity: activity, for: nil)
73       |> Jason.encode!()
74       |> Jason.decode!()
75
76     assert json == view_json
77   end
78
79   describe "with a valid user token" do
80     setup do
81       {:ok, app} =
82         Pleroma.Repo.insert(
83           OAuth.App.register_changeset(%OAuth.App{}, %{
84             client_name: "client",
85             scopes: ["read"],
86             redirect_uris: "url"
87           })
88         )
89
90       user = insert(:user)
91
92       {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
93
94       {:ok, token} = OAuth.Token.exchange_token(app, auth)
95
96       %{app: app, user: user, token: token}
97     end
98
99     test "accepts valid tokens", state do
100       assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}")
101     end
102
103     test "accepts the 'user' stream", %{token: token} = _state do
104       assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}")
105
106       capture_log(fn ->
107         assert {:error, %WebSockex.RequestError{code: 401}} = start_socket("?stream=user")
108         Process.sleep(30)
109       end)
110     end
111
112     test "accepts the 'user:notification' stream", %{token: token} = _state do
113       assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}")
114
115       capture_log(fn ->
116         assert {:error, %WebSockex.RequestError{code: 401}} =
117                  start_socket("?stream=user:notification")
118
119         Process.sleep(30)
120       end)
121     end
122
123     test "accepts valid token on Sec-WebSocket-Protocol header", %{token: token} do
124       assert {:ok, _} = start_socket("?stream=user", [{"Sec-WebSocket-Protocol", token.token}])
125
126       capture_log(fn ->
127         assert {:error, %WebSockex.RequestError{code: 401}} =
128                  start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}])
129
130         Process.sleep(30)
131       end)
132     end
133
134     test "disconnect when token is revoked", %{app: app, user: user, token: token} do
135       assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}")
136       assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}")
137
138       {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
139
140       {:ok, token2} = OAuth.Token.exchange_token(app, auth)
141       assert {:ok, _} = start_socket("?stream=user&access_token=#{token2.token}")
142
143       OAuth.Token.Strategy.Revoke.revoke(token)
144
145       assert_receive {:close, _}
146       assert_receive {:close, _}
147       refute_receive {:close, _}
148     end
149   end
150 end