diff options
Diffstat (limited to 'test')
30 files changed, 1793 insertions, 270 deletions
| diff --git a/test/activity_test.exs b/test/activity_test.exs index ad889f544..7e91d534b 100644 --- a/test/activity_test.exs +++ b/test/activity_test.exs @@ -5,6 +5,7 @@  defmodule Pleroma.ActivityTest do    use Pleroma.DataCase    alias Pleroma.Activity +  alias Pleroma.Bookmark    import Pleroma.Factory    test "returns an activity by it's AP id" do @@ -28,4 +29,48 @@ defmodule Pleroma.ActivityTest do      assert activity == found_activity    end + +  test "preloading a bookmark" do +    user = insert(:user) +    user2 = insert(:user) +    user3 = insert(:user) +    activity = insert(:note_activity) +    {:ok, _bookmark} = Bookmark.create(user.id, activity.id) +    {:ok, _bookmark2} = Bookmark.create(user2.id, activity.id) +    {:ok, bookmark3} = Bookmark.create(user3.id, activity.id) + +    queried_activity = +      Ecto.Query.from(Pleroma.Activity) +      |> Activity.with_preloaded_bookmark(user3) +      |> Repo.one() + +    assert queried_activity.bookmark == bookmark3 +  end + +  describe "getting a bookmark" do +    test "when association is loaded" do +      user = insert(:user) +      activity = insert(:note_activity) +      {:ok, bookmark} = Bookmark.create(user.id, activity.id) + +      queried_activity = +        Ecto.Query.from(Pleroma.Activity) +        |> Activity.with_preloaded_bookmark(user) +        |> Repo.one() + +      assert Activity.get_bookmark(queried_activity, user) == bookmark +    end + +    test "when association is not loaded" do +      user = insert(:user) +      activity = insert(:note_activity) +      {:ok, bookmark} = Bookmark.create(user.id, activity.id) + +      queried_activity = +        Ecto.Query.from(Pleroma.Activity) +        |> Repo.one() + +      assert Activity.get_bookmark(queried_activity, user) == bookmark +    end +  end  end diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs new file mode 100644 index 000000000..7d5d68d11 --- /dev/null +++ b/test/bbs/handler_test.exs @@ -0,0 +1,83 @@ +defmodule Pleroma.BBS.HandlerTest do +  use Pleroma.DataCase +  alias Pleroma.Activity +  alias Pleroma.BBS.Handler +  alias Pleroma.Object +  alias Pleroma.Repo +  alias Pleroma.User +  alias Pleroma.Web.CommonAPI + +  import ExUnit.CaptureIO +  import Pleroma.Factory +  import Ecto.Query + +  test "getting the home timeline" do +    user = insert(:user) +    followed = insert(:user) + +    {:ok, user} = User.follow(user, followed) + +    {:ok, _first} = CommonAPI.post(user, %{"status" => "hey"}) +    {:ok, _second} = CommonAPI.post(followed, %{"status" => "hello"}) + +    output = +      capture_io(fn -> +        Handler.handle_command(%{user: user}, "home") +      end) + +    assert output =~ user.nickname +    assert output =~ followed.nickname + +    assert output =~ "hey" +    assert output =~ "hello" +  end + +  test "posting" do +    user = insert(:user) + +    output = +      capture_io(fn -> +        Handler.handle_command(%{user: user}, "p this is a test post") +      end) + +    assert output =~ "Posted" + +    activity = +      Repo.one( +        from(a in Activity, +          where: fragment("?->>'type' = ?", a.data, "Create") +        ) +      ) + +    assert activity.actor == user.ap_id +    object = Object.normalize(activity) +    assert object.data["content"] == "this is a test post" +  end + +  test "replying" do +    user = insert(:user) +    another_user = insert(:user) + +    {:ok, activity} = CommonAPI.post(another_user, %{"status" => "this is a test post"}) + +    output = +      capture_io(fn -> +        Handler.handle_command(%{user: user}, "r #{activity.id} this is a reply") +      end) + +    assert output =~ "Replied" + +    reply = +      Repo.one( +        from(a in Activity, +          where: fragment("?->>'type' = ?", a.data, "Create"), +          where: a.actor == ^user.ap_id +        ) +      ) + +    assert reply.actor == user.ap_id +    object = Object.normalize(reply) +    assert object.data["content"] == "this is a reply" +    assert object.data["inReplyTo"] == activity.data["object"] +  end +end diff --git a/test/bookmark_test.exs b/test/bookmark_test.exs new file mode 100644 index 000000000..b81c102ef --- /dev/null +++ b/test/bookmark_test.exs @@ -0,0 +1,52 @@ +defmodule Pleroma.BookmarkTest do +  use Pleroma.DataCase +  import Pleroma.Factory +  alias Pleroma.Bookmark +  alias Pleroma.Web.CommonAPI + +  describe "create/2" do +    test "with valid params" do +      user = insert(:user) +      {:ok, activity} = CommonAPI.post(user, %{"status" => "Some cool information"}) +      {:ok, bookmark} = Bookmark.create(user.id, activity.id) +      assert bookmark.user_id == user.id +      assert bookmark.activity_id == activity.id +    end + +    test "with invalid params" do +      {:error, changeset} = Bookmark.create(nil, "") +      refute changeset.valid? + +      assert changeset.errors == [ +               user_id: {"can't be blank", [validation: :required]}, +               activity_id: {"can't be blank", [validation: :required]} +             ] +    end +  end + +  describe "destroy/2" do +    test "with valid params" do +      user = insert(:user) + +      {:ok, activity} = CommonAPI.post(user, %{"status" => "Some cool information"}) +      {:ok, _bookmark} = Bookmark.create(user.id, activity.id) + +      {:ok, _deleted_bookmark} = Bookmark.destroy(user.id, activity.id) +    end +  end + +  describe "get/2" do +    test "gets a bookmark" do +      user = insert(:user) + +      {:ok, activity} = +        CommonAPI.post(user, %{ +          "status" => +            "Scientists Discover The Secret Behind Tenshi Eating A Corndog Being So Cute – Science Daily" +        }) + +      {:ok, bookmark} = Bookmark.create(user.id, activity.id) +      assert bookmark == Bookmark.get(user.id, activity.id) +    end +  end +end diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs new file mode 100644 index 000000000..568953b07 --- /dev/null +++ b/test/conversation/participation_test.exs @@ -0,0 +1,89 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Conversation.ParticipationTest do +  use Pleroma.DataCase +  import Pleroma.Factory +  alias Pleroma.Conversation.Participation +  alias Pleroma.Web.CommonAPI + +  test "it creates a participation for a conversation and a user" do +    user = insert(:user) +    conversation = insert(:conversation) + +    {:ok, %Participation{} = participation} = +      Participation.create_for_user_and_conversation(user, conversation) + +    assert participation.user_id == user.id +    assert participation.conversation_id == conversation.id + +    :timer.sleep(1000) +    # Creating again returns the same participation +    {:ok, %Participation{} = participation_two} = +      Participation.create_for_user_and_conversation(user, conversation) + +    assert participation.id == participation_two.id +    refute participation.updated_at == participation_two.updated_at +  end + +  test "recreating an existing participations sets it to unread" do +    participation = insert(:participation, %{read: true}) + +    {:ok, participation} = +      Participation.create_for_user_and_conversation( +        participation.user, +        participation.conversation +      ) + +    refute participation.read +  end + +  test "it marks a participation as read" do +    participation = insert(:participation, %{read: false}) +    {:ok, participation} = Participation.mark_as_read(participation) + +    assert participation.read +  end + +  test "it marks a participation as unread" do +    participation = insert(:participation, %{read: true}) +    {:ok, participation} = Participation.mark_as_unread(participation) + +    refute participation.read +  end + +  test "gets all the participations for a user, ordered by updated at descending" do +    user = insert(:user) +    {:ok, activity_one} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"}) +    :timer.sleep(1000) +    {:ok, activity_two} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"}) +    :timer.sleep(1000) + +    {:ok, activity_three} = +      CommonAPI.post(user, %{ +        "status" => "x", +        "visibility" => "direct", +        "in_reply_to_status_id" => activity_one.id +      }) + +    assert [participation_one, participation_two] = Participation.for_user(user) + +    object2 = Pleroma.Object.normalize(activity_two) +    object3 = Pleroma.Object.normalize(activity_three) + +    assert participation_one.conversation.ap_id == object3.data["context"] +    assert participation_two.conversation.ap_id == object2.data["context"] + +    # Pagination +    assert [participation_one] = Participation.for_user(user, %{"limit" => 1}) + +    assert participation_one.conversation.ap_id == object3.data["context"] + +    # With last_activity_id +    assert [participation_one] = +             Participation.for_user_with_last_activity_id(user, %{"limit" => 1}) + +    assert participation_one.last_activity_id == activity_three.id +  end +end diff --git a/test/conversation_test.exs b/test/conversation_test.exs new file mode 100644 index 000000000..864b2eb03 --- /dev/null +++ b/test/conversation_test.exs @@ -0,0 +1,175 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.ConversationTest do +  use Pleroma.DataCase +  alias Pleroma.Activity +  alias Pleroma.Conversation +  alias Pleroma.Object +  alias Pleroma.Web.CommonAPI + +  import Pleroma.Factory + +  test "it creates a conversation for given ap_id" do +    assert {:ok, %Conversation{} = conversation} = +             Conversation.create_for_ap_id("https://some_ap_id") + +    # Inserting again returns the same +    assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id") +    assert conversation_two.id == conversation.id +  end + +  test "public posts don't create conversations" do +    user = insert(:user) +    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"}) + +    object = Pleroma.Object.normalize(activity) +    context = object.data["context"] + +    conversation = Conversation.get_for_ap_id(context) + +    refute conversation +  end + +  test "it creates or updates a conversation and participations for a given DM" do +    har = insert(:user) +    jafnhar = insert(:user, local: false) +    tridi = insert(:user) + +    {:ok, activity} = +      CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"}) + +    object = Pleroma.Object.normalize(activity) +    context = object.data["context"] + +    conversation = +      Conversation.get_for_ap_id(context) +      |> Repo.preload(:participations) + +    assert conversation + +    assert Enum.find(conversation.participations, fn %{user_id: user_id} -> har.id == user_id end) + +    assert Enum.find(conversation.participations, fn %{user_id: user_id} -> +             jafnhar.id == user_id +           end) + +    {:ok, activity} = +      CommonAPI.post(jafnhar, %{ +        "status" => "Hey @#{har.nickname}", +        "visibility" => "direct", +        "in_reply_to_status_id" => activity.id +      }) + +    object = Pleroma.Object.normalize(activity) +    context = object.data["context"] + +    conversation_two = +      Conversation.get_for_ap_id(context) +      |> Repo.preload(:participations) + +    assert conversation_two.id == conversation.id + +    assert Enum.find(conversation_two.participations, fn %{user_id: user_id} -> +             har.id == user_id +           end) + +    assert Enum.find(conversation_two.participations, fn %{user_id: user_id} -> +             jafnhar.id == user_id +           end) + +    {:ok, activity} = +      CommonAPI.post(tridi, %{ +        "status" => "Hey @#{har.nickname}", +        "visibility" => "direct", +        "in_reply_to_status_id" => activity.id +      }) + +    object = Pleroma.Object.normalize(activity) +    context = object.data["context"] + +    conversation_three = +      Conversation.get_for_ap_id(context) +      |> Repo.preload([:participations, :users]) + +    assert conversation_three.id == conversation.id + +    assert Enum.find(conversation_three.participations, fn %{user_id: user_id} -> +             har.id == user_id +           end) + +    assert Enum.find(conversation_three.participations, fn %{user_id: user_id} -> +             jafnhar.id == user_id +           end) + +    assert Enum.find(conversation_three.participations, fn %{user_id: user_id} -> +             tridi.id == user_id +           end) + +    assert Enum.find(conversation_three.users, fn %{id: user_id} -> +             har.id == user_id +           end) + +    assert Enum.find(conversation_three.users, fn %{id: user_id} -> +             jafnhar.id == user_id +           end) + +    assert Enum.find(conversation_three.users, fn %{id: user_id} -> +             tridi.id == user_id +           end) +  end + +  test "create_or_bump_for returns the conversation with participations" do +    har = insert(:user) +    jafnhar = insert(:user, local: false) + +    {:ok, activity} = +      CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"}) + +    {:ok, conversation} = Conversation.create_or_bump_for(activity) + +    assert length(conversation.participations) == 2 + +    {:ok, activity} = +      CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "public"}) + +    assert {:error, _} = Conversation.create_or_bump_for(activity) +  end + +  test "create_or_bump_for does not normalize objects before checking the activity type" do +    note = insert(:note) +    note_id = note.data["id"] +    Repo.delete(note) +    refute Object.get_by_ap_id(note_id) + +    Tesla.Mock.mock(fn env -> +      case env.url do +        ^note_id -> +          # TODO: add attributedTo and tag to the note factory +          body = +            note.data +            |> Map.put("attributedTo", note.data["actor"]) +            |> Map.put("tag", []) +            |> Jason.encode!() + +          %Tesla.Env{status: 200, body: body} +      end +    end) + +    undo = %Activity{ +      id: "fake", +      data: %{ +        "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(), +        "actor" => note.data["actor"], +        "to" => [note.data["actor"]], +        "object" => note_id, +        "type" => "Undo" +      } +    } + +    Conversation.create_or_bump_for(undo) + +    refute Object.get_by_ap_id(note_id) +  end +end diff --git a/test/formatter_test.exs b/test/formatter_test.exs index 97eb2f583..06f4f6e50 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -147,7 +147,7 @@ defmodule Pleroma.FormatterTest do      end      test "gives a replacement for user links when the user is using Osada" do -      mike = User.get_or_fetch("mike@osada.macgirvin.com") +      {:ok, mike} = User.get_or_fetch("mike@osada.macgirvin.com")        text = "@mike@osada.macgirvin.com test" @@ -248,7 +248,7 @@ defmodule Pleroma.FormatterTest do      text = "I love :firefox:"      expected_result = -      "I love <img height=\"32px\" width=\"32px\" alt=\"firefox\" title=\"firefox\" src=\"/emoji/Firefox.gif\" />" +      "I love <img class=\"emoji\" alt=\"firefox\" title=\"firefox\" src=\"/emoji/Firefox.gif\" />"      assert Formatter.emojify(text) == expected_result    end @@ -263,7 +263,7 @@ defmodule Pleroma.FormatterTest do      }      expected_result = -      "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />" +      "I love <img class=\"emoji\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"      assert Formatter.emojify(text, custom_emoji) == expected_result    end diff --git a/test/media_proxy_test.exs b/test/media_proxy_test.exs index ddbadfbf5..0a02039a6 100644 --- a/test/media_proxy_test.exs +++ b/test/media_proxy_test.exs @@ -7,15 +7,15 @@ defmodule Pleroma.MediaProxyTest do    import Pleroma.Web.MediaProxy    alias Pleroma.Web.MediaProxy.MediaProxyController +  setup do +    enabled = Pleroma.Config.get([:media_proxy, :enabled]) +    on_exit(fn -> Pleroma.Config.put([:media_proxy, :enabled], enabled) end) +    :ok +  end +    describe "when enabled" do      setup do -      enabled = Pleroma.Config.get([:media_proxy, :enabled]) - -      unless enabled do -        Pleroma.Config.put([:media_proxy, :enabled], true) -        on_exit(fn -> Pleroma.Config.put([:media_proxy, :enabled], enabled) end) -      end - +      Pleroma.Config.put([:media_proxy, :enabled], true)        :ok      end @@ -177,4 +177,13 @@ defmodule Pleroma.MediaProxyTest do      {:ok, decoded} = decode_url(sig, base64)      decoded    end + +  test "mediaproxy whitelist" do +    Pleroma.Config.put([:media_proxy, :enabled], true) +    Pleroma.Config.put([:media_proxy, :whitelist], ["google.com", "feld.me"]) +    url = "https://feld.me/foo.png" + +    unencoded = url(url) +    assert unencoded == url +  end  end diff --git a/test/plugs/oauth_plug_test.exs b/test/plugs/oauth_plug_test.exs index 17fdba916..5a2ed11cc 100644 --- a/test/plugs/oauth_plug_test.exs +++ b/test/plugs/oauth_plug_test.exs @@ -38,6 +38,26 @@ defmodule Pleroma.Plugs.OAuthPlugTest do      assert conn.assigns[:user] == opts[:user]    end +  test "with valid token(downcase) in url parameters, it assings the user", opts do +    conn = +      :get +      |> build_conn("/?access_token=#{opts[:token]}") +      |> put_req_header("content-type", "application/json") +      |> fetch_query_params() +      |> OAuthPlug.call(%{}) + +    assert conn.assigns[:user] == opts[:user] +  end + +  test "with valid token(downcase) in body parameters, it assigns the user", opts do +    conn = +      :post +      |> build_conn("/api/v1/statuses", access_token: opts[:token], status: "test") +      |> OAuthPlug.call(%{}) + +    assert conn.assigns[:user] == opts[:user] +  end +    test "with invalid token, it not assigns the user", %{conn: conn} do      conn =        conn diff --git a/test/plugs/rate_limit_plug_test.exs b/test/plugs/rate_limit_plug_test.exs new file mode 100644 index 000000000..2ec9a8fb7 --- /dev/null +++ b/test/plugs/rate_limit_plug_test.exs @@ -0,0 +1,50 @@ +defmodule Pleroma.Plugs.RateLimitPlugTest do +  use ExUnit.Case, async: true +  use Plug.Test + +  alias Pleroma.Plugs.RateLimitPlug + +  @opts RateLimitPlug.init(%{max_requests: 5, interval: 1}) + +  setup do +    enabled = Pleroma.Config.get([:app_account_creation, :enabled]) + +    Pleroma.Config.put([:app_account_creation, :enabled], true) + +    on_exit(fn -> +      Pleroma.Config.put([:app_account_creation, :enabled], enabled) +    end) + +    :ok +  end + +  test "it restricts by opts" do +    conn = conn(:get, "/") +    bucket_name = conn.remote_ip |> Tuple.to_list() |> Enum.join(".") +    ms = 1000 + +    conn = RateLimitPlug.call(conn, @opts) +    {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) +    conn = RateLimitPlug.call(conn, @opts) +    {2, 3, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) +    conn = RateLimitPlug.call(conn, @opts) +    {3, 2, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) +    conn = RateLimitPlug.call(conn, @opts) +    {4, 1, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) +    conn = RateLimitPlug.call(conn, @opts) +    {5, 0, to_reset, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) +    conn = RateLimitPlug.call(conn, @opts) +    assert conn.status == 403 +    assert conn.halted +    assert conn.resp_body == "{\"error\":\"Rate limit exceeded.\"}" + +    Process.sleep(to_reset) + +    conn = conn(:get, "/") +    conn = RateLimitPlug.call(conn, @opts) +    {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) +    refute conn.status == 403 +    refute conn.halted +    refute conn.resp_body +  end +end diff --git a/test/repo_test.exs b/test/repo_test.exs new file mode 100644 index 000000000..5382289c7 --- /dev/null +++ b/test/repo_test.exs @@ -0,0 +1,44 @@ +defmodule Pleroma.RepoTest do +  use Pleroma.DataCase +  import Pleroma.Factory + +  describe "find_resource/1" do +    test "returns user" do +      user = insert(:user) +      query = from(t in Pleroma.User, where: t.id == ^user.id) +      assert Repo.find_resource(query) == {:ok, user} +    end + +    test "returns not_found" do +      query = from(t in Pleroma.User, where: t.id == ^"9gBuXNpD2NyDmmxxdw") +      assert Repo.find_resource(query) == {:error, :not_found} +    end +  end + +  describe "get_assoc/2" do +    test "get assoc from preloaded data" do +      user = %Pleroma.User{name: "Agent Smith"} +      token = %Pleroma.Web.OAuth.Token{insert(:oauth_token) | user: user} +      assert Repo.get_assoc(token, :user) == {:ok, user} +    end + +    test "get one-to-one assoc from repo" do +      user = insert(:user, name: "Jimi Hendrix") +      token = refresh_record(insert(:oauth_token, user: user)) + +      assert Repo.get_assoc(token, :user) == {:ok, user} +    end + +    test "get one-to-many assoc from repo" do +      user = insert(:user) +      notification = refresh_record(insert(:notification, user: user)) + +      assert Repo.get_assoc(user, :notifications) == {:ok, [notification]} +    end + +    test "return error if has not assoc " do +      token = insert(:oauth_token, user: nil) +      assert Repo.get_assoc(token, :user) == {:error, :not_found} +    end +  end +end diff --git a/test/support/factory.ex b/test/support/factory.ex index ea59912cf..2a2954ad6 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -5,6 +5,23 @@  defmodule Pleroma.Factory do    use ExMachina.Ecto, repo: Pleroma.Repo +  def participation_factory do +    conversation = insert(:conversation) +    user = insert(:user) + +    %Pleroma.Conversation.Participation{ +      conversation: conversation, +      user: user, +      read: false +    } +  end + +  def conversation_factory do +    %Pleroma.Conversation{ +      ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}") +    } +  end +    def user_factory do      user = %Pleroma.User{        name: sequence(:name, &"Test テスト User #{&1}"), diff --git a/test/user_test.exs b/test/user_test.exs index 42d570c50..60de0206e 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -349,7 +349,7 @@ defmodule Pleroma.UserTest do      end      test "it creates confirmed user if :confirmed option is given" do -      changeset = User.register_changeset(%User{}, @full_user_data, confirmed: true) +      changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false)        assert changeset.valid?        {:ok, user} = Repo.insert(changeset) @@ -362,7 +362,7 @@ defmodule Pleroma.UserTest do    describe "get_or_fetch/1" do      test "gets an existing user by nickname" do        user = insert(:user) -      fetched_user = User.get_or_fetch(user.nickname) +      {:ok, fetched_user} = User.get_or_fetch(user.nickname)        assert user == fetched_user      end @@ -379,7 +379,7 @@ defmodule Pleroma.UserTest do            info: %{}          ) -      fetched_user = User.get_or_fetch(ap_id) +      {:ok, fetched_user} = User.get_or_fetch(ap_id)        freshed_user = refresh_record(user)        assert freshed_user == fetched_user      end @@ -388,14 +388,14 @@ defmodule Pleroma.UserTest do    describe "fetching a user from nickname or trying to build one" do      test "gets an existing user" do        user = insert(:user) -      fetched_user = User.get_or_fetch_by_nickname(user.nickname) +      {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname)        assert user == fetched_user      end      test "gets an existing user, case insensitive" do        user = insert(:user, nickname: "nick") -      fetched_user = User.get_or_fetch_by_nickname("NICK") +      {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK")        assert user == fetched_user      end @@ -403,7 +403,7 @@ defmodule Pleroma.UserTest do      test "gets an existing user by fully qualified nickname" do        user = insert(:user) -      fetched_user = +      {:ok, fetched_user} =          User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host())        assert user == fetched_user @@ -413,24 +413,24 @@ defmodule Pleroma.UserTest do        user = insert(:user, nickname: "nick")        casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host()) -      fetched_user = User.get_or_fetch_by_nickname(casing_altered_fqn) +      {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn)        assert user == fetched_user      end      test "fetches an external user via ostatus if no user exists" do -      fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la") +      {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la")        assert fetched_user.nickname == "shp@social.heldscal.la"      end      test "returns nil if no user could be fetched" do -      fetched_user = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la") -      assert fetched_user == nil +      {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la") +      assert fetched_user == "not found nonexistant@social.heldscal.la"      end      test "returns nil for nonexistant local user" do -      fetched_user = User.get_or_fetch_by_nickname("nonexistant") -      assert fetched_user == nil +      {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant") +      assert fetched_user == "not found nonexistant"      end      test "updates an existing user, if stale" do @@ -448,7 +448,7 @@ defmodule Pleroma.UserTest do        assert orig_user.last_refreshed_at == a_week_ago -      user = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin") +      {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")        assert user.info.source_data["endpoints"]        refute user.last_refreshed_at == orig_user.last_refreshed_at @@ -829,10 +829,12 @@ defmodule Pleroma.UserTest do      user = insert(:user)      {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"}) -    {:ok, _} = User.delete_user_activities(user) -    # TODO: Remove favorites, repeats, delete activities. -    refute Activity.get_by_id(activity.id) +    Ecto.Adapters.SQL.Sandbox.unboxed_run(Repo, fn -> +      {:ok, _} = User.delete_user_activities(user) +      # TODO: Remove favorites, repeats, delete activities. +      refute Activity.get_by_id(activity.id) +    end)    end    test ".delete deactivates a user, all follow relationships and all create activities" do @@ -1103,7 +1105,7 @@ defmodule Pleroma.UserTest do        expected_text =          "A.k.a. <span class='h-card'><a data-user='#{remote_user.id}' class='u-url mention' href='#{            remote_user.ap_id -        }'>" <> "@<span>nick@domain.com</span></a></span>" +        }'>@<span>nick@domain.com</span></a></span>"        assert expected_text == User.parse_bio(bio, user)      end @@ -1125,33 +1127,6 @@ defmodule Pleroma.UserTest do      end    end -  test "bookmarks" do -    user = insert(:user) - -    {:ok, activity1} = -      CommonAPI.post(user, %{ -        "status" => "heweoo!" -      }) - -    id1 = Object.normalize(activity1).data["id"] - -    {:ok, activity2} = -      CommonAPI.post(user, %{ -        "status" => "heweoo!" -      }) - -    id2 = Object.normalize(activity2).data["id"] - -    assert {:ok, user_state1} = User.bookmark(user, id1) -    assert user_state1.bookmarks == [id1] - -    assert {:ok, user_state2} = User.unbookmark(user, id1) -    assert user_state2.bookmarks == [] - -    assert {:ok, user_state3} = User.bookmark(user, id2) -    assert user_state3.bookmarks == [id2] -  end -    test "follower count is updated when a follower is blocked" do      user = insert(:user)      follower = insert(:user) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index f8e987e58..0f90aa1ac 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -10,6 +10,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do    alias Pleroma.Object    alias Pleroma.User    alias Pleroma.Web.ActivityPub.ActivityPub +  alias Pleroma.Web.ActivityPub.Publisher    alias Pleroma.Web.ActivityPub.Utils    alias Pleroma.Web.CommonAPI @@ -22,6 +23,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do      :ok    end +  describe "streaming out participations" do +    test "it streams them out" do +      user = insert(:user) +      {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"}) + +      {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity) + +      participations = +        conversation.participations +        |> Repo.preload(:user) + +      with_mock Pleroma.Web.Streamer, +        stream: fn _, _ -> nil end do +        ActivityPub.stream_out_participations(conversation.participations) + +        Enum.each(participations, fn participation -> +          assert called(Pleroma.Web.Streamer.stream("participation", participation)) +        end) +      end +    end +  end +    describe "fetching restricted by visibility" do      test "it restricts by the appropriate visibility" do        user = insert(:user) @@ -130,9 +153,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do      end      test "doesn't drop activities with content being null" do +      user = insert(:user) +        data = %{ -        "ok" => true, +        "actor" => user.ap_id, +        "to" => [],          "object" => %{ +          "actor" => user.ap_id, +          "to" => [], +          "type" => "Note",            "content" => nil          }        } @@ -148,8 +177,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do      end      test "inserts a given map into the activity database, giving it an id if it has none." do +      user = insert(:user) +        data = %{ -        "ok" => true +        "actor" => user.ap_id, +        "to" => [], +        "object" => %{ +          "actor" => user.ap_id, +          "to" => [], +          "type" => "Note", +          "content" => "hey" +        }        }        {:ok, %Activity{} = activity} = ActivityPub.insert(data) @@ -159,9 +197,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        given_id = "bla"        data = %{ -        "ok" => true,          "id" => given_id, -        "context" => "blabla" +        "actor" => user.ap_id, +        "to" => [], +        "context" => "blabla", +        "object" => %{ +          "actor" => user.ap_id, +          "to" => [], +          "type" => "Note", +          "content" => "hey" +        }        }        {:ok, %Activity{} = activity} = ActivityPub.insert(data) @@ -172,26 +217,39 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do      end      test "adds a context when none is there" do +      user = insert(:user) +        data = %{ -        "id" => "some_id", +        "actor" => user.ap_id, +        "to" => [],          "object" => %{ -          "id" => "object_id" +          "actor" => user.ap_id, +          "to" => [], +          "type" => "Note", +          "content" => "hey"          }        }        {:ok, %Activity{} = activity} = ActivityPub.insert(data) +      object = Pleroma.Object.normalize(activity)        assert is_binary(activity.data["context"]) -      assert is_binary(activity.data["object"]["context"]) +      assert is_binary(object.data["context"])        assert activity.data["context_id"] -      assert activity.data["object"]["context_id"] +      assert object.data["context_id"]      end      test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do +      user = insert(:user) +        data = %{ +        "actor" => user.ap_id, +        "to" => [],          "object" => %{ +          "actor" => user.ap_id, +          "to" => [],            "type" => "Note", -          "ok" => true +          "content" => "hey"          }        } @@ -906,8 +964,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"]) -      assert [public_activity, private_activity_1, private_activity_3] == -               activities +      assert [public_activity, private_activity_1, private_activity_3] == activities        assert length(activities) == 3 @@ -1000,7 +1057,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        actor = insert(:user)        inbox = "http://200.site/users/nick1/inbox" -      assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) +      assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})        assert called(Instances.set_reachable(inbox))      end @@ -1013,7 +1070,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        inbox = "http://200.site/users/nick1/inbox"        assert {:ok, _} = -               ActivityPub.publish_one(%{ +               Publisher.publish_one(%{                   inbox: inbox,                   json: "{}",                   actor: actor, @@ -1032,7 +1089,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        inbox = "http://200.site/users/nick1/inbox"        assert {:ok, _} = -               ActivityPub.publish_one(%{ +               Publisher.publish_one(%{                   inbox: inbox,                   json: "{}",                   actor: actor, @@ -1050,8 +1107,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        actor = insert(:user)        inbox = "http://404.site/users/nick1/inbox" -      assert {:error, _} = -               ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) +      assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})        assert called(Instances.set_unreachable(inbox))      end @@ -1063,8 +1119,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        actor = insert(:user)        inbox = "http://connrefused.site/users/nick1/inbox" -      assert {:error, _} = -               ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) +      assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})        assert called(Instances.set_unreachable(inbox))      end @@ -1076,7 +1131,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        actor = insert(:user)        inbox = "http://200.site/users/nick1/inbox" -      assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) +      assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})        refute called(Instances.set_unreachable(inbox))      end @@ -1089,7 +1144,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        inbox = "http://connrefused.site/users/nick1/inbox"        assert {:error, _} = -               ActivityPub.publish_one(%{ +               Publisher.publish_one(%{                   inbox: inbox,                   json: "{}",                   actor: actor, diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 78429c7c6..c24b50f8c 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -219,7 +219,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        Pleroma.Config.put([:user, :deny_follow_blocked], true)        user = insert(:user) -      target = User.get_or_fetch("http://mastodon.example.org/users/admin") +      {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")        {:ok, user} = User.block(user, target) diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index b89c42327..6c1897b5a 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -9,7 +9,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    alias Pleroma.UserInviteToken    import Pleroma.Factory -  describe "/api/pleroma/admin/user" do +  describe "/api/pleroma/admin/users" do      test "Delete" do        admin = insert(:user, info: %{is_admin: true})        user = insert(:user) @@ -18,7 +18,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          build_conn()          |> assign(:user, admin)          |> put_req_header("accept", "application/json") -        |> delete("/api/pleroma/admin/user?nickname=#{user.nickname}") +        |> delete("/api/pleroma/admin/users?nickname=#{user.nickname}")        assert json_response(conn, 200) == user.nickname      end @@ -30,7 +30,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          build_conn()          |> assign(:user, admin)          |> put_req_header("accept", "application/json") -        |> post("/api/pleroma/admin/user", %{ +        |> post("/api/pleroma/admin/users", %{            "nickname" => "lain",            "email" => "lain@example.org",            "password" => "test" @@ -75,7 +75,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end    end -  describe "/api/pleroma/admin/user/follow" do +  describe "/api/pleroma/admin/users/follow" do      test "allows to force-follow another user" do        admin = insert(:user, info: %{is_admin: true})        user = insert(:user) @@ -84,7 +84,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        build_conn()        |> assign(:user, admin)        |> put_req_header("accept", "application/json") -      |> post("/api/pleroma/admin/user/follow", %{ +      |> post("/api/pleroma/admin/users/follow", %{          "follower" => follower.nickname,          "followed" => user.nickname        }) @@ -96,7 +96,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end    end -  describe "/api/pleroma/admin/user/unfollow" do +  describe "/api/pleroma/admin/users/unfollow" do      test "allows to force-unfollow another user" do        admin = insert(:user, info: %{is_admin: true})        user = insert(:user) @@ -107,7 +107,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        build_conn()        |> assign(:user, admin)        |> put_req_header("accept", "application/json") -      |> post("/api/pleroma/admin/user/unfollow", %{ +      |> post("/api/pleroma/admin/users/unfollow", %{          "follower" => follower.nickname,          "followed" => user.nickname        }) @@ -191,7 +191,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end    end -  describe "/api/pleroma/admin/permission_group" do +  describe "/api/pleroma/admin/users/:nickname/permission_group" do      test "GET is giving user_info" do        admin = insert(:user, info: %{is_admin: true}) @@ -199,7 +199,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          build_conn()          |> assign(:user, admin)          |> put_req_header("accept", "application/json") -        |> get("/api/pleroma/admin/permission_group/#{admin.nickname}") +        |> get("/api/pleroma/admin/users/#{admin.nickname}/permission_group/")        assert json_response(conn, 200) == %{                 "is_admin" => true, @@ -215,7 +215,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          build_conn()          |> assign(:user, admin)          |> put_req_header("accept", "application/json") -        |> post("/api/pleroma/admin/permission_group/#{user.nickname}/admin") +        |> post("/api/pleroma/admin/users/#{user.nickname}/permission_group/admin")        assert json_response(conn, 200) == %{                 "is_admin" => true @@ -230,7 +230,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          build_conn()          |> assign(:user, admin)          |> put_req_header("accept", "application/json") -        |> delete("/api/pleroma/admin/permission_group/#{user.nickname}/admin") +        |> delete("/api/pleroma/admin/users/#{user.nickname}/permission_group/admin")        assert json_response(conn, 200) == %{                 "is_admin" => false @@ -238,7 +238,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end    end -  describe "PUT /api/pleroma/admin/activation_status" do +  describe "PUT /api/pleroma/admin/users/:nickname/activation_status" do      setup %{conn: conn} do        admin = insert(:user, info: %{is_admin: true}) @@ -255,7 +255,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        conn =          conn -        |> put("/api/pleroma/admin/activation_status/#{user.nickname}", %{status: false}) +        |> put("/api/pleroma/admin/users/#{user.nickname}/activation_status", %{status: false})        user = User.get_cached_by_id(user.id)        assert user.info.deactivated == true @@ -267,7 +267,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        conn =          conn -        |> put("/api/pleroma/admin/activation_status/#{user.nickname}", %{status: true}) +        |> put("/api/pleroma/admin/users/#{user.nickname}/activation_status", %{status: true})        user = User.get_cached_by_id(user.id)        assert user.info.deactivated == false @@ -280,7 +280,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        conn =          conn          |> assign(:user, user) -        |> put("/api/pleroma/admin/activation_status/#{user.nickname}", %{status: false}) +        |> put("/api/pleroma/admin/users/#{user.nickname}/activation_status", %{status: false})        assert json_response(conn, :forbidden)      end @@ -309,7 +309,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        conn =          conn          |> assign(:user, user) -        |> post("/api/pleroma/admin/email_invite?email=#{recipient_email}&name=#{recipient_name}") +        |> post( +          "/api/pleroma/admin/users/email_invite?email=#{recipient_email}&name=#{recipient_name}" +        )        assert json_response(conn, :no_content) @@ -341,13 +343,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        conn =          conn          |> assign(:user, non_admin_user) -        |> post("/api/pleroma/admin/email_invite?email=foo@bar.com&name=JD") +        |> post("/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD")        assert json_response(conn, :forbidden)      end    end -  describe "POST /api/pleroma/admin/email_invite, with invalid config" do +  describe "POST /api/pleroma/admin/users/email_invite, with invalid config" do      setup do        [user: insert(:user, info: %{is_admin: true})]      end @@ -367,7 +369,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        conn =          conn          |> assign(:user, user) -        |> post("/api/pleroma/admin/email_invite?email=foo@bar.com&name=JD") +        |> post("/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD")        assert json_response(conn, :internal_server_error)      end @@ -387,7 +389,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        conn =          conn          |> assign(:user, user) -        |> post("/api/pleroma/admin/email_invite?email=foo@bar.com&name=JD") +        |> post("/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD")        assert json_response(conn, :internal_server_error)      end @@ -405,7 +407,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      assert conn.status == 200    end -  test "/api/pleroma/admin/password_reset" do +  test "/api/pleroma/admin/users/:nickname/password_reset" do      admin = insert(:user, info: %{is_admin: true})      user = insert(:user) @@ -413,20 +415,25 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        build_conn()        |> assign(:user, admin)        |> put_req_header("accept", "application/json") -      |> get("/api/pleroma/admin/password_reset?nickname=#{user.nickname}") +      |> get("/api/pleroma/admin/users/#{user.nickname}/password_reset")      assert conn.status == 200    end    describe "GET /api/pleroma/admin/users" do -    test "renders users array for the first page" do +    setup do        admin = insert(:user, info: %{is_admin: true}) -      user = insert(:user, local: false, tags: ["foo", "bar"])        conn =          build_conn()          |> assign(:user, admin) -        |> get("/api/pleroma/admin/users?page=1") + +      {:ok, conn: conn, admin: admin} +    end + +    test "renders users array for the first page", %{conn: conn, admin: admin} do +      user = insert(:user, local: false, tags: ["foo", "bar"]) +      conn = get(conn, "/api/pleroma/admin/users?page=1")        assert json_response(conn, 200) == %{                 "count" => 2, @@ -452,14 +459,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do               }      end -    test "renders empty array for the second page" do -      admin = insert(:user, info: %{is_admin: true}) +    test "renders empty array for the second page", %{conn: conn} do        insert(:user) -      conn = -        build_conn() -        |> assign(:user, admin) -        |> get("/api/pleroma/admin/users?page=2") +      conn = get(conn, "/api/pleroma/admin/users?page=2")        assert json_response(conn, 200) == %{                 "count" => 2, @@ -468,14 +471,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do               }      end -    test "regular search" do -      admin = insert(:user, info: %{is_admin: true}) +    test "regular search", %{conn: conn} do        user = insert(:user, nickname: "bob") -      conn = -        build_conn() -        |> assign(:user, admin) -        |> get("/api/pleroma/admin/users?query=bo") +      conn = get(conn, "/api/pleroma/admin/users?query=bo")        assert json_response(conn, 200) == %{                 "count" => 1, @@ -493,17 +492,101 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do               }      end -    test "regular search with page size" do -      admin = insert(:user, info: %{is_admin: true}) +    test "search by domain", %{conn: conn} do +      user = insert(:user, nickname: "nickname@domain.com") +      insert(:user) + +      conn = get(conn, "/api/pleroma/admin/users?query=domain.com") + +      assert json_response(conn, 200) == %{ +               "count" => 1, +               "page_size" => 50, +               "users" => [ +                 %{ +                   "deactivated" => user.info.deactivated, +                   "id" => user.id, +                   "nickname" => user.nickname, +                   "roles" => %{"admin" => false, "moderator" => false}, +                   "local" => true, +                   "tags" => [] +                 } +               ] +             } +    end + +    test "search by full nickname", %{conn: conn} do +      user = insert(:user, nickname: "nickname@domain.com") +      insert(:user) + +      conn = get(conn, "/api/pleroma/admin/users?query=nickname@domain.com") + +      assert json_response(conn, 200) == %{ +               "count" => 1, +               "page_size" => 50, +               "users" => [ +                 %{ +                   "deactivated" => user.info.deactivated, +                   "id" => user.id, +                   "nickname" => user.nickname, +                   "roles" => %{"admin" => false, "moderator" => false}, +                   "local" => true, +                   "tags" => [] +                 } +               ] +             } +    end + +    test "search by display name", %{conn: conn} do +      user = insert(:user, name: "Display name") +      insert(:user) + +      conn = get(conn, "/api/pleroma/admin/users?name=display") + +      assert json_response(conn, 200) == %{ +               "count" => 1, +               "page_size" => 50, +               "users" => [ +                 %{ +                   "deactivated" => user.info.deactivated, +                   "id" => user.id, +                   "nickname" => user.nickname, +                   "roles" => %{"admin" => false, "moderator" => false}, +                   "local" => true, +                   "tags" => [] +                 } +               ] +             } +    end + +    test "search by email", %{conn: conn} do +      user = insert(:user, email: "email@example.com") +      insert(:user) + +      conn = get(conn, "/api/pleroma/admin/users?email=email@example.com") + +      assert json_response(conn, 200) == %{ +               "count" => 1, +               "page_size" => 50, +               "users" => [ +                 %{ +                   "deactivated" => user.info.deactivated, +                   "id" => user.id, +                   "nickname" => user.nickname, +                   "roles" => %{"admin" => false, "moderator" => false}, +                   "local" => true, +                   "tags" => [] +                 } +               ] +             } +    end + +    test "regular search with page size", %{conn: conn} do        user = insert(:user, nickname: "aalice")        user2 = insert(:user, nickname: "alice") -      conn = -        build_conn() -        |> assign(:user, admin) -        |> get("/api/pleroma/admin/users?query=a&page_size=1&page=1") +      conn1 = get(conn, "/api/pleroma/admin/users?query=a&page_size=1&page=1") -      assert json_response(conn, 200) == %{ +      assert json_response(conn1, 200) == %{                 "count" => 2,                 "page_size" => 1,                 "users" => [ @@ -518,12 +601,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                 ]               } -      conn = -        build_conn() -        |> assign(:user, admin) -        |> get("/api/pleroma/admin/users?query=a&page_size=1&page=2") +      conn2 = get(conn, "/api/pleroma/admin/users?query=a&page_size=1&page=2") -      assert json_response(conn, 200) == %{ +      assert json_response(conn2, 200) == %{                 "count" => 2,                 "page_size" => 1,                 "users" => [ @@ -566,7 +646,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do               }      end -    test "only local users with no query" do +    test "only local users with no query", %{admin: old_admin} do        admin = insert(:user, info: %{is_admin: true}, nickname: "john")        user = insert(:user, nickname: "bob") @@ -578,7 +658,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          |> get("/api/pleroma/admin/users?filters=local")        assert json_response(conn, 200) == %{ -               "count" => 2, +               "count" => 3,                 "page_size" => 50,                 "users" => [                   %{ @@ -596,6 +676,100 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "roles" => %{"admin" => true, "moderator" => false},                     "local" => true,                     "tags" => [] +                 }, +                 %{ +                   "deactivated" => false, +                   "id" => old_admin.id, +                   "local" => true, +                   "nickname" => old_admin.nickname, +                   "roles" => %{"admin" => true, "moderator" => false}, +                   "tags" => [] +                 } +               ] +             } +    end + +    test "load only admins", %{conn: conn, admin: admin} do +      second_admin = insert(:user, info: %{is_admin: true}) +      insert(:user) +      insert(:user) + +      conn = get(conn, "/api/pleroma/admin/users?filters=is_admin") + +      assert json_response(conn, 200) == %{ +               "count" => 2, +               "page_size" => 50, +               "users" => [ +                 %{ +                   "deactivated" => false, +                   "id" => admin.id, +                   "nickname" => admin.nickname, +                   "roles" => %{"admin" => true, "moderator" => false}, +                   "local" => admin.local, +                   "tags" => [] +                 }, +                 %{ +                   "deactivated" => false, +                   "id" => second_admin.id, +                   "nickname" => second_admin.nickname, +                   "roles" => %{"admin" => true, "moderator" => false}, +                   "local" => second_admin.local, +                   "tags" => [] +                 } +               ] +             } +    end + +    test "load only moderators", %{conn: conn} do +      moderator = insert(:user, info: %{is_moderator: true}) +      insert(:user) +      insert(:user) + +      conn = get(conn, "/api/pleroma/admin/users?filters=is_moderator") + +      assert json_response(conn, 200) == %{ +               "count" => 1, +               "page_size" => 50, +               "users" => [ +                 %{ +                   "deactivated" => false, +                   "id" => moderator.id, +                   "nickname" => moderator.nickname, +                   "roles" => %{"admin" => false, "moderator" => true}, +                   "local" => moderator.local, +                   "tags" => [] +                 } +               ] +             } +    end + +    test "load users with tags list", %{conn: conn} do +      user1 = insert(:user, tags: ["first"]) +      user2 = insert(:user, tags: ["second"]) +      insert(:user) +      insert(:user) + +      conn = get(conn, "/api/pleroma/admin/users?tags[]=first&tags[]=second") + +      assert json_response(conn, 200) == %{ +               "count" => 2, +               "page_size" => 50, +               "users" => [ +                 %{ +                   "deactivated" => false, +                   "id" => user1.id, +                   "nickname" => user1.nickname, +                   "roles" => %{"admin" => false, "moderator" => false}, +                   "local" => user1.local, +                   "tags" => ["first"] +                 }, +                 %{ +                   "deactivated" => false, +                   "id" => user2.id, +                   "nickname" => user2.nickname, +                   "roles" => %{"admin" => false, "moderator" => false}, +                   "local" => user2.local, +                   "tags" => ["second"]                   }                 ]               } @@ -650,14 +824,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do               }    end -  describe "GET /api/pleroma/admin/invite_token" do -    test "without options" do +  describe "GET /api/pleroma/admin/users/invite_token" do +    setup do        admin = insert(:user, info: %{is_admin: true})        conn =          build_conn()          |> assign(:user, admin) -        |> get("/api/pleroma/admin/invite_token") + +      {:ok, conn: conn} +    end + +    test "without options", %{conn: conn} do +      conn = get(conn, "/api/pleroma/admin/users/invite_token")        token = json_response(conn, 200)        invite = UserInviteToken.find_by_token!(token) @@ -667,13 +846,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert invite.invite_type == "one_time"      end -    test "with expires_at" do -      admin = insert(:user, info: %{is_admin: true}) - +    test "with expires_at", %{conn: conn} do        conn = -        build_conn() -        |> assign(:user, admin) -        |> get("/api/pleroma/admin/invite_token", %{ +        get(conn, "/api/pleroma/admin/users/invite_token", %{            "invite" => %{"expires_at" => Date.to_string(Date.utc_today())}          }) @@ -686,13 +861,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert invite.invite_type == "date_limited"      end -    test "with max_use" do -      admin = insert(:user, info: %{is_admin: true}) - +    test "with max_use", %{conn: conn} do        conn = -        build_conn() -        |> assign(:user, admin) -        |> get("/api/pleroma/admin/invite_token", %{ +        get(conn, "/api/pleroma/admin/users/invite_token", %{            "invite" => %{"max_use" => 150}          }) @@ -704,13 +875,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert invite.invite_type == "reusable"      end -    test "with max use and expires_at" do -      admin = insert(:user, info: %{is_admin: true}) - +    test "with max use and expires_at", %{conn: conn} do        conn = -        build_conn() -        |> assign(:user, admin) -        |> get("/api/pleroma/admin/invite_token", %{ +        get(conn, "/api/pleroma/admin/users/invite_token", %{            "invite" => %{"max_use" => 150, "expires_at" => Date.to_string(Date.utc_today())}          }) @@ -723,26 +890,27 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end    end -  describe "GET /api/pleroma/admin/invites" do -    test "no invites" do +  describe "GET /api/pleroma/admin/users/invites" do +    setup do        admin = insert(:user, info: %{is_admin: true})        conn =          build_conn()          |> assign(:user, admin) -        |> get("/api/pleroma/admin/invites") + +      {:ok, conn: conn} +    end + +    test "no invites", %{conn: conn} do +      conn = get(conn, "/api/pleroma/admin/users/invites")        assert json_response(conn, 200) == %{"invites" => []}      end -    test "with invite" do -      admin = insert(:user, info: %{is_admin: true}) +    test "with invite", %{conn: conn} do        {:ok, invite} = UserInviteToken.create_invite() -      conn = -        build_conn() -        |> assign(:user, admin) -        |> get("/api/pleroma/admin/invites") +      conn = get(conn, "/api/pleroma/admin/users/invites")        assert json_response(conn, 200) == %{                 "invites" => [ @@ -760,7 +928,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end    end -  describe "POST /api/pleroma/admin/revoke_invite" do +  describe "POST /api/pleroma/admin/users/revoke_invite" do      test "with token" do        admin = insert(:user, info: %{is_admin: true})        {:ok, invite} = UserInviteToken.create_invite() @@ -768,7 +936,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        conn =          build_conn()          |> assign(:user, admin) -        |> post("/api/pleroma/admin/revoke_invite", %{"token" => invite.token}) +        |> post("/api/pleroma/admin/users/revoke_invite", %{"token" => invite.token})        assert json_response(conn, 200) == %{                 "expires_at" => nil, diff --git a/test/web/admin_api/search_test.exs b/test/web/admin_api/search_test.exs index 3950996ed..501a8d007 100644 --- a/test/web/admin_api/search_test.exs +++ b/test/web/admin_api/search_test.exs @@ -70,11 +70,11 @@ defmodule Pleroma.Web.AdminAPI.SearchTest do      test "it returns specific user" do        insert(:user)        insert(:user) -      insert(:user, nickname: "bob", local: true, info: %{deactivated: false}) +      user = insert(:user, nickname: "bob", local: true, info: %{deactivated: false})        {:ok, _results, total_count} = Search.user(%{query: ""}) -      {:ok, _results, count} = +      {:ok, [^user], count} =          Search.user(%{            query: "Bo",            active: true, @@ -84,5 +84,87 @@ defmodule Pleroma.Web.AdminAPI.SearchTest do        assert total_count == 3        assert count == 1      end + +    test "it returns user by domain" do +      insert(:user) +      insert(:user) +      user = insert(:user, nickname: "some@domain.com") + +      {:ok, _results, total} = Search.user() +      {:ok, [^user], count} = Search.user(%{query: "domain.com"}) +      assert total == 3 +      assert count == 1 +    end + +    test "it return user by full nickname" do +      insert(:user) +      insert(:user) +      user = insert(:user, nickname: "some@domain.com") + +      {:ok, _results, total} = Search.user() +      {:ok, [^user], count} = Search.user(%{query: "some@domain.com"}) +      assert total == 3 +      assert count == 1 +    end + +    test "it returns admin user" do +      admin = insert(:user, info: %{is_admin: true}) +      insert(:user) +      insert(:user) + +      {:ok, _results, total} = Search.user() +      {:ok, [^admin], count} = Search.user(%{is_admin: true}) +      assert total == 3 +      assert count == 1 +    end + +    test "it returns moderator user" do +      moderator = insert(:user, info: %{is_moderator: true}) +      insert(:user) +      insert(:user) + +      {:ok, _results, total} = Search.user() +      {:ok, [^moderator], count} = Search.user(%{is_moderator: true}) +      assert total == 3 +      assert count == 1 +    end + +    test "it returns users with tags" do +      user1 = insert(:user, tags: ["first"]) +      user2 = insert(:user, tags: ["second"]) +      insert(:user) +      insert(:user) + +      {:ok, _results, total} = Search.user() +      {:ok, users, count} = Search.user(%{tags: ["first", "second"]}) +      assert total == 4 +      assert count == 2 +      assert user1 in users +      assert user2 in users +    end + +    test "it returns user by display name" do +      user = insert(:user, name: "Display name") +      insert(:user) +      insert(:user) + +      {:ok, _results, total} = Search.user() +      {:ok, [^user], count} = Search.user(%{name: "display"}) + +      assert total == 3 +      assert count == 1 +    end + +    test "it returns user by email" do +      user = insert(:user, email: "some@example.com") +      insert(:user) +      insert(:user) + +      {:ok, _results, total} = Search.user() +      {:ok, [^user], count} = Search.user(%{email: "some@example.com"}) + +      assert total == 3 +      assert count == 1 +    end    end  end diff --git a/test/web/auth/authenticator_test.exs b/test/web/auth/authenticator_test.exs new file mode 100644 index 000000000..fea5c8209 --- /dev/null +++ b/test/web/auth/authenticator_test.exs @@ -0,0 +1,42 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.Auth.AuthenticatorTest do +  use Pleroma.Web.ConnCase + +  alias Pleroma.Web.Auth.Authenticator +  import Pleroma.Factory + +  describe "fetch_user/1" do +    test "returns user by name" do +      user = insert(:user) +      assert Authenticator.fetch_user(user.nickname) == user +    end + +    test "returns user by email" do +      user = insert(:user) +      assert Authenticator.fetch_user(user.email) == user +    end + +    test "returns nil" do +      assert Authenticator.fetch_user("email") == nil +    end +  end + +  describe "fetch_credentials/1" do +    test "returns name and password from authorization params" do +      params = %{"authorization" => %{"name" => "test", "password" => "test-pass"}} +      assert Authenticator.fetch_credentials(params) == {:ok, {"test", "test-pass"}} +    end + +    test "returns name and password with grant_type 'password'" do +      params = %{"grant_type" => "password", "username" => "test", "password" => "test-pass"} +      assert Authenticator.fetch_credentials(params) == {:ok, {"test", "test-pass"}} +    end + +    test "returns error" do +      assert Authenticator.fetch_credentials(%{}) == {:error, :invalid_credentials} +    end +  end +end diff --git a/test/web/common_api/common_api_utils_test.exs b/test/web/common_api/common_api_utils_test.exs index 837a66063..ab4c62b35 100644 --- a/test/web/common_api/common_api_utils_test.exs +++ b/test/web/common_api/common_api_utils_test.exs @@ -119,6 +119,31 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do        assert output == expected      end +    test "works for bare text/bbcode" do +      text = "[b]hello world[/b]" +      expected = "<strong>hello world</strong>" + +      {output, [], []} = Utils.format_input(text, "text/bbcode") + +      assert output == expected + +      text = "[b]hello world![/b]\n\nsecond paragraph!" +      expected = "<strong>hello world!</strong><br>\n<br>\nsecond paragraph!" + +      {output, [], []} = Utils.format_input(text, "text/bbcode") + +      assert output == expected + +      text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>" + +      expected = +        "<strong>hello world!</strong><br>\n<br>\n<strong>second paragraph!</strong>" + +      {output, [], []} = Utils.format_input(text, "text/bbcode") + +      assert output == expected +    end +      test "works for text/markdown with mentions" do        {:ok, user} =          UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"}) diff --git a/test/web/federator_test.exs b/test/web/federator_test.exs index 52729eb50..0f43bc8f2 100644 --- a/test/web/federator_test.exs +++ b/test/web/federator_test.exs @@ -58,7 +58,7 @@ defmodule Pleroma.Web.FederatorTest do    describe "Targets reachability filtering in `publish`" do      test_with_mock "it federates only to reachable instances via AP", -                   Federator, +                   Pleroma.Web.ActivityPub.Publisher,                     [:passthrough],                     [] do        user = insert(:user) @@ -88,13 +88,18 @@ defmodule Pleroma.Web.FederatorTest do        {:ok, _activity} =          CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"}) -      assert called(Federator.publish_single_ap(%{inbox: inbox1, unreachable_since: dt})) +      assert called( +               Pleroma.Web.ActivityPub.Publisher.publish_one(%{ +                 inbox: inbox1, +                 unreachable_since: dt +               }) +             ) -      refute called(Federator.publish_single_ap(%{inbox: inbox2})) +      refute called(Pleroma.Web.ActivityPub.Publisher.publish_one(%{inbox: inbox2}))      end      test_with_mock "it federates only to reachable instances via Websub", -                   Federator, +                   Pleroma.Web.Websub,                     [:passthrough],                     [] do        user = insert(:user) @@ -122,17 +127,17 @@ defmodule Pleroma.Web.FederatorTest do        {:ok, _activity} = CommonAPI.post(user, %{"status" => "HI"})        assert called( -               Federator.publish_single_websub(%{ +               Pleroma.Web.Websub.publish_one(%{                   callback: sub2.callback,                   unreachable_since: dt                 })               ) -      refute called(Federator.publish_single_websub(%{callback: sub1.callback})) +      refute called(Pleroma.Web.Websub.publish_one(%{callback: sub1.callback}))      end      test_with_mock "it federates only to reachable instances via Salmon", -                   Federator, +                   Pleroma.Web.Salmon,                     [:passthrough],                     [] do        user = insert(:user) @@ -162,13 +167,13 @@ defmodule Pleroma.Web.FederatorTest do          CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})        assert called( -               Federator.publish_single_salmon(%{ +               Pleroma.Web.Salmon.publish_one(%{                   recipient: remote_user2,                   unreachable_since: dt                 })               ) -      refute called(Federator.publish_single_websub(%{recipient: remote_user1})) +      refute called(Pleroma.Web.Salmon.publish_one(%{recipient: remote_user1}))      end    end diff --git a/test/web/mastodon_api/account_view_test.exs b/test/web/mastodon_api/account_view_test.exs index 0730201bd..a24f2a050 100644 --- a/test/web/mastodon_api/account_view_test.exs +++ b/test/web/mastodon_api/account_view_test.exs @@ -56,14 +56,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        bot: false,        source: %{          note: "", -        privacy: "public", -        sensitive: false +        sensitive: false, +        pleroma: %{}        },        pleroma: %{          confirmation_pending: false,          tags: [],          is_admin: false,          is_moderator: false, +        hide_favorites: true, +        hide_followers: false, +        hide_follows: false,          relationship: %{}        }      } @@ -81,8 +84,12 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        "follows" => true      } -    assert %{pleroma: %{notification_settings: ^notification_settings}} = -             AccountView.render("account.json", %{user: user, for: user}) +    privacy = user.info.default_scope + +    assert %{ +             pleroma: %{notification_settings: ^notification_settings}, +             source: %{privacy: ^privacy} +           } = AccountView.render("account.json", %{user: user, for: user})    end    test "Represent a Service(bot) account" do @@ -114,14 +121,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        bot: true,        source: %{          note: "", -        privacy: "public", -        sensitive: false +        sensitive: false, +        pleroma: %{}        },        pleroma: %{          confirmation_pending: false,          tags: [],          is_admin: false,          is_moderator: false, +        hide_favorites: true, +        hide_followers: false, +        hide_follows: false,          relationship: %{}        }      } @@ -200,14 +210,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        bot: true,        source: %{          note: "", -        privacy: "public", -        sensitive: false +        sensitive: false, +        pleroma: %{}        },        pleroma: %{          confirmation_pending: false,          tags: [],          is_admin: false,          is_moderator: false, +        hide_favorites: true, +        hide_followers: false, +        hide_follows: false,          relationship: %{            id: to_string(user.id),            following: false, diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index a22944088..5c79ee633 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -16,6 +16,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do    alias Pleroma.Web.CommonAPI    alias Pleroma.Web.MastodonAPI.FilterView    alias Pleroma.Web.OAuth.App +  alias Pleroma.Web.OAuth.Token    alias Pleroma.Web.OStatus    alias Pleroma.Web.Push    alias Pleroma.Web.TwitterAPI.TwitterAPI @@ -300,6 +301,65 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      assert status["url"] != direct.data["id"]    end +  test "Conversations", %{conn: conn} do +    user_one = insert(:user) +    user_two = insert(:user) + +    {:ok, user_two} = User.follow(user_two, user_one) + +    {:ok, direct} = +      CommonAPI.post(user_one, %{ +        "status" => "Hi @#{user_two.nickname}!", +        "visibility" => "direct" +      }) + +    {:ok, _follower_only} = +      CommonAPI.post(user_one, %{ +        "status" => "Hi @#{user_two.nickname}!", +        "visibility" => "private" +      }) + +    res_conn = +      conn +      |> assign(:user, user_one) +      |> get("/api/v1/conversations") + +    assert response = json_response(res_conn, 200) + +    assert [ +             %{ +               "id" => res_id, +               "accounts" => res_accounts, +               "last_status" => res_last_status, +               "unread" => unread +             } +           ] = response + +    assert length(res_accounts) == 2 +    assert is_binary(res_id) +    assert unread == true +    assert res_last_status["id"] == direct.id + +    # Apparently undocumented API endpoint +    res_conn = +      conn +      |> assign(:user, user_one) +      |> post("/api/v1/conversations/#{res_id}/read") + +    assert response = json_response(res_conn, 200) +    assert length(response["accounts"]) == 2 +    assert response["last_status"]["id"] == direct.id +    assert response["unread"] == false + +    # (vanilla) Mastodon frontend behaviour +    res_conn = +      conn +      |> assign(:user, user_one) +      |> get("/api/v1/statuses/#{res_last_status["id"]}/context") + +    assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200) +  end +    test "doesn't include DMs from blocked users", %{conn: conn} do      blocker = insert(:user)      blocked = insert(:user) @@ -513,6 +573,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do        assert response = json_response(conn, 200)        assert response["phrase"] == filter.phrase        assert response["context"] == filter.context +      assert response["irreversible"] == false        assert response["id"] != nil        assert response["id"] != ""      end @@ -1022,7 +1083,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do        user2 = insert(:user)        user3 = insert(:user)        CommonAPI.favorite(activity.id, user2) -      {:ok, user2} = User.bookmark(user2, activity.data["object"]["id"]) +      {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)        {:ok, reblog_activity1, _object} = CommonAPI.repeat(activity.id, user1)        {:ok, _, _object} = CommonAPI.repeat(activity.id, user2) @@ -2214,6 +2275,78 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do        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 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) @@ -2279,6 +2412,33 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do          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 @@ -3057,4 +3217,129 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do      replied_to_user = User.get_by_ap_id(replied_to.data["actor"])      assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id    end + +  describe "create account by app" do +    setup do +      enabled = Pleroma.Config.get([:app_account_creation, :enabled]) +      max_requests = Pleroma.Config.get([:app_account_creation, :max_requests]) +      interval = Pleroma.Config.get([:app_account_creation, :interval]) + +      Pleroma.Config.put([:app_account_creation, :enabled], true) +      Pleroma.Config.put([:app_account_creation, :max_requests], 5) +      Pleroma.Config.put([:app_account_creation, :interval], 1) + +      on_exit(fn -> +        Pleroma.Config.put([:app_account_creation, :enabled], enabled) +        Pleroma.Config.put([:app_account_creation, :max_requests], max_requests) +        Pleroma.Config.put([:app_account_creation, :interval], interval) +      end) + +      :ok +    end + +    test "Account registration via Application", %{conn: conn} do +      conn = +        conn +        |> post("/api/v1/apps", %{ +          client_name: "client_name", +          redirect_uris: "urn:ietf:wg:oauth:2.0:oob", +          scopes: "read, write, follow" +        }) + +      %{ +        "client_id" => client_id, +        "client_secret" => client_secret, +        "id" => _, +        "name" => "client_name", +        "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob", +        "vapid_key" => _, +        "website" => nil +      } = json_response(conn, 200) + +      conn = +        conn +        |> post("/oauth/token", %{ +          grant_type: "client_credentials", +          client_id: client_id, +          client_secret: client_secret +        }) + +      assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} = +               json_response(conn, 200) + +      assert token +      token_from_db = Repo.get_by(Token, token: token) +      assert token_from_db +      assert refresh +      assert scope == "read write follow" + +      conn = +        build_conn() +        |> put_req_header("authorization", "Bearer " <> token) +        |> post("/api/v1/accounts", %{ +          username: "lain", +          email: "lain@example.org", +          password: "PlzDontHackLain", +          agreement: true +        }) + +      %{ +        "access_token" => token, +        "created_at" => _created_at, +        "scope" => _scope, +        "token_type" => "Bearer" +      } = json_response(conn, 200) + +      token_from_db = Repo.get_by(Token, token: token) +      assert token_from_db +      token_from_db = Repo.preload(token_from_db, :user) +      assert token_from_db.user + +      assert token_from_db.user.info.confirmation_pending +    end + +    test "rate limit", %{conn: conn} do +      app_token = insert(:oauth_token, user: nil) + +      conn = +        put_req_header(conn, "authorization", "Bearer " <> app_token.token) +        |> Map.put(:remote_ip, {15, 15, 15, 15}) + +      for i <- 1..5 do +        conn = +          conn +          |> post("/api/v1/accounts", %{ +            username: "#{i}lain", +            email: "#{i}lain@example.org", +            password: "PlzDontHackLain", +            agreement: true +          }) + +        %{ +          "access_token" => token, +          "created_at" => _created_at, +          "scope" => _scope, +          "token_type" => "Bearer" +        } = json_response(conn, 200) + +        token_from_db = Repo.get_by(Token, token: token) +        assert token_from_db +        token_from_db = Repo.preload(token_from_db, :user) +        assert token_from_db.user + +        assert token_from_db.user.info.confirmation_pending +      end + +      conn = +        conn +        |> post("/api/v1/accounts", %{ +          username: "6lain", +          email: "6lain@example.org", +          password: "PlzDontHackLain", +          agreement: true +        }) + +      assert json_response(conn, 403) == %{"error" => "Rate limit exceeded."} +    end +  end  end diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index f74726212..d7c800e83 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -6,6 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do    use Pleroma.DataCase    alias Pleroma.Activity +  alias Pleroma.Bookmark    alias Pleroma.Object    alias Pleroma.Repo    alias Pleroma.User @@ -153,6 +154,27 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do      assert status.muted == true    end +  test "tells if the status is bookmarked" do +    user = insert(:user) + +    {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"}) +    status = StatusView.render("status.json", %{activity: activity}) + +    assert status.bookmarked == false + +    status = StatusView.render("status.json", %{activity: activity, for: user}) + +    assert status.bookmarked == false + +    {:ok, _bookmark} = Bookmark.create(user.id, activity.id) + +    activity = Activity.get_by_id_with_object(activity.id) + +    status = StatusView.render("status.json", %{activity: activity, for: user}) + +    assert status.bookmarked == true +  end +    test "a reply" do      note = insert(:note_activity)      user = insert(:user) diff --git a/test/web/oauth/oauth_controller_test.exs b/test/web/oauth/oauth_controller_test.exs index 6e96537ec..1c04ac9ad 100644 --- a/test/web/oauth/oauth_controller_test.exs +++ b/test/web/oauth/oauth_controller_test.exs @@ -12,6 +12,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do    alias Pleroma.Web.OAuth.Authorization    alias Pleroma.Web.OAuth.Token +  @oauth_config_path [:oauth2, :issue_new_refresh_token]    @session_opts [      store: :cookie,      key: "_test", @@ -613,6 +614,27 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do        assert token.scopes == ["scope1", "scope2"]      end +    test "issue a token for client_credentials grant type" do +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      conn = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "client_credentials", +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) + +      assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} = +               json_response(conn, 200) + +      assert token +      token_from_db = Repo.get_by(Token, token: token) +      assert token_from_db +      assert refresh +      assert scope == "read write" +    end +      test "rejects token exchange with invalid client credentials" do        user = insert(:user)        app = insert(:oauth_app) @@ -643,7 +665,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do        password = "testpassword"        user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password)) -      info_change = Pleroma.User.Info.confirmation_changeset(user.info, :unconfirmed) +      info_change = Pleroma.User.Info.confirmation_changeset(user.info, need_confirmation: true)        {:ok, user} =          user @@ -714,4 +736,199 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do        refute Map.has_key?(resp, "access_token")      end    end + +  describe "POST /oauth/token - refresh token" do +    setup do +      oauth_token_config = Pleroma.Config.get(@oauth_config_path) + +      on_exit(fn -> +        Pleroma.Config.get(@oauth_config_path, oauth_token_config) +      end) +    end + +    test "issues a new access token with keep fresh token" do +      Pleroma.Config.put(@oauth_config_path, true) +      user = insert(:user) +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) +      {:ok, token} = Token.exchange_token(app, auth) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => token.refresh_token, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(200) + +      ap_id = user.ap_id + +      assert match?( +               %{ +                 "scope" => "write", +                 "token_type" => "Bearer", +                 "expires_in" => 600, +                 "access_token" => _, +                 "refresh_token" => _, +                 "me" => ^ap_id +               }, +               response +             ) + +      refute Repo.get_by(Token, token: token.token) +      new_token = Repo.get_by(Token, token: response["access_token"]) +      assert new_token.refresh_token == token.refresh_token +      assert new_token.scopes == auth.scopes +      assert new_token.user_id == user.id +      assert new_token.app_id == app.id +    end + +    test "issues a new access token with new fresh token" do +      Pleroma.Config.put(@oauth_config_path, false) +      user = insert(:user) +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) +      {:ok, token} = Token.exchange_token(app, auth) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => token.refresh_token, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(200) + +      ap_id = user.ap_id + +      assert match?( +               %{ +                 "scope" => "write", +                 "token_type" => "Bearer", +                 "expires_in" => 600, +                 "access_token" => _, +                 "refresh_token" => _, +                 "me" => ^ap_id +               }, +               response +             ) + +      refute Repo.get_by(Token, token: token.token) +      new_token = Repo.get_by(Token, token: response["access_token"]) +      refute new_token.refresh_token == token.refresh_token +      assert new_token.scopes == auth.scopes +      assert new_token.user_id == user.id +      assert new_token.app_id == app.id +    end + +    test "returns 400 if we try use access token" do +      user = insert(:user) +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) +      {:ok, token} = Token.exchange_token(app, auth) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => token.token, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(400) + +      assert %{"error" => "Invalid credentials"} == response +    end + +    test "returns 400 if refresh_token invalid" do +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => "token.refresh_token", +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(400) + +      assert %{"error" => "Invalid credentials"} == response +    end + +    test "issues a new token if token expired" do +      user = insert(:user) +      app = insert(:oauth_app, scopes: ["read", "write"]) + +      {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) +      {:ok, token} = Token.exchange_token(app, auth) + +      change = +        Ecto.Changeset.change( +          token, +          %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)} +        ) + +      {:ok, access_token} = Repo.update(change) + +      response = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "refresh_token", +          "refresh_token" => access_token.refresh_token, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) +        |> json_response(200) + +      ap_id = user.ap_id + +      assert match?( +               %{ +                 "scope" => "write", +                 "token_type" => "Bearer", +                 "expires_in" => 600, +                 "access_token" => _, +                 "refresh_token" => _, +                 "me" => ^ap_id +               }, +               response +             ) + +      refute Repo.get_by(Token, token: token.token) +      token = Repo.get_by(Token, token: response["access_token"]) +      assert token +      assert token.scopes == auth.scopes +      assert token.user_id == user.id +      assert token.app_id == app.id +    end +  end + +  describe "POST /oauth/token - bad request" do +    test "returns 500" do +      response = +        build_conn() +        |> post("/oauth/token", %{}) +        |> json_response(500) + +      assert %{"error" => "Bad request"} == response +    end +  end + +  describe "POST /oauth/revoke - bad request" do +    test "returns 500" do +      response = +        build_conn() +        |> post("/oauth/revoke", %{}) +        |> json_response(500) + +      assert %{"error" => "Bad request"} == response +    end +  end  end diff --git a/test/web/ostatus/activity_representer_test.exs b/test/web/ostatus/activity_representer_test.exs index a4bb68c4d..16ee02abb 100644 --- a/test/web/ostatus/activity_representer_test.exs +++ b/test/web/ostatus/activity_representer_test.exs @@ -67,37 +67,31 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do    end    test "a reply note" do -    note = insert(:note_activity) -    answer = insert(:note_activity) -    object = answer.data["object"] -    object = Map.put(object, "inReplyTo", note.data["object"]["id"]) - -    data = %{answer.data | "object" => object} -    answer = %{answer | data: data} - -    note_object = Object.get_by_ap_id(note.data["object"]["id"]) +    user = insert(:user) +    note_object = insert(:note) +    _note = insert(:note_activity, %{note: note_object}) +    object = insert(:note, %{data: %{"inReplyTo" => note_object.data["id"]}}) +    answer = insert(:note_activity, %{note: object})      Repo.update!(        Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})      ) -    user = User.get_cached_by_ap_id(answer.data["actor"]) -      expected = """      <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>      <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb> -    <id>#{answer.data["object"]["id"]}</id> +    <id>#{object.data["id"]}</id>      <title>New note by #{user.nickname}</title> -    <content type="html">#{answer.data["object"]["content"]}</content> -    <published>#{answer.data["object"]["published"]}</published> -    <updated>#{answer.data["object"]["published"]}</updated> +    <content type="html">#{object.data["content"]}</content> +    <published>#{object.data["published"]}</published> +    <updated>#{object.data["published"]}</updated>      <ostatus:conversation ref="#{answer.data["context"]}">#{answer.data["context"]}</ostatus:conversation>      <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />      <summary>2hu</summary> -    <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" /> -    <link type="text/html" href="#{answer.data["object"]["id"]}" rel="alternate" /> +    <link type="application/atom+xml" href="#{object.data["id"]}" rel="self" /> +    <link type="text/html" href="#{object.data["id"]}" rel="alternate" />      <category term="2hu"/> -    <thr:in-reply-to ref="#{note.data["object"]["id"]}" href="someurl" /> +    <thr:in-reply-to ref="#{note_object.data["id"]}" href="someurl" />      <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>      <link name="2hu" rel="emoji" href="corndog.png" />      """ diff --git a/test/web/push/impl_test.exs b/test/web/push/impl_test.exs index 49b2a9203..1e948086a 100644 --- a/test/web/push/impl_test.exs +++ b/test/web/push/impl_test.exs @@ -5,6 +5,8 @@  defmodule Pleroma.Web.Push.ImplTest do    use Pleroma.DataCase +  alias Pleroma.Object +  alias Pleroma.Web.CommonAPI    alias Pleroma.Web.Push.Impl    alias Pleroma.Web.Push.Subscription @@ -52,16 +54,12 @@ defmodule Pleroma.Web.Push.ImplTest do        data: %{alerts: %{"follow" => true, "mention" => false}}      ) +    {:ok, activity} = CommonAPI.post(user, %{"status" => "<Lorem ipsum dolor sit amet."}) +      notif =        insert(:notification,          user: user, -        activity: %Pleroma.Activity{ -          data: %{ -            "type" => "Create", -            "actor" => user.ap_id, -            "object" => %{"content" => "<Lorem ipsum dolor sit amet."} -          } -        } +        activity: activity        )      assert Impl.perform(notif) == [:ok, :ok] @@ -100,48 +98,65 @@ defmodule Pleroma.Web.Push.ImplTest do    end    test "renders body for create activity" do +    user = insert(:user, nickname: "Bob") + +    {:ok, activity} = +      CommonAPI.post(user, %{ +        "status" => +          "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis." +      }) + +    object = Object.normalize(activity) +      assert Impl.format_body(               %{ -               activity: %{ -                 data: %{ -                   "type" => "Create", -                   "object" => %{ -                     "content" => -                       "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis." -                   } -                 } -               } +               activity: activity               }, -             %{nickname: "Bob"} +             user, +             object             ) ==               "@Bob: Lorem ipsum dolor sit amet, consectetur  adipiscing elit. Fusce sagittis fini..."    end    test "renders body for follow activity" do -    assert Impl.format_body(%{activity: %{data: %{"type" => "Follow"}}}, %{nickname: "Bob"}) == +    user = insert(:user, nickname: "Bob") +    other_user = insert(:user) +    {:ok, _, _, activity} = CommonAPI.follow(user, other_user) +    object = Object.normalize(activity) + +    assert Impl.format_body(%{activity: activity}, user, object) ==               "@Bob has followed you"    end    test "renders body for announce activity" do      user = insert(:user) -    note = -      insert(:note, %{ -        data: %{ -          "content" => -            "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis." -        } +    {:ok, activity} = +      CommonAPI.post(user, %{ +        "status" => +          "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."        }) -    note_activity = insert(:note_activity, %{note: note}) -    announce_activity = insert(:announce_activity, %{user: user, note_activity: note_activity}) +    {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user) +    object = Object.normalize(activity) -    assert Impl.format_body(%{activity: announce_activity}, user) == +    assert Impl.format_body(%{activity: announce_activity}, user, object) ==               "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur  adipiscing elit. Fusce sagittis fini..."    end    test "renders body for like activity" do -    assert Impl.format_body(%{activity: %{data: %{"type" => "Like"}}}, %{nickname: "Bob"}) == +    user = insert(:user, nickname: "Bob") + +    {:ok, activity} = +      CommonAPI.post(user, %{ +        "status" => +          "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis." +      }) + +    {:ok, activity, _} = CommonAPI.favorite(activity.id, user) +    object = Object.normalize(activity) + +    assert Impl.format_body(%{activity: activity}, user, object) ==               "@Bob has favorited your post"    end  end diff --git a/test/web/salmon/salmon_test.exs b/test/web/salmon/salmon_test.exs index 7532578ca..232082779 100644 --- a/test/web/salmon/salmon_test.exs +++ b/test/web/salmon/salmon_test.exs @@ -7,7 +7,9 @@ defmodule Pleroma.Web.Salmon.SalmonTest do    alias Pleroma.Activity    alias Pleroma.Repo    alias Pleroma.User +  alias Pleroma.Web.Federator.Publisher    alias Pleroma.Web.Salmon +  import Mock    import Pleroma.Factory    @magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB" @@ -77,7 +79,10 @@ defmodule Pleroma.Web.Salmon.SalmonTest do               "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"    end -  test "it pushes an activity to remote accounts it's addressed to" do +  test_with_mock "it pushes an activity to remote accounts it's addressed to", +                 Publisher, +                 [:passthrough], +                 [] do      user_data = %{        info: %{          salmon: "http://test-example.org/salmon" @@ -102,10 +107,8 @@ defmodule Pleroma.Web.Salmon.SalmonTest do      user = User.get_cached_by_ap_id(activity.data["actor"])      {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user) -    poster = fn url, _data, _headers -> -      assert url == "http://test-example.org/salmon" -    end +    Salmon.publish(user, activity) -    Salmon.publish(user, activity, poster) +    assert called(Publisher.enqueue_one(Salmon, %{recipient: mentioned_user}))    end  end diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 43ad71a16..e194f14fb 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1094,7 +1094,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do    describe "GET /api/account/confirm_email/:id/:token" do      setup do        user = insert(:user) -      info_change = User.Info.confirmation_changeset(user.info, :unconfirmed) +      info_change = User.Info.confirmation_changeset(user.info, need_confirmation: true)        {:ok, user} =          user @@ -1145,7 +1145,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do        end        user = insert(:user) -      info_change = User.Info.confirmation_changeset(user.info, :unconfirmed) +      info_change = User.Info.confirmation_changeset(user.info, need_confirmation: true)        {:ok, user} =          user @@ -1611,6 +1611,34 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do        assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})      end + +    # Broken before the change to class="emoji" and non-<img/> in the DB +    @tag :skip +    test "it formats emojos", %{conn: conn} do +      user = insert(:user) + +      conn = +        conn +        |> assign(:user, user) +        |> post("/api/account/update_profile.json", %{ +          "bio" => "I love our :moominmamma:" +        }) + +      assert response = json_response(conn, 200) + +      assert %{ +               "description" => "I love our :moominmamma:", +               "description_html" => +                 ~s{I love our <img class="emoji" alt="moominmamma" title="moominmamma" src="} <> +                   _ +             } = response + +      conn = +        conn +        |> get("/api/users/show.json?user_id=#{user.nickname}") + +      assert response == json_response(conn, 200) +    end    end    defp valid_user(_context) do diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs index d84ab7420..43bd77f78 100644 --- a/test/web/twitter_api/views/activity_view_test.exs +++ b/test/web/twitter_api/views/activity_view_test.exs @@ -100,7 +100,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do      expected = ":firefox: meow"      expected_html = -      "<img height=\"32px\" width=\"32px\" alt=\"firefox\" title=\"firefox\" src=\"http://localhost:4001/emoji/Firefox.gif\" /> meow" +      "<img class=\"emoji\" alt=\"firefox\" title=\"firefox\" src=\"http://localhost:4001/emoji/Firefox.gif\" /> meow"      assert result["summary"] == expected      assert result["summary_html"] == expected_html @@ -295,8 +295,8 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do        "id" => announce.id,        "is_local" => true,        "is_post_verb" => false, -      "statusnet_html" => "shp retweeted a status.", -      "text" => "shp retweeted a status.", +      "statusnet_html" => "shp repeated a status.", +      "text" => "shp repeated a status.",        "uri" => "tag:#{announce.data["id"]}:objectType=note",        "user" => UserView.render("show.json", user: other_user),        "retweeted_status" => ActivityView.render("activity.json", activity: activity), @@ -371,4 +371,14 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do      assert length(result["attachments"]) == 1      assert result["summary"] == "Friday Night"    end + +  test "special characters are not escaped in text field for status created" do +    text = "<3 is on the way" + +    {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text}) + +    result = ActivityView.render("activity.json", activity: activity) + +    assert result["text"] == text +  end  end diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 36b461992..74526673c 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -32,7 +32,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do    test "A user with emoji in username" do      expected = -      "<img height=\"32px\" width=\"32px\" alt=\"karjalanpiirakka\" title=\"karjalanpiirakka\" src=\"/file.png\" /> man" +      "<img class=\"emoji\" alt=\"karjalanpiirakka\" title=\"karjalanpiirakka\" src=\"/file.png\" /> man"      user =        insert(:user, %{ @@ -89,29 +89,34 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do        "following" => false,        "follows_you" => false,        "statusnet_blocking" => false, -      "rights" => %{ -        "delete_others_notice" => false, -        "admin" => false -      },        "statusnet_profile_url" => user.ap_id,        "cover_photo" => banner,        "background_image" => nil,        "is_local" => true,        "locked" => false, -      "default_scope" => "public", -      "no_rich_text" => false,        "hide_follows" => false,        "hide_followers" => false,        "fields" => [],        "pleroma" => %{          "confirmation_pending" => false,          "tags" => [] -      } +      }, +      "rights" => %{"admin" => false, "delete_others_notice" => false}, +      "role" => "member"      }      assert represented == UserView.render("show.json", %{user: user})    end +  test "User exposes settings for themselves and only for themselves", %{user: user} do +    as_user = UserView.render("show.json", %{user: user, for: user}) +    assert as_user["default_scope"] == user.info.default_scope +    assert as_user["no_rich_text"] == user.info.no_rich_text +    as_stranger = UserView.render("show.json", %{user: user}) +    refute as_stranger["default_scope"] +    refute as_stranger["no_rich_text"] +  end +    test "A user for a given other follower", %{user: user} do      follower = insert(:user, %{following: [User.ap_followers(user)]})      {:ok, user} = User.update_follower_count(user) @@ -137,24 +142,20 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do        "following" => true,        "follows_you" => false,        "statusnet_blocking" => false, -      "rights" => %{ -        "delete_others_notice" => false, -        "admin" => false -      },        "statusnet_profile_url" => user.ap_id,        "cover_photo" => banner,        "background_image" => nil,        "is_local" => true,        "locked" => false, -      "default_scope" => "public", -      "no_rich_text" => false,        "hide_follows" => false,        "hide_followers" => false,        "fields" => [],        "pleroma" => %{          "confirmation_pending" => false,          "tags" => [] -      } +      }, +      "rights" => %{"admin" => false, "delete_others_notice" => false}, +      "role" => "member"      }      assert represented == UserView.render("show.json", %{user: user, for: follower}) @@ -186,24 +187,20 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do        "following" => false,        "follows_you" => true,        "statusnet_blocking" => false, -      "rights" => %{ -        "delete_others_notice" => false, -        "admin" => false -      },        "statusnet_profile_url" => follower.ap_id,        "cover_photo" => banner,        "background_image" => nil,        "is_local" => true,        "locked" => false, -      "default_scope" => "public", -      "no_rich_text" => false,        "hide_follows" => false,        "hide_followers" => false,        "fields" => [],        "pleroma" => %{          "confirmation_pending" => false,          "tags" => [] -      } +      }, +      "rights" => %{"admin" => false, "delete_others_notice" => false}, +      "role" => "member"      }      assert represented == UserView.render("show.json", %{user: follower, for: user}) @@ -272,24 +269,20 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do        "following" => false,        "follows_you" => false,        "statusnet_blocking" => true, -      "rights" => %{ -        "delete_others_notice" => false, -        "admin" => false -      },        "statusnet_profile_url" => user.ap_id,        "cover_photo" => banner,        "background_image" => nil,        "is_local" => true,        "locked" => false, -      "default_scope" => "public", -      "no_rich_text" => false,        "hide_follows" => false,        "hide_followers" => false,        "fields" => [],        "pleroma" => %{          "confirmation_pending" => false,          "tags" => [] -      } +      }, +      "rights" => %{"admin" => false, "delete_others_notice" => false}, +      "role" => "member"      }      blocker = User.get_cached_by_id(blocker.id) diff --git a/test/web/views/error_view_test.exs b/test/web/views/error_view_test.exs index d529fd2c3..3857d585f 100644 --- a/test/web/views/error_view_test.exs +++ b/test/web/views/error_view_test.exs @@ -4,6 +4,7 @@  defmodule Pleroma.Web.ErrorViewTest do    use Pleroma.Web.ConnCase, async: true +  import ExUnit.CaptureLog    # Bring render/3 and render_to_string/3 for testing custom views    import Phoenix.View @@ -13,17 +14,23 @@ defmodule Pleroma.Web.ErrorViewTest do    end    test "render 500.json" do -    assert render(Pleroma.Web.ErrorView, "500.json", []) == -             %{errors: %{detail: "Internal server error", reason: "nil"}} +    assert capture_log(fn -> +             assert render(Pleroma.Web.ErrorView, "500.json", []) == +                      %{errors: %{detail: "Internal server error", reason: "nil"}} +           end) =~ "[error] Internal server error: nil"    end    test "render any other" do -    assert render(Pleroma.Web.ErrorView, "505.json", []) == -             %{errors: %{detail: "Internal server error", reason: "nil"}} +    assert capture_log(fn -> +             assert render(Pleroma.Web.ErrorView, "505.json", []) == +                      %{errors: %{detail: "Internal server error", reason: "nil"}} +           end) =~ "[error] Internal server error: nil"    end    test "render 500.json with reason" do -    assert render(Pleroma.Web.ErrorView, "500.json", reason: "test reason") == -             %{errors: %{detail: "Internal server error", reason: "\"test reason\""}} +    assert capture_log(fn -> +             assert render(Pleroma.Web.ErrorView, "500.json", reason: "test reason") == +                      %{errors: %{detail: "Internal server error", reason: "\"test reason\""}} +           end) =~ "[error] Internal server error: \"test reason\""    end  end | 
