fix formatting
[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 Pleroma.Factory
9
10 alias Pleroma.Integration.WebsocketClient
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.OAuth
13 alias Pleroma.Web.Streamer
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 do
22 GenServer.start(Streamer, %{}, name: Streamer)
23
24 on_exit(fn ->
25 if pid = Process.whereis(Streamer) do
26 Process.exit(pid, :kill)
27 end
28 end)
29 end
30
31 def start_socket(qs \\ nil, headers \\ []) do
32 path =
33 case qs do
34 nil -> @path
35 qs -> @path <> qs
36 end
37
38 WebsocketClient.start_link(self(), path, headers)
39 end
40
41 test "refuses invalid requests" do
42 assert {:error, {400, _}} = start_socket()
43 assert {:error, {404, _}} = start_socket("?stream=ncjdk")
44 end
45
46 test "requires authentication and a valid token for protected streams" do
47 assert {:error, {403, _}} = start_socket("?stream=user&access_token=aaaaaaaaaaaa")
48 assert {:error, {403, _}} = start_socket("?stream=user")
49 end
50
51 test "allows public streams without authentication" do
52 assert {:ok, _} = start_socket("?stream=public")
53 assert {:ok, _} = start_socket("?stream=public:local")
54 assert {:ok, _} = start_socket("?stream=hashtag&tag=lain")
55 end
56
57 test "receives well formatted events" do
58 user = insert(:user)
59 {:ok, _} = start_socket("?stream=public")
60 {:ok, activity} = CommonAPI.post(user, %{"status" => "nice echo chamber"})
61
62 assert_receive {:text, raw_json}, 1_000
63 assert {:ok, json} = Jason.decode(raw_json)
64
65 assert "update" == json["event"]
66 assert json["payload"]
67 assert {:ok, json} = Jason.decode(json["payload"])
68
69 view_json =
70 Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: activity, for: nil)
71 |> Jason.encode!()
72 |> Jason.decode!()
73
74 assert json == view_json
75 end
76
77 describe "with a valid user token" do
78 setup do
79 {:ok, app} =
80 Pleroma.Repo.insert(
81 OAuth.App.register_changeset(%OAuth.App{}, %{
82 client_name: "client",
83 scopes: ["scope"],
84 redirect_uris: "url"
85 })
86 )
87
88 user = insert(:user)
89
90 {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
91
92 {:ok, token} = OAuth.Token.exchange_token(app, auth)
93
94 %{user: user, token: token}
95 end
96
97 test "accepts valid tokens", state do
98 assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}")
99 end
100
101 test "accepts the 'user' stream", %{token: token} = _state do
102 assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}")
103 assert {:error, {403, "Forbidden"}} = start_socket("?stream=user")
104 end
105
106 test "accepts the 'user:notification' stream", %{token: token} = _state do
107 assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}")
108 assert {:error, {403, "Forbidden"}} = start_socket("?stream=user:notification")
109 end
110
111 test "accepts valid token on Sec-WebSocket-Protocol header", %{token: token} do
112 assert {:ok, _} = start_socket("?stream=user", [{"Sec-WebSocket-Protocol", token.token}])
113
114 assert {:error, {403, "Forbidden"}} =
115 start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}])
116 end
117 end
118 end