Allow users to remove their emails if instance does not need email to register

This commit is contained in:
Tusooa Zhu 2021-09-05 20:56:16 -04:00
parent 6b1282a829
commit 198250dcef
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
5 changed files with 95 additions and 6 deletions

View file

@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Unreleased
### Changed
- Allow users to remove their emails if instance does not need email to register
### Added

View file

@ -2248,7 +2248,7 @@ defmodule Pleroma.User do
def change_email(user, email) do
user
|> cast(%{email: email}, [:email])
|> validate_required([:email])
|> maybe_validate_required_email(false)
|> unique_constraint(:email)
|> validate_format(:email, @email_regex)
|> update_and_set_cache()

View file

@ -121,7 +121,10 @@ defmodule Pleroma.Web.ApiSpec.TwitterUtilOperation do
type: :object,
required: [:email, :password],
properties: %{
email: %Schema{type: :string, description: "New email"},
email: %Schema{
type: :string,
description: "New email. Set to blank to remove the user's email."
},
password: %Schema{type: :string, description: "Current password"}
}
}

View file

@ -2199,11 +2199,40 @@ defmodule Pleroma.UserTest do
[user: insert(:user)]
end
test "blank email returns error", %{user: user} do
test "blank email returns error if we require an email on registration", %{user: user} do
orig_account_activation_required =
Pleroma.Config.get([:instance, :account_activation_required])
Pleroma.Config.put([:instance, :account_activation_required], true)
on_exit(fn ->
Pleroma.Config.put(
[:instance, :account_activation_required],
orig_account_activation_required
)
end)
assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, "")
assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
end
test "blank email should be fine if we do not require an email on registration", %{user: user} do
orig_account_activation_required =
Pleroma.Config.get([:instance, :account_activation_required])
Pleroma.Config.put([:instance, :account_activation_required], false)
on_exit(fn ->
Pleroma.Config.put(
[:instance, :account_activation_required],
orig_account_activation_required
)
end)
assert {:ok, %User{email: nil}} = User.change_email(user, "")
assert {:ok, %User{email: nil}} = User.change_email(user, nil)
end
test "non unique email returns error", %{user: user} do
%{email: email} = insert(:user)
@ -2219,6 +2248,25 @@ defmodule Pleroma.UserTest do
test "changes email", %{user: user} do
assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
end
test "adds email", %{user: user} do
orig_account_activation_required =
Pleroma.Config.get([:instance, :account_activation_required])
Pleroma.Config.put([:instance, :account_activation_required], false)
on_exit(fn ->
Pleroma.Config.put(
[:instance, :account_activation_required],
orig_account_activation_required
)
end)
assert {:ok, _} = User.change_email(user, "")
Pleroma.Config.put([:instance, :account_activation_required], true)
assert {:ok, %User{email: "cofe2@cofe.party"}} = User.change_email(user, "cofe2@cofe.party")
end
end
describe "get_cached_by_nickname_or_id" do

View file

@ -302,9 +302,22 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
assert %{"error" => "Missing field: email."} = json_response_and_validate_schema(conn, 400)
end
test "with proper permissions, valid password and blank email", %{
test "with proper permissions, valid password and blank email, when instance requires user email",
%{
conn: conn
} do
orig_account_activation_required =
Pleroma.Config.get([:instance, :account_activation_required])
Pleroma.Config.put([:instance, :account_activation_required], true)
on_exit(fn ->
Pleroma.Config.put(
[:instance, :account_activation_required],
orig_account_activation_required
)
end)
conn =
conn
|> put_req_header("content-type", "multipart/form-data")
@ -313,6 +326,30 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
assert json_response_and_validate_schema(conn, 200) == %{"error" => "Email can't be blank."}
end
test "with proper permissions, valid password and blank email, when instance does not require user email",
%{
conn: conn
} do
orig_account_activation_required =
Pleroma.Config.get([:instance, :account_activation_required])
Pleroma.Config.put([:instance, :account_activation_required], false)
on_exit(fn ->
Pleroma.Config.put(
[:instance, :account_activation_required],
orig_account_activation_required
)
end)
conn =
conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/pleroma/change_email", %{password: "test", email: ""})
assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
end
test "with proper permissions, valid password and non unique email", %{
conn: conn
} do