Allow users to remove their emails if instance does not need email to register
authorTusooa Zhu <tusooa@kazv.moe>
Mon, 6 Sep 2021 00:56:16 +0000 (20:56 -0400)
committerTusooa Zhu <tusooa@kazv.moe>
Mon, 6 Sep 2021 01:27:02 +0000 (21:27 -0400)
CHANGELOG.md
lib/pleroma/user.ex
lib/pleroma/web/api_spec/operations/twitter_util_operation.ex
test/pleroma/user_test.exs
test/pleroma/web/twitter_api/util_controller_test.exs

index 00335065850416d5ad5781cfb41a50ecdf454cc8..edd8b911af067cfa13a60c52fa292c87321e2900 100644 (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
 
index 62506f37ad6d29ef6e10ffb86cdb14651f9e8667..04deb3f7930881c2f4d277ce2310014c93db117a 100644 (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()
index 879b2227ebd6401deac6cecec48742a40fd13599..ebcfd3be231e661871a1409147331bf8fd228875 100644 (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"}
       }
     }
index c2ed2c2a3f2b4d47ed07713d72ddaed29beabf97..1e6448495885107703c96780dcd9824af362a6f1 100644 (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
index f030483d80b1e57ed587044b6cb45b7a9dd82a69..67bd4b27f4531f9d43dae6087b73e15b0e5ecc05 100644 (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", %{
-      conn: conn
-    } do
+    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