Files @ e1ab82613133
Branch filter:

Location: kallithea/kallithea/tests/functional/test_admin_notifications.py

Alessandro Molina
backend: replace Pylons with TurboGears2

Replace the no-longer-supported Pylons application framework by TurboGears2
which is largely compatible/similar to Pylons.
Some interesting history is described at:
https://en.wikipedia.org/wiki/TurboGears

Changes by Dominik Ruf:
- fix sql config in test.ini

Changes by Thomas De Schampheleire:
- set-up of test suite
- tests: 'fix' repo archival test failure
Between Pylons and TurboGears2, there seems to be a small difference in the
headers sent for repository archive files, related to character encoding.
It is assumed that this difference is not important, and that the test
should just align with reality.
- remove need to import helpers/app_globals in lib
TurboGears2 by default expects helpers and app_globals to be available
in lib. For this reason kallithea/lib/__init__.py was originally changed
to include those files. However, this triggered several types of
circular import problems. If module A imported something from lib (e.g.
lib.annotate), and lib.helpers imported (possibly indirectly) module A,
then there was a circular import. Fix this by overruling the relevant
method of tg AppConfig, which is also hinted in the TurboGears2 code.
Hereby, the include of something from lib does not automatically import
helpers, greatly reducing the chances of circular import problems.
- make sure HTTP error '400' uses the custom error pages
TurboGears2 does not by default handle HTTP status code
'400 (Bad Request)' via the custom error page handling, causing a
standard non-styled error page.
- disable transaction manager
Kallithea currently handles its own transactions and does not need the
TurboGears2 transaction manager. However, TurboGears2 tries to enable it
by default and fails, throwing an error during application initialization.
The error itself seemed to be harmless for normal application functioning,
but was nevertheless confusing.
- add backlash as required dependency: backlash is meant as the WebError
replacement in TurboGears2 (originally WebError is part of Pylons). When
debug==true, it provides an interactive debugger in the browser. When
debug==false, backlash is necessary to show backtraces on the console.
- misc fixes
from kallithea.tests.base import *
from kallithea.model.db import User

from kallithea.model.user import UserModel
from kallithea.model.notification import NotificationModel
from kallithea.model.meta import Session
from kallithea.lib import helpers as h

from tg.util.webtest import test_context

class TestNotificationsController(TestController):
    def setup_method(self, method):
        self.remove_all_notifications()

    def test_index(self, create_test_user):
        self.log_user()

        u1 = create_test_user(dict(username='u1', password='qweqwe',
                                   email='u1@example.com',
                                   firstname=u'u1', lastname=u'u1',
                                   active=True))
        u1 = u1.user_id
        Session().commit()

        response = self.app.get(url('notifications'))
        response.mustcontain('<div>No notifications here yet</div>')

        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()

        response = self.app.get(url('notifications'))
        response.mustcontain('id="notification_%s"' % notif.notification_id)

    def test_delete(self, create_test_user):
        self.log_user()
        cur_user = self._get_logged_user()

        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))

            # 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)

        # check DB
        get_notif = lambda un: [x.notification for x in un]
        assert get_notif(cur_user.notifications) == [notification]
        assert get_notif(u1.notifications) == [notification]
        assert get_notif(u2.notifications) == [notification]
        cur_usr_id = cur_user.user_id

        response = self.app.post(
            url('notification_delete', notification_id=notification.notification_id),
            params={'_authentication_token': self.authentication_token()})
        assert response.body == 'ok'

        cur_user = User.get(cur_usr_id)
        assert cur_user.notifications == []

    def test_show(self, create_test_user):
        self.log_user()
        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()

            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])

        response = self.app.get(url('notification',
                                    notification_id=notification.notification_id))

        response.mustcontain(subject)
        response.mustcontain(notif_body)

    def test_description_with_age(self):
        self.log_user()
        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)

            description = NotificationModel().make_description(notification)
            assert description == "{0} sent message {1}".format(
                    cur_user.username,
                    h.age(notification.created_on)
                    )

    def test_description_with_datetime(self):
        self.log_user()
        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)

            description = NotificationModel().make_description(notification, False)
            assert description == "{0} sent message at {1}".format(
                    cur_user.username,
                    h.fmt_date(notification.created_on)
                    )

    def test_mark_all_read(self, create_test_user):
        self.log_user()
        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

            assert [n.read for n in u0.notifications] == [False]
            assert u1.notifications == []
            assert [n.read for n in u2.notifications] == [False]

        # 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]