diff options
Diffstat (limited to 'test/pleroma')
-rw-r--r-- | test/pleroma/notification_test.exs | 90 | ||||
-rw-r--r-- | test/pleroma/user_test.exs | 25 | ||||
-rw-r--r-- | test/pleroma/web/mastodon_api/update_credentials_test.exs | 32 | ||||
-rw-r--r-- | test/pleroma/web/mastodon_api/views/account_view_test.exs | 12 | ||||
-rw-r--r-- | test/pleroma/workers/cron/email_mentions_worker_test.exs | 107 |
5 files changed, 252 insertions, 14 deletions
diff --git a/test/pleroma/notification_test.exs b/test/pleroma/notification_test.exs index abf1b0410..28ac57ed2 100644 --- a/test/pleroma/notification_test.exs +++ b/test/pleroma/notification_test.exs @@ -1155,4 +1155,94 @@ defmodule Pleroma.NotificationTest do assert length(Notification.for_user(user)) == 1 end end + + describe "users_ids_with_unread_mentions/0" do + setup do + now = NaiveDateTime.utc_now() + inserted_at = NaiveDateTime.add(now, -61) + + insert(:notification, seen: true, type: "mention", inserted_at: inserted_at) + insert(:notification, type: "follow", inserted_at: inserted_at) + insert(:notification, type: "mention") + mention = insert(:notification, type: "mention", inserted_at: inserted_at) + chat_mention = insert(:notification, type: "pleroma:chat_mention", inserted_at: inserted_at) + + insert(:notification, + type: "mention", + notified_at: now, + inserted_at: inserted_at + ) + + [ + mention: mention, + chat_mention: chat_mention, + now: now + ] + end + + test "when mentions are in the timeframe", %{ + mention: mention, + chat_mention: chat_mention, + now: now + } do + assert Notification.users_ids_with_unread_mentions(NaiveDateTime.add(now, -60)) == [ + mention.user_id, + chat_mention.user_id + ] + end + + test "when mentions are out of the timeframe", %{now: now} do + assert Notification.users_ids_with_unread_mentions(NaiveDateTime.add(now, -62)) == [] + end + end + + describe "for_user_unread_mentions/1" do + setup do + [user, muted, blocked] = insert_list(3, :user) + {:ok, _} = User.mute(user, muted) + {:ok, _} = CommonAPI.post(muted, %{status: "hey @#{user.nickname}"}) + {:ok, _} = User.block(user, blocked) + {:ok, _} = CommonAPI.post(blocked, %{status: "hey @#{user.nickname}"}) + + insert(:notification, type: "mention", user: user) + insert(:notification, type: "pleroma:chat_mention", user: user) + + inserted_at = NaiveDateTime.add(NaiveDateTime.utc_now(), -61) + Repo.update_all(Notification, set: [inserted_at: inserted_at]) + [user: user, max: NaiveDateTime.add(NaiveDateTime.utc_now(), -60)] + end + + test "when mentions are in timeframe, exclude blocks and mutes", %{user: user, max: max} do + assert Repo.aggregate(Notification, :count, :id) == 4 + assert user |> Notification.for_user_unread_mentions(max) |> length() == 2 + end + + test "when mentions are out of the timeframe, exclude blocks and mutes", %{ + user: user, + max: max + } do + assert Notification.for_user_unread_mentions(user, NaiveDateTime.add(max, -2)) == [] + end + + test "respect user notification types", %{user: user, max: max} do + user = + Map.update!( + user, + :email_notifications, + &Map.put(&1, "notifications", ["pleroma:chat_mention"]) + ) + + [mention] = Notification.for_user_unread_mentions(user, max) + assert mention.type == "pleroma:chat_mention" + end + end + + test "update_notified_at/1" do + notifs = insert_list(2, :notification) + + assert {2, nil} = + notifs + |> Enum.map(& &1.id) + |> Notification.update_notified_at() + end end diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index d81c1b8eb..060d14101 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -2239,18 +2239,19 @@ defmodule Pleroma.UserTest do end end - describe "update_email_notifications/2" do - setup do - user = insert(:user, email_notifications: %{"digest" => true}) - - {:ok, user: user} - end - - test "Notifications are updated", %{user: user} do - true = user.email_notifications["digest"] - assert {:ok, result} = User.update_email_notifications(user, %{"digest" => false}) - assert result.email_notifications["digest"] == false - end + test "update_email_notifications/2" do + user = insert(:user, email_notifications: %{"digest" => false, "notifications" => []}) + assert user.email_notifications["digest"] == false + assert user.email_notifications["notifications"] == [] + + assert {:ok, result} = + User.update_email_notifications(user, %{ + "digest" => true, + "notifications" => ["mention", "pleroma:chat_mention"] + }) + + assert result.email_notifications["digest"] + assert result.email_notifications["notifications"] == ["mention", "pleroma:chat_mention"] end describe "local_nickname/1" do diff --git a/test/pleroma/web/mastodon_api/update_credentials_test.exs b/test/pleroma/web/mastodon_api/update_credentials_test.exs index cfbe6cf0e..d24264fea 100644 --- a/test/pleroma/web/mastodon_api/update_credentials_test.exs +++ b/test/pleroma/web/mastodon_api/update_credentials_test.exs @@ -206,6 +206,38 @@ defmodule Pleroma.Web.MastodonAPI.UpdateCredentialsTest do assert user_data["source"]["pleroma"]["no_rich_text"] == true end + test "updates the user's email_notifications setting", %{conn: conn} do + resp = + patch(conn, "/api/v1/accounts/update_credentials", %{ + email_notifications: %{ + "digest" => true, + "notifications" => [] + } + }) + + assert user_data = json_response_and_validate_schema(resp, 200) + + assert user_data["pleroma"]["email_notifications"] == %{ + "digest" => true, + "notifications" => [] + } + + resp = + patch(conn, "/api/v1/accounts/update_credentials", %{ + email_notifications: %{ + "digest" => false, + "notifications" => ["mention", "pleroma:chat_mention"] + } + }) + + assert user_data = json_response_and_validate_schema(resp, 200) + + assert user_data["pleroma"]["email_notifications"] == %{ + "digest" => false, + "notifications" => ["mention", "pleroma:chat_mention"] + } + end + test "updates the user's name", %{conn: conn} do conn = patch(conn, "/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"}) diff --git a/test/pleroma/web/mastodon_api/views/account_view_test.exs b/test/pleroma/web/mastodon_api/views/account_view_test.exs index 5373a17c3..d69224319 100644 --- a/test/pleroma/web/mastodon_api/views/account_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/account_view_test.exs @@ -90,7 +90,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do hide_follows_count: false, relationship: %{}, skip_thread_containment: false, - accepts_chat_messages: nil + accepts_chat_messages: nil, + email_notifications: %{ + "digest" => false, + "notifications" => ["mention", "pleroma:chat_mention"] + } } } @@ -190,7 +194,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do hide_follows_count: false, relationship: %{}, skip_thread_containment: false, - accepts_chat_messages: nil + accepts_chat_messages: nil, + email_notifications: %{ + "digest" => false, + "notifications" => ["mention", "pleroma:chat_mention"] + } } } diff --git a/test/pleroma/workers/cron/email_mentions_worker_test.exs b/test/pleroma/workers/cron/email_mentions_worker_test.exs new file mode 100644 index 000000000..3ffe2561d --- /dev/null +++ b/test/pleroma/workers/cron/email_mentions_worker_test.exs @@ -0,0 +1,107 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Workers.Cron.EmailMentionsWorkerTest do + use Pleroma.DataCase + use Oban.Testing, repo: Pleroma.Repo + + import Pleroma.Factory + import Swoosh.TestAssertions + + alias Pleroma.Workers.Cron.EmailMentionsWorker + + setup do + clear_config(EmailMentionsWorker, enabled: true, timeframe: 1) + inserted_at = NaiveDateTime.add(NaiveDateTime.utc_now(), -61) + + n1 = insert(:notification, seen: true, type: "mention", inserted_at: inserted_at) + n2 = insert(:notification, type: "follow", inserted_at: inserted_at) + n3 = insert(:notification, type: "mention") + mention = insert(:notification, type: "mention", inserted_at: inserted_at) + chat_mention = insert(:notification, type: "pleroma:chat_mention", inserted_at: inserted_at) + + n4 = + insert(:notification, + type: "mention", + notified_at: NaiveDateTime.utc_now(), + inserted_at: inserted_at + ) + + [ + mention: mention, + chat_mention: chat_mention, + other_user_ids: [n1.user_id, n2.user_id, n3.user_id, n4.user_id] + ] + end + + test "creates jobs for users", %{ + mention: mention, + chat_mention: chat_mention, + other_user_ids: ids + } do + assert EmailMentionsWorker.perform(%{}) == :ok + + assert_enqueued( + worker: EmailMentionsWorker, + args: %{op: "email_mentions", user_id: mention.user_id} + ) + + assert_enqueued( + worker: EmailMentionsWorker, + args: %{op: "email_mentions", user_id: chat_mention.user_id} + ) + + Enum.each(ids, fn id -> + refute_enqueued(worker: EmailMentionsWorker, args: %{op: "email_mentions", user_id: id}) + end) + + assert Repo.aggregate(Oban.Job, :count, :id) == 2 + + EmailMentionsWorker.perform(%{}) + + # no duplicates + assert Repo.aggregate(Oban.Job, :count, :id) == 2 + end + + test "doesn't create jobs for users without emails", %{mention: mention} do + %{user: user} = Repo.preload(mention, :user) + + user + |> Ecto.Changeset.change(email: nil) + |> Repo.update() + + assert EmailMentionsWorker.perform(%{}) == :ok + + refute_enqueued( + worker: EmailMentionsWorker, + args: %{op: "email_mentions", user_id: mention.user_id} + ) + end + + test "sends emails", %{mention: mention, chat_mention: chat_mention} do + assert EmailMentionsWorker.perform(%{}) == :ok + + mention = Repo.preload(mention, :user) + + assert EmailMentionsWorker.perform(%Oban.Job{ + args: %{"op" => "email_mentions", "user_id" => mention.user_id} + }) == :ok + + assert_email_sent( + to: {mention.user.name, mention.user.email}, + html_body: ~r/here is what you've missed!/i + ) + + chat_mention = Repo.preload(chat_mention, :user) + + assert EmailMentionsWorker.perform(%Oban.Job{ + args: %{"op" => "email_mentions", "user_id" => chat_mention.user_id} + }) == :ok + + assert_email_sent( + to: {chat_mention.user.name, chat_mention.user.email}, + html_body: ~r/here is what you've missed!/i + ) + end +end |