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