1 defmodule Pleroma.Integration.MastodonWebsocketTest do
6 alias Pleroma.Web.CommonAPI
7 alias Pleroma.Web.OAuth
8 alias Pleroma.Integration.WebsocketClient
9 alias Pleroma.Web.Streamer
11 @path Pleroma.Web.Endpoint.url()
13 |> Map.put(:scheme, "ws")
14 |> Map.put(:path, "/api/v1/streaming")
18 GenServer.start(Streamer, %{}, name: Streamer)
21 if pid = Process.whereis(Streamer) do
22 Process.exit(pid, :kill)
27 def start_socket(qs \\ nil, headers \\ []) do
34 WebsocketClient.start_link(self(), path, headers)
37 test "refuses invalid requests" do
38 assert {:error, {400, _}} = start_socket()
39 assert {:error, {404, _}} = start_socket("?stream=ncjdk")
42 test "requires authentication and a valid token for protected streams" do
43 assert {:error, {403, _}} = start_socket("?stream=user&access_token=aaaaaaaaaaaa")
44 assert {:error, {403, _}} = start_socket("?stream=user")
47 test "allows public streams without authentication" do
48 assert {:ok, _} = start_socket("?stream=public")
49 assert {:ok, _} = start_socket("?stream=public:local")
50 assert {:ok, _} = start_socket("?stream=hashtag&tag=lain")
53 test "receives well formatted events" do
55 {:ok, _} = start_socket("?stream=public")
56 {:ok, activity} = CommonAPI.post(user, %{"status" => "nice echo chamber"})
58 assert_receive {:text, raw_json}, 1_000
59 assert {:ok, json} = Jason.decode(raw_json)
61 assert "update" == json["event"]
62 assert json["payload"]
63 assert {:ok, json} = Jason.decode(json["payload"])
65 # Note: we remove the "statuses_count" from this result as it changes in the meantime
68 Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: activity, for: nil)
71 |> put_in(["account", "statuses_count"], 0)
73 assert json == view_json
76 describe "with a valid user token" do
80 OAuth.App.register_changeset(%OAuth.App{}, %{
81 client_name: "client",
89 {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
91 {:ok, token} = OAuth.Token.exchange_token(app, auth)
93 %{user: user, token: token}
96 test "accepts valid tokens", state do
97 assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}")