From b12a90491156f1b31defd7aa1b322a86867dbf2b Mon Sep 17 00:00:00 2001 From: href Date: Mon, 17 Dec 2018 17:09:06 +0100 Subject: Integration tests for mastodon websocket --- test/integration/mastodon_websocket_test.exs | 100 +++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 test/integration/mastodon_websocket_test.exs (limited to 'test/integration') diff --git a/test/integration/mastodon_websocket_test.exs b/test/integration/mastodon_websocket_test.exs new file mode 100644 index 000000000..b5f3d3a47 --- /dev/null +++ b/test/integration/mastodon_websocket_test.exs @@ -0,0 +1,100 @@ +defmodule Pleroma.Integration.MastodonWebsocketTest do + use Pleroma.DataCase + + import Pleroma.Factory + + alias Pleroma.Web.CommonAPI + alias Pleroma.Web.OAuth + alias Pleroma.Integration.WebsocketClient + alias Pleroma.Web.Streamer + + @path Pleroma.Web.Endpoint.url() + |> URI.parse() + |> Map.put(:scheme, "ws") + |> Map.put(:path, "/api/v1/streaming") + |> URI.to_string() + + setup do + GenServer.start(Streamer, %{}, name: Streamer) + + on_exit(fn -> + if pid = Process.whereis(Streamer) do + Process.exit(pid, :kill) + end + end) + end + + def start_socket(qs \\ nil, headers \\ []) do + path = + case qs do + nil -> @path + qs -> @path <> qs + end + + WebsocketClient.start_link(self(), path, headers) + end + + test "refuses invalid requests" do + assert {:error, {400, _}} = start_socket() + assert {:error, {404, _}} = start_socket("?stream=ncjdk") + 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") + end + + test "allows public streams without authentication" do + assert {:ok, _} = start_socket("?stream=public") + assert {:ok, _} = start_socket("?stream=public:local") + assert {:ok, _} = start_socket("?stream=hashtag&tag=lain") + end + + test "receives well formatted events" do + user = insert(:user) + {:ok, _} = start_socket("?stream=public") + {:ok, activity} = CommonAPI.post(user, %{"status" => "nice echo chamber"}) + + assert_receive {:text, raw_json}, 1_000 + assert {:ok, json} = Jason.decode(raw_json) + + assert "update" == json["event"] + assert json["payload"] + assert {:ok, json} = Jason.decode(json["payload"]) + + # Note: we remove the "statuses_count" from this result as it changes in the meantime + + view_json = + Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: activity, for: nil) + |> Jason.encode!() + |> Jason.decode!() + |> put_in(["account", "statuses_count"], 0) + + assert json == view_json + end + + describe "with a valid user token" do + setup do + {:ok, app} = + Pleroma.Repo.insert( + OAuth.App.register_changeset(%OAuth.App{}, %{ + client_name: "client", + scopes: "scope", + redirect_uris: "url" + }) + ) + + user = insert(:user) + + {:ok, auth} = OAuth.Authorization.create_authorization(app, user) + + {:ok, token} = OAuth.Token.exchange_token(app, auth) + + %{user: user, token: token} + end + + test "accepts valid tokens", state do + assert {:ok, _} = start_socket("?stream=user&access_token=#{state.token.token}") + end + end +end -- cgit v1.2.3 From 3c08d229db423052d0dd88b8a36fb39b0ae81ead Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 23 Dec 2018 20:11:29 +0000 Subject: tests: add legal boilerplate --- test/integration/mastodon_websocket_test.exs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test/integration') diff --git a/test/integration/mastodon_websocket_test.exs b/test/integration/mastodon_websocket_test.exs index b5f3d3a47..03aabf12c 100644 --- a/test/integration/mastodon_websocket_test.exs +++ b/test/integration/mastodon_websocket_test.exs @@ -1,3 +1,7 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2018 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Integration.MastodonWebsocketTest do use Pleroma.DataCase -- cgit v1.2.3 From 789a9843da0eef1e5426ab6558f1adf24231ce02 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Mon, 21 Jan 2019 14:30:01 +0300 Subject: [#530] Fixed test. --- test/integration/mastodon_websocket_test.exs | 3 --- 1 file changed, 3 deletions(-) (limited to 'test/integration') diff --git a/test/integration/mastodon_websocket_test.exs b/test/integration/mastodon_websocket_test.exs index 03aabf12c..2e385f5ad 100644 --- a/test/integration/mastodon_websocket_test.exs +++ b/test/integration/mastodon_websocket_test.exs @@ -66,13 +66,10 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do assert json["payload"] assert {:ok, json} = Jason.decode(json["payload"]) - # Note: we remove the "statuses_count" from this result as it changes in the meantime - view_json = Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: activity, for: nil) |> Jason.encode!() |> Jason.decode!() - |> put_in(["account", "statuses_count"], 0) assert json == view_json end -- cgit v1.2.3 From 063baca5e4f3a100c0d45dffb14e4968599ef43b Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Thu, 14 Feb 2019 00:29:29 +0300 Subject: [#468] User UI for OAuth permissions restriction. Standardized storage format for `scopes` fields, updated usages. --- test/integration/mastodon_websocket_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/integration') diff --git a/test/integration/mastodon_websocket_test.exs b/test/integration/mastodon_websocket_test.exs index 2e385f5ad..0c513b6e7 100644 --- a/test/integration/mastodon_websocket_test.exs +++ b/test/integration/mastodon_websocket_test.exs @@ -80,7 +80,7 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do Pleroma.Repo.insert( OAuth.App.register_changeset(%OAuth.App{}, %{ client_name: "client", - scopes: "scope", + scopes: ["scope"], redirect_uris: "url" }) ) -- cgit v1.2.3 From a3a9cec4835738216800d2cebd295fb8dbf10f34 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Tue, 5 Mar 2019 03:52:23 +0100 Subject: [Credo] fix Credo.Check.Readability.AliasOrder --- test/integration/mastodon_websocket_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/integration') diff --git a/test/integration/mastodon_websocket_test.exs b/test/integration/mastodon_websocket_test.exs index 0c513b6e7..b42c9ef07 100644 --- a/test/integration/mastodon_websocket_test.exs +++ b/test/integration/mastodon_websocket_test.exs @@ -7,9 +7,9 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do import Pleroma.Factory + alias Pleroma.Integration.WebsocketClient alias Pleroma.Web.CommonAPI alias Pleroma.Web.OAuth - alias Pleroma.Integration.WebsocketClient alias Pleroma.Web.Streamer @path Pleroma.Web.Endpoint.url() -- cgit v1.2.3