blob: 997d39063973b0ef57808fc6250aaa94eccca7b7 (
plain)
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
|
from django.core.mail.backends.base import BaseEmailBackend
class EmailBackend(BaseEmailBackend):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.test_outbox = []
def send_messages(self, email_messages):
# Messages are stored in an instance variable for testing.
self.test_outbox.extend(email_messages)
return len(email_messages)
class OptionsCapturingBackend(BaseEmailBackend):
"""Capture init kwargs and sent messages for use in test assertions.
Test cases using this backend _must_ ensure reset() is called::
def test_something(self):
self.addCleanup(OptionsCapturingBackend.reset)
...
Failing to call reset() will cause unexpected behavior in other tests that
use the OptionsCapturingBackend.
"""
init_kwargs = []
sent_messages = []
@classmethod
def reset(cls):
cls.init_kwargs = []
cls.sent_messages = []
def __init__(self, **kwargs):
self.init_kwargs.append(kwargs)
if "alias" in kwargs:
super().__init__(alias=kwargs["alias"])
else:
super().__init__()
def send_messages(self, email_messages):
self.sent_messages.extend(email_messages)
return len(email_messages)
class FailingEmailBackend(OptionsCapturingBackend):
"""Raise on send_messages(), or do nothing if fail_silently is set.
Test cases using this backend _must_ ensure reset() is called::
def test_something(self):
self.addCleanup(FailingEmailBackend.reset)
...
Failing to call reset() will cause unexpected behavior in other tests that
use the FailingEmailBackend.
"""
init_kwargs = []
sent_messages = []
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.fail_silently = kwargs.get("fail_silently", False)
def send_messages(self, email_messages):
if self.fail_silently:
return 0
raise ValueError("FailingEmailBackend is doomed to fail.")
|