diff options
Diffstat (limited to 'test/web/mastodon_api')
5 files changed, 144 insertions, 10 deletions
| diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index 260ad2306..9c7b5e9b2 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -708,7 +708,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        followed = insert(:user)        other_user = insert(:user) -      ret_conn = post(conn, "/api/v1/accounts/#{followed.id}/follow?reblogs=false") +      ret_conn = +        conn +        |> put_req_header("content-type", "application/json") +        |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false})        assert %{"showing_reblogs" => false} = json_response_and_validate_schema(ret_conn, 200) @@ -722,7 +725,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        assert %{"showing_reblogs" => true} =                 conn -               |> post("/api/v1/accounts/#{followed.id}/follow?reblogs=true") +               |> put_req_header("content-type", "application/json") +               |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: true})                 |> json_response_and_validate_schema(200)        assert [%{"id" => ^reblog_id}] = @@ -731,6 +735,35 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do                 |> json_response(200)      end +    test "following with reblogs" do +      %{conn: conn} = oauth_access(["follow", "read:statuses"]) +      followed = insert(:user) +      other_user = insert(:user) + +      ret_conn = post(conn, "/api/v1/accounts/#{followed.id}/follow") + +      assert %{"showing_reblogs" => true} = json_response_and_validate_schema(ret_conn, 200) + +      {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"}) +      {:ok, %{id: reblog_id}} = CommonAPI.repeat(activity.id, followed) + +      assert [%{"id" => ^reblog_id}] = +               conn +               |> get("/api/v1/timelines/home") +               |> json_response(200) + +      assert %{"showing_reblogs" => false} = +               conn +               |> put_req_header("content-type", "application/json") +               |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false}) +               |> json_response_and_validate_schema(200) + +      assert [] == +               conn +               |> get("/api/v1/timelines/home") +               |> json_response(200) +    end +      test "following / unfollowing errors", %{user: user, conn: conn} do        # self follow        conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow") @@ -904,7 +937,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        %{          "access_token" => token,          "created_at" => _created_at, -        "scope" => _scope, +        "scope" => ^scope,          "token_type" => "Bearer"        } = json_response_and_validate_schema(conn, 200) @@ -1066,7 +1099,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        assert %{                 "access_token" => access_token,                 "created_at" => _, -               "scope" => ["read", "write", "follow", "push"], +               "scope" => "read write follow push",                 "token_type" => "Bearer"               } = response @@ -1184,7 +1217,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        assert %{                 "access_token" => access_token,                 "created_at" => _, -               "scope" => ["read"], +               "scope" => "read",                 "token_type" => "Bearer"               } =                 conn diff --git a/test/web/mastodon_api/controllers/follow_request_controller_test.exs b/test/web/mastodon_api/controllers/follow_request_controller_test.exs index 44e12d15a..6749e0e83 100644 --- a/test/web/mastodon_api/controllers/follow_request_controller_test.exs +++ b/test/web/mastodon_api/controllers/follow_request_controller_test.exs @@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do    use Pleroma.Web.ConnCase    alias Pleroma.User -  alias Pleroma.Web.ActivityPub.ActivityPub +  alias Pleroma.Web.CommonAPI    import Pleroma.Factory @@ -20,7 +20,7 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do      test "/api/v1/follow_requests works", %{user: user, conn: conn} do        other_user = insert(:user) -      {:ok, _activity} = ActivityPub.follow(other_user, user) +      {:ok, _, _, _activity} = CommonAPI.follow(other_user, user)        {:ok, other_user} = User.follow(other_user, user, :follow_pending)        assert User.following?(other_user, user) == false @@ -34,7 +34,7 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do      test "/api/v1/follow_requests/:id/authorize works", %{user: user, conn: conn} do        other_user = insert(:user) -      {:ok, _activity} = ActivityPub.follow(other_user, user) +      {:ok, _, _, _activity} = CommonAPI.follow(other_user, user)        {:ok, other_user} = User.follow(other_user, user, :follow_pending)        user = User.get_cached_by_id(user.id) @@ -56,7 +56,7 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do      test "/api/v1/follow_requests/:id/reject works", %{user: user, conn: conn} do        other_user = insert(:user) -      {:ok, _activity} = ActivityPub.follow(other_user, user) +      {:ok, _, _, _activity} = CommonAPI.follow(other_user, user)        user = User.get_cached_by_id(user.id) diff --git a/test/web/mastodon_api/controllers/timeline_controller_test.exs b/test/web/mastodon_api/controllers/timeline_controller_test.exs index f069390c1..50e0d783d 100644 --- a/test/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/web/mastodon_api/controllers/timeline_controller_test.exs @@ -418,4 +418,78 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do        assert [status_none] == json_response_and_validate_schema(all_test, :ok)      end    end + +  describe "hashtag timeline handling of :restrict_unauthenticated setting" do +    setup do +      user = insert(:user) +      {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"}) +      {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"}) + +      activity1 +      |> Ecto.Changeset.change(%{local: false}) +      |> Pleroma.Repo.update() + +      base_uri = "/api/v1/timelines/tag/tag1" +      error_response = %{"error" => "authorization required for timeline view"} + +      %{base_uri: base_uri, error_response: error_response} +    end + +    defp ensure_authenticated_access(base_uri) do +      %{conn: auth_conn} = oauth_access(["read:statuses"]) + +      res_conn = get(auth_conn, "#{base_uri}?local=true") +      assert length(json_response(res_conn, 200)) == 1 + +      res_conn = get(auth_conn, "#{base_uri}?local=false") +      assert length(json_response(res_conn, 200)) == 2 +    end + +    test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{ +      conn: conn, +      base_uri: base_uri, +      error_response: error_response +    } do +      clear_config([:restrict_unauthenticated, :timelines, :local], true) +      clear_config([:restrict_unauthenticated, :timelines, :federated], true) + +      for local <- [true, false] do +        res_conn = get(conn, "#{base_uri}?local=#{local}") + +        assert json_response(res_conn, :unauthorized) == error_response +      end + +      ensure_authenticated_access(base_uri) +    end + +    test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline", +         %{conn: conn, base_uri: base_uri, error_response: error_response} do +      clear_config([:restrict_unauthenticated, :timelines, :local], false) +      clear_config([:restrict_unauthenticated, :timelines, :federated], true) + +      res_conn = get(conn, "#{base_uri}?local=true") +      assert length(json_response(res_conn, 200)) == 1 + +      res_conn = get(conn, "#{base_uri}?local=false") +      assert json_response(res_conn, :unauthorized) == error_response + +      ensure_authenticated_access(base_uri) +    end + +    test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <> +           "(but not to local public activities which are delivered as part of federated timeline)", +         %{conn: conn, base_uri: base_uri, error_response: error_response} do +      clear_config([:restrict_unauthenticated, :timelines, :local], true) +      clear_config([:restrict_unauthenticated, :timelines, :federated], false) + +      res_conn = get(conn, "#{base_uri}?local=true") +      assert json_response(res_conn, :unauthorized) == error_response + +      # Note: local activities get delivered as part of federated timeline +      res_conn = get(conn, "#{base_uri}?local=false") +      assert length(json_response(res_conn, 200)) == 2 + +      ensure_authenticated_access(base_uri) +    end +  end  end diff --git a/test/web/mastodon_api/mastodon_api_test.exs b/test/web/mastodon_api/mastodon_api_test.exs index a7f9c5205..c08be37d4 100644 --- a/test/web/mastodon_api/mastodon_api_test.exs +++ b/test/web/mastodon_api/mastodon_api_test.exs @@ -18,7 +18,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPITest do        follower = insert(:user)        user = insert(:user, local: true, deactivated: true)        {:error, error} = MastodonAPI.follow(follower, user) -      assert error == "Could not follow user: #{user.nickname} is deactivated." +      assert error == :rejected      end      test "following for user" do diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index 80b1f734c..f5bfc9c67 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -5,6 +5,7 @@  defmodule Pleroma.Web.MastodonAPI.AccountViewTest do    use Pleroma.DataCase +  alias Pleroma.Config    alias Pleroma.User    alias Pleroma.UserRelationship    alias Pleroma.Web.CommonAPI @@ -18,6 +19,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do      :ok    end +  setup do: clear_config([:instances_favicons, :enabled]) +    test "Represent a user account" do      background_image = %{        "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}] @@ -75,6 +78,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        pleroma: %{          ap_id: user.ap_id,          background_image: "https://example.com/images/asuka_hospital.png", +        favicon: +          "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png",          confirmation_pending: false,          tags: [],          is_admin: false, @@ -92,6 +97,23 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do      assert expected == AccountView.render("show.json", %{user: user})    end +  test "Favicon is nil when :instances_favicons is disabled" do +    user = insert(:user) + +    Config.put([:instances_favicons, :enabled], true) + +    assert %{ +             pleroma: %{ +               favicon: +                 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png" +             } +           } = AccountView.render("show.json", %{user: user}) + +    Config.put([:instances_favicons, :enabled], false) + +    assert %{pleroma: %{favicon: nil}} = AccountView.render("show.json", %{user: user}) +  end +    test "Represent the user account for the account owner" do      user = insert(:user) @@ -152,6 +174,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        pleroma: %{          ap_id: user.ap_id,          background_image: nil, +        favicon: +          "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png",          confirmation_pending: false,          tags: [],          is_admin: false, @@ -372,6 +396,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        user = insert(:user, hide_followers: true, hide_follows: true)        other_user = insert(:user)        {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user) + +      assert User.following?(user, other_user) +      assert Pleroma.FollowingRelationship.follower_count(other_user) == 1        {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)        assert %{ | 
