From 7c235b8874d575cb1582b1fca2817a8db492bf4c Mon Sep 17 00:00:00 2001 From: link0ff Date: Sun, 16 Dec 2018 18:04:31 +0200 Subject: [PATCH] Add more Mix task tests in relay_test.exs and uploads_test.exs. Rename test/tasks/user.exs to test/tasks/user_test.exs. --- test/tasks/relay_test.exs | 65 ++++++++++++++++++++++++++ test/tasks/uploads_test.exs | 47 +++++++++++++++++++ test/tasks/{user.exs => user_test.exs} | 44 ++++++++++++----- 3 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 test/tasks/relay_test.exs create mode 100644 test/tasks/uploads_test.exs rename test/tasks/{user.exs => user_test.exs} (90%) diff --git a/test/tasks/relay_test.exs b/test/tasks/relay_test.exs new file mode 100644 index 000000000..737293865 --- /dev/null +++ b/test/tasks/relay_test.exs @@ -0,0 +1,65 @@ +defmodule Mix.Tasks.Pleroma.RelayTest do + alias Pleroma.Activity + alias Pleroma.Web.ActivityPub.{ActivityPub, Relay, Utils} + alias Pleroma.User + use Pleroma.DataCase + + setup_all do + Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) + + Mix.shell(Mix.Shell.Process) + + on_exit(fn -> + Mix.shell(Mix.Shell.IO) + end) + + :ok + end + + describe "running follow" do + test "relay is followed" do + target_instance = "http://mastodon.example.org/users/admin" + + Mix.Tasks.Pleroma.Relay.run(["follow", target_instance]) + + local_user = Relay.get_actor() + assert local_user.ap_id =~ "/relay" + + target_user = User.get_by_ap_id(target_instance) + refute target_user.local + + activity = Utils.fetch_latest_follow(local_user, target_user) + assert activity.data["type"] == "Follow" + assert activity.data["actor"] == local_user.ap_id + assert activity.data["object"] == target_user.ap_id + end + end + + describe "running unfollow" do + test "relay is unfollowed" do + target_instance = "http://mastodon.example.org/users/admin" + + Mix.Tasks.Pleroma.Relay.run(["follow", target_instance]) + + %User{ap_id: follower_id} = local_user = Relay.get_actor() + target_user = User.get_by_ap_id(target_instance) + follow_activity = Utils.fetch_latest_follow(local_user, target_user) + + Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance]) + + cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"]) + assert cancelled_activity.data["state"] == "cancelled" + + [undo_activity] = + ActivityPub.fetch_activities([], %{ + "type" => "Undo", + "actor_id" => follower_id, + "limit" => 1 + }) + + assert undo_activity.data["type"] == "Undo" + assert undo_activity.data["actor"] == local_user.ap_id + assert undo_activity.data["object"] == cancelled_activity.data + end + end +end diff --git a/test/tasks/uploads_test.exs b/test/tasks/uploads_test.exs new file mode 100644 index 000000000..a76e96df5 --- /dev/null +++ b/test/tasks/uploads_test.exs @@ -0,0 +1,47 @@ +defmodule Mix.Tasks.Pleroma.UploadsTest do + alias Pleroma.Upload + use Pleroma.DataCase + + import Mock + + setup_all do + Mix.shell(Mix.Shell.Process) + + on_exit(fn -> + Mix.shell(Mix.Shell.IO) + end) + + :ok + end + + describe "running migrate_local" do + test "uploads migrated" do + with_mock Upload, + store: fn %Upload{name: _file, path: _path}, _opts -> {:ok, %{}} end do + Mix.Tasks.Pleroma.Uploads.run(["migrate_local", "S3"]) + + assert_received {:mix_shell, :info, [message]} + assert message =~ "Migrating files from local" + + assert_received {:mix_shell, :info, [message]} + + assert %{"total_count" => total_count} = + Regex.named_captures(~r"^Found (?\d+) uploads$", message) + + assert_received {:mix_shell, :info, [message]} + + assert %{"count" => ^total_count, "total_count" => ^total_count} = + Regex.named_captures( + ~r"^Uploaded (?\d+)/(?\d+) files$", + message + ) + end + end + + test "nonexistent uploader" do + assert_raise RuntimeError, ~r/The uploader .* is not an existing/, fn -> + Mix.Tasks.Pleroma.Uploads.run(["migrate_local", "nonexistent"]) + end + end + end +end diff --git a/test/tasks/user.exs b/test/tasks/user_test.exs similarity index 90% rename from test/tasks/user.exs rename to test/tasks/user_test.exs index 9f0942ba7..7479bf749 100644 --- a/test/tasks/user.exs +++ b/test/tasks/user_test.exs @@ -39,8 +39,10 @@ test "user is created" do assert_received {:mix_shell, :info, [message]} assert message =~ "user will be created" + assert_received {:mix_shell, :yes?, [message]} assert message =~ "Continue" + assert_received {:mix_shell, :info, [message]} assert message =~ "created" @@ -57,11 +59,15 @@ test "user is not created" do # prepare to answer no send(self(), {:mix_shell_input, :yes?, false}) + Mix.Tasks.Pleroma.User.run(["new", unsaved.nickname, unsaved.email]) + assert_received {:mix_shell, :info, [message]} assert message =~ "user will be created" + assert_received {:mix_shell, :yes?, [message]} assert message =~ "Continue" + assert_received {:mix_shell, :info, [message]} assert message =~ "will not be created" @@ -74,6 +80,7 @@ test "user is deleted" do user = insert(:user) Mix.Tasks.Pleroma.User.run(["rm", user.nickname]) + assert_received {:mix_shell, :info, [message]} assert message =~ " deleted" @@ -82,7 +89,8 @@ test "user is deleted" do end test "no user to delete" do - Mix.Tasks.Pleroma.User.run(["rm", "nonexistant"]) + Mix.Tasks.Pleroma.User.run(["rm", "nonexistent"]) + assert_received {:mix_shell, :error, [message]} assert message =~ "No local user" end @@ -93,6 +101,7 @@ test "user is deactivated" do user = insert(:user) Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname]) + assert_received {:mix_shell, :info, [message]} assert message =~ " deactivated" @@ -104,6 +113,7 @@ test "user is activated" do user = insert(:user, info: %{deactivated: true}) Mix.Tasks.Pleroma.User.run(["toggle_activated", user.nickname]) + assert_received {:mix_shell, :info, [message]} assert message =~ " activated" @@ -112,7 +122,8 @@ test "user is activated" do end test "no user to toggle" do - Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistant"]) + Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistent"]) + assert_received {:mix_shell, :error, [message]} assert message =~ "No user" end @@ -124,10 +135,13 @@ test "user is unsubscribed" do user = insert(:user, %{following: [User.ap_followers(followed)]}) Mix.Tasks.Pleroma.User.run(["unsubscribe", user.nickname]) + assert_received {:mix_shell, :info, [message]} assert message =~ "Deactivating" + assert_received {:mix_shell, :info, [message]} assert message =~ "Unsubscribing" + # Note that the task has delay :timer.sleep(500) assert_received {:mix_shell, :info, [message]} assert message =~ "Successfully unsubscribed" @@ -138,7 +152,8 @@ test "user is unsubscribed" do end test "no user to unsubscribe" do - Mix.Tasks.Pleroma.User.run(["unsubscribe", "nonexistant"]) + Mix.Tasks.Pleroma.User.run(["unsubscribe", "nonexistent"]) + assert_received {:mix_shell, :error, [message]} assert message =~ "No user" end @@ -149,12 +164,15 @@ test "All statuses set" do user = insert(:user) Mix.Tasks.Pleroma.User.run(["set", user.nickname, "--moderator", "--admin", "--locked"]) + assert_received {:mix_shell, :info, [message]} - assert message =~ ~r"Moderator status .* true" + assert message =~ ~r/Moderator status .* true/ + assert_received {:mix_shell, :info, [message]} - assert message =~ ~r"Locked status .* true" + assert message =~ ~r/Locked status .* true/ + assert_received {:mix_shell, :info, [message]} - assert message =~ ~r"Admin status .* true" + assert message =~ ~r/Admin status .* true/ user = User.get_by_nickname(user.nickname) assert user.info.is_moderator @@ -174,11 +192,13 @@ test "All statuses unset" do ]) assert_received {:mix_shell, :info, [message]} - assert message =~ ~r"Moderator status .* false" + assert message =~ ~r/Moderator status .* false/ + assert_received {:mix_shell, :info, [message]} - assert message =~ ~r"Locked status .* false" + assert message =~ ~r/Locked status .* false/ + assert_received {:mix_shell, :info, [message]} - assert message =~ ~r"Admin status .* false" + assert message =~ ~r/Admin status .* false/ user = User.get_by_nickname(user.nickname) refute user.info.is_moderator @@ -187,7 +207,8 @@ test "All statuses unset" do end test "no user to set status" do - Mix.Tasks.Pleroma.User.run(["set", "nonexistant", "--moderator"]) + Mix.Tasks.Pleroma.User.run(["set", "nonexistent", "--moderator"]) + assert_received {:mix_shell, :error, [message]} assert message =~ "No local user" end @@ -206,7 +227,8 @@ test "password reset token is generated" do end test "no user to reset password" do - Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistant"]) + Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistent"]) + assert_received {:mix_shell, :error, [message]} assert message =~ "No local user" end