1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from mediagoblin
.tools
import template
, mail
23 from mediagoblin
.db
.models
import Notification
, CommentNotification
, \
25 from mediagoblin
.db
.base
import Session
27 from mediagoblin
.notifications
import mark_comment_notification_seen
29 from mediagoblin
.tests
.tools
import fixture_add_comment
, \
30 fixture_media_entry
, fixture_add_user
, \
31 fixture_comment_subscription
34 class TestNotifications
:
35 @pytest.fixture(autouse
=True)
36 def setup(self
, test_app
):
37 self
.test_app
= test_app
39 # TODO: Possibly abstract into a decorator like:
40 # @as_authenticated_user('chris')
41 self
.test_user
= fixture_add_user(privileges
=[u
'active',u
'commenter'])
43 self
.current_user
= None
47 def login(self
, username
=u
'chris', password
=u
'toast'):
48 response
= self
.test_app
.post(
51 'password': password
})
55 assert urlparse
.urlsplit(response
.location
)[2] == '/'
56 assert 'mediagoblin/root.html' in template
.TEMPLATE_TEST_CONTEXT
58 ctx
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/root.html']
60 assert Session
.merge(ctx
['request'].user
).username
== username
62 self
.current_user
= ctx
['request'].user
65 self
.test_app
.get('/auth/logout/')
66 self
.current_user
= None
68 @pytest.mark
.parametrize('wants_email', [True, False])
69 def test_comment_notification(self
, wants_email
):
72 - if a notification is created when posting a comment on
73 another users media entry.
74 - that the comment data is consistent and exists.
77 user
= fixture_add_user('otherperson', password
='nosreprehto',
78 wants_comment_notification
=wants_email
,
79 privileges
=[u
'active',u
'commenter'])
81 assert user
.wants_comment_notification
== wants_email
85 media_entry
= fixture_media_entry(uploader
=user
.id, state
=u
'processed')
87 media_entry_id
= media_entry
.id
89 subscription
= fixture_comment_subscription(media_entry
)
91 subscription_id
= subscription
.id
93 media_uri_id
= '/u/{0}/m/{1}/'.format(user
.username
,
95 media_uri_slug
= '/u/{0}/m/{1}/'.format(user
.username
,
99 media_uri_id
+ 'comment/add/',
101 'comment_content': u
'Test comment #42'
105 notifications
= Notification
.query
.filter_by(
106 user_id
=user
.id).all()
108 assert len(notifications
) == 1
110 notification
= notifications
[0]
112 assert type(notification
) == CommentNotification
113 assert notification
.seen
== False
114 assert notification
.user_id
== user
.id
115 assert notification
.subject
.get_author
.id == self
.test_user
.id
116 assert notification
.subject
.content
== u
'Test comment #42'
118 if wants_email
== True:
119 assert mail
.EMAIL_TEST_MBOX_INBOX
== [
120 {'from': 'notice@mediagoblin.example.org',
121 'message': 'Content-Type: text/plain; \
122 charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: \
123 base64\nSubject: GNU MediaGoblin - chris commented on your \
124 post\nFrom: notice@mediagoblin.example.org\nTo: \
125 otherperson@example.com\n\nSGkgb3RoZXJwZXJzb24sCmNocmlzIGNvbW1lbnRlZCBvbiB5b3VyIHBvc3QgKGh0dHA6Ly9sb2Nh\nbGhvc3Q6ODAvdS9vdGhlcnBlcnNvbi9tL3NvbWUtdGl0bGUvYy8xLyNjb21tZW50KSBhdCBHTlUg\nTWVkaWFHb2JsaW4KClRlc3QgY29tbWVudCAjNDIKCkdOVSBNZWRpYUdvYmxpbg==\n',
126 'to': [u
'otherperson@example.com']}]
128 assert mail
.EMAIL_TEST_MBOX_INBOX
== []
131 # Save the ids temporarily because of DetachedInstanceError
132 notification_id
= notification
.id
133 comment_id
= notification
.subject
.id
136 self
.login('otherperson', 'nosreprehto')
138 self
.test_app
.get(media_uri_slug
+ '/c/{0}/'.format(comment_id
))
140 notification
= Notification
.query
.filter_by(id=notification_id
).first()
142 assert notification
.seen
== True
144 self
.test_app
.get(media_uri_slug
+ '/notifications/silence/')
146 subscription
= CommentSubscription
.query
.filter_by(id=subscription_id
)\
149 assert subscription
.notify
== False
151 notifications
= Notification
.query
.filter_by(
152 user_id
=user_id
).all()
154 # User should not have been notified
155 assert len(notifications
) == 1
157 def test_mark_all_comment_notifications_seen(self
):
158 """ Test that mark_all_comments_seen works"""
160 user
= fixture_add_user('otherperson', password
='nosreprehto',
161 privileges
=[u
'active'])
163 media_entry
= fixture_media_entry(uploader
=user
.id, state
=u
'processed')
165 fixture_comment_subscription(media_entry
)
167 media_uri_id
= '/u/{0}/m/{1}/'.format(user
.username
,
172 media_uri_id
+ 'comment/add/',
174 'comment_content': u
'Test comment #43'
179 media_uri_id
+ 'comment/add/',
181 'comment_content': u
'Test comment #44'
185 notifications
= Notification
.query
.filter_by(
186 user_id
=user
.id).all()
188 assert len(notifications
) == 2
190 # both comments should not be marked seen
191 assert notifications
[0].seen
== False
192 assert notifications
[1].seen
== False
194 # login with other user to mark notifications seen
196 self
.login('otherperson', 'nosreprehto')
198 # mark all comment notifications seen
199 res
= self
.test_app
.get('/notifications/comments/mark_all_seen/')
202 assert urlparse
.urlsplit(res
.location
)[2] == '/'
204 notifications
= Notification
.query
.filter_by(
205 user_id
=user
.id).all()
207 # both notifications should be marked seen
208 assert notifications
[0].seen
== True
209 assert notifications
[1].seen
== True