4
4
import django_dynamic_fixture as fixture
5
5
from django .test import TestCase
6
6
from django .test .utils import override_settings
7
- from django .contrib .auth .models import User
7
+ from django .contrib .auth .models import User , AnonymousUser
8
+ from messages_extends .models import Message as PersistentMessage
8
9
9
10
from readthedocs .notifications import Notification
10
11
from readthedocs .notifications .backends import EmailBackend , SiteBackend
12
+ from readthedocs .notifications .constants import ERROR
11
13
from readthedocs .builds .models import Build
12
14
13
15
22
24
class NotificationTests (TestCase ):
23
25
24
26
def test_notification_custom (self , send , render_to_string ):
27
+ render_to_string .return_value = 'Test'
28
+
25
29
class TestNotification (Notification ):
26
30
name = 'foo'
27
31
subject = 'This is {{ foo.id }}'
@@ -54,23 +58,36 @@ class NotificationBackendTests(TestCase):
54
58
55
59
@mock .patch ('readthedocs.notifications.backends.send_email' )
56
60
def test_email_backend (self , send_email , render_to_string ):
61
+ render_to_string .return_value = 'Test'
62
+
57
63
class TestNotification (Notification ):
58
64
name = 'foo'
59
65
subject = 'This is {{ foo.id }}'
60
66
context_object_name = 'foo'
67
+ level = ERROR
61
68
62
69
build = fixture .get (Build )
63
70
req = mock .MagicMock ()
64
- notify = TestNotification (object = build , request = req )
71
+ user = fixture .get (User )
72
+ notify = TestNotification (object = build , request = req , user = user )
65
73
backend = EmailBackend (request = req )
66
74
backend .send (notify )
67
75
76
+ self .assertEqual (render_to_string .call_count , 1 )
68
77
send_email .assert_has_calls ([
69
- mock .call (level = 21 , request = req , message = mock .ANY , extra_tags = '' )
78
+ mock .call (
79
+ request = mock .ANY ,
80
+ template = 'core/email/common.txt' ,
81
+ context = {'content' : 'Test' },
82
+ subject = u'This is 1' ,
83
+ template_html = 'core/email/common.html' ,
84
+ recipient = user .email ,
85
+ )
70
86
])
71
87
72
- @mock .patch ('readthedocs.notifications.backends.add_message' )
73
- def test_email_backend (self , add_message , render_to_string ):
88
+ def test_message_backend (self , render_to_string ):
89
+ render_to_string .return_value = 'Test'
90
+
74
91
class TestNotification (Notification ):
75
92
name = 'foo'
76
93
subject = 'This is {{ foo.id }}'
@@ -83,7 +100,33 @@ class TestNotification(Notification):
83
100
backend = SiteBackend (request = req )
84
101
backend .send (notify )
85
102
86
- add_message .assert_has_calls ([
87
- mock .call (level = 21 , request = req , message = mock .ANY , extra_tags = '' ,
88
- user = user )
89
- ])
103
+ self .assertEqual (render_to_string .call_count , 1 )
104
+ self .assertEqual (PersistentMessage .objects .count (), 1 )
105
+
106
+ message = PersistentMessage .objects .first ()
107
+ self .assertEqual (message .user , user )
108
+
109
+ def test_message_anonymous_user (self , render_to_string ):
110
+ """Anonymous user still throwns exception on persistent messages"""
111
+ render_to_string .return_value = 'Test'
112
+
113
+ class TestNotification (Notification ):
114
+ name = 'foo'
115
+ subject = 'This is {{ foo.id }}'
116
+ context_object_name = 'foo'
117
+
118
+ build = fixture .get (Build )
119
+ user = AnonymousUser ()
120
+ req = mock .MagicMock ()
121
+ notify = TestNotification (object = build , request = req , user = user )
122
+ backend = SiteBackend (request = req )
123
+
124
+ self .assertEqual (PersistentMessage .objects .count (), 0 )
125
+
126
+ # We should never be adding persistent messages for anonymous users.
127
+ # Make sure message_extends sitll throws an exception here
128
+ with self .assertRaises (NotImplementedError ):
129
+ backend .send (notify )
130
+
131
+ self .assertEqual (render_to_string .call_count , 1 )
132
+ self .assertEqual (PersistentMessage .objects .count (), 0 )
0 commit comments