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 ## Unreleased
### Changed ### Changed
- Allow users to remove their emails if instance does not need email to register
### Added ### Added

View file

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

View file

@ -121,7 +121,10 @@ defp change_email_request do
type: :object, type: :object,
required: [:email, :password], required: [:email, :password],
properties: %{ 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"} password: %Schema{type: :string, description: "Current password"}
} }
} }

View file

@ -2199,11 +2199,40 @@ test "syncronizes the counters with the remote instance for the follower when en
[user: insert(:user)] [user: insert(:user)]
end 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, "")
assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil) assert {:error, %{errors: [email: {"can't be blank", _}]}} = User.change_email(user, nil)
end 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 test "non unique email returns error", %{user: user} do
%{email: email} = insert(:user) %{email: email} = insert(:user)
@ -2219,6 +2248,25 @@ test "invalid email returns error", %{user: user} do
test "changes email", %{user: user} do test "changes email", %{user: user} do
assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party") assert {:ok, %User{email: "cofe@cofe.party"}} = User.change_email(user, "cofe@cofe.party")
end 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 end
describe "get_cached_by_nickname_or_id" do describe "get_cached_by_nickname_or_id" do

View file

@ -302,9 +302,22 @@ test "with proper permissions, valid password and no email", %{
assert %{"error" => "Missing field: email."} = json_response_and_validate_schema(conn, 400) assert %{"error" => "Missing field: email."} = json_response_and_validate_schema(conn, 400)
end 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 conn: conn
} do } 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 =
conn conn
|> put_req_header("content-type", "multipart/form-data") |> put_req_header("content-type", "multipart/form-data")
@ -313,6 +326,30 @@ test "with proper permissions, valid password and blank email", %{
assert json_response_and_validate_schema(conn, 200) == %{"error" => "Email can't be blank."} assert json_response_and_validate_schema(conn, 200) == %{"error" => "Email can't be blank."}
end 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", %{ test "with proper permissions, valid password and non unique email", %{
conn: conn conn: conn
} do } do