test_admin_notifications.py
172 lines
| 7.4 KiB
| text/x-python
|
PythonLexer
|
r6180 | from kallithea.tests.base import * | ||
|
r5397 | from kallithea.model.db import User | ||
|
r4187 | |||
from kallithea.model.user import UserModel | ||||
from kallithea.model.notification import NotificationModel | ||||
from kallithea.model.meta import Session | ||||
|
r5114 | from kallithea.lib import helpers as h | ||
|
r4187 | |||
|
r6522 | from tg.util.webtest import test_context | ||
|
r4187 | |||
|
r6789 | |||
|
r5885 | class TestNotificationsController(TestController): | ||
|
r5764 | def setup_method(self, method): | ||
|
r5883 | self.remove_all_notifications() | ||
|
r4187 | |||
|
r5940 | def test_index(self, create_test_user): | ||
|
r4187 | self.log_user() | ||
|
r5940 | u1 = create_test_user(dict(username='u1', password='qweqwe', | ||
email='u1@example.com', | ||||
firstname=u'u1', lastname=u'u1', | ||||
active=True)) | ||||
|
r4187 | u1 = u1.user_id | ||
|
r5763 | Session().commit() | ||
|
r4187 | |||
response = self.app.get(url('notifications')) | ||||
|
r6383 | response.mustcontain('<div>No notifications here yet</div>') | ||
|
r4187 | |||
|
r6305 | with test_context(self.app): | ||
cur_user = self._get_logged_user() | ||||
notif = NotificationModel().create(created_by=u1, subject=u'test_notification_1', | ||||
body=u'notification_1', recipients=[cur_user]) | ||||
Session().commit() | ||||
|
r4187 | response = self.app.get(url('notifications')) | ||
response.mustcontain('id="notification_%s"' % notif.notification_id) | ||||
|
r5940 | def test_delete(self, create_test_user): | ||
|
r4187 | self.log_user() | ||
cur_user = self._get_logged_user() | ||||
|
r6305 | with test_context(self.app): | ||
u1 = create_test_user(dict(username='u1', password='qweqwe', | ||||
email='u1@example.com', | ||||
firstname=u'u1', lastname=u'u1', | ||||
active=True)) | ||||
u2 = create_test_user(dict(username='u2', password='qweqwe', | ||||
email='u2@example.com', | ||||
firstname=u'u2', lastname=u'u2', | ||||
active=True)) | ||||
|
r4187 | |||
|
r6305 | # make notifications | ||
notification = NotificationModel().create(created_by=cur_user, | ||||
subject=u'test', | ||||
body=u'hi there', | ||||
recipients=[cur_user, u1, u2]) | ||||
Session().commit() | ||||
u1 = User.get(u1.user_id) | ||||
u2 = User.get(u2.user_id) | ||||
|
r4187 | |||
# check DB | ||||
get_notif = lambda un: [x.notification for x in un] | ||||
|
r5877 | assert get_notif(cur_user.notifications) == [notification] | ||
assert get_notif(u1.notifications) == [notification] | ||||
assert get_notif(u2.notifications) == [notification] | ||||
|
r4187 | cur_usr_id = cur_user.user_id | ||
|
r5471 | response = self.app.post( | ||
|
r6039 | url('notification_delete', notification_id=notification.notification_id), | ||
params={'_authentication_token': self.authentication_token()}) | ||||
|
r5877 | assert response.body == 'ok' | ||
|
r4187 | |||
cur_user = User.get(cur_usr_id) | ||||
|
r5877 | assert cur_user.notifications == [] | ||
|
r4187 | |||
|
r5940 | def test_show(self, create_test_user): | ||
|
r4187 | self.log_user() | ||
|
r6305 | with test_context(self.app): | ||
cur_user = self._get_logged_user() | ||||
u1 = create_test_user(dict(username='u1', password='qweqwe', | ||||
email='u1@example.com', | ||||
firstname=u'u1', lastname=u'u1', | ||||
active=True)) | ||||
u2 = create_test_user(dict(username='u2', password='qweqwe', | ||||
email='u2@example.com', | ||||
firstname=u'u2', lastname=u'u2', | ||||
active=True)) | ||||
Session().commit() | ||||
|
r4187 | |||
|
r6305 | subject = u'test' | ||
notif_body = u'hi there' | ||||
notification = NotificationModel().create(created_by=cur_user, | ||||
subject=subject, | ||||
body=notif_body, | ||||
recipients=[cur_user, u1, u2]) | ||||
|
r4187 | |||
response = self.app.get(url('notification', | ||||
notification_id=notification.notification_id)) | ||||
response.mustcontain(subject) | ||||
response.mustcontain(notif_body) | ||||
|
r5114 | |||
def test_description_with_age(self): | ||||
self.log_user() | ||||
|
r6305 | with test_context(self.app): | ||
cur_user = self._get_logged_user() | ||||
subject = u'test' | ||||
notify_body = u'hi there' | ||||
|
r5114 | |||
|
r6305 | notification = NotificationModel().create(created_by = cur_user, | ||
subject = subject, | ||||
body = notify_body) | ||||
description = NotificationModel().make_description(notification) | ||||
assert description == "{0} sent message {1}".format( | ||||
cur_user.username, | ||||
h.age(notification.created_on) | ||||
) | ||||
|
r5114 | |||
def test_description_with_datetime(self): | ||||
self.log_user() | ||||
|
r6305 | with test_context(self.app): | ||
cur_user = self._get_logged_user() | ||||
subject = u'test' | ||||
notify_body = u'hi there' | ||||
notification = NotificationModel().create(created_by = cur_user, | ||||
subject = subject, | ||||
body = notify_body) | ||||
|
r5114 | |||
|
r6305 | description = NotificationModel().make_description(notification, False) | ||
assert description == "{0} sent message at {1}".format( | ||||
cur_user.username, | ||||
h.fmt_date(notification.created_on) | ||||
) | ||||
|
r5941 | |||
def test_mark_all_read(self, create_test_user): | ||||
self.log_user() | ||||
|
r6305 | with test_context(self.app): | ||
u0 = self._get_logged_user() | ||||
u1 = create_test_user(dict(username='u1', password='qweqwe', | ||||
email='u1@example.com', | ||||
firstname=u'u1', lastname=u'u1', | ||||
active=True)) | ||||
u2 = create_test_user(dict(username='u2', password='qweqwe', | ||||
email='u2@example.com', | ||||
firstname=u'u2', lastname=u'u2', | ||||
active=True)) | ||||
notif = NotificationModel().create(created_by=u1, | ||||
subject=u'subject', | ||||
body=u'body', | ||||
recipients=[u0, u2]) | ||||
u0_id, u1_id, u2_id = u0.user_id, u1.user_id, u2.user_id | ||||
|
r5941 | |||
|
r6305 | assert [n.read for n in u0.notifications] == [False] | ||
assert u1.notifications == [] | ||||
assert [n.read for n in u2.notifications] == [False] | ||||
|
r5941 | |||
# Mark all read for current user. | ||||
response = self.app.get(url('notifications_mark_all_read'), # TODO: should be POST | ||||
extra_environ=dict(HTTP_X_PARTIAL_XHR='1')) | ||||
assert response.status_int == 200 | ||||
response.mustcontain('id="notification_%s"' % notif.notification_id) | ||||
u0 = User.get(u0_id) | ||||
u1 = User.get(u1_id) | ||||
u2 = User.get(u2_id) | ||||
assert [n.read for n in u0.notifications] == [True] | ||||
assert u1.notifications == [] | ||||
assert [n.read for n in u2.notifications] == [False] | ||||