forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_queued_mail.py
169 lines (147 loc) · 5.83 KB
/
test_queued_mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Ported from tests.test_mails
import datetime as dt
import pytest
from unittest import mock
from django.utils import timezone
from waffle.testutils import override_switch
from .factories import UserFactory, NodeFactory
from osf.features import DISABLE_ENGAGEMENT_EMAILS
from osf.models.queued_mail import (
queue_mail, WELCOME_OSF4M,
NO_LOGIN, NO_ADDON, NEW_PUBLIC_PROJECT
)
from website.mails import mails
from website.settings import DOMAIN
@pytest.fixture()
def user():
return UserFactory(is_registered=True)
@pytest.mark.django_db
class TestQueuedMail:
def queue_mail(self, mail, user, send_at=None, **kwargs):
mail = queue_mail(
to_addr=user.username if user else user.username,
send_at=send_at or timezone.now(),
user=user,
mail=mail,
fullname=user.fullname if user else user.username,
**kwargs
)
return mail
@mock.patch('osf.models.queued_mail.send_mail')
def test_no_login_presend_for_active_user(self, mock_mail, user):
mail = self.queue_mail(mail=NO_LOGIN, user=user)
user.date_last_login = timezone.now() + dt.timedelta(seconds=10)
user.save()
assert mail.send_mail() is False
@mock.patch('osf.models.queued_mail.send_mail')
def test_no_login_presend_for_inactive_user(self, mock_mail, user):
mail = self.queue_mail(mail=NO_LOGIN, user=user)
user.date_last_login = timezone.now() - dt.timedelta(weeks=10)
user.save()
assert timezone.now() - dt.timedelta(days=1) > user.date_last_login
assert bool(mail.send_mail()) is True
@mock.patch('osf.models.queued_mail.send_mail')
def test_no_addon_presend(self, mock_mail, user):
mail = self.queue_mail(mail=NO_ADDON, user=user)
assert mail.send_mail() is True
@mock.patch('osf.models.queued_mail.send_mail')
def test_new_public_project_presend_for_no_project(self, mock_mail, user):
mail = self.queue_mail(
mail=NEW_PUBLIC_PROJECT,
user=user,
project_title='Oh noes',
nid='',
)
assert bool(mail.send_mail()) is False
@mock.patch('osf.models.queued_mail.send_mail')
def test_new_public_project_presend_success(self, mock_mail, user):
node = NodeFactory(is_public=True)
mail = self.queue_mail(
mail=NEW_PUBLIC_PROJECT,
user=user,
project_title='Oh yass',
nid=node._id
)
assert bool(mail.send_mail()) is True
@mock.patch('osf.models.queued_mail.send_mail')
def test_welcome_osf4m_presend(self, mock_mail, user):
user.date_last_login = timezone.now() - dt.timedelta(days=13)
user.save()
mail = self.queue_mail(
mail=WELCOME_OSF4M,
user=user,
conference='Buttjamz conference',
fid='',
domain=DOMAIN
)
assert bool(mail.send_mail()) is True
assert mail.data['downloads'] == 0
@mock.patch('osf.models.queued_mail.send_mail')
def test_finding_other_emails_sent_to_user(self, mock_mail, user):
mail = self.queue_mail(
user=user,
mail=NO_ADDON,
)
assert len(mail.find_sent_of_same_type_and_user()) == 0
mail.send_mail()
assert len(mail.find_sent_of_same_type_and_user()) == 1
@mock.patch('osf.models.queued_mail.send_mail')
def test_user_is_active(self, mock_mail, user):
mail = self.queue_mail(
user=user,
mail=NO_ADDON,
)
assert bool(mail.send_mail()) is True
@mock.patch('osf.models.queued_mail.send_mail')
def test_user_is_not_active_no_password(self, mock_mail):
user = UserFactory.build()
user.set_unusable_password()
user.save()
mail = self.queue_mail(
user=user,
mail=NO_ADDON,
)
assert mail.send_mail() is False
@mock.patch('osf.models.queued_mail.send_mail')
def test_user_is_not_active_not_registered(self, mock_mail):
user = UserFactory(is_registered=False)
mail = self.queue_mail(
user=user,
mail=NO_ADDON,
)
assert mail.send_mail() is False
@mock.patch('osf.models.queued_mail.send_mail')
def test_user_is_not_active_is_merged(self, mock_mail):
other_user = UserFactory()
user = UserFactory(merged_by=other_user)
mail = self.queue_mail(
user=user,
mail=NO_ADDON,
)
assert mail.send_mail() is False
@mock.patch('osf.models.queued_mail.send_mail')
def test_user_is_not_active_is_disabled(self, mock_mail):
user = UserFactory(date_disabled=timezone.now())
mail = self.queue_mail(
user=user,
mail=NO_ADDON,
)
assert mail.send_mail() is False
@mock.patch('osf.models.queued_mail.send_mail')
def test_user_is_not_active_is_not_confirmed(self, mock_mail):
user = UserFactory(date_confirmed=None)
mail = self.queue_mail(
user=user,
mail=NO_ADDON,
)
assert mail.send_mail() is False
def test_disabled_queued_emails_not_sent_if_switch_active(self, user):
with override_switch(DISABLE_ENGAGEMENT_EMAILS, active=True):
assert self.queue_mail(mail=NO_ADDON, user=user) is False
assert self.queue_mail(mail=NO_LOGIN, user=user) is False
assert self.queue_mail(mail=WELCOME_OSF4M, user=user) is False
assert self.queue_mail(mail=NEW_PUBLIC_PROJECT, user=user) is False
def test_disabled_triggered_emails_not_sent_if_switch_active(self):
with override_switch(DISABLE_ENGAGEMENT_EMAILS, active=True):
assert mails.send_mail(to_addr='', mail=mails.WELCOME) is False
assert mails.send_mail(to_addr='', mail=mails.WELCOME_OSF4I) is False