Change user.deactivated field to user.is_active
[akkoma] / test / mix / tasks / pleroma / email_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 Mix.Tasks.Pleroma.EmailTest do
6 use Pleroma.DataCase
7
8 import Swoosh.TestAssertions
9
10 alias Pleroma.Config
11 alias Pleroma.Tests.ObanHelpers
12
13 import Pleroma.Factory
14
15 setup_all do
16 Mix.shell(Mix.Shell.Process)
17
18 on_exit(fn ->
19 Mix.shell(Mix.Shell.IO)
20 end)
21
22 :ok
23 end
24
25 setup do: clear_config([Pleroma.Emails.Mailer, :enabled], true)
26 setup do: clear_config([:instance, :account_activation_required], true)
27
28 describe "pleroma.email test" do
29 test "Sends test email with no given address" do
30 mail_to = Config.get([:instance, :email])
31
32 :ok = Mix.Tasks.Pleroma.Email.run(["test"])
33
34 ObanHelpers.perform_all()
35
36 assert_receive {:mix_shell, :info, [message]}
37 assert message =~ "Test email has been sent"
38
39 assert_email_sent(
40 to: mail_to,
41 html_body: ~r/a test email was requested./i
42 )
43 end
44
45 test "Sends test email with given address" do
46 mail_to = "hewwo@example.com"
47
48 :ok = Mix.Tasks.Pleroma.Email.run(["test", "--to", mail_to])
49
50 ObanHelpers.perform_all()
51
52 assert_receive {:mix_shell, :info, [message]}
53 assert message =~ "Test email has been sent"
54
55 assert_email_sent(
56 to: mail_to,
57 html_body: ~r/a test email was requested./i
58 )
59 end
60
61 test "Sends confirmation emails" do
62 local_user1 =
63 insert(:user, %{
64 confirmation_pending: true,
65 confirmation_token: "mytoken",
66 is_active: true,
67 email: "local1@pleroma.com",
68 local: true
69 })
70
71 local_user2 =
72 insert(:user, %{
73 confirmation_pending: true,
74 confirmation_token: "mytoken",
75 is_active: true,
76 email: "local2@pleroma.com",
77 local: true
78 })
79
80 :ok = Mix.Tasks.Pleroma.Email.run(["resend_confirmation_emails"])
81
82 ObanHelpers.perform_all()
83
84 assert_email_sent(to: {local_user1.name, local_user1.email})
85 assert_email_sent(to: {local_user2.name, local_user2.email})
86 end
87
88 test "Does not send confirmation email to inappropriate users" do
89 # confirmed user
90 insert(:user, %{
91 confirmation_pending: false,
92 confirmation_token: "mytoken",
93 is_active: true,
94 email: "confirmed@pleroma.com",
95 local: true
96 })
97
98 # remote user
99 insert(:user, %{
100 is_active: true,
101 email: "remote@not-pleroma.com",
102 local: false
103 })
104
105 # deactivated user =
106 insert(:user, %{
107 is_active: false,
108 email: "deactivated@pleroma.com",
109 local: false
110 })
111
112 # invisible user
113 insert(:user, %{
114 is_active: true,
115 email: "invisible@pleroma.com",
116 local: true,
117 invisible: true
118 })
119
120 :ok = Mix.Tasks.Pleroma.Email.run(["resend_confirmation_emails"])
121
122 ObanHelpers.perform_all()
123
124 refute_email_sent()
125 end
126 end
127 end