logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

mastodon_websocket_test.exs (3762B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Integration.MastodonWebsocketTest do
  5. use Pleroma.DataCase
  6. import ExUnit.CaptureLog
  7. import Pleroma.Factory
  8. alias Pleroma.Integration.WebsocketClient
  9. alias Pleroma.Web.CommonAPI
  10. alias Pleroma.Web.OAuth
  11. @moduletag needs_streamer: true, capture_log: true
  12. @path Pleroma.Web.Endpoint.url()
  13. |> URI.parse()
  14. |> Map.put(:scheme, "ws")
  15. |> Map.put(:path, "/api/v1/streaming")
  16. |> URI.to_string()
  17. def start_socket(qs \\ nil, headers \\ []) do
  18. path =
  19. case qs do
  20. nil -> @path
  21. qs -> @path <> qs
  22. end
  23. WebsocketClient.start_link(self(), path, headers)
  24. end
  25. test "refuses invalid requests" do
  26. capture_log(fn ->
  27. assert {:error, {404, _}} = start_socket()
  28. assert {:error, {404, _}} = start_socket("?stream=ncjdk")
  29. Process.sleep(30)
  30. end)
  31. end
  32. test "requires authentication and a valid token for protected streams" do
  33. capture_log(fn ->
  34. assert {:error, {401, _}} = start_socket("?stream=user&access_token=aaaaaaaaaaaa")
  35. assert {:error, {401, _}} = start_socket("?stream=user")
  36. Process.sleep(30)
  37. end)
  38. end
  39. test "allows public streams without authentication" do
  40. assert {:ok, _} = start_socket("?stream=public")
  41. assert {:ok, _} = start_socket("?stream=public:local")
  42. assert {:ok, _} = start_socket("?stream=hashtag&tag=lain")
  43. end
  44. test "receives well formatted events" do
  45. user = insert(:user)
  46. {:ok, _} = start_socket("?stream=public")
  47. {:ok, activity} = CommonAPI.post(user, %{status: "nice echo chamber"})
  48. assert_receive {:text, raw_json}, 1_000
  49. assert {:ok, json} = Jason.decode(raw_json)
  50. assert "update" == json["event"]
  51. assert json["payload"]
  52. assert {:ok, json} = Jason.decode(json["payload"])
  53. view_json =
  54. Pleroma.Web.MastodonAPI.StatusView.render("show.json", activity: activity, for: nil)
  55. |> Jason.encode!()
  56. |> Jason.decode!()
  57. assert json == view_json
  58. end
  59. describe "with a valid user token" do
  60. setup do
  61. {:ok, app} =
  62. Pleroma.Repo.insert(
  63. OAuth.App.register_changeset(%OAuth.App{}, %{
  64. client_name: "client",
  65. scopes: ["read"],
  66. redirect_uris: "url"
  67. })
  68. )
  69. user = insert(:user)
  70. {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
  71. {:ok, token} = OAuth.Token.exchange_token(app, auth)
  72. %{user: user, token: token}
  73. end
  74. test "accepts valid tokens", state do
  75. assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}")
  76. end
  77. test "accepts the 'user' stream", %{token: token} = _state do
  78. assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}")
  79. capture_log(fn ->
  80. assert {:error, {401, _}} = start_socket("?stream=user")
  81. Process.sleep(30)
  82. end)
  83. end
  84. test "accepts the 'user:notification' stream", %{token: token} = _state do
  85. assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}")
  86. capture_log(fn ->
  87. assert {:error, {401, _}} = start_socket("?stream=user:notification")
  88. Process.sleep(30)
  89. end)
  90. end
  91. test "accepts valid token on Sec-WebSocket-Protocol header", %{token: token} do
  92. assert {:ok, _} = start_socket("?stream=user", [{"Sec-WebSocket-Protocol", token.token}])
  93. capture_log(fn ->
  94. assert {:error, {401, _}} =
  95. start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}])
  96. Process.sleep(30)
  97. end)
  98. end
  99. end
  100. end