diff options
Diffstat (limited to 'test/web')
| -rw-r--r-- | test/web/admin_api/admin_api_controller_test.exs | 172 | ||||
| -rw-r--r-- | test/web/admin_api/config_test.exs | 183 | ||||
| -rw-r--r-- | test/web/mastodon_api/account_view_test.exs | 14 | ||||
| -rw-r--r-- | test/web/mastodon_api/mastodon_api_controller/update_credentials_test.exs | 304 | ||||
| -rw-r--r-- | test/web/mastodon_api/mastodon_api_controller_test.exs | 387 | ||||
| -rw-r--r-- | test/web/mastodon_api/search_controller_test.exs | 128 | ||||
| -rw-r--r-- | test/web/oauth/oauth_controller_test.exs | 33 | ||||
| -rw-r--r-- | test/web/rich_media/parser_test.exs | 22 | 
8 files changed, 857 insertions, 386 deletions
diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 43dcf945a..18f64f2b7 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1292,4 +1292,176 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert json_response(conn, :bad_request) == "Could not delete"      end    end + +  describe "GET /api/pleroma/admin/config" do +    setup %{conn: conn} do +      admin = insert(:user, info: %{is_admin: true}) + +      %{conn: assign(conn, :user, admin)} +    end + +    test "without any settings in db", %{conn: conn} do +      conn = get(conn, "/api/pleroma/admin/config") + +      assert json_response(conn, 200) == %{"configs" => []} +    end + +    test "with settings in db", %{conn: conn} do +      config1 = insert(:config) +      config2 = insert(:config) + +      conn = get(conn, "/api/pleroma/admin/config") + +      %{ +        "configs" => [ +          %{ +            "key" => key1, +            "value" => _ +          }, +          %{ +            "key" => key2, +            "value" => _ +          } +        ] +      } = json_response(conn, 200) + +      assert key1 == config1.key +      assert key2 == config2.key +    end +  end + +  describe "POST /api/pleroma/admin/config" do +    setup %{conn: conn} do +      admin = insert(:user, info: %{is_admin: true}) + +      temp_file = "config/test.migrated.secret.exs" + +      on_exit(fn -> +        Application.delete_env(:pleroma, :key1) +        Application.delete_env(:pleroma, :key2) +        Application.delete_env(:pleroma, :key3) +        Application.delete_env(:pleroma, :key4) +        Application.delete_env(:pleroma, :keyaa1) +        Application.delete_env(:pleroma, :keyaa2) +        :ok = File.rm(temp_file) +      end) + +      dynamic = Pleroma.Config.get([:instance, :dynamic_configuration]) + +      Pleroma.Config.put([:instance, :dynamic_configuration], true) + +      on_exit(fn -> +        Pleroma.Config.put([:instance, :dynamic_configuration], dynamic) +      end) + +      %{conn: assign(conn, :user, admin)} +    end + +    test "create new config setting in db", %{conn: conn} do +      conn = +        post(conn, "/api/pleroma/admin/config", %{ +          configs: [ +            %{key: "key1", value: "value1"}, +            %{ +              key: "key2", +              value: %{ +                "nested_1" => "nested_value1", +                "nested_2" => [ +                  %{"nested_22" => "nested_value222"}, +                  %{"nested_33" => %{"nested_44" => "nested_444"}} +                ] +              } +            }, +            %{ +              key: "key3", +              value: [ +                %{"nested_3" => ":nested_3", "nested_33" => "nested_33"}, +                %{"nested_4" => ":true"} +              ] +            }, +            %{ +              key: "key4", +              value: %{"nested_5" => ":upload", "endpoint" => "https://example.com"} +            } +          ] +        }) + +      assert json_response(conn, 200) == %{ +               "configs" => [ +                 %{ +                   "key" => "key1", +                   "value" => "value1" +                 }, +                 %{ +                   "key" => "key2", +                   "value" => [ +                     %{"nested_1" => "nested_value1"}, +                     %{ +                       "nested_2" => [ +                         %{"nested_22" => "nested_value222"}, +                         %{"nested_33" => %{"nested_44" => "nested_444"}} +                       ] +                     } +                   ] +                 }, +                 %{ +                   "key" => "key3", +                   "value" => [ +                     [%{"nested_3" => "nested_3"}, %{"nested_33" => "nested_33"}], +                     %{"nested_4" => true} +                   ] +                 }, +                 %{ +                   "key" => "key4", +                   "value" => [%{"endpoint" => "https://example.com"}, %{"nested_5" => "upload"}] +                 } +               ] +             } + +      assert Application.get_env(:pleroma, :key1) == "value1" + +      assert Application.get_env(:pleroma, :key2) == [ +               nested_1: "nested_value1", +               nested_2: [ +                 [nested_22: "nested_value222"], +                 [nested_33: [nested_44: "nested_444"]] +               ] +             ] + +      assert Application.get_env(:pleroma, :key3) == [ +               [nested_3: :nested_3, nested_33: "nested_33"], +               [nested_4: true] +             ] + +      assert Application.get_env(:pleroma, :key4) == [ +               endpoint: "https://example.com", +               nested_5: :upload +             ] +    end + +    test "update config setting & delete", %{conn: conn} do +      config1 = insert(:config, key: "keyaa1") +      config2 = insert(:config, key: "keyaa2") + +      conn = +        post(conn, "/api/pleroma/admin/config", %{ +          configs: [ +            %{key: config1.key, value: "another_value"}, +            %{key: config2.key, delete: "true"} +          ] +        }) + +      assert json_response(conn, 200) == %{ +               "configs" => [ +                 %{ +                   "key" => config1.key, +                   "value" => "another_value" +                 } +               ] +             } + +      assert Application.get_env(:pleroma, :keyaa1) == "another_value" +      refute Application.get_env(:pleroma, :keyaa2) +    end +  end  end diff --git a/test/web/admin_api/config_test.exs b/test/web/admin_api/config_test.exs new file mode 100644 index 000000000..a2fedca40 --- /dev/null +++ b/test/web/admin_api/config_test.exs @@ -0,0 +1,183 @@ +defmodule Pleroma.Web.AdminAPI.ConfigTest do +  use Pleroma.DataCase, async: true +  import Pleroma.Factory +  alias Pleroma.Web.AdminAPI.Config + +  test "get_by_key/1" do +    config = insert(:config) +    insert(:config) + +    assert config == Config.get_by_key(config.key) +  end + +  test "create/1" do +    {:ok, config} = Config.create(%{key: "some_key", value: "some_value"}) +    assert config == Config.get_by_key("some_key") +  end + +  test "update/1" do +    config = insert(:config) +    {:ok, updated} = Config.update(config, %{value: "some_value"}) +    loaded = Config.get_by_key(config.key) +    assert loaded == updated +  end + +  test "update_or_create/1" do +    config = insert(:config) +    key2 = "another_key" + +    params = [ +      %{key: key2, value: "another_value"}, +      %{key: config.key, value: "new_value"} +    ] + +    assert Repo.all(Config) |> length() == 1 + +    Enum.each(params, &Config.update_or_create(&1)) + +    assert Repo.all(Config) |> length() == 2 + +    config1 = Config.get_by_key(config.key) +    config2 = Config.get_by_key(key2) + +    assert config1.value == Config.transform("new_value") +    assert config2.value == Config.transform("another_value") +  end + +  test "delete/1" do +    config = insert(:config) +    {:ok, _} = Config.delete(config.key) +    refute Config.get_by_key(config.key) +  end + +  describe "transform/1" do +    test "string" do +      binary = Config.transform("value as string") +      assert binary == :erlang.term_to_binary("value as string") +      assert Config.from_binary(binary) == "value as string" +    end + +    test "list of modules" do +      binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"]) +      assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity]) +      assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity] +    end + +    test "list of strings" do +      binary = Config.transform(["string1", "string2"]) +      assert binary == :erlang.term_to_binary(["string1", "string2"]) +      assert Config.from_binary(binary) == ["string1", "string2"] +    end + +    test "map" do +      binary = +        Config.transform(%{ +          "types" => "Pleroma.PostgresTypes", +          "telemetry_event" => ["Pleroma.Repo.Instrumenter"], +          "migration_lock" => "" +        }) + +      assert binary == +               :erlang.term_to_binary( +                 telemetry_event: [Pleroma.Repo.Instrumenter], +                 types: Pleroma.PostgresTypes +               ) + +      assert Config.from_binary(binary) == [ +               telemetry_event: [Pleroma.Repo.Instrumenter], +               types: Pleroma.PostgresTypes +             ] +    end + +    test "complex map with nested integers, lists and atoms" do +      binary = +        Config.transform(%{ +          "uploader" => "Pleroma.Uploaders.Local", +          "filters" => ["Pleroma.Upload.Filter.Dedupe"], +          "link_name" => ":true", +          "proxy_remote" => ":false", +          "proxy_opts" => %{ +            "redirect_on_failure" => ":false", +            "max_body_length" => "i:1048576", +            "http" => %{ +              "follow_redirect" => ":true", +              "pool" => ":upload" +            } +          } +        }) + +      assert binary == +               :erlang.term_to_binary( +                 filters: [Pleroma.Upload.Filter.Dedupe], +                 link_name: true, +                 proxy_opts: [ +                   http: [ +                     follow_redirect: true, +                     pool: :upload +                   ], +                   max_body_length: 1_048_576, +                   redirect_on_failure: false +                 ], +                 proxy_remote: false, +                 uploader: Pleroma.Uploaders.Local +               ) + +      assert Config.from_binary(binary) == +               [ +                 filters: [Pleroma.Upload.Filter.Dedupe], +                 link_name: true, +                 proxy_opts: [ +                   http: [ +                     follow_redirect: true, +                     pool: :upload +                   ], +                   max_body_length: 1_048_576, +                   redirect_on_failure: false +                 ], +                 proxy_remote: false, +                 uploader: Pleroma.Uploaders.Local +               ] +    end + +    test "keyword" do +      binary = +        Config.transform(%{ +          "level" => ":warn", +          "meta" => [":all"], +          "webhook_url" => "https://hooks.slack.com/services/YOUR-KEY-HERE" +        }) + +      assert binary == +               :erlang.term_to_binary( +                 level: :warn, +                 meta: [:all], +                 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE" +               ) + +      assert Config.from_binary(binary) == [ +               level: :warn, +               meta: [:all], +               webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE" +             ] +    end + +    test "complex map with sigil" do +      binary = +        Config.transform(%{ +          federated_timeline_removal: [], +          reject: [~r/comp[lL][aA][iI][nN]er/], +          replace: [] +        }) + +      assert binary == +               :erlang.term_to_binary( +                 federated_timeline_removal: [], +                 reject: [~r/comp[lL][aA][iI][nN]er/], +                 replace: [] +               ) + +      assert Config.from_binary(binary) == +               [federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []] +    end +  end +end diff --git a/test/web/mastodon_api/account_view_test.exs b/test/web/mastodon_api/account_view_test.exs index e2244dcb7..2ba7c0505 100644 --- a/test/web/mastodon_api/account_view_test.exs +++ b/test/web/mastodon_api/account_view_test.exs @@ -19,9 +19,18 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        ]      } +    background_image = %{ +      "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}] +    } +      user =        insert(:user, %{ -        info: %{note_count: 5, follower_count: 3, source_data: source_data}, +        info: %{ +          note_count: 5, +          follower_count: 3, +          source_data: source_data, +          background: background_image +        },          nickname: "shp@shitposter.club",          name: ":karjalanpiirakka: shp",          bio: "<script src=\"invalid-html\"></script><span>valid html</span>", @@ -60,6 +69,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          pleroma: %{}        },        pleroma: %{ +        background_image: "https://example.com/images/asuka_hospital.png",          confirmation_pending: false,          tags: [],          is_admin: false, @@ -126,6 +136,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          pleroma: %{}        },        pleroma: %{ +        background_image: nil,          confirmation_pending: false,          tags: [],          is_admin: false, @@ -216,6 +227,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          pleroma: %{}        },        pleroma: %{ +        background_image: nil,          confirmation_pending: false,          tags: [],          is_admin: false, diff --git a/test/web/mastodon_api/mastodon_api_controller/update_credentials_test.exs b/test/web/mastodon_api/mastodon_api_controller/update_credentials_test.exs new file mode 100644 index 000000000..71d0c8af8 --- /dev/null +++ b/test/web/mastodon_api/mastodon_api_controller/update_credentials_test.exs @@ -0,0 +1,304 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do +  alias Pleroma.Repo +  alias Pleroma.User + +  use Pleroma.Web.ConnCase + +  import Pleroma.Factory + +  describe "updating credentials" do +    test "sets user settings in a generic way", %{conn: conn} do +      user = insert(:user) + +      res_conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{ +          "pleroma_settings_store" => %{ +            pleroma_fe: %{ +              theme: "bla" +            } +          } +        }) + +      assert user = json_response(res_conn, 200) +      assert user["pleroma"]["settings_store"] == %{"pleroma_fe" => %{"theme" => "bla"}} + +      user = Repo.get(User, user["id"]) + +      res_conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{ +          "pleroma_settings_store" => %{ +            masto_fe: %{ +              theme: "bla" +            } +          } +        }) + +      assert user = json_response(res_conn, 200) + +      assert user["pleroma"]["settings_store"] == +               %{ +                 "pleroma_fe" => %{"theme" => "bla"}, +                 "masto_fe" => %{"theme" => "bla"} +               } + +      user = Repo.get(User, user["id"]) + +      res_conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{ +          "pleroma_settings_store" => %{ +            masto_fe: %{ +              theme: "blub" +            } +          } +        }) + +      assert user = json_response(res_conn, 200) + +      assert user["pleroma"]["settings_store"] == +               %{ +                 "pleroma_fe" => %{"theme" => "bla"}, +                 "masto_fe" => %{"theme" => "blub"} +               } +    end + +    test "updates the user's bio", %{conn: conn} do +      user = insert(:user) +      user2 = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{ +          "note" => "I drink #cofe with @#{user2.nickname}" +        }) + +      assert user = json_response(conn, 200) + +      assert user["note"] == +               ~s(I drink <a class="hashtag" data-tag="cofe" href="http://localhost:4001/tag/cofe" rel="tag">#cofe</a> with <span class="h-card"><a data-user=") <> +                 user2.id <> +                 ~s(" class="u-url mention" href=") <> +                 user2.ap_id <> ~s(">@<span>) <> user2.nickname <> ~s(</span></a></span>) +    end + +    test "updates the user's locking status", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{locked: "true"}) + +      assert user = json_response(conn, 200) +      assert user["locked"] == true +    end + +    test "updates the user's default scope", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{default_scope: "cofe"}) + +      assert user = json_response(conn, 200) +      assert user["source"]["privacy"] == "cofe" +    end + +    test "updates the user's hide_followers status", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{hide_followers: "true"}) + +      assert user = json_response(conn, 200) +      assert user["pleroma"]["hide_followers"] == true +    end + +    test "updates the user's skip_thread_containment option", %{conn: conn} do +      user = insert(:user) + +      response = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{skip_thread_containment: "true"}) +        |> json_response(200) + +      assert response["pleroma"]["skip_thread_containment"] == true +      assert refresh_record(user).info.skip_thread_containment +    end + +    test "updates the user's hide_follows status", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{hide_follows: "true"}) + +      assert user = json_response(conn, 200) +      assert user["pleroma"]["hide_follows"] == true +    end + +    test "updates the user's hide_favorites status", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{hide_favorites: "true"}) + +      assert user = json_response(conn, 200) +      assert user["pleroma"]["hide_favorites"] == true +    end + +    test "updates the user's show_role status", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{show_role: "false"}) + +      assert user = json_response(conn, 200) +      assert user["source"]["pleroma"]["show_role"] == false +    end + +    test "updates the user's no_rich_text status", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{no_rich_text: "true"}) + +      assert user = json_response(conn, 200) +      assert user["source"]["pleroma"]["no_rich_text"] == true +    end + +    test "updates the user's name", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"}) + +      assert user = json_response(conn, 200) +      assert user["display_name"] == "markorepairs" +    end + +    test "updates the user's avatar", %{conn: conn} do +      user = insert(:user) + +      new_avatar = %Plug.Upload{ +        content_type: "image/jpg", +        path: Path.absname("test/fixtures/image.jpg"), +        filename: "an_image.jpg" +      } + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar}) + +      assert user_response = json_response(conn, 200) +      assert user_response["avatar"] != User.avatar_url(user) +    end + +    test "updates the user's banner", %{conn: conn} do +      user = insert(:user) + +      new_header = %Plug.Upload{ +        content_type: "image/jpg", +        path: Path.absname("test/fixtures/image.jpg"), +        filename: "an_image.jpg" +      } + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header}) + +      assert user_response = json_response(conn, 200) +      assert user_response["header"] != User.banner_url(user) +    end + +    test "updates the user's background", %{conn: conn} do +      user = insert(:user) + +      new_header = %Plug.Upload{ +        content_type: "image/jpg", +        path: Path.absname("test/fixtures/image.jpg"), +        filename: "an_image.jpg" +      } + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{ +          "pleroma_background_image" => new_header +        }) + +      assert user_response = json_response(conn, 200) +      assert user_response["pleroma"]["background_image"] +    end + +    test "requires 'write' permission", %{conn: conn} do +      token1 = insert(:oauth_token, scopes: ["read"]) +      token2 = insert(:oauth_token, scopes: ["write", "follow"]) + +      for token <- [token1, token2] do +        conn = +          conn +          |> put_req_header("authorization", "Bearer #{token.token}") +          |> patch("/api/v1/accounts/update_credentials", %{}) + +        if token == token1 do +          assert %{"error" => "Insufficient permissions: write."} == json_response(conn, 403) +        else +          assert json_response(conn, 200) +        end +      end +    end + +    test "updates profile emojos", %{conn: conn} do +      user = insert(:user) + +      note = "*sips :blank:*" +      name = "I am :firefox:" + +      conn = +        conn +        |> assign(:user, user) +        |> patch("/api/v1/accounts/update_credentials", %{ +          "note" => note, +          "display_name" => name +        }) + +      assert json_response(conn, 200) + +      conn = +        conn +        |> get("/api/v1/accounts/#{user.id}") + +      assert user = json_response(conn, 200) + +      assert user["note"] == note +      assert user["display_name"] == name +      assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user["emojis"] +    end +  end +end diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 15d3fdb65..707723421 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -542,7 +542,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do        |> assign(:user, user)        |> get("/api/v1/accounts/verify_credentials") -    assert %{"id" => id, "source" => %{"privacy" => "public"}} = json_response(conn, 200) +    response = json_response(conn, 200) + +    assert %{"id" => id, "source" => %{"privacy" => "public"}} = response +    assert response["pleroma"]["chat_token"]      assert id == to_string(user.id)    end @@ -2134,116 +2137,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      end)    end -  test "account search", %{conn: conn} do -    user = insert(:user) -    user_two = insert(:user, %{nickname: "shp@shitposter.club"}) -    user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"}) - -    results = -      conn -      |> assign(:user, user) -      |> get("/api/v1/accounts/search", %{"q" => "shp"}) -      |> json_response(200) - -    result_ids = for result <- results, do: result["acct"] - -    assert user_two.nickname in result_ids -    assert user_three.nickname in result_ids - -    results = -      conn -      |> assign(:user, user) -      |> get("/api/v1/accounts/search", %{"q" => "2hu"}) -      |> json_response(200) - -    result_ids = for result <- results, do: result["acct"] - -    assert user_three.nickname in result_ids -  end - -  test "search", %{conn: conn} do -    user = insert(:user) -    user_two = insert(:user, %{nickname: "shp@shitposter.club"}) -    user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"}) - -    {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"}) - -    {:ok, _activity} = -      CommonAPI.post(user, %{ -        "status" => "This is about 2hu, but private", -        "visibility" => "private" -      }) - -    {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"}) - -    conn = -      conn -      |> get("/api/v1/search", %{"q" => "2hu"}) - -    assert results = json_response(conn, 200) - -    [account | _] = results["accounts"] -    assert account["id"] == to_string(user_three.id) - -    assert results["hashtags"] == [] - -    [status] = results["statuses"] -    assert status["id"] == to_string(activity.id) -  end - -  test "search fetches remote statuses", %{conn: conn} do -    capture_log(fn -> -      conn = -        conn -        |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"}) - -      assert results = json_response(conn, 200) - -      [status] = results["statuses"] -      assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" -    end) -  end - -  test "search doesn't show statuses that it shouldn't", %{conn: conn} do -    {:ok, activity} = -      CommonAPI.post(insert(:user), %{ -        "status" => "This is about 2hu, but private", -        "visibility" => "private" -      }) - -    capture_log(fn -> -      conn = -        conn -        |> get("/api/v1/search", %{"q" => Object.normalize(activity).data["id"]}) - -      assert results = json_response(conn, 200) - -      [] = results["statuses"] -    end) -  end - -  test "search fetches remote accounts", %{conn: conn} do -    user = insert(:user) - -    conn = -      conn -      |> assign(:user, user) -      |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"}) - -    assert results = json_response(conn, 200) -    [account] = results["accounts"] -    assert account["acct"] == "shp@social.heldscal.la" -  end - -  test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do -    conn = -      conn -      |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "false"}) - -    assert results = json_response(conn, 200) -    assert [] == results["accounts"] -  end -    test "returns the favorites of a user", %{conn: conn} do      user = insert(:user)      other_user = insert(:user) @@ -2484,278 +2377,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      end    end -  describe "updating credentials" do -    test "sets user settings in a generic way", %{conn: conn} do -      user = insert(:user) - -      res_conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{ -          "pleroma_settings_store" => %{ -            pleroma_fe: %{ -              theme: "bla" -            } -          } -        }) - -      assert user = json_response(res_conn, 200) -      assert user["pleroma"]["settings_store"] == %{"pleroma_fe" => %{"theme" => "bla"}} - -      user = Repo.get(User, user["id"]) - -      res_conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{ -          "pleroma_settings_store" => %{ -            masto_fe: %{ -              theme: "bla" -            } -          } -        }) - -      assert user = json_response(res_conn, 200) - -      assert user["pleroma"]["settings_store"] == -               %{ -                 "pleroma_fe" => %{"theme" => "bla"}, -                 "masto_fe" => %{"theme" => "bla"} -               } - -      user = Repo.get(User, user["id"]) - -      res_conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{ -          "pleroma_settings_store" => %{ -            masto_fe: %{ -              theme: "blub" -            } -          } -        }) - -      assert user = json_response(res_conn, 200) - -      assert user["pleroma"]["settings_store"] == -               %{ -                 "pleroma_fe" => %{"theme" => "bla"}, -                 "masto_fe" => %{"theme" => "blub"} -               } -    end - -    test "updates the user's bio", %{conn: conn} do -      user = insert(:user) -      user2 = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{ -          "note" => "I drink #cofe with @#{user2.nickname}" -        }) - -      assert user = json_response(conn, 200) - -      assert user["note"] == -               ~s(I drink <a class="hashtag" data-tag="cofe" href="http://localhost:4001/tag/cofe" rel="tag">#cofe</a> with <span class="h-card"><a data-user=") <> -                 user2.id <> -                 ~s(" class="u-url mention" href=") <> -                 user2.ap_id <> ~s(">@<span>) <> user2.nickname <> ~s(</span></a></span>) -    end - -    test "updates the user's locking status", %{conn: conn} do -      user = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{locked: "true"}) - -      assert user = json_response(conn, 200) -      assert user["locked"] == true -    end - -    test "updates the user's default scope", %{conn: conn} do -      user = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{default_scope: "cofe"}) - -      assert user = json_response(conn, 200) -      assert user["source"]["privacy"] == "cofe" -    end - -    test "updates the user's hide_followers status", %{conn: conn} do -      user = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{hide_followers: "true"}) - -      assert user = json_response(conn, 200) -      assert user["pleroma"]["hide_followers"] == true -    end - -    test "updates the user's skip_thread_containment option", %{conn: conn} do -      user = insert(:user) - -      response = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{skip_thread_containment: "true"}) -        |> json_response(200) - -      assert response["pleroma"]["skip_thread_containment"] == true -      assert refresh_record(user).info.skip_thread_containment -    end - -    test "updates the user's hide_follows status", %{conn: conn} do -      user = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{hide_follows: "true"}) - -      assert user = json_response(conn, 200) -      assert user["pleroma"]["hide_follows"] == true -    end - -    test "updates the user's hide_favorites status", %{conn: conn} do -      user = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{hide_favorites: "true"}) - -      assert user = json_response(conn, 200) -      assert user["pleroma"]["hide_favorites"] == true -    end - -    test "updates the user's show_role status", %{conn: conn} do -      user = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{show_role: "false"}) - -      assert user = json_response(conn, 200) -      assert user["source"]["pleroma"]["show_role"] == false -    end - -    test "updates the user's no_rich_text status", %{conn: conn} do -      user = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{no_rich_text: "true"}) - -      assert user = json_response(conn, 200) -      assert user["source"]["pleroma"]["no_rich_text"] == true -    end - -    test "updates the user's name", %{conn: conn} do -      user = insert(:user) - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"}) - -      assert user = json_response(conn, 200) -      assert user["display_name"] == "markorepairs" -    end - -    test "updates the user's avatar", %{conn: conn} do -      user = insert(:user) - -      new_avatar = %Plug.Upload{ -        content_type: "image/jpg", -        path: Path.absname("test/fixtures/image.jpg"), -        filename: "an_image.jpg" -      } - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar}) - -      assert user_response = json_response(conn, 200) -      assert user_response["avatar"] != User.avatar_url(user) -    end - -    test "updates the user's banner", %{conn: conn} do -      user = insert(:user) - -      new_header = %Plug.Upload{ -        content_type: "image/jpg", -        path: Path.absname("test/fixtures/image.jpg"), -        filename: "an_image.jpg" -      } - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header}) - -      assert user_response = json_response(conn, 200) -      assert user_response["header"] != User.banner_url(user) -    end - -    test "requires 'write' permission", %{conn: conn} do -      token1 = insert(:oauth_token, scopes: ["read"]) -      token2 = insert(:oauth_token, scopes: ["write", "follow"]) - -      for token <- [token1, token2] do -        conn = -          conn -          |> put_req_header("authorization", "Bearer #{token.token}") -          |> patch("/api/v1/accounts/update_credentials", %{}) - -        if token == token1 do -          assert %{"error" => "Insufficient permissions: write."} == json_response(conn, 403) -        else -          assert json_response(conn, 200) -        end -      end -    end - -    test "updates profile emojos", %{conn: conn} do -      user = insert(:user) - -      note = "*sips :blank:*" -      name = "I am :firefox:" - -      conn = -        conn -        |> assign(:user, user) -        |> patch("/api/v1/accounts/update_credentials", %{ -          "note" => note, -          "display_name" => name -        }) - -      assert json_response(conn, 200) - -      conn = -        conn -        |> get("/api/v1/accounts/#{user.id}") - -      assert user = json_response(conn, 200) - -      assert user["note"] == note -      assert user["display_name"] == name -      assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user["emojis"] -    end -  end -    test "get instance information", %{conn: conn} do      conn = get(conn, "/api/v1/instance")      assert result = json_response(conn, 200) diff --git a/test/web/mastodon_api/search_controller_test.exs b/test/web/mastodon_api/search_controller_test.exs new file mode 100644 index 000000000..c3f531590 --- /dev/null +++ b/test/web/mastodon_api/search_controller_test.exs @@ -0,0 +1,128 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do +  use Pleroma.Web.ConnCase + +  alias Pleroma.Object +  alias Pleroma.Web.CommonAPI +  import Pleroma.Factory +  import ExUnit.CaptureLog +  import Tesla.Mock + +  setup do +    mock(fn env -> apply(HttpRequestMock, :request, [env]) end) +    :ok +  end + +  test "account search", %{conn: conn} do +    user = insert(:user) +    user_two = insert(:user, %{nickname: "shp@shitposter.club"}) +    user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"}) + +    results = +      conn +      |> assign(:user, user) +      |> get("/api/v1/accounts/search", %{"q" => "shp"}) +      |> json_response(200) + +    result_ids = for result <- results, do: result["acct"] + +    assert user_two.nickname in result_ids +    assert user_three.nickname in result_ids + +    results = +      conn +      |> assign(:user, user) +      |> get("/api/v1/accounts/search", %{"q" => "2hu"}) +      |> json_response(200) + +    result_ids = for result <- results, do: result["acct"] + +    assert user_three.nickname in result_ids +  end + +  test "search", %{conn: conn} do +    user = insert(:user) +    user_two = insert(:user, %{nickname: "shp@shitposter.club"}) +    user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"}) + +    {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"}) + +    {:ok, _activity} = +      CommonAPI.post(user, %{ +        "status" => "This is about 2hu, but private", +        "visibility" => "private" +      }) + +    {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"}) + +    conn = +      conn +      |> get("/api/v1/search", %{"q" => "2hu"}) + +    assert results = json_response(conn, 200) + +    [account | _] = results["accounts"] +    assert account["id"] == to_string(user_three.id) + +    assert results["hashtags"] == [] + +    [status] = results["statuses"] +    assert status["id"] == to_string(activity.id) +  end + +  test "search fetches remote statuses", %{conn: conn} do +    capture_log(fn -> +      conn = +        conn +        |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"}) + +      assert results = json_response(conn, 200) + +      [status] = results["statuses"] +      assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" +    end) +  end + +  test "search doesn't show statuses that it shouldn't", %{conn: conn} do +    {:ok, activity} = +      CommonAPI.post(insert(:user), %{ +        "status" => "This is about 2hu, but private", +        "visibility" => "private" +      }) + +    capture_log(fn -> +      conn = +        conn +        |> get("/api/v1/search", %{"q" => Object.normalize(activity).data["id"]}) + +      assert results = json_response(conn, 200) + +      [] = results["statuses"] +    end) +  end + +  test "search fetches remote accounts", %{conn: conn} do +    user = insert(:user) + +    conn = +      conn +      |> assign(:user, user) +      |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"}) + +    assert results = json_response(conn, 200) +    [account] = results["accounts"] +    assert account["acct"] == "shp@social.heldscal.la" +  end + +  test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do +    conn = +      conn +      |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "false"}) + +    assert results = json_response(conn, 200) +    assert [] == results["accounts"] +  end +end diff --git a/test/web/oauth/oauth_controller_test.exs b/test/web/oauth/oauth_controller_test.exs index 1c04ac9ad..242b7fdb3 100644 --- a/test/web/oauth/oauth_controller_test.exs +++ b/test/web/oauth/oauth_controller_test.exs @@ -408,7 +408,11 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do        assert html_response(conn, 200) =~ ~s(type="submit")      end -    test "redirects to app if user is already authenticated", %{app: app, conn: conn} do +    test "with existing authentication and non-OOB `redirect_uri`, redirects to app with `token` and `state` params", +         %{ +           app: app, +           conn: conn +         } do        token = insert(:oauth_token, app_id: app.id)        conn = @@ -420,11 +424,36 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do              "response_type" => "code",              "client_id" => app.client_id,              "redirect_uri" => app.redirect_uris, +            "state" => "specific_client_state", +            "scope" => "read" +          } +        ) + +      assert URI.decode(redirected_to(conn)) == +               "https://redirect.url?access_token=#{token.token}&state=specific_client_state" +    end + +    test "with existing authentication and OOB `redirect_uri`, redirects to app with `token` and `state` params", +         %{ +           app: app, +           conn: conn +         } do +      token = insert(:oauth_token, app_id: app.id) + +      conn = +        conn +        |> put_session(:oauth_token, token.token) +        |> get( +          "/oauth/authorize", +          %{ +            "response_type" => "code", +            "client_id" => app.client_id, +            "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob",              "scope" => "read"            }          ) -      assert redirected_to(conn) == "https://redirect.url" +      assert html_response(conn, 200) =~ "Authorization exists"      end    end diff --git a/test/web/rich_media/parser_test.exs b/test/web/rich_media/parser_test.exs index 3a9cc1854..a49ba9549 100644 --- a/test/web/rich_media/parser_test.exs +++ b/test/web/rich_media/parser_test.exs @@ -11,6 +11,15 @@ defmodule Pleroma.Web.RichMedia.ParserTest do        %{          method: :get, +        url: "http://example.com/ogp-missing-title" +      } -> +        %Tesla.Env{ +          status: 200, +          body: File.read!("test/fixtures/rich_media/ogp-missing-title.html") +        } + +      %{ +        method: :get,          url: "http://example.com/twitter-card"        } ->          %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")} @@ -51,6 +60,19 @@ defmodule Pleroma.Web.RichMedia.ParserTest do                }}    end +  test "falls back to <title> when ogp:title is missing" do +    assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp-missing-title") == +             {:ok, +              %{ +                image: "http://ia.media-imdb.com/images/rock.jpg", +                title: "The Rock (1996)", +                description: +                  "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.", +                type: "video.movie", +                url: "http://www.imdb.com/title/tt0117500/" +              }} +  end +    test "parses twitter card" do      assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/twitter-card") ==               {:ok,  | 
