1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Integration.MastodonWebsocketTest do
6 # Needs a streamer, needs to stay synchronous
9 import ExUnit.CaptureLog
10 import Pleroma.Factory
12 alias Pleroma.Integration.WebsocketClient
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.OAuth
16 @moduletag needs_streamer: true, capture_log: true
18 @path Pleroma.Web.Endpoint.url()
20 |> Map.put(:scheme, "ws")
21 |> Map.put(:path, "/api/v1/streaming")
24 def start_socket(qs \\ nil, headers \\ []) do
31 WebsocketClient.start_link(self(), path, headers)
34 test "allows multi-streams" do
36 assert {:ok, _} = start_socket()
38 assert {:error, %WebSockex.RequestError{code: 404, message: "Not Found"}} =
39 start_socket("?stream=ncjdk")
45 test "requires authentication and a valid token for protected streams" do
47 assert {:error, %WebSockex.RequestError{code: 401}} =
48 start_socket("?stream=user&access_token=aaaaaaaaaaaa")
50 assert {:error, %WebSockex.RequestError{code: 401}} = start_socket("?stream=user")
55 test "allows public streams without authentication" do
56 assert {:ok, _} = start_socket("?stream=public")
57 assert {:ok, _} = start_socket("?stream=public:local")
58 assert {:ok, _} = start_socket("?stream=public:remote&instance=lain.com")
59 assert {:ok, _} = start_socket("?stream=hashtag&tag=lain")
62 test "receives well formatted events" do
64 {:ok, _} = start_socket("?stream=public")
65 {:ok, activity} = CommonAPI.post(user, %{status: "nice echo chamber"})
67 assert_receive {:text, raw_json}, 1_000
68 assert {:ok, json} = Jason.decode(raw_json)
70 assert "update" == json["event"]
71 assert json["payload"]
72 assert {:ok, json} = Jason.decode(json["payload"])
75 Pleroma.Web.MastodonAPI.StatusView.render("show.json", activity: activity, for: nil)
79 assert json == view_json
82 describe "with a valid user token" do
86 OAuth.App.register_changeset(%OAuth.App{}, %{
87 client_name: "client",
95 {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
97 {:ok, token} = OAuth.Token.exchange_token(app, auth)
99 %{app: app, user: user, token: token}
102 test "accepts valid tokens", state do
103 assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}")
106 test "accepts the 'user' stream", %{token: token} = _state do
107 assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}")
110 assert {:error, %WebSockex.RequestError{code: 401}} = start_socket("?stream=user")
115 test "accepts the 'user:notification' stream", %{token: token} = _state do
116 assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}")
119 assert {:error, %WebSockex.RequestError{code: 401}} =
120 start_socket("?stream=user:notification")
126 test "accepts valid token on Sec-WebSocket-Protocol header", %{token: token} do
127 assert {:ok, _} = start_socket("?stream=user", [{"Sec-WebSocket-Protocol", token.token}])
130 assert {:error, %WebSockex.RequestError{code: 401}} =
131 start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}])
137 test "disconnect when token is revoked", %{app: app, user: user, token: token} do
138 assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}")
139 assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}")
141 {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
143 {:ok, token2} = OAuth.Token.exchange_token(app, auth)
144 assert {:ok, _} = start_socket("?stream=user&access_token=#{token2.token}")
146 OAuth.Token.Strategy.Revoke.revoke(token)
148 assert_receive {:close, _}
149 assert_receive {:close, _}
150 refute_receive {:close, _}