Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.9KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2018 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 Pleroma.Factory
  7. alias Pleroma.Web.CommonAPI
  8. alias Pleroma.Web.OAuth
  9. alias Pleroma.Integration.WebsocketClient
  10. alias Pleroma.Web.Streamer
  11. @path Pleroma.Web.Endpoint.url()
  12. |> URI.parse()
  13. |> Map.put(:scheme, "ws")
  14. |> Map.put(:path, "/api/v1/streaming")
  15. |> URI.to_string()
  16. setup do
  17. GenServer.start(Streamer, %{}, name: Streamer)
  18. on_exit(fn ->
  19. if pid = Process.whereis(Streamer) do
  20. Process.exit(pid, :kill)
  21. end
  22. end)
  23. end
  24. def start_socket(qs \\ nil, headers \\ []) do
  25. path =
  26. case qs do
  27. nil -> @path
  28. qs -> @path <> qs
  29. end
  30. WebsocketClient.start_link(self(), path, headers)
  31. end
  32. test "refuses invalid requests" do
  33. assert {:error, {400, _}} = start_socket()
  34. assert {:error, {404, _}} = start_socket("?stream=ncjdk")
  35. end
  36. test "requires authentication and a valid token for protected streams" do
  37. assert {:error, {403, _}} = start_socket("?stream=user&access_token=aaaaaaaaaaaa")
  38. assert {:error, {403, _}} = start_socket("?stream=user")
  39. end
  40. test "allows public streams without authentication" do
  41. assert {:ok, _} = start_socket("?stream=public")
  42. assert {:ok, _} = start_socket("?stream=public:local")
  43. assert {:ok, _} = start_socket("?stream=hashtag&tag=lain")
  44. end
  45. test "receives well formatted events" do
  46. user = insert(:user)
  47. {:ok, _} = start_socket("?stream=public")
  48. {:ok, activity} = CommonAPI.post(user, %{"status" => "nice echo chamber"})
  49. assert_receive {:text, raw_json}, 1_000
  50. assert {:ok, json} = Jason.decode(raw_json)
  51. assert "update" == json["event"]
  52. assert json["payload"]
  53. assert {:ok, json} = Jason.decode(json["payload"])
  54. # Note: we remove the "statuses_count" from this result as it changes in the meantime
  55. view_json =
  56. Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: activity, for: nil)
  57. |> Jason.encode!()
  58. |> Jason.decode!()
  59. |> put_in(["account", "statuses_count"], 0)
  60. assert json == view_json
  61. end
  62. describe "with a valid user token" do
  63. setup do
  64. {:ok, app} =
  65. Pleroma.Repo.insert(
  66. OAuth.App.register_changeset(%OAuth.App{}, %{
  67. client_name: "client",
  68. scopes: "scope",
  69. redirect_uris: "url"
  70. })
  71. )
  72. user = insert(:user)
  73. {:ok, auth} = OAuth.Authorization.create_authorization(app, user)
  74. {:ok, token} = OAuth.Token.exchange_token(app, auth)
  75. %{user: user, token: token}
  76. end
  77. test "accepts valid tokens", state do
  78. assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}")
  79. end
  80. end
  81. end