1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Integration.MastodonWebsocketTest do
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.OAuth
12 alias Pleroma.Integration.WebsocketClient
13 alias Pleroma.Web.Streamer
15 @path Pleroma.Web.Endpoint.url()
17 |> Map.put(:scheme, "ws")
18 |> Map.put(:path, "/api/v1/streaming")
22 GenServer.start(Streamer, %{}, name: Streamer)
25 if pid = Process.whereis(Streamer) do
26 Process.exit(pid, :kill)
31 def start_socket(qs \\ nil, headers \\ []) do
38 WebsocketClient.start_link(self(), path, headers)
41 test "refuses invalid requests" do
42 assert {:error, {400, _}} = start_socket()
43 assert {:error, {404, _}} = start_socket("?stream=ncjdk")
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")
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")
57 test "receives well formatted events" do
59 {:ok, _} = start_socket("?stream=public")
60 {:ok, activity} = CommonAPI.post(user, %{"status" => "nice echo chamber"})
62 assert_receive {:text, raw_json}, 1_000
63 assert {:ok, json} = Jason.decode(raw_json)
65 assert "update" == json["event"]
66 assert json["payload"]
67 assert {:ok, json} = Jason.decode(json["payload"])
69 # Note: we remove the "statuses_count" from this result as it changes in the meantime
72 Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: activity, for: nil)
75 |> put_in(["account", "statuses_count"], 0)
77 assert json == view_json
80 describe "with a valid user token" do
84 OAuth.App.register_changeset(%OAuth.App{}, %{
85 client_name: "client",
93 {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
95 {:ok, token} = OAuth.Token.exchange_token(app, auth)
97 %{user: user, token: token}
100 test "accepts valid tokens", state do
101 assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}")