This was a very small update, I'm hoping to rebase after this to solve some
[mediagoblin.git] / mediagoblin / tests / test_notifications.py
CommitLineData
2d7b6bde
JW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
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.
8#
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.
13#
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/>.
16
17import pytest
18
19import urlparse
20
21from mediagoblin.tools import template, mail
22
23from mediagoblin.db.models import Notification, CommentNotification, \
24 CommentSubscription
25from mediagoblin.db.base import Session
26
27from mediagoblin.notifications import mark_comment_notification_seen
28
29from mediagoblin.tests.tools import fixture_add_comment, \
30 fixture_media_entry, fixture_add_user, \
31 fixture_comment_subscription
32
33
34class TestNotifications:
35 @pytest.fixture(autouse=True)
36 def setup(self, test_app):
37 self.test_app = test_app
38
39 # TODO: Possibly abstract into a decorator like:
40 # @as_authenticated_user('chris')
41 self.test_user = fixture_add_user()
42
43 self.current_user = None
44
45 self.login()
46
47 def login(self, username=u'chris', password=u'toast'):
48 response = self.test_app.post(
49 '/auth/login/', {
50 'username': username,
51 'password': password})
52
53 response.follow()
54
55 assert urlparse.urlsplit(response.location)[2] == '/'
56 assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
57
58 ctx = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
59
60 assert Session.merge(ctx['request'].user).username == username
61
62 self.current_user = ctx['request'].user
63
64 def logout(self):
65 self.test_app.get('/auth/logout/')
66 self.current_user = None
67
68 @pytest.mark.parametrize('wants_email', [True, False])
69 def test_comment_notification(self, wants_email):
70 '''
71 Test
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.
75
76 '''
77 user = fixture_add_user('otherperson', password='nosreprehto',
78 wants_comment_notification=wants_email)
79
80 user_id = user.id
81
82 media_entry = fixture_media_entry(uploader=user.id, state=u'processed')
83
84 media_entry_id = media_entry.id
85
86 subscription = fixture_comment_subscription(media_entry)
87
88 subscription_id = subscription.id
89
90 media_uri_id = '/u/{0}/m/{1}/'.format(user.username,
91 media_entry.id)
92 media_uri_slug = '/u/{0}/m/{1}/'.format(user.username,
93 media_entry.slug)
94
95 self.test_app.post(
96 media_uri_id + 'comment/add/',
97 {
98 'comment_content': u'Test comment #42'
99 }
100 )
101
102 notifications = Notification.query.filter_by(
103 user_id=user.id).all()
104
105 assert len(notifications) == 1
106
107 notification = notifications[0]
108
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'
114
115 if wants_email == True:
116 assert mail.EMAIL_TEST_MBOX_INBOX == [
117 {'from': 'notice@mediagoblin.example.org',
118 'message': 'Content-Type: text/plain; \
119charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: \
120base64\nSubject: GNU MediaGoblin - chris commented on your \
121post\nFrom: notice@mediagoblin.example.org\nTo: \
122otherperson@example.com\n\nSGkgb3RoZXJwZXJzb24sCmNocmlzIGNvbW1lbnRlZCBvbiB5b3VyIHBvc3QgKGh0dHA6Ly9sb2Nh\nbGhvc3Q6ODAvdS9vdGhlcnBlcnNvbi9tL3NvbWUtdGl0bGUvYy8xLyNjb21tZW50KSBhdCBHTlUg\nTWVkaWFHb2JsaW4KClRlc3QgY29tbWVudCAjNDIKCkdOVSBNZWRpYUdvYmxpbg==\n',
123 'to': [u'otherperson@example.com']}]
124 else:
125 assert mail.EMAIL_TEST_MBOX_INBOX == []
126
127 # Save the ids temporarily because of DetachedInstanceError
128 notification_id = notification.id
129 comment_id = notification.subject.id
130
131 self.logout()
132 self.login('otherperson', 'nosreprehto')
133
134 self.test_app.get(media_uri_slug + '/c/{0}/'.format(comment_id))
135
136 notification = Notification.query.filter_by(id=notification_id).first()
137
138 assert notification.seen == True
139
140 self.test_app.get(media_uri_slug + '/notifications/silence/')
141
142 subscription = CommentSubscription.query.filter_by(id=subscription_id)\
143 .first()
144
145 assert subscription.notify == False
146
147 notifications = Notification.query.filter_by(
148 user_id=user_id).all()
149
150 # User should not have been notified
151 assert len(notifications) == 1