Merge branch 'develop' into 'fix/2412-filters'
[akkoma] / test / pleroma / workers / cron / digest_emails_worker_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do
6 use Pleroma.DataCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13
14 setup do: clear_config([:email_notifications, :digest])
15
16 setup do
17 clear_config([:email_notifications, :digest], %{
18 active: true,
19 inactivity_threshold: 7,
20 interval: 7
21 })
22
23 user = insert(:user)
24
25 date =
26 Timex.now()
27 |> Timex.shift(days: -10)
28 |> Timex.to_naive_datetime()
29
30 user2 = insert(:user, last_digest_emailed_at: date)
31 {:ok, _} = User.switch_email_notifications(user2, "digest", true)
32 CommonAPI.post(user, %{status: "hey @#{user2.nickname}!"})
33
34 {:ok, user2: user2}
35 end
36
37 test "it sends digest emails", %{user2: user2} do
38 Pleroma.Workers.Cron.DigestEmailsWorker.perform(%Oban.Job{})
39 # Performing job(s) enqueued at previous step
40 ObanHelpers.perform_all()
41
42 assert_received {:email, email}
43 assert email.to == [{user2.name, user2.email}]
44 assert email.subject == "Your digest from #{Pleroma.Config.get(:instance)[:name]}"
45 end
46
47 test "it doesn't fail when a user has no email", %{user2: user2} do
48 {:ok, _} = user2 |> Ecto.Changeset.change(%{email: nil}) |> Pleroma.Repo.update()
49
50 Pleroma.Workers.Cron.DigestEmailsWorker.perform(%Oban.Job{})
51 # Performing job(s) enqueued at previous step
52 ObanHelpers.perform_all()
53 end
54 end