From 36f1af232a3e34594617f2044652d7d02b19a234 Mon Sep 17 00:00:00 2001 From: link0ff Date: Sun, 16 Dec 2018 02:14:23 +0200 Subject: [PATCH 1/3] Add tests for Mix tasks --- test/tasks/user.exs | 225 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 test/tasks/user.exs diff --git a/test/tasks/user.exs b/test/tasks/user.exs new file mode 100644 index 000000000..9f0942ba7 --- /dev/null +++ b/test/tasks/user.exs @@ -0,0 +1,225 @@ +defmodule Mix.Tasks.Pleroma.UserTest do + alias Pleroma.User + use Pleroma.DataCase + + import Pleroma.Factory + import ExUnit.CaptureIO + + setup_all do + Mix.shell(Mix.Shell.Process) + + on_exit(fn -> + Mix.shell(Mix.Shell.IO) + end) + + :ok + end + + describe "running new" do + test "user is created" do + # just get random data + unsaved = build(:user) + + # prepare to answer yes + send(self(), {:mix_shell_input, :yes?, true}) + + Mix.Tasks.Pleroma.User.run([ + "new", + unsaved.nickname, + unsaved.email, + "--name", + unsaved.name, + "--bio", + unsaved.bio, + "--password", + "test", + "--moderator", + "--admin" + ]) + + 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" + + user = User.get_by_nickname(unsaved.nickname) + assert user.name == unsaved.name + assert user.email == unsaved.email + assert user.bio == unsaved.bio + assert user.info.is_moderator + assert user.info.is_admin + end + + test "user is not created" do + unsaved = build(:user) + + # 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" + + refute User.get_by_nickname(unsaved.nickname) + end + end + + describe "running rm" do + 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" + + user = User.get_by_nickname(user.nickname) + assert user.info.deactivated + end + + test "no user to delete" do + Mix.Tasks.Pleroma.User.run(["rm", "nonexistant"]) + assert_received {:mix_shell, :error, [message]} + assert message =~ "No local user" + end + end + + describe "running toggle_activated" do + 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" + + user = User.get_by_nickname(user.nickname) + assert user.info.deactivated + end + + 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" + + user = User.get_by_nickname(user.nickname) + refute user.info.deactivated + end + + test "no user to toggle" do + Mix.Tasks.Pleroma.User.run(["toggle_activated", "nonexistant"]) + assert_received {:mix_shell, :error, [message]} + assert message =~ "No user" + end + end + + describe "running unsubscribe" do + test "user is unsubscribed" do + followed = insert(:user) + 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" + + user = User.get_by_nickname(user.nickname) + assert length(user.following) == 0 + assert user.info.deactivated + end + + test "no user to unsubscribe" do + Mix.Tasks.Pleroma.User.run(["unsubscribe", "nonexistant"]) + assert_received {:mix_shell, :error, [message]} + assert message =~ "No user" + end + end + + describe "running set" do + 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_received {:mix_shell, :info, [message]} + assert message =~ ~r"Locked status .* true" + assert_received {:mix_shell, :info, [message]} + assert message =~ ~r"Admin status .* true" + + user = User.get_by_nickname(user.nickname) + assert user.info.is_moderator + assert user.info.locked + assert user.info.is_admin + end + + test "All statuses unset" do + user = insert(:user, info: %{is_moderator: true, locked: true, is_admin: true}) + + Mix.Tasks.Pleroma.User.run([ + "set", + user.nickname, + "--no-moderator", + "--no-admin", + "--no-locked" + ]) + + assert_received {:mix_shell, :info, [message]} + assert message =~ ~r"Moderator status .* false" + assert_received {:mix_shell, :info, [message]} + assert message =~ ~r"Locked status .* false" + assert_received {:mix_shell, :info, [message]} + assert message =~ ~r"Admin status .* false" + + user = User.get_by_nickname(user.nickname) + refute user.info.is_moderator + refute user.info.locked + refute user.info.is_admin + end + + test "no user to set status" do + Mix.Tasks.Pleroma.User.run(["set", "nonexistant", "--moderator"]) + assert_received {:mix_shell, :error, [message]} + assert message =~ "No local user" + end + end + + describe "running reset_password" do + test "password reset token is generated" do + user = insert(:user) + + assert capture_io(fn -> + Mix.Tasks.Pleroma.User.run(["reset_password", user.nickname]) + end) =~ "URL:" + + assert_received {:mix_shell, :info, [message]} + assert message =~ "Generated" + end + + test "no user to reset password" do + Mix.Tasks.Pleroma.User.run(["reset_password", "nonexistant"]) + assert_received {:mix_shell, :error, [message]} + assert message =~ "No local user" + end + end + + describe "running invite" do + test "invite token is generated" do + assert capture_io(fn -> + Mix.Tasks.Pleroma.User.run(["invite"]) + end) =~ "http" + + assert_received {:mix_shell, :info, [message]} + assert message =~ "Generated" + end + end +end From 7c235b8874d575cb1582b1fca2817a8db492bf4c Mon Sep 17 00:00:00 2001 From: link0ff Date: Sun, 16 Dec 2018 18:04:31 +0200 Subject: [PATCH 2/3] 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 From 22d483d4f74ff404fd6ae0cd88d2e5bcc7cceaf6 Mon Sep 17 00:00:00 2001 From: link0ff Date: Sun, 16 Dec 2018 18:25:31 +0200 Subject: [PATCH 3/3] Use bindings dbuser and dbname in sample_psql.eex --- lib/mix/tasks/pleroma/sample_psql.eex | 6 +++--- lib/mix/tasks/pleroma/user.ex | 18 ++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/mix/tasks/pleroma/sample_psql.eex b/lib/mix/tasks/pleroma/sample_psql.eex index c89b34ef2..f0ac05e57 100644 --- a/lib/mix/tasks/pleroma/sample_psql.eex +++ b/lib/mix/tasks/pleroma/sample_psql.eex @@ -1,6 +1,6 @@ -CREATE USER pleroma WITH ENCRYPTED PASSWORD '<%= dbpass %>'; -CREATE DATABASE pleroma_dev OWNER pleroma; -\c pleroma_dev; +CREATE USER <%= dbuser %> WITH ENCRYPTED PASSWORD '<%= dbpass %>'; +CREATE DATABASE <%= dbname %> OWNER <%= dbuser %>; +\c <%= dbname %>; --Extensions made by ecto.migrate that need superuser access CREATE EXTENSION IF NOT EXISTS citext; CREATE EXTENSION IF NOT EXISTS pg_trgm; diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index fe6e6935f..3d30e3a81 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -94,16 +94,14 @@ def run(["new", nickname, email | rest]) do unless not proceed? do Common.start_pleroma() - params = - %{ - nickname: nickname, - email: email, - password: password, - password_confirmation: password, - name: name, - bio: bio - } - |> IO.inspect() + params = %{ + nickname: nickname, + email: email, + password: password, + password_confirmation: password, + name: name, + bio: bio + } user = User.register_changeset(%User{}, params) Repo.insert!(user)