diff options
Diffstat (limited to 'test')
19 files changed, 537 insertions, 12 deletions
| diff --git a/test/pleroma/config/transfer_task_test.exs b/test/pleroma/config/transfer_task_test.exs index 7d51fd84c..9e3f11f1a 100644 --- a/test/pleroma/config/transfer_task_test.exs +++ b/test/pleroma/config/transfer_task_test.exs @@ -82,6 +82,7 @@ defmodule Pleroma.Config.TransferTaskTest do        on_exit(fn -> Restarter.Pleroma.refresh() end)      end +    @tag :erratic      test "don't restart if no reboot time settings were changed" do        clear_config(:emoji)        insert(:config, key: :emoji, value: [groups: [a: 1, b: 2]]) @@ -92,18 +93,21 @@ defmodule Pleroma.Config.TransferTaskTest do               )      end +    @tag :erratic      test "on reboot time key" do        clear_config(:shout)        insert(:config, key: :shout, value: [enabled: false])        assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"      end +    @tag :erratic      test "on reboot time subkey" do        clear_config(Pleroma.Captcha)        insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60])        assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted"      end +    @tag :erratic      test "don't restart pleroma on reboot time key and subkey if there is false flag" do        clear_config(:shout)        clear_config(Pleroma.Captcha) diff --git a/test/pleroma/user/query_test.exs b/test/pleroma/user/query_test.exs index 357016e3e..363da7665 100644 --- a/test/pleroma/user/query_test.exs +++ b/test/pleroma/user/query_test.exs @@ -34,4 +34,14 @@ defmodule Pleroma.User.QueryTest do        assert %{internal: true} |> Query.build() |> Repo.aggregate(:count) == 2      end    end + +  test "is_suggested param" do +    _user1 = insert(:user, is_suggested: false) +    user2 = insert(:user, is_suggested: true) + +    assert [^user2] = +             %{is_suggested: true} +             |> User.Query.build() +             |> Repo.all() +  end  end diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 5fef81245..6cd93c34c 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -1718,6 +1718,38 @@ defmodule Pleroma.UserTest do      assert user.banner == %{}    end +  describe "set_suggestion" do +    test "suggests a user" do +      user = insert(:user, is_suggested: false) +      refute user.is_suggested +      {:ok, user} = User.set_suggestion(user, true) +      assert user.is_suggested +    end + +    test "suggests a list of users" do +      unsuggested_users = [ +        insert(:user, is_suggested: false), +        insert(:user, is_suggested: false), +        insert(:user, is_suggested: false) +      ] + +      {:ok, users} = User.set_suggestion(unsuggested_users, true) + +      assert Enum.count(users) == 3 + +      Enum.each(users, fn user -> +        assert user.is_suggested +      end) +    end + +    test "unsuggests a user" do +      user = insert(:user, is_suggested: true) +      assert user.is_suggested +      {:ok, user} = User.set_suggestion(user, false) +      refute user.is_suggested +    end +  end +    test "get_public_key_for_ap_id fetches a user that's not in the db" do      assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")    end @@ -2410,13 +2442,16 @@ defmodule Pleroma.UserTest do    test "active_user_count/1" do      insert(:user)      insert(:user, %{local: false}) -    insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -5)}) -    insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -3)})      insert(:user, %{last_active_at: NaiveDateTime.utc_now()}) +    insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), days: -15)}) +    insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -6)}) +    insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), months: -7)}) +    insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), years: -2)})      assert User.active_user_count() == 2 -    assert User.active_user_count(6) == 3 -    assert User.active_user_count(1) == 1 +    assert User.active_user_count(180) == 3 +    assert User.active_user_count(365) == 4 +    assert User.active_user_count(1000) == 5    end    describe "pins" do diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index a61244c76..574ef0d71 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -776,6 +776,20 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do      assert Enum.member?(activities, activity_one)    end +  test "doesn't return activities from deactivated users" do +    _user = insert(:user) +    deactivated = insert(:user) +    active = insert(:user) +    {:ok, activity_one} = CommonAPI.post(deactivated, %{status: "hey!"}) +    {:ok, activity_two} = CommonAPI.post(active, %{status: "yay!"}) +    {:ok, _updated_user} = User.set_activation(deactivated, false) + +    activities = ActivityPub.fetch_activities([], %{}) + +    refute Enum.member?(activities, activity_one) +    assert Enum.member?(activities, activity_two) +  end +    test "always see your own posts even when they address people you block" do      user = insert(:user)      blockee = insert(:user) diff --git a/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs index 0e49fda99..9150b8d41 100644 --- a/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs @@ -105,5 +105,37 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidatorTest do        assert attachment.mediaType == "image/jpeg"      end + +    test "it transforms image dimentions to our internal format" do +      attachment = %{ +        "type" => "Document", +        "name" => "Hello world", +        "url" => "https://media.example.tld/1.jpg", +        "width" => 880, +        "height" => 960, +        "mediaType" => "image/jpeg", +        "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of" +      } + +      expected = %AttachmentValidator{ +        type: "Document", +        name: "Hello world", +        mediaType: "image/jpeg", +        blurhash: "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of", +        url: [ +          %AttachmentValidator.UrlObjectValidator{ +            type: "Link", +            mediaType: "image/jpeg", +            href: "https://media.example.tld/1.jpg", +            width: 880, +            height: 960 +          } +        ] +      } + +      {:ok, ^expected} = +        AttachmentValidator.cast_and_validate(attachment) +        |> Ecto.Changeset.apply_action(:insert) +    end    end  end diff --git a/test/pleroma/web/activity_pub/side_effects_test.exs b/test/pleroma/web/activity_pub/side_effects_test.exs index d0988619d..c6155ed18 100644 --- a/test/pleroma/web/activity_pub/side_effects_test.exs +++ b/test/pleroma/web/activity_pub/side_effects_test.exs @@ -88,6 +88,16 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do        assert User.blocks?(user, blocked)      end +    test "it updates following relationship", %{user: user, blocked: blocked, block: block} do +      {:ok, _, _} = SideEffects.handle(block) + +      refute Pleroma.FollowingRelationship.get(user, blocked) +      assert User.get_follow_state(user, blocked) == nil +      assert User.get_follow_state(blocked, user) == nil +      assert User.get_follow_state(user, blocked, nil) == nil +      assert User.get_follow_state(blocked, user, nil) == nil +    end +      test "it blocks but does not unfollow if the relevant setting is set", %{        user: user,        blocked: blocked, @@ -542,4 +552,74 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do        end      end    end + +  describe "removing a follower" do +    setup do +      user = insert(:user) +      followed = insert(:user) + +      {:ok, _, _, follow_activity} = CommonAPI.follow(user, followed) + +      {:ok, reject_data, []} = Builder.reject(followed, follow_activity) +      {:ok, reject, _meta} = ActivityPub.persist(reject_data, local: true) + +      %{user: user, followed: followed, reject: reject} +    end + +    test "", %{user: user, followed: followed, reject: reject} do +      assert User.following?(user, followed) +      assert Pleroma.FollowingRelationship.get(user, followed) + +      {:ok, _, _} = SideEffects.handle(reject) + +      refute User.following?(user, followed) +      refute Pleroma.FollowingRelationship.get(user, followed) +      assert User.get_follow_state(user, followed) == nil +      assert User.get_follow_state(user, followed, nil) == nil +    end +  end + +  describe "removing a follower from remote" do +    setup do +      user = insert(:user) +      followed = insert(:user, local: false) + +      # Mock a local-to-remote follow +      {:ok, follow_data, []} = Builder.follow(user, followed) + +      follow_data = +        follow_data +        |> Map.put("state", "accept") + +      {:ok, follow, _meta} = ActivityPub.persist(follow_data, local: true) +      {:ok, _, _} = SideEffects.handle(follow) + +      # Mock a remote-to-local accept +      {:ok, accept_data, _} = Builder.accept(followed, follow) +      {:ok, accept, _} = ActivityPub.persist(accept_data, local: false) +      {:ok, _, _} = SideEffects.handle(accept) + +      # Mock a remote-to-local reject +      {:ok, reject_data, []} = Builder.reject(followed, follow) +      {:ok, reject, _meta} = ActivityPub.persist(reject_data, local: false) + +      %{user: user, followed: followed, reject: reject} +    end + +    test "", %{user: user, followed: followed, reject: reject} do +      assert User.following?(user, followed) +      assert Pleroma.FollowingRelationship.get(user, followed) + +      {:ok, _, _} = SideEffects.handle(reject) + +      refute User.following?(user, followed) +      refute Pleroma.FollowingRelationship.get(user, followed) + +      assert Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, followed).data["state"] == +               "reject" + +      assert User.get_follow_state(user, followed) == nil +      assert User.get_follow_state(user, followed, nil) == nil +    end +  end  end diff --git a/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs index fc3ec7450..87c53ebf4 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs @@ -58,7 +58,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.VideoHandlingTest do                     "href" =>                       "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",                     "mediaType" => "video/mp4", -                   "type" => "Link" +                   "type" => "Link", +                   "width" => 480                   }                 ]               } @@ -79,7 +80,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.VideoHandlingTest do                     "href" =>                       "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4",                     "mediaType" => "video/mp4", -                   "type" => "Link" +                   "type" => "Link", +                   "height" => 1080                   }                 ]               } @@ -107,7 +109,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.VideoHandlingTest do                     "href" =>                       "https://peertube.stream/static/streaming-playlists/hls/abece3c3-b9c6-47f4-8040-f3eed8c602e6/abece3c3-b9c6-47f4-8040-f3eed8c602e6-1080-fragmented.mp4",                     "mediaType" => "video/mp4", -                   "type" => "Link" +                   "type" => "Link", +                   "height" => 1080                   }                 ]               } diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index 5a3b57acb..06daf6a9f 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -524,4 +524,44 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do                 )      end    end + +  describe "fix_attachments/1" do +    test "puts dimensions into attachment url field" do +      object = %{ +        "attachment" => [ +          %{ +            "type" => "Document", +            "name" => "Hello world", +            "url" => "https://media.example.tld/1.jpg", +            "width" => 880, +            "height" => 960, +            "mediaType" => "image/jpeg", +            "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of" +          } +        ] +      } + +      expected = %{ +        "attachment" => [ +          %{ +            "type" => "Document", +            "name" => "Hello world", +            "url" => [ +              %{ +                "type" => "Link", +                "mediaType" => "image/jpeg", +                "href" => "https://media.example.tld/1.jpg", +                "width" => 880, +                "height" => 960 +              } +            ], +            "mediaType" => "image/jpeg", +            "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of" +          } +        ] +      } + +      assert Transmogrifier.fix_attachments(object) == expected +    end +  end  end diff --git a/test/pleroma/web/activity_pub/utils_test.exs b/test/pleroma/web/activity_pub/utils_test.exs index ee3e1014e..62dc02f61 100644 --- a/test/pleroma/web/activity_pub/utils_test.exs +++ b/test/pleroma/web/activity_pub/utils_test.exs @@ -213,6 +213,20 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do        assert refresh_record(follow_activity).data["state"] == "accept"        assert refresh_record(follow_activity_two).data["state"] == "accept"      end + +    test "also updates the state of accepted follows" do +      user = insert(:user) +      follower = insert(:user) + +      {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user) +      {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user) + +      {:ok, follow_activity_two} = +        Utils.update_follow_state_for_all(follow_activity_two, "reject") + +      assert refresh_record(follow_activity).data["state"] == "reject" +      assert refresh_record(follow_activity_two).data["state"] == "reject" +    end    end    describe "update_follow_state/2" do diff --git a/test/pleroma/web/admin_api/controllers/user_controller_test.exs b/test/pleroma/web/admin_api/controllers/user_controller_test.exs index d9da34f6e..b199fa704 100644 --- a/test/pleroma/web/admin_api/controllers/user_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/user_controller_test.exs @@ -873,6 +873,56 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do               "@#{admin.nickname} approved users: @#{user_one.nickname}, @#{user_two.nickname}"    end +  test "PATCH /api/pleroma/admin/users/suggest", %{admin: admin, conn: conn} do +    user1 = insert(:user, is_suggested: false) +    user2 = insert(:user, is_suggested: false) + +    response = +      conn +      |> put_req_header("content-type", "application/json") +      |> patch( +        "/api/pleroma/admin/users/suggest", +        %{nicknames: [user1.nickname, user2.nickname]} +      ) +      |> json_response_and_validate_schema(200) + +    assert Enum.map(response["users"], & &1["is_suggested"]) == [true, true] +    [user1, user2] = Repo.reload!([user1, user2]) + +    assert user1.is_suggested +    assert user2.is_suggested + +    log_entry = Repo.one(ModerationLog) + +    assert ModerationLog.get_log_entry_message(log_entry) == +             "@#{admin.nickname} added suggested users: @#{user1.nickname}, @#{user2.nickname}" +  end + +  test "PATCH /api/pleroma/admin/users/unsuggest", %{admin: admin, conn: conn} do +    user1 = insert(:user, is_suggested: true) +    user2 = insert(:user, is_suggested: true) + +    response = +      conn +      |> put_req_header("content-type", "application/json") +      |> patch( +        "/api/pleroma/admin/users/unsuggest", +        %{nicknames: [user1.nickname, user2.nickname]} +      ) +      |> json_response_and_validate_schema(200) + +    assert Enum.map(response["users"], & &1["is_suggested"]) == [false, false] +    [user1, user2] = Repo.reload!([user1, user2]) + +    refute user1.is_suggested +    refute user2.is_suggested + +    log_entry = Repo.one(ModerationLog) + +    assert ModerationLog.get_log_entry_message(log_entry) == +             "@#{admin.nickname} removed suggested users: @#{user1.nickname}, @#{user2.nickname}" +  end +    test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admin, conn: conn} do      user = insert(:user) @@ -906,6 +956,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do        "display_name" => HTML.strip_tags(user.name || user.nickname),        "is_confirmed" => true,        "is_approved" => true, +      "is_suggested" => false,        "url" => user.ap_id,        "registration_reason" => nil,        "actor_type" => "Person", diff --git a/test/pleroma/web/manifest_controller_test.exs b/test/pleroma/web/manifest_controller_test.exs new file mode 100644 index 000000000..b7a4940db --- /dev/null +++ b/test/pleroma/web/manifest_controller_test.exs @@ -0,0 +1,17 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ManifestControllerTest do +  use Pleroma.Web.ConnCase + +  setup do +    clear_config([:instance, :name], "Manifest Test") +    clear_config([:manifest, :theme_color], "#ff0000") +  end + +  test "manifest.json", %{conn: conn} do +    conn = get(conn, "/manifest.json") +    assert %{"name" => "Manifest Test", "theme_color" => "#ff0000"} = json_response(conn, 200) +  end +end diff --git a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs index 168966fc9..89273e67b 100644 --- a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs @@ -4,8 +4,11 @@  defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do    use Pleroma.Web.ConnCase, async: true +  alias Pleroma.UserRelationship +  alias Pleroma.Web.CommonAPI +  import Pleroma.Factory -  setup do: oauth_access(["read"]) +  setup do: oauth_access(["read", "write"])    test "returns empty result", %{conn: conn} do      res = @@ -15,4 +18,66 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do      assert res == []    end + +  test "returns v2 suggestions", %{conn: conn} do +    %{id: user_id} = insert(:user, is_suggested: true) + +    res = +      conn +      |> get("/api/v2/suggestions") +      |> json_response_and_validate_schema(200) + +    assert [%{"source" => "staff", "account" => %{"id" => ^user_id}}] = res +  end + +  test "returns v2 suggestions excluding dismissed accounts", %{conn: conn} do +    %{id: user_id} = insert(:user, is_suggested: true) + +    conn +    |> delete("/api/v1/suggestions/#{user_id}") +    |> json_response_and_validate_schema(200) + +    res = +      conn +      |> get("/api/v2/suggestions") +      |> json_response_and_validate_schema(200) + +    assert [] = res +  end + +  test "returns v2 suggestions excluding blocked accounts", %{conn: conn, user: blocker} do +    blocked = insert(:user, is_suggested: true) +    {:ok, _} = CommonAPI.block(blocker, blocked) + +    res = +      conn +      |> get("/api/v2/suggestions") +      |> json_response_and_validate_schema(200) + +    assert [] = res +  end + +  test "returns v2 suggestions excluding followed accounts", %{conn: conn, user: follower} do +    followed = insert(:user, is_suggested: true) +    {:ok, _, _, _} = CommonAPI.follow(follower, followed) + +    res = +      conn +      |> get("/api/v2/suggestions") +      |> json_response_and_validate_schema(200) + +    assert [] = res +  end + +  test "dismiss suggestion", %{conn: conn, user: source} do +    target = insert(:user, is_suggested: true) + +    res = +      conn +      |> delete("/api/v1/suggestions/#{target.id}") +      |> json_response_and_validate_schema(200) + +    assert res == %{} +    assert UserRelationship.exists?(:suggestion_dismiss, source, target) +  end  end diff --git a/test/pleroma/web/mastodon_api/views/account_view_test.exs b/test/pleroma/web/mastodon_api/views/account_view_test.exs index 60881756d..9af588778 100644 --- a/test/pleroma/web/mastodon_api/views/account_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/account_view_test.exs @@ -83,6 +83,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          tags: [],          is_admin: false,          is_moderator: false, +        is_suggested: false,          hide_favorites: true,          hide_followers: false,          hide_follows: false, @@ -183,6 +184,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          tags: [],          is_admin: false,          is_moderator: false, +        is_suggested: false,          hide_favorites: true,          hide_followers: false,          hide_follows: false, diff --git a/test/pleroma/web/mastodon_api/views/suggestion_view_test.exs b/test/pleroma/web/mastodon_api/views/suggestion_view_test.exs new file mode 100644 index 000000000..5aae36ce9 --- /dev/null +++ b/test/pleroma/web/mastodon_api/views/suggestion_view_test.exs @@ -0,0 +1,34 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.MastodonAPI.SuggestionViewTest do +  use Pleroma.DataCase, async: true +  import Pleroma.Factory +  alias Pleroma.Web.MastodonAPI.SuggestionView, as: View + +  test "show.json" do +    user = insert(:user, is_suggested: true) +    json = View.render("show.json", %{user: user, source: :staff, skip_visibility_check: true}) + +    assert json.source == :staff +    assert json.account.id == user.id +  end + +  test "index.json" do +    user1 = insert(:user, is_suggested: true) +    user2 = insert(:user, is_suggested: true) +    user3 = insert(:user, is_suggested: true) + +    [suggestion1, suggestion2, suggestion3] = +      View.render("index.json", %{ +        users: [user1, user2, user3], +        source: :staff, +        skip_visibility_check: true +      }) + +    assert suggestion1.source == :staff +    assert suggestion2.account.id == user2.id +    assert suggestion3.account.url == user3.ap_id +  end +end diff --git a/test/pleroma/web/plugs/frontend_static_plug_test.exs b/test/pleroma/web/plugs/frontend_static_plug_test.exs index a9342e6f0..52379b86a 100644 --- a/test/pleroma/web/plugs/frontend_static_plug_test.exs +++ b/test/pleroma/web/plugs/frontend_static_plug_test.exs @@ -94,7 +94,10 @@ defmodule Pleroma.Web.Plugs.FrontendStaticPlugTest do        "internal",        ".well-known",        "nodeinfo", +      "manifest.json", +      "auth",        "proxy", +      "phoenix",        "test",        "user_exists",        "check_password" diff --git a/test/pleroma/web/plugs/rate_limiter_test.exs b/test/pleroma/web/plugs/rate_limiter_test.exs index d007e3f26..b7cfde1f7 100644 --- a/test/pleroma/web/plugs/rate_limiter_test.exs +++ b/test/pleroma/web/plugs/rate_limiter_test.exs @@ -48,6 +48,7 @@ defmodule Pleroma.Web.Plugs.RateLimiterTest do      refute RateLimiter.disabled?(build_conn())    end +  @tag :erratic    test "it restricts based on config values" do      limiter_name = :test_plug_opts      scale = 80 @@ -137,6 +138,7 @@ defmodule Pleroma.Web.Plugs.RateLimiterTest do    end    describe "unauthenticated users" do +    @tag :erratic      test "are restricted based on remote IP" do        limiter_name = :test_unauthenticated        clear_config([:rate_limit, limiter_name], [{1000, 5}, {1, 10}]) @@ -174,6 +176,7 @@ defmodule Pleroma.Web.Plugs.RateLimiterTest do        :ok      end +    @tag :erratic      test "can have limits separate from unauthenticated connections" do        limiter_name = :test_authenticated1 @@ -199,6 +202,7 @@ defmodule Pleroma.Web.Plugs.RateLimiterTest do        assert conn.halted      end +    @tag :erratic      test "different users are counted independently" do        limiter_name = :test_authenticated2        clear_config([:rate_limit, limiter_name], [{1, 10}, {1000, 5}]) diff --git a/test/pleroma/web/twitter_api/password_controller_test.exs b/test/pleroma/web/twitter_api/password_controller_test.exs index cf99e2434..45ab10a8a 100644 --- a/test/pleroma/web/twitter_api/password_controller_test.exs +++ b/test/pleroma/web/twitter_api/password_controller_test.exs @@ -5,10 +5,14 @@  defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do    use Pleroma.Web.ConnCase +  alias Pleroma.Config    alias Pleroma.PasswordResetToken +  alias Pleroma.Repo +  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.OAuth.Token    import Pleroma.Factory +  import Swoosh.TestAssertions    describe "GET /api/pleroma/password_reset/token" do      test "it returns error when token invalid", %{conn: conn} do @@ -116,4 +120,94 @@ defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do        assert User.get_by_id(user.id).password_reset_pending == false      end    end + +  describe "POST /auth/password, with valid parameters" do +    setup %{conn: conn} do +      user = insert(:user) +      conn = post(conn, "/auth/password?email=#{user.email}") +      %{conn: conn, user: user} +    end + +    test "it returns 204", %{conn: conn} do +      assert empty_json_response(conn) +    end + +    test "it creates a PasswordResetToken record for user", %{user: user} do +      token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id) +      assert token_record +    end + +    test "it sends an email to user", %{user: user} do +      ObanHelpers.perform_all() +      token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id) + +      email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token) +      notify_email = Config.get([:instance, :notify_email]) +      instance_name = Config.get([:instance, :name]) + +      assert_email_sent( +        from: {instance_name, notify_email}, +        to: {user.name, user.email}, +        html_body: email.html_body +      ) +    end +  end + +  describe "POST /auth/password, with nickname" do +    test "it returns 204", %{conn: conn} do +      user = insert(:user) + +      assert conn +             |> post("/auth/password?nickname=#{user.nickname}") +             |> empty_json_response() + +      ObanHelpers.perform_all() +      token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id) + +      email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token) +      notify_email = Config.get([:instance, :notify_email]) +      instance_name = Config.get([:instance, :name]) + +      assert_email_sent( +        from: {instance_name, notify_email}, +        to: {user.name, user.email}, +        html_body: email.html_body +      ) +    end + +    test "it doesn't fail when a user has no email", %{conn: conn} do +      user = insert(:user, %{email: nil}) + +      assert conn +             |> post("/auth/password?nickname=#{user.nickname}") +             |> empty_json_response() +    end +  end + +  describe "POST /auth/password, with invalid parameters" do +    setup do +      user = insert(:user) +      {:ok, user: user} +    end + +    test "it returns 204 when user is not found", %{conn: conn, user: user} do +      conn = post(conn, "/auth/password?email=nonexisting_#{user.email}") + +      assert empty_json_response(conn) +    end + +    test "it returns 204 when user is not local", %{conn: conn, user: user} do +      {:ok, user} = Repo.update(Ecto.Changeset.change(user, local: false)) +      conn = post(conn, "/auth/password?email=#{user.email}") + +      assert empty_json_response(conn) +    end + +    test "it returns 204 when user is deactivated", %{conn: conn, user: user} do +      {:ok, user} = Repo.update(Ecto.Changeset.change(user, is_active: false, local: true)) +      conn = post(conn, "/auth/password?email=#{user.email}") + +      assert empty_json_response(conn) +    end +  end  end diff --git a/test/pleroma/web/twitter_api/util_controller_test.exs b/test/pleroma/web/twitter_api/util_controller_test.exs index 3380aec22..ee658ddf6 100644 --- a/test/pleroma/web/twitter_api/util_controller_test.exs +++ b/test/pleroma/web/twitter_api/util_controller_test.exs @@ -473,7 +473,10 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do      test "with proper permissions and wrong or missing password", %{conn: conn} do        for params <- [%{"password" => "hi"}, %{}] do -        ret_conn = post(conn, "/api/pleroma/delete_account", params) +        ret_conn = +          conn +          |> put_req_header("content-type", "application/json") +          |> post("/api/pleroma/delete_account", params)          assert json_response_and_validate_schema(ret_conn, 200) == %{                   "error" => "Invalid password." @@ -481,8 +484,28 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do        end      end -    test "with proper permissions and valid password", %{conn: conn, user: user} do -      conn = post(conn, "/api/pleroma/delete_account?password=test") +    test "with proper permissions and valid password (URL query)", %{conn: conn, user: user} do +      conn = +        conn +        |> put_req_header("content-type", "application/json") +        |> post("/api/pleroma/delete_account?password=test") + +      ObanHelpers.perform_all() +      assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"} + +      user = User.get_by_id(user.id) +      refute user.is_active +      assert user.name == nil +      assert user.bio == "" +      assert user.password_hash == nil +    end + +    test "with proper permissions and valid password (JSON body)", %{conn: conn, user: user} do +      conn = +        conn +        |> put_req_header("content-type", "application/json") +        |> post("/api/pleroma/delete_account", %{password: "test"}) +        ObanHelpers.perform_all()        assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"} diff --git a/test/test_helper.exs b/test/test_helper.exs index 0c9783076..9fb41e985 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -3,7 +3,7 @@  # SPDX-License-Identifier: AGPL-3.0-only  os_exclude = if :os.type() == {:unix, :darwin}, do: [skip_on_mac: true], else: [] -ExUnit.start(exclude: [:federated | os_exclude]) +ExUnit.start(exclude: [:federated, :erratic] ++ os_exclude)  Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, :manual) | 
