From dfb28085356aea63c2b0c755d7861946315c3931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 18 Jan 2022 19:29:21 +0100 Subject: [PATCH] Birth dates: Add tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/user.ex | 12 ++++- lib/pleroma/user/query.ex | 2 + lib/pleroma/web/api_spec/schemas/account.ex | 2 +- .../web/mastodon_api/views/account_view.ex | 26 ++++++--- .../controllers/account_controller.ex | 2 +- test/pleroma/user_test.exs | 48 +++++++++++++++++ .../controllers/account_controller_test.exs | 54 +++++++++++++++++++ .../mastodon_api/update_credentials_test.exs | 20 +++++++ .../controllers/account_controller_test.exs | 53 ++++++++++++++++++ 9 files changed, 210 insertions(+), 9 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index cb7740292..5655762ac 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -535,7 +535,8 @@ defmodule Pleroma.User do :actor_type, :accepts_chat_messages, :disclose_client, - :birth_date + :birth_date, + :hide_birth_date ] ) |> validate_min_age() @@ -2583,4 +2584,13 @@ defmodule Pleroma.User do _ -> {:error, user} end end + + def get_friends_birthdays_query(%User{} = user, day, month) do + User.Query.build(%{ + friends: user, + deactivated: false, + birth_day: day, + birth_month: month + }) + end end diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index 9e594013a..ea1706c9e 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -234,12 +234,14 @@ defmodule Pleroma.User.Query do defp compose_query({:birth_day, day}, query) do query + |> where([u], u.hide_birth_date == false) |> where([u], not is_nil(u.birth_date)) |> where([u], fragment("date_part('day', ?)", u.birth_date) == ^day) end defp compose_query({:birth_month, month}, query) do query + |> where([u], u.hide_birth_date == false) |> where([u], not is_nil(u.birth_date)) |> where([u], fragment("date_part('month', ?)", u.birth_date) == ^month) end diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex index a20964342..3796bccfd 100644 --- a/lib/pleroma/web/api_spec/schemas/account.ex +++ b/lib/pleroma/web/api_spec/schemas/account.ex @@ -54,7 +54,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do description: "whether the user account is waiting on email confirmation to be activated" }, - hide_birth_date: %Schema{type: :boolean}, + hide_birth_date: %Schema{type: :boolean, nullable: true}, hide_favorites: %Schema{type: :boolean}, hide_followers_count: %Schema{ type: :boolean, diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index f42d13aa6..07b3c776f 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -249,11 +249,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do nil end - birth_date = - if !user.hide_birth_date or opts[:for] == user, - do: user.birth_date, - else: nil - %{ id: to_string(user.id), username: username_from_nickname(user.nickname), @@ -303,7 +298,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do background_image: image_url(user.background) |> MediaProxy.url(), accepts_chat_messages: user.accepts_chat_messages, favicon: favicon, - birth_date: birth_date + birth_date: user.birth_date, + hide_birth_date: user.hide_birth_date } } |> maybe_put_role(user, opts[:for]) @@ -317,6 +313,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do |> maybe_put_unread_conversation_count(user, opts[:for]) |> maybe_put_unread_notification_count(user, opts[:for]) |> maybe_put_email_address(user, opts[:for]) + |> maybe_hide_birth_date(user, opts[:for]) end defp username_from_nickname(string) when is_binary(string) do @@ -438,6 +435,23 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do defp maybe_put_email_address(data, _, _), do: data + defp maybe_hide_birth_date(data, %User{id: user_id}, %User{id: user_id}) do + data + end + + defp maybe_hide_birth_date(data, %User{hide_birth_date: true}, _) do + data + |> Kernel.pop_in([:pleroma, :birth_date]) + |> Kernel.pop_in([:pleroma, :hide_birth_date]) + |> elem(1) + end + + defp maybe_hide_birth_date(data, _, _) do + data + |> Kernel.pop_in([:pleroma, :hide_birth_date]) + |> elem(1) + end + defp image_url(%{"url" => [%{"href" => href} | _]}), do: href defp image_url(_), do: nil end diff --git a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex index 4a833275e..20697fa46 100644 --- a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex @@ -146,7 +146,7 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do @doc "GET /api/v1/pleroma/birthday_reminders" def birthdays(%{assigns: %{user: %User{} = user}} = conn, %{day: day, month: month} = _params) do birthdays = - User.Query.build(%{friends: user, deactivated: false, birth_day: day, birth_month: month}) + User.get_friends_birthdays_query(user, day, month) |> Pleroma.Repo.all() conn diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 0345a9290..263c2b274 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -755,6 +755,54 @@ defmodule Pleroma.UserTest do end end + describe "user registration, with :birth_date_required and :birth_date_min_age" do + @full_user_data %{ + bio: "A guy", + name: "my name", + nickname: "nick", + password: "test", + password_confirmation: "test", + email: "email@example.com" + } + + setup do + clear_config([:instance, :birth_date_required], true) + clear_config([:instance, :birth_date_min_age], 18 * 365) + end + + test "it passes when correct birth date is provided" do + today = Date.utc_today() + birth_date = Date.add(today, -19 * 365) + + params = + @full_user_data + |> Map.put(:birth_date, birth_date) + + changeset = User.register_changeset(%User{}, params) + + assert changeset.valid? + end + + test "it fails when birth date is not provided" do + changeset = User.register_changeset(%User{}, @full_user_data) + + refute changeset.valid? + end + + test "it fails when provided invalid birth date" do + today = Date.utc_today() + birth_date = Date.add(today, -17 * 365) + + params = + @full_user_data + |> Map.put(:birth_date, birth_date) + + changeset = User.register_changeset(%User{}, params) + + refute changeset.valid? + end + end + describe "get_or_fetch/1" do test "gets an existing user by nickname" do user = insert(:user) diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index bba528d83..d8ebd98cc 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -1586,6 +1586,60 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do end end + describe "create account with required birth date" do + setup %{conn: conn} do + clear_config([:instance, :birth_date_required], true) + clear_config([:instance, :birth_date_min_age], 18 * 365) + + app_token = insert(:oauth_token, user: nil) + + conn = + conn + |> put_req_header("authorization", "Bearer " <> app_token.token) + |> put_req_header("content-type", "multipart/form-data") + + [conn: conn] + end + + test "creates an account if provided valid birth date", %{conn: conn} do + birth_date = + Date.utc_today() + |> Date.add(-19 * 365) + |> Date.to_string() + + params = %{ + username: "mkljczk", + email: "mkljczk@example.org", + password: "dupa.8", + agreement: true, + birth_date: birth_date + } + + res = + conn + |> post("/api/v1/accounts", params) + + assert json_response_and_validate_schema(res, 200) + end + + test "returns an error if missing birth date", %{conn: conn} do + params = %{ + username: "mkljczk", + email: "mkljczk@example.org", + password: "dupa.8", + agreement: true + } + + res = + conn + |> post("/api/v1/accounts", params) + + assert json_response_and_validate_schema(res, 400) == %{ + "error" => "{\"birth_date\":[\"can't be blank\"]}" + } + end + end + describe "GET /api/v1/accounts/:id/lists - account_lists" do test "returns lists to which the account belongs" do %{user: user, conn: conn} = oauth_access(["read:lists"]) diff --git a/test/pleroma/web/mastodon_api/update_credentials_test.exs b/test/pleroma/web/mastodon_api/update_credentials_test.exs index 1d2027899..e89f597a9 100644 --- a/test/pleroma/web/mastodon_api/update_credentials_test.exs +++ b/test/pleroma/web/mastodon_api/update_credentials_test.exs @@ -370,6 +370,26 @@ defmodule Pleroma.Web.MastodonAPI.UpdateCredentialsTest do ] end + test "updates birth date", %{conn: conn, user: user} do + res = + patch(conn, "/api/v1/accounts/update_credentials", %{ + "birth_date" => "2001-02-12" + }) + + assert user_data = json_response_and_validate_schema(res, 200) + assert user_data["pleroma"]["birth_date"] == "2001-02-12" + end + + test "updates the user's hide_birth_date status", %{conn: conn} do + res = + patch(conn, "/api/v1/accounts/update_credentials", %{ + "hide_birth_date" => true + }) + + assert user_data = json_response_and_validate_schema(res, 200) + assert user_data["pleroma"]["hide_birth_date"] == true + end + test "emojis in fields labels", %{conn: conn} do fields = [ %{"name" => ":firefox:", "value" => "is best 2hu"}, diff --git a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs index d9aa8ce55..5b74bb0b2 100644 --- a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs @@ -304,4 +304,57 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"} end end + + describe "birthday reminders" do + test "returns a list of friends having birthday on specified day" do + %{user: user, conn: conn} = oauth_access(["read:accounts"]) + + %{id: id1} = + user1 = + insert(:user, %{ + birth_date: "2001-02-12" + }) + + user2 = + insert(:user, %{ + birth_date: "2001-02-14" + }) + + user3 = insert(:user) + + CommonAPI.follow(user, user1) + CommonAPI.follow(user, user2) + CommonAPI.follow(user, user3) + + [%{"id" => ^id1}] = + conn + |> get("/api/v1/pleroma/birthday_reminders?day=12&month=2") + |> json_response_and_validate_schema(:ok) + end + + test "the list doesn't list friends with hidden birth date" do + %{user: user, conn: conn} = oauth_access(["read:accounts"]) + + user1 = + insert(:user, %{ + birth_date: "2001-02-12", + hide_birth_date: true + }) + + %{id: id2} = + user2 = + insert(:user, %{ + birth_date: "2001-02-12", + hide_birth_date: false + }) + + CommonAPI.follow(user, user1) + CommonAPI.follow(user, user2) + + [%{"id" => ^id2}] = + conn + |> get("/api/v1/pleroma/birthday_reminders?day=12&month=2") + |> json_response_and_validate_schema(:ok) + end + end end