Merge branch 'fix/no-email-no-fail' into 'develop'

Do not fail when user has no email

See merge request pleroma/pleroma!2249
This commit is contained in:
lain 2020-03-04 12:43:06 +00:00
commit 6f7a8c43a2
8 changed files with 75 additions and 3 deletions

View File

@ -660,7 +660,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
{:ok, activity} <- insert(flag_data, local), {:ok, activity} <- insert(flag_data, local),
{:ok, stripped_activity} <- strip_report_status_data(activity), {:ok, stripped_activity} <- strip_report_status_data(activity),
:ok <- maybe_federate(stripped_activity) do :ok <- maybe_federate(stripped_activity) do
Enum.each(User.all_superusers(), fn superuser -> User.all_superusers()
|> Enum.filter(fn user -> not is_nil(user.email) end)
|> Enum.each(fn superuser ->
superuser superuser
|> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content) |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
|> Pleroma.Emails.Mailer.deliver_async() |> Pleroma.Emails.Mailer.deliver_async()

View File

@ -99,7 +99,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
def password_reset(nickname_or_email) do def password_reset(nickname_or_email) do
with true <- is_binary(nickname_or_email), with true <- is_binary(nickname_or_email),
%User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email), %User{local: true, email: email} = user when not is_nil(email) <-
User.get_by_nickname_or_email(nickname_or_email),
{:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
user user
|> UserEmail.password_reset_email(token_record.token) |> UserEmail.password_reset_email(token_record.token)
@ -110,6 +111,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
false -> false ->
{:error, "bad user identifier"} {:error, "bad user identifier"}
%User{local: true, email: nil} ->
{:ok, :noop}
%User{local: false} -> %User{local: false} ->
{:error, "remote user"} {:error, "remote user"}

View File

@ -31,6 +31,7 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorker do
from(u in inactive_users_query, from(u in inactive_users_query,
where: fragment(~s(? ->'digest' @> 'true'), u.email_notifications), where: fragment(~s(? ->'digest' @> 'true'), u.email_notifications),
where: not is_nil(u.email),
where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"), where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
select: u select: u
) )

View File

@ -51,6 +51,7 @@ defmodule Pleroma.Workers.Cron.NewUsersDigestWorker do
if users_and_statuses != [] do if users_and_statuses != [] do
%{is_admin: true} %{is_admin: true}
|> User.Query.build() |> User.Query.build()
|> where([u], not is_nil(u.email))
|> Repo.all() |> Repo.all()
|> Enum.map(&Pleroma.Emails.NewUsersDigestEmail.new_users(&1, users_and_statuses)) |> Enum.map(&Pleroma.Emails.NewUsersDigestEmail.new_users(&1, users_and_statuses))
|> Enum.each(&Pleroma.Emails.Mailer.deliver/1) |> Enum.each(&Pleroma.Emails.Mailer.deliver/1)

View File

@ -85,6 +85,37 @@ defmodule Pleroma.Web.MastodonAPI.AuthControllerTest do
end end
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}")
|> json_response(:no_content)
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}")
|> json_response(:no_content)
end
end
describe "POST /auth/password, with invalid parameters" do describe "POST /auth/password, with invalid parameters" do
setup do setup do
user = insert(:user) user = insert(:user)

View File

@ -75,4 +75,13 @@ defmodule Pleroma.Web.MastodonAPI.ReportControllerTest do
assert json_response(conn, 400) == %{"error" => "Account not found"} assert json_response(conn, 400) == %{"error" => "Account not found"}
end end
test "doesn't fail if an admin has no email", %{conn: conn, target_user: target_user} do
insert(:user, %{is_admin: true, email: nil})
assert %{"action_taken" => false, "id" => _} =
conn
|> post("/api/v1/reports", %{"account_id" => target_user.id})
|> json_response(200)
end
end end

View File

@ -13,7 +13,7 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do
clear_config([:email_notifications, :digest]) clear_config([:email_notifications, :digest])
test "it sends digest emails" do setup do
Pleroma.Config.put([:email_notifications, :digest], %{ Pleroma.Config.put([:email_notifications, :digest], %{
active: true, active: true,
inactivity_threshold: 7, inactivity_threshold: 7,
@ -31,6 +31,10 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do
{:ok, _} = User.switch_email_notifications(user2, "digest", true) {:ok, _} = User.switch_email_notifications(user2, "digest", true)
CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}!"}) CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}!"})
{:ok, user2: user2}
end
test "it sends digest emails", %{user2: user2} do
Pleroma.Workers.Cron.DigestEmailsWorker.perform(:opts, :pid) Pleroma.Workers.Cron.DigestEmailsWorker.perform(:opts, :pid)
# Performing job(s) enqueued at previous step # Performing job(s) enqueued at previous step
ObanHelpers.perform_all() ObanHelpers.perform_all()
@ -39,4 +43,12 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do
assert email.to == [{user2.name, user2.email}] assert email.to == [{user2.name, user2.email}]
assert email.subject == "Your digest from #{Pleroma.Config.get(:instance)[:name]}" assert email.subject == "Your digest from #{Pleroma.Config.get(:instance)[:name]}"
end end
test "it doesn't fail when a user has no email", %{user2: user2} do
{:ok, _} = user2 |> Ecto.Changeset.change(%{email: nil}) |> Pleroma.Repo.update()
Pleroma.Workers.Cron.DigestEmailsWorker.perform(:opts, :pid)
# Performing job(s) enqueued at previous step
ObanHelpers.perform_all()
end
end end

View File

@ -29,4 +29,16 @@ defmodule Pleroma.Workers.Cron.NewUsersDigestWorkerTest do
assert email.html_body =~ user2.nickname assert email.html_body =~ user2.nickname
assert email.html_body =~ "cofe" assert email.html_body =~ "cofe"
end end
test "it doesn't fail when admin has no email" do
yesterday = NaiveDateTime.utc_now() |> Timex.shift(days: -1)
insert(:user, %{is_admin: true, email: nil})
insert(:user, %{inserted_at: yesterday})
user = insert(:user, %{inserted_at: yesterday})
CommonAPI.post(user, %{"status" => "cofe"})
NewUsersDigestWorker.perform(nil, nil)
ObanHelpers.perform_all()
end
end end