Changeset - 8e3137064ab6
[Not reviewed]
default
0 4 0
Thomas De Schampheleire - 9 years ago 2016-11-22 09:04:45
thomas.de.schampheleire@gmail.com
tests: use test_context for tests needing internationalization

Instead of relying on the top-level handling of the translator, use the
newly introduced test_context.
4 files changed with 296 insertions and 274 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/functional/test_admin_notifications.py
Show inline comments
 
@@ -6,6 +6,7 @@ from kallithea.model.notification import
 
from kallithea.model.meta import Session
 
from kallithea.lib import helpers as h
 

	
 
from kallithea.tests.test_context import test_context
 

	
 
class TestNotificationsController(TestController):
 
    def setup_method(self, method):
 
@@ -24,10 +25,12 @@ class TestNotificationsController(TestCo
 
        response = self.app.get(url('notifications'))
 
        response.mustcontain('<div class="table">No notifications here yet</div>')
 

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

	
 
@@ -35,23 +38,24 @@ class TestNotificationsController(TestCo
 
        self.log_user()
 
        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))
 
        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)
 
            # 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]
 
@@ -70,23 +74,24 @@ class TestNotificationsController(TestCo
 

	
 
    def test_show(self, create_test_user):
 
        self.log_user()
 
        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()
 
        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])
 
            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))
 
@@ -96,54 +101,58 @@ class TestNotificationsController(TestCo
 

	
 
    def test_description_with_age(self):
 
        self.log_user()
 
        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)
 
        with test_context(self.app):
 
            cur_user = self._get_logged_user()
 
            subject = u'test'
 
            notify_body = u'hi there'
 

	
 
        description = NotificationModel().make_description(notification)
 
        assert description == "{0} sent message {1}".format(
 
                cur_user.username,
 
                h.age(notification.created_on)
 
                )
 
            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()
 
        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)
 
        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)
 
                )
 
            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()
 
        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
 
        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]
 
            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.
 

	
kallithea/tests/functional/test_admin_users.py
Show inline comments
 
@@ -17,6 +17,7 @@ from sqlalchemy.orm.exc import NoResultF
 
import pytest
 
from kallithea.tests.base import *
 
from kallithea.tests.fixture import Fixture
 
from kallithea.tests.test_context import test_context
 
from kallithea.controllers.admin.users import UsersController
 
from kallithea.model.db import User, Permission, UserIpMap, UserApiKeys
 
from kallithea.lib.auth import check_password
 
@@ -513,22 +514,23 @@ class TestAdminUsersController(TestContr
 
        response.mustcontain(no=[api_key])
 

	
 

	
 
class TestAdminUsersController_unittest(object):
 
class TestAdminUsersController_unittest(TestController):
 
    """ Unit tests for the users controller """
 

	
 
    def test_get_user_or_raise_if_default(self, monkeypatch):
 
        # flash complains about an non-existing session
 
        def flash_mock(*args, **kwargs):
 
            pass
 
        monkeypatch.setattr(h, 'flash', flash_mock)
 
        with test_context(self.app):
 
            # flash complains about an non-existing session
 
            def flash_mock(*args, **kwargs):
 
                pass
 
            monkeypatch.setattr(h, 'flash', flash_mock)
 

	
 
        u = UsersController()
 
        # a regular user should work correctly
 
        user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
 
        assert u._get_user_or_raise_if_default(user.user_id) == user
 
        # the default user should raise
 
        with pytest.raises(HTTPNotFound):
 
            u._get_user_or_raise_if_default(User.get_default_user().user_id)
 
            u = UsersController()
 
            # a regular user should work correctly
 
            user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
 
            assert u._get_user_or_raise_if_default(user.user_id) == user
 
            # the default user should raise
 
            with pytest.raises(HTTPNotFound):
 
                u._get_user_or_raise_if_default(User.get_default_user().user_id)
 

	
 

	
 
class TestAdminUsersControllerForDefaultUser(TestController):
kallithea/tests/models/test_notifications.py
Show inline comments
 
@@ -14,6 +14,7 @@ from kallithea.model.notification import
 
import kallithea.lib.celerylib
 
import kallithea.lib.celerylib.tasks
 

	
 
from kallithea.tests.test_context import test_context
 

	
 
class TestNotifications(TestController):
 

	
 
@@ -45,125 +46,130 @@ class TestNotifications(TestController):
 
        assert [] == UserNotification.query().all()
 

	
 
    def test_create_notification(self):
 
        usrs = [self.u1, self.u2]
 
        def send_email(recipients, subject, body='', html_body='', headers=None, author=None):
 
            assert recipients == ['u2@example.com']
 
            assert subject == 'Test Message'
 
            assert body == u"hi there"
 
            assert '>hi there<' in html_body
 
            assert author.username == 'u1'
 
        with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email):
 
            notification = NotificationModel().create(created_by=self.u1,
 
                                               subject=u'subj', body=u'hi there',
 
                                               recipients=usrs)
 
        Session().commit()
 
        u1 = User.get(self.u1)
 
        u2 = User.get(self.u2)
 
        u3 = User.get(self.u3)
 
        notifications = Notification.query().all()
 
        assert len(notifications) == 1
 
        with test_context(self.app):
 
            usrs = [self.u1, self.u2]
 
            def send_email(recipients, subject, body='', html_body='', headers=None, author=None):
 
                assert recipients == ['u2@example.com']
 
                assert subject == 'Test Message'
 
                assert body == u"hi there"
 
                assert '>hi there<' in html_body
 
                assert author.username == 'u1'
 
            with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email):
 
                notification = NotificationModel().create(created_by=self.u1,
 
                                                   subject=u'subj', body=u'hi there',
 
                                                   recipients=usrs)
 
                Session().commit()
 
                u1 = User.get(self.u1)
 
                u2 = User.get(self.u2)
 
                u3 = User.get(self.u3)
 
                notifications = Notification.query().all()
 
                assert len(notifications) == 1
 

	
 
        assert notifications[0].recipients == [u1, u2]
 
        assert notification.notification_id == notifications[0].notification_id
 
                assert notifications[0].recipients == [u1, u2]
 
                assert notification.notification_id == notifications[0].notification_id
 

	
 
        unotification = UserNotification.query() \
 
            .filter(UserNotification.notification == notification).all()
 
                unotification = UserNotification.query() \
 
                    .filter(UserNotification.notification == notification).all()
 

	
 
        assert len(unotification) == len(usrs)
 
        assert set([x.user_id for x in unotification]) == set(usrs)
 
                assert len(unotification) == len(usrs)
 
                assert set([x.user_id for x in unotification]) == set(usrs)
 

	
 
    def test_user_notifications(self):
 
        notification1 = NotificationModel().create(created_by=self.u1,
 
                                            subject=u'subj', body=u'hi there1',
 
                                            recipients=[self.u3])
 
        Session().commit()
 
        notification2 = NotificationModel().create(created_by=self.u1,
 
                                            subject=u'subj', body=u'hi there2',
 
                                            recipients=[self.u3])
 
        Session().commit()
 
        u3 = Session().query(User).get(self.u3)
 
        with test_context(self.app):
 
            notification1 = NotificationModel().create(created_by=self.u1,
 
                                                subject=u'subj', body=u'hi there1',
 
                                                recipients=[self.u3])
 
            Session().commit()
 
            notification2 = NotificationModel().create(created_by=self.u1,
 
                                                subject=u'subj', body=u'hi there2',
 
                                                recipients=[self.u3])
 
            Session().commit()
 
            u3 = Session().query(User).get(self.u3)
 

	
 
        assert sorted([x.notification for x in u3.notifications]) == sorted([notification2, notification1])
 
            assert sorted([x.notification for x in u3.notifications]) == sorted([notification2, notification1])
 

	
 
    def test_delete_notifications(self):
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 
        notifications = Notification.query().all()
 
        assert notification in notifications
 
        with test_context(self.app):
 
            notification = NotificationModel().create(created_by=self.u1,
 
                                               subject=u'title', body=u'hi there3',
 
                                        recipients=[self.u3, self.u1, self.u2])
 
            Session().commit()
 
            notifications = Notification.query().all()
 
            assert notification in notifications
 

	
 
        Notification.delete(notification.notification_id)
 
        Session().commit()
 
            Notification.delete(notification.notification_id)
 
            Session().commit()
 

	
 
        notifications = Notification.query().all()
 
        assert not notification in notifications
 
            notifications = Notification.query().all()
 
            assert not notification in notifications
 

	
 
        un = UserNotification.query().filter(UserNotification.notification
 
                                             == notification).all()
 
        assert un == []
 
            un = UserNotification.query().filter(UserNotification.notification
 
                                                 == notification).all()
 
            assert un == []
 

	
 
    def test_delete_association(self):
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 
        with test_context(self.app):
 
            notification = NotificationModel().create(created_by=self.u1,
 
                                               subject=u'title', body=u'hi there3',
 
                                        recipients=[self.u3, self.u1, self.u2])
 
            Session().commit()
 

	
 
        unotification = UserNotification.query() \
 
                            .filter(UserNotification.notification ==
 
                                    notification) \
 
                            .filter(UserNotification.user_id == self.u3) \
 
                            .scalar()
 
            unotification = UserNotification.query() \
 
                                .filter(UserNotification.notification ==
 
                                        notification) \
 
                                .filter(UserNotification.user_id == self.u3) \
 
                                .scalar()
 

	
 
        assert unotification.user_id == self.u3
 
            assert unotification.user_id == self.u3
 

	
 
        NotificationModel().delete(self.u3,
 
                                   notification.notification_id)
 
        Session().commit()
 
            NotificationModel().delete(self.u3,
 
                                       notification.notification_id)
 
            Session().commit()
 

	
 
        u3notification = UserNotification.query() \
 
                            .filter(UserNotification.notification ==
 
                                    notification) \
 
                            .filter(UserNotification.user_id == self.u3) \
 
                            .scalar()
 
            u3notification = UserNotification.query() \
 
                                .filter(UserNotification.notification ==
 
                                        notification) \
 
                                .filter(UserNotification.user_id == self.u3) \
 
                                .scalar()
 

	
 
        assert u3notification == None
 
            assert u3notification == None
 

	
 
        # notification object is still there
 
        assert Notification.query().all() == [notification]
 
            # notification object is still there
 
            assert Notification.query().all() == [notification]
 

	
 
        #u1 and u2 still have assignments
 
        u1notification = UserNotification.query() \
 
                            .filter(UserNotification.notification ==
 
                                    notification) \
 
                            .filter(UserNotification.user_id == self.u1) \
 
                            .scalar()
 
        assert u1notification != None
 
        u2notification = UserNotification.query() \
 
                            .filter(UserNotification.notification ==
 
                                    notification) \
 
                            .filter(UserNotification.user_id == self.u2) \
 
                            .scalar()
 
        assert u2notification != None
 
            #u1 and u2 still have assignments
 
            u1notification = UserNotification.query() \
 
                                .filter(UserNotification.notification ==
 
                                        notification) \
 
                                .filter(UserNotification.user_id == self.u1) \
 
                                .scalar()
 
            assert u1notification != None
 
            u2notification = UserNotification.query() \
 
                                .filter(UserNotification.notification ==
 
                                        notification) \
 
                                .filter(UserNotification.user_id == self.u2) \
 
                                .scalar()
 
            assert u2notification != None
 

	
 
    def test_notification_counter(self):
 
        NotificationModel().create(created_by=self.u1,
 
                            subject=u'title', body=u'hi there_delete',
 
                            recipients=[self.u3, self.u1])
 
        Session().commit()
 

	
 
        assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0
 
        assert NotificationModel().get_unread_cnt_for_user(self.u2) == 0
 
        assert NotificationModel().get_unread_cnt_for_user(self.u3) == 1
 
        with test_context(self.app):
 
            NotificationModel().create(created_by=self.u1,
 
                                subject=u'title', body=u'hi there_delete',
 
                                recipients=[self.u3, self.u1])
 
            Session().commit()
 

	
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 
            assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0
 
            assert NotificationModel().get_unread_cnt_for_user(self.u2) == 0
 
            assert NotificationModel().get_unread_cnt_for_user(self.u3) == 1
 

	
 
        assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0
 
        assert NotificationModel().get_unread_cnt_for_user(self.u2) == 1
 
        assert NotificationModel().get_unread_cnt_for_user(self.u3) == 2
 
            notification = NotificationModel().create(created_by=self.u1,
 
                                               subject=u'title', body=u'hi there3',
 
                                        recipients=[self.u3, self.u1, self.u2])
 
            Session().commit()
 

	
 
            assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0
 
            assert NotificationModel().get_unread_cnt_for_user(self.u2) == 1
 
            assert NotificationModel().get_unread_cnt_for_user(self.u3) == 2
 

	
 
    @mock.patch.object(h, 'canonical_url', (lambda arg, **kwargs: 'http://%s/?%s' % (arg, '&'.join('%s=%s' % (k, v) for (k, v) in sorted(kwargs.items())))))
 
    def test_dump_html_mails(self):
 
@@ -184,92 +190,93 @@ class TestNotifications(TestController):
 
            l.append(html_body)
 
            l.append('<hr/>\n')
 

	
 
        with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email):
 
            pr_kwargs = dict(
 
                pr_nice_id='#7',
 
                pr_title='The Title',
 
                pr_title_short='The Title',
 
                pr_url='http://pr.org/7',
 
                pr_target_repo='http://mainline.com/repo',
 
                pr_target_branch='trunk',
 
                pr_source_repo='https://dev.org/repo',
 
                pr_source_branch='devbranch',
 
                pr_owner=User.get(self.u2),
 
                pr_owner_username='u2'
 
                )
 
        with test_context(self.app):
 
            with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email):
 
                pr_kwargs = dict(
 
                    pr_nice_id='#7',
 
                    pr_title='The Title',
 
                    pr_title_short='The Title',
 
                    pr_url='http://pr.org/7',
 
                    pr_target_repo='http://mainline.com/repo',
 
                    pr_target_branch='trunk',
 
                    pr_source_repo='https://dev.org/repo',
 
                    pr_source_branch='devbranch',
 
                    pr_owner=User.get(self.u2),
 
                    pr_owner_username='u2'
 
                    )
 

	
 
            for type_, body, kwargs in [
 
                (Notification.TYPE_CHANGESET_COMMENT,
 
                 u'This is the new comment.\n\n - and here it ends indented.',
 
                 dict(
 
                    short_id='cafe1234',
 
                    raw_id='cafe1234c0ffeecafe',
 
                    branch='brunch',
 
                    cs_comment_user='Opinionated User (jsmith)',
 
                    cs_comment_url='http://comment.org',
 
                    is_mention=[False, True],
 
                    message='This changeset did something clever which is hard to explain',
 
                    message_short='This changeset did something cl...',
 
                    status_change=[None, 'Approved'],
 
                    cs_target_repo='repo_target',
 
                    cs_url='http://changeset.com',
 
                    cs_author=User.get(self.u2))),
 
                (Notification.TYPE_MESSAGE,
 
                 u'This is the body of the test message\n - nothing interesting here except indentation.',
 
                 dict()),
 
                #(Notification.TYPE_MENTION, '$body', None), # not used
 
                (Notification.TYPE_REGISTRATION,
 
                 u'Registration body',
 
                 dict(
 
                    new_username='newbie',
 
                    registered_user_url='http://newbie.org',
 
                    new_email='new@email.com',
 
                    new_full_name='New Full Name')),
 
                (Notification.TYPE_PULL_REQUEST,
 
                 u'This PR is awesome because it does stuff\n - please approve indented!',
 
                 dict(
 
                    pr_user_created='Requesting User (root)', # pr_owner should perhaps be used for @mention in description ...
 
                    is_mention=[False, True],
 
                    pr_revisions=[('123abc'*7, "Introduce one and two\n\nand that's it"), ('567fed'*7, 'Make one plus two equal tree')],
 
                    org_repo_name='repo_org',
 
                    **pr_kwargs)),
 
                (Notification.TYPE_PULL_REQUEST_COMMENT,
 
                 u'Me too!\n\n - and indented on second line',
 
                 dict(
 
                    closing_pr=[False, True],
 
                    is_mention=[False, True],
 
                    pr_comment_user='Opinionated User (jsmith)',
 
                    pr_comment_url='http://pr.org/comment',
 
                    status_change=[None, 'Under Review'],
 
                    **pr_kwargs)),
 
                ]:
 
                kwargs['repo_name'] = u'repo/name'
 
                params = [(type_, type_, body, kwargs)]
 
                for param_name in ['is_mention', 'status_change', 'closing_pr']: # TODO: inline/general
 
                    if not isinstance(kwargs.get(param_name), list):
 
                        continue
 
                    new_params = []
 
                    for v in kwargs[param_name]:
 
                        for desc, type_, body, kwargs in params:
 
                            kwargs = dict(kwargs)
 
                            kwargs[param_name] = v
 
                            new_params.append(('%s, %s=%r' % (desc, param_name, v), type_, body, kwargs))
 
                    params = new_params
 
                for type_, body, kwargs in [
 
                    (Notification.TYPE_CHANGESET_COMMENT,
 
                     u'This is the new comment.\n\n - and here it ends indented.',
 
                     dict(
 
                        short_id='cafe1234',
 
                        raw_id='cafe1234c0ffeecafe',
 
                        branch='brunch',
 
                        cs_comment_user='Opinionated User (jsmith)',
 
                        cs_comment_url='http://comment.org',
 
                        is_mention=[False, True],
 
                        message='This changeset did something clever which is hard to explain',
 
                        message_short='This changeset did something cl...',
 
                        status_change=[None, 'Approved'],
 
                        cs_target_repo='repo_target',
 
                        cs_url='http://changeset.com',
 
                        cs_author=User.get(self.u2))),
 
                    (Notification.TYPE_MESSAGE,
 
                     u'This is the body of the test message\n - nothing interesting here except indentation.',
 
                     dict()),
 
                    #(Notification.TYPE_MENTION, '$body', None), # not used
 
                    (Notification.TYPE_REGISTRATION,
 
                     u'Registration body',
 
                     dict(
 
                        new_username='newbie',
 
                        registered_user_url='http://newbie.org',
 
                        new_email='new@email.com',
 
                        new_full_name='New Full Name')),
 
                    (Notification.TYPE_PULL_REQUEST,
 
                     u'This PR is awesome because it does stuff\n - please approve indented!',
 
                     dict(
 
                        pr_user_created='Requesting User (root)', # pr_owner should perhaps be used for @mention in description ...
 
                        is_mention=[False, True],
 
                        pr_revisions=[('123abc'*7, "Introduce one and two\n\nand that's it"), ('567fed'*7, 'Make one plus two equal tree')],
 
                        org_repo_name='repo_org',
 
                        **pr_kwargs)),
 
                    (Notification.TYPE_PULL_REQUEST_COMMENT,
 
                     u'Me too!\n\n - and indented on second line',
 
                     dict(
 
                        closing_pr=[False, True],
 
                        is_mention=[False, True],
 
                        pr_comment_user='Opinionated User (jsmith)',
 
                        pr_comment_url='http://pr.org/comment',
 
                        status_change=[None, 'Under Review'],
 
                        **pr_kwargs)),
 
                    ]:
 
                    kwargs['repo_name'] = u'repo/name'
 
                    params = [(type_, type_, body, kwargs)]
 
                    for param_name in ['is_mention', 'status_change', 'closing_pr']: # TODO: inline/general
 
                        if not isinstance(kwargs.get(param_name), list):
 
                            continue
 
                        new_params = []
 
                        for v in kwargs[param_name]:
 
                            for desc, type_, body, kwargs in params:
 
                                kwargs = dict(kwargs)
 
                                kwargs[param_name] = v
 
                                new_params.append(('%s, %s=%r' % (desc, param_name, v), type_, body, kwargs))
 
                        params = new_params
 

	
 
                for desc, type_, body, kwargs in params:
 
                    # desc is used as "global" variable
 
                    notification = NotificationModel().create(created_by=self.u1,
 
                                                       subject=u'unused', body=body, email_kwargs=kwargs,
 
                                                       recipients=[self.u2], type_=type_)
 
                    for desc, type_, body, kwargs in params:
 
                        # desc is used as "global" variable
 
                        notification = NotificationModel().create(created_by=self.u1,
 
                                                           subject=u'unused', body=body, email_kwargs=kwargs,
 
                                                           recipients=[self.u2], type_=type_)
 

	
 
            # Email type TYPE_PASSWORD_RESET has no corresponding notification type - test it directly:
 
            desc = 'TYPE_PASSWORD_RESET'
 
            kwargs = dict(user='John Doe', reset_token='decbf64715098db5b0bd23eab44bd792670ab746', reset_url='http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746')
 
            kallithea.lib.celerylib.tasks.send_email(['john@doe.com'],
 
                "Password reset link",
 
                EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'txt', **kwargs),
 
                EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'html', **kwargs),
 
                author=User.get(self.u1))
 
                # Email type TYPE_PASSWORD_RESET has no corresponding notification type - test it directly:
 
                desc = 'TYPE_PASSWORD_RESET'
 
                kwargs = dict(user='John Doe', reset_token='decbf64715098db5b0bd23eab44bd792670ab746', reset_url='http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746')
 
                kallithea.lib.celerylib.tasks.send_email(['john@doe.com'],
 
                    "Password reset link",
 
                    EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'txt', **kwargs),
 
                    EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'html', **kwargs),
 
                    author=User.get(self.u1))
 

	
 
        out = '<!doctype html>\n<html lang="en">\n<head><title>Notifications</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>\n<body>\n%s\n</body>\n</html>\n' % \
 
            re.sub(r'<(/?(?:!doctype|html|head|title|meta|body)\b[^>]*)>', r'<!--\1-->', ''.join(l))
kallithea/tests/other/test_libs.py
Show inline comments
 
@@ -31,6 +31,7 @@ import mock
 
from kallithea.tests.base import *
 
from kallithea.lib.utils2 import AttributeDict
 
from kallithea.model.db import Repository
 
from kallithea.tests.test_context import test_context
 

	
 
proto = 'http'
 
TEST_URLS = [
 
@@ -154,9 +155,10 @@ class TestLibs(TestController):
 
    def test_age(self, age_args, expected):
 
        from kallithea.lib.utils2 import age
 
        from dateutil import relativedelta
 
        n = datetime.datetime(year=2012, month=5, day=17)
 
        delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
 
        assert age(n + delt(**age_args), now=n) == expected
 
        with test_context(self.app):
 
            n = datetime.datetime(year=2012, month=5, day=17)
 
            delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
 
            assert age(n + delt(**age_args), now=n) == expected
 

	
 
    @parametrize('age_args,expected', [
 
        (dict(), u'just now'),
 
@@ -178,9 +180,10 @@ class TestLibs(TestController):
 
    def test_age_short(self, age_args, expected):
 
        from kallithea.lib.utils2 import age
 
        from dateutil import relativedelta
 
        n = datetime.datetime(year=2012, month=5, day=17)
 
        delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
 
        assert age(n + delt(**age_args), show_short_version=True, now=n) == expected
 
        with test_context(self.app):
 
            n = datetime.datetime(year=2012, month=5, day=17)
 
            delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
 
            assert age(n + delt(**age_args), show_short_version=True, now=n) == expected
 

	
 
    @parametrize('age_args,expected', [
 
        (dict(), u'just now'),
 
@@ -196,9 +199,10 @@ class TestLibs(TestController):
 
    def test_age_in_future(self, age_args, expected):
 
        from kallithea.lib.utils2 import age
 
        from dateutil import relativedelta
 
        n = datetime.datetime(year=2012, month=5, day=17)
 
        delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
 
        assert age(n + delt(**age_args), now=n) == expected
 
        with test_context(self.app):
 
            n = datetime.datetime(year=2012, month=5, day=17)
 
            delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
 
            assert age(n + delt(**age_args), now=n) == expected
 

	
 
    def test_tag_extractor(self):
 
        sample = (
0 comments (0 inline, 0 general)