Revert "Merge branch 'streamer-refactoring' into 'develop'"
[akkoma] / test / integration / mastodon_websocket_test.exs
index b42c9ef073deec7672a24cc43b79d20758d9b5f6..63bf73412f9bf1b03c299ca1e0fc48c7c6ca31d3 100644 (file)
@@ -5,6 +5,7 @@
 defmodule Pleroma.Integration.MastodonWebsocketTest do
   use Pleroma.DataCase
 
+  import ExUnit.CaptureLog
   import Pleroma.Factory
 
   alias Pleroma.Integration.WebsocketClient
@@ -39,13 +40,17 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do
   end
 
   test "refuses invalid requests" do
-    assert {:error, {400, _}} = start_socket()
-    assert {:error, {404, _}} = start_socket("?stream=ncjdk")
+    capture_log(fn ->
+      assert {:error, {400, _}} = start_socket()
+      assert {:error, {404, _}} = start_socket("?stream=ncjdk")
+    end)
   end
 
   test "requires authentication and a valid token for protected streams" do
-    assert {:error, {403, _}} = start_socket("?stream=user&access_token=aaaaaaaaaaaa")
-    assert {:error, {403, _}} = start_socket("?stream=user")
+    capture_log(fn ->
+      assert {:error, {403, _}} = start_socket("?stream=user&access_token=aaaaaaaaaaaa")
+      assert {:error, {403, _}} = start_socket("?stream=user")
+    end)
   end
 
   test "allows public streams without authentication" do
@@ -97,5 +102,30 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do
     test "accepts valid tokens", state do
       assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}")
     end
+
+    test "accepts the 'user' stream", %{token: token} = _state do
+      assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}")
+
+      assert capture_log(fn ->
+               assert {:error, {403, "Forbidden"}} = start_socket("?stream=user")
+             end) =~ ":badarg"
+    end
+
+    test "accepts the 'user:notification' stream", %{token: token} = _state do
+      assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}")
+
+      assert capture_log(fn ->
+               assert {:error, {403, "Forbidden"}} = start_socket("?stream=user:notification")
+             end) =~ ":badarg"
+    end
+
+    test "accepts valid token on Sec-WebSocket-Protocol header", %{token: token} do
+      assert {:ok, _} = start_socket("?stream=user", [{"Sec-WebSocket-Protocol", token.token}])
+
+      assert capture_log(fn ->
+               assert {:error, {403, "Forbidden"}} =
+                        start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}])
+             end) =~ ":badarg"
+    end
   end
 end