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()
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
)
82 media_entry
= fixture_media_entry(uploader
=user
.id, state
=u
'processed')
84 media_entry_id
= media_entry
.id
86 subscription
= fixture_comment_subscription(media_entry
)
88 subscription_id
= subscription
.id
90 media_uri_id
= '/u/{0}/m/{1}/'.format(user
.username
,
92 media_uri_slug
= '/u/{0}/m/{1}/'.format(user
.username
,
96 media_uri_id
+ 'comment/add/',
98 'comment_content': u
'Test comment #42'
102 notifications
= Notification
.query
.filter_by(
103 user_id
=user
.id).all()
105 assert len(notifications
) == 1
107 notification
= notifications
[0]
109 assert type(notification
) == CommentNotification
110 assert notification
.seen
== False
111 assert notification
.user_id
== user
.id
112 assert notification
.subject
.get_author
.id == self
.test_user
.id
113 assert notification
.subject
.content
== u
'Test comment #42'
115 if wants_email
== True:
116 assert mail
.EMAIL_TEST_MBOX_INBOX
== [
117 {'from': 'notice@mediagoblin.example.org',
118 'message': 'Content-Type: text/plain; \
119 charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: \
120 base64\nSubject: GNU MediaGoblin - chris commented on your \
121 post\nFrom: notice@mediagoblin.example.org\nTo: \
122 otherperson@example.com\n\nSGkgb3RoZXJwZXJzb24sCmNocmlzIGNvbW1lbnRlZCBvbiB5b3VyIHBvc3QgKGh0dHA6Ly9sb2Nh\nbGhvc3Q6ODAvdS9vdGhlcnBlcnNvbi9tL3NvbWUtdGl0bGUvYy8xLyNjb21tZW50KSBhdCBHTlUg\nTWVkaWFHb2JsaW4KClRlc3QgY29tbWVudCAjNDIKCkdOVSBNZWRpYUdvYmxpbg==\n',
123 'to': [u
'otherperson@example.com']}]
125 assert mail
.EMAIL_TEST_MBOX_INBOX
== []
127 # Save the ids temporarily because of DetachedInstanceError
128 notification_id
= notification
.id
129 comment_id
= notification
.subject
.id
132 self
.login('otherperson', 'nosreprehto')
134 self
.test_app
.get(media_uri_slug
+ '/c/{0}/'.format(comment_id
))
136 notification
= Notification
.query
.filter_by(id=notification_id
).first()
138 assert notification
.seen
== True
140 self
.test_app
.get(media_uri_slug
+ '/notifications/silence/')
142 subscription
= CommentSubscription
.query
.filter_by(id=subscription_id
)\
145 assert subscription
.notify
== False
147 notifications
= Notification
.query
.filter_by(
148 user_id
=user_id
).all()
150 # User should not have been notified
151 assert len(notifications
) == 1