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