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