Show More
@@ -6,6 +6,7 b' from kallithea.model.notification import' | |||||
6 | from kallithea.model.meta import Session |
|
6 | from kallithea.model.meta import Session | |
7 | from kallithea.lib import helpers as h |
|
7 | from kallithea.lib import helpers as h | |
8 |
|
8 | |||
|
9 | from kallithea.tests.test_context import test_context | |||
9 |
|
10 | |||
10 | class TestNotificationsController(TestController): |
|
11 | class TestNotificationsController(TestController): | |
11 | def setup_method(self, method): |
|
12 | def setup_method(self, method): | |
@@ -24,10 +25,12 b' class TestNotificationsController(TestCo' | |||||
24 | response = self.app.get(url('notifications')) |
|
25 | response = self.app.get(url('notifications')) | |
25 | response.mustcontain('<div class="table">No notifications here yet</div>') |
|
26 | response.mustcontain('<div class="table">No notifications here yet</div>') | |
26 |
|
27 | |||
27 | cur_user = self._get_logged_user() |
|
28 | with test_context(self.app): | |
28 | notif = NotificationModel().create(created_by=u1, subject=u'test_notification_1', |
|
29 | cur_user = self._get_logged_user() | |
29 | body=u'notification_1', recipients=[cur_user]) |
|
30 | notif = NotificationModel().create(created_by=u1, subject=u'test_notification_1', | |
30 | Session().commit() |
|
31 | body=u'notification_1', recipients=[cur_user]) | |
|
32 | Session().commit() | |||
|
33 | ||||
31 | response = self.app.get(url('notifications')) |
|
34 | response = self.app.get(url('notifications')) | |
32 | response.mustcontain('id="notification_%s"' % notif.notification_id) |
|
35 | response.mustcontain('id="notification_%s"' % notif.notification_id) | |
33 |
|
36 | |||
@@ -35,23 +38,24 b' class TestNotificationsController(TestCo' | |||||
35 | self.log_user() |
|
38 | self.log_user() | |
36 | cur_user = self._get_logged_user() |
|
39 | cur_user = self._get_logged_user() | |
37 |
|
40 | |||
38 | u1 = create_test_user(dict(username='u1', password='qweqwe', |
|
41 | with test_context(self.app): | |
39 | email='u1@example.com', |
|
42 | u1 = create_test_user(dict(username='u1', password='qweqwe', | |
40 |
|
|
43 | email='u1@example.com', | |
41 |
|
|
44 | firstname=u'u1', lastname=u'u1', | |
42 | u2 = create_test_user(dict(username='u2', password='qweqwe', |
|
45 | active=True)) | |
43 | email='u2@example.com', |
|
46 | u2 = create_test_user(dict(username='u2', password='qweqwe', | |
44 |
|
|
47 | email='u2@example.com', | |
45 |
|
|
48 | firstname=u'u2', lastname=u'u2', | |
|
49 | active=True)) | |||
46 |
|
50 | |||
47 | # make notifications |
|
51 | # make notifications | |
48 | notification = NotificationModel().create(created_by=cur_user, |
|
52 | notification = NotificationModel().create(created_by=cur_user, | |
49 | subject=u'test', |
|
53 | subject=u'test', | |
50 | body=u'hi there', |
|
54 | body=u'hi there', | |
51 | recipients=[cur_user, u1, u2]) |
|
55 | recipients=[cur_user, u1, u2]) | |
52 | Session().commit() |
|
56 | Session().commit() | |
53 | u1 = User.get(u1.user_id) |
|
57 | u1 = User.get(u1.user_id) | |
54 | u2 = User.get(u2.user_id) |
|
58 | u2 = User.get(u2.user_id) | |
55 |
|
59 | |||
56 | # check DB |
|
60 | # check DB | |
57 | get_notif = lambda un: [x.notification for x in un] |
|
61 | get_notif = lambda un: [x.notification for x in un] | |
@@ -70,23 +74,24 b' class TestNotificationsController(TestCo' | |||||
70 |
|
74 | |||
71 | def test_show(self, create_test_user): |
|
75 | def test_show(self, create_test_user): | |
72 | self.log_user() |
|
76 | self.log_user() | |
73 | cur_user = self._get_logged_user() |
|
77 | with test_context(self.app): | |
74 | u1 = create_test_user(dict(username='u1', password='qweqwe', |
|
78 | cur_user = self._get_logged_user() | |
75 | email='u1@example.com', |
|
79 | u1 = create_test_user(dict(username='u1', password='qweqwe', | |
76 |
|
|
80 | email='u1@example.com', | |
77 |
|
|
81 | firstname=u'u1', lastname=u'u1', | |
78 | u2 = create_test_user(dict(username='u2', password='qweqwe', |
|
82 | active=True)) | |
79 | email='u2@example.com', |
|
83 | u2 = create_test_user(dict(username='u2', password='qweqwe', | |
80 |
|
|
84 | email='u2@example.com', | |
81 |
|
|
85 | firstname=u'u2', lastname=u'u2', | |
82 | Session().commit() |
|
86 | active=True)) | |
|
87 | Session().commit() | |||
83 |
|
88 | |||
84 | subject = u'test' |
|
89 | subject = u'test' | |
85 | notif_body = u'hi there' |
|
90 | notif_body = u'hi there' | |
86 | notification = NotificationModel().create(created_by=cur_user, |
|
91 | notification = NotificationModel().create(created_by=cur_user, | |
87 | subject=subject, |
|
92 | subject=subject, | |
88 | body=notif_body, |
|
93 | body=notif_body, | |
89 | recipients=[cur_user, u1, u2]) |
|
94 | recipients=[cur_user, u1, u2]) | |
90 |
|
95 | |||
91 | response = self.app.get(url('notification', |
|
96 | response = self.app.get(url('notification', | |
92 | notification_id=notification.notification_id)) |
|
97 | notification_id=notification.notification_id)) | |
@@ -96,54 +101,58 b' class TestNotificationsController(TestCo' | |||||
96 |
|
101 | |||
97 | def test_description_with_age(self): |
|
102 | def test_description_with_age(self): | |
98 | self.log_user() |
|
103 | self.log_user() | |
99 | cur_user = self._get_logged_user() |
|
104 | with test_context(self.app): | |
100 | subject = u'test' |
|
105 | cur_user = self._get_logged_user() | |
101 | notify_body = u'hi there' |
|
106 | subject = u'test' | |
102 | notification = NotificationModel().create(created_by = cur_user, |
|
107 | notify_body = u'hi there' | |
103 | subject = subject, |
|
|||
104 | body = notify_body) |
|
|||
105 |
|
108 | |||
106 | description = NotificationModel().make_description(notification) |
|
109 | notification = NotificationModel().create(created_by = cur_user, | |
107 | assert description == "{0} sent message {1}".format( |
|
110 | subject = subject, | |
108 | cur_user.username, |
|
111 | body = notify_body) | |
109 | h.age(notification.created_on) |
|
112 | ||
110 | ) |
|
113 | description = NotificationModel().make_description(notification) | |
|
114 | assert description == "{0} sent message {1}".format( | |||
|
115 | cur_user.username, | |||
|
116 | h.age(notification.created_on) | |||
|
117 | ) | |||
111 |
|
118 | |||
112 | def test_description_with_datetime(self): |
|
119 | def test_description_with_datetime(self): | |
113 | self.log_user() |
|
120 | self.log_user() | |
114 | cur_user = self._get_logged_user() |
|
121 | with test_context(self.app): | |
115 | subject = u'test' |
|
122 | cur_user = self._get_logged_user() | |
116 | notify_body = u'hi there' |
|
123 | subject = u'test' | |
117 | notification = NotificationModel().create(created_by = cur_user, |
|
124 | notify_body = u'hi there' | |
118 | subject = subject, |
|
125 | notification = NotificationModel().create(created_by = cur_user, | |
119 |
|
|
126 | subject = subject, | |
|
127 | body = notify_body) | |||
120 |
|
128 | |||
121 | description = NotificationModel().make_description(notification, False) |
|
129 | description = NotificationModel().make_description(notification, False) | |
122 | assert description == "{0} sent message at {1}".format( |
|
130 | assert description == "{0} sent message at {1}".format( | |
123 | cur_user.username, |
|
131 | cur_user.username, | |
124 | h.fmt_date(notification.created_on) |
|
132 | h.fmt_date(notification.created_on) | |
125 | ) |
|
133 | ) | |
126 |
|
134 | |||
127 | def test_mark_all_read(self, create_test_user): |
|
135 | def test_mark_all_read(self, create_test_user): | |
128 | self.log_user() |
|
136 | self.log_user() | |
129 | u0 = self._get_logged_user() |
|
137 | with test_context(self.app): | |
130 | u1 = create_test_user(dict(username='u1', password='qweqwe', |
|
138 | u0 = self._get_logged_user() | |
131 | email='u1@example.com', |
|
139 | u1 = create_test_user(dict(username='u1', password='qweqwe', | |
132 |
|
|
140 | email='u1@example.com', | |
133 |
|
|
141 | firstname=u'u1', lastname=u'u1', | |
134 | u2 = create_test_user(dict(username='u2', password='qweqwe', |
|
142 | active=True)) | |
135 | email='u2@example.com', |
|
143 | u2 = create_test_user(dict(username='u2', password='qweqwe', | |
136 |
|
|
144 | email='u2@example.com', | |
137 |
|
|
145 | firstname=u'u2', lastname=u'u2', | |
138 | notif = NotificationModel().create(created_by=u1, |
|
146 | active=True)) | |
139 | subject=u'subject', |
|
147 | notif = NotificationModel().create(created_by=u1, | |
140 |
|
|
148 | subject=u'subject', | |
141 |
|
|
149 | body=u'body', | |
142 | u0_id, u1_id, u2_id = u0.user_id, u1.user_id, u2.user_id |
|
150 | recipients=[u0, u2]) | |
|
151 | u0_id, u1_id, u2_id = u0.user_id, u1.user_id, u2.user_id | |||
143 |
|
152 | |||
144 | assert [n.read for n in u0.notifications] == [False] |
|
153 | assert [n.read for n in u0.notifications] == [False] | |
145 | assert u1.notifications == [] |
|
154 | assert u1.notifications == [] | |
146 | assert [n.read for n in u2.notifications] == [False] |
|
155 | assert [n.read for n in u2.notifications] == [False] | |
147 |
|
156 | |||
148 | # Mark all read for current user. |
|
157 | # Mark all read for current user. | |
149 |
|
158 |
@@ -17,6 +17,7 b' from sqlalchemy.orm.exc import NoResultF' | |||||
17 | import pytest |
|
17 | import pytest | |
18 | from kallithea.tests.base import * |
|
18 | from kallithea.tests.base import * | |
19 | from kallithea.tests.fixture import Fixture |
|
19 | from kallithea.tests.fixture import Fixture | |
|
20 | from kallithea.tests.test_context import test_context | |||
20 | from kallithea.controllers.admin.users import UsersController |
|
21 | from kallithea.controllers.admin.users import UsersController | |
21 | from kallithea.model.db import User, Permission, UserIpMap, UserApiKeys |
|
22 | from kallithea.model.db import User, Permission, UserIpMap, UserApiKeys | |
22 | from kallithea.lib.auth import check_password |
|
23 | from kallithea.lib.auth import check_password | |
@@ -513,22 +514,23 b' class TestAdminUsersController(TestContr' | |||||
513 | response.mustcontain(no=[api_key]) |
|
514 | response.mustcontain(no=[api_key]) | |
514 |
|
515 | |||
515 |
|
516 | |||
516 |
class TestAdminUsersController_unittest( |
|
517 | class TestAdminUsersController_unittest(TestController): | |
517 | """ Unit tests for the users controller """ |
|
518 | """ Unit tests for the users controller """ | |
518 |
|
519 | |||
519 | def test_get_user_or_raise_if_default(self, monkeypatch): |
|
520 | def test_get_user_or_raise_if_default(self, monkeypatch): | |
520 | # flash complains about an non-existing session |
|
521 | with test_context(self.app): | |
521 | def flash_mock(*args, **kwargs): |
|
522 | # flash complains about an non-existing session | |
522 | pass |
|
523 | def flash_mock(*args, **kwargs): | |
523 | monkeypatch.setattr(h, 'flash', flash_mock) |
|
524 | pass | |
|
525 | monkeypatch.setattr(h, 'flash', flash_mock) | |||
524 |
|
526 | |||
525 | u = UsersController() |
|
527 | u = UsersController() | |
526 | # a regular user should work correctly |
|
528 | # a regular user should work correctly | |
527 | user = User.get_by_username(TEST_USER_REGULAR_LOGIN) |
|
529 | user = User.get_by_username(TEST_USER_REGULAR_LOGIN) | |
528 | assert u._get_user_or_raise_if_default(user.user_id) == user |
|
530 | assert u._get_user_or_raise_if_default(user.user_id) == user | |
529 | # the default user should raise |
|
531 | # the default user should raise | |
530 | with pytest.raises(HTTPNotFound): |
|
532 | with pytest.raises(HTTPNotFound): | |
531 | u._get_user_or_raise_if_default(User.get_default_user().user_id) |
|
533 | u._get_user_or_raise_if_default(User.get_default_user().user_id) | |
532 |
|
534 | |||
533 |
|
535 | |||
534 | class TestAdminUsersControllerForDefaultUser(TestController): |
|
536 | class TestAdminUsersControllerForDefaultUser(TestController): |
@@ -14,6 +14,7 b' from kallithea.model.notification import' | |||||
14 | import kallithea.lib.celerylib |
|
14 | import kallithea.lib.celerylib | |
15 | import kallithea.lib.celerylib.tasks |
|
15 | import kallithea.lib.celerylib.tasks | |
16 |
|
16 | |||
|
17 | from kallithea.tests.test_context import test_context | |||
17 |
|
18 | |||
18 | class TestNotifications(TestController): |
|
19 | class TestNotifications(TestController): | |
19 |
|
20 | |||
@@ -45,125 +46,130 b' class TestNotifications(TestController):' | |||||
45 | assert [] == UserNotification.query().all() |
|
46 | assert [] == UserNotification.query().all() | |
46 |
|
47 | |||
47 | def test_create_notification(self): |
|
48 | def test_create_notification(self): | |
48 | usrs = [self.u1, self.u2] |
|
49 | with test_context(self.app): | |
49 | def send_email(recipients, subject, body='', html_body='', headers=None, author=None): |
|
50 | usrs = [self.u1, self.u2] | |
50 | assert recipients == ['u2@example.com'] |
|
51 | def send_email(recipients, subject, body='', html_body='', headers=None, author=None): | |
51 |
assert |
|
52 | assert recipients == ['u2@example.com'] | |
52 |
assert |
|
53 | assert subject == 'Test Message' | |
53 |
assert |
|
54 | assert body == u"hi there" | |
54 | assert author.username == 'u1' |
|
55 | assert '>hi there<' in html_body | |
55 | with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email): |
|
56 | assert author.username == 'u1' | |
56 | notification = NotificationModel().create(created_by=self.u1, |
|
57 | with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email): | |
57 | subject=u'subj', body=u'hi there', |
|
58 | notification = NotificationModel().create(created_by=self.u1, | |
58 |
|
|
59 | subject=u'subj', body=u'hi there', | |
59 | Session().commit() |
|
60 | recipients=usrs) | |
60 | u1 = User.get(self.u1) |
|
61 | Session().commit() | |
61 |
u |
|
62 | u1 = User.get(self.u1) | |
62 |
u |
|
63 | u2 = User.get(self.u2) | |
63 | notifications = Notification.query().all() |
|
64 | u3 = User.get(self.u3) | |
64 | assert len(notifications) == 1 |
|
65 | notifications = Notification.query().all() | |
|
66 | assert len(notifications) == 1 | |||
65 |
|
67 | |||
66 | assert notifications[0].recipients == [u1, u2] |
|
68 | assert notifications[0].recipients == [u1, u2] | |
67 | assert notification.notification_id == notifications[0].notification_id |
|
69 | assert notification.notification_id == notifications[0].notification_id | |
68 |
|
70 | |||
69 | unotification = UserNotification.query() \ |
|
71 | unotification = UserNotification.query() \ | |
70 | .filter(UserNotification.notification == notification).all() |
|
72 | .filter(UserNotification.notification == notification).all() | |
71 |
|
73 | |||
72 | assert len(unotification) == len(usrs) |
|
74 | assert len(unotification) == len(usrs) | |
73 | assert set([x.user_id for x in unotification]) == set(usrs) |
|
75 | assert set([x.user_id for x in unotification]) == set(usrs) | |
74 |
|
76 | |||
75 | def test_user_notifications(self): |
|
77 | def test_user_notifications(self): | |
76 | notification1 = NotificationModel().create(created_by=self.u1, |
|
78 | with test_context(self.app): | |
77 | subject=u'subj', body=u'hi there1', |
|
79 | notification1 = NotificationModel().create(created_by=self.u1, | |
78 |
|
|
80 | subject=u'subj', body=u'hi there1', | |
79 | Session().commit() |
|
81 | recipients=[self.u3]) | |
80 | notification2 = NotificationModel().create(created_by=self.u1, |
|
82 | Session().commit() | |
81 | subject=u'subj', body=u'hi there2', |
|
83 | notification2 = NotificationModel().create(created_by=self.u1, | |
82 |
|
|
84 | subject=u'subj', body=u'hi there2', | |
83 | Session().commit() |
|
85 | recipients=[self.u3]) | |
84 | u3 = Session().query(User).get(self.u3) |
|
86 | Session().commit() | |
|
87 | u3 = Session().query(User).get(self.u3) | |||
85 |
|
88 | |||
86 | assert sorted([x.notification for x in u3.notifications]) == sorted([notification2, notification1]) |
|
89 | assert sorted([x.notification for x in u3.notifications]) == sorted([notification2, notification1]) | |
87 |
|
90 | |||
88 | def test_delete_notifications(self): |
|
91 | def test_delete_notifications(self): | |
89 | notification = NotificationModel().create(created_by=self.u1, |
|
92 | with test_context(self.app): | |
90 | subject=u'title', body=u'hi there3', |
|
93 | notification = NotificationModel().create(created_by=self.u1, | |
91 | recipients=[self.u3, self.u1, self.u2]) |
|
94 | subject=u'title', body=u'hi there3', | |
92 | Session().commit() |
|
95 | recipients=[self.u3, self.u1, self.u2]) | |
93 | notifications = Notification.query().all() |
|
96 | Session().commit() | |
94 | assert notification in notifications |
|
97 | notifications = Notification.query().all() | |
|
98 | assert notification in notifications | |||
95 |
|
99 | |||
96 | Notification.delete(notification.notification_id) |
|
100 | Notification.delete(notification.notification_id) | |
97 | Session().commit() |
|
101 | Session().commit() | |
98 |
|
102 | |||
99 | notifications = Notification.query().all() |
|
103 | notifications = Notification.query().all() | |
100 | assert not notification in notifications |
|
104 | assert not notification in notifications | |
101 |
|
105 | |||
102 | un = UserNotification.query().filter(UserNotification.notification |
|
106 | un = UserNotification.query().filter(UserNotification.notification | |
103 | == notification).all() |
|
107 | == notification).all() | |
104 | assert un == [] |
|
108 | assert un == [] | |
105 |
|
109 | |||
106 | def test_delete_association(self): |
|
110 | def test_delete_association(self): | |
107 | notification = NotificationModel().create(created_by=self.u1, |
|
111 | with test_context(self.app): | |
108 | subject=u'title', body=u'hi there3', |
|
112 | notification = NotificationModel().create(created_by=self.u1, | |
109 | recipients=[self.u3, self.u1, self.u2]) |
|
113 | subject=u'title', body=u'hi there3', | |
110 | Session().commit() |
|
114 | recipients=[self.u3, self.u1, self.u2]) | |
|
115 | Session().commit() | |||
111 |
|
116 | |||
112 | unotification = UserNotification.query() \ |
|
117 | unotification = UserNotification.query() \ | |
113 | .filter(UserNotification.notification == |
|
118 | .filter(UserNotification.notification == | |
114 | notification) \ |
|
119 | notification) \ | |
115 | .filter(UserNotification.user_id == self.u3) \ |
|
120 | .filter(UserNotification.user_id == self.u3) \ | |
116 | .scalar() |
|
121 | .scalar() | |
117 |
|
122 | |||
118 | assert unotification.user_id == self.u3 |
|
123 | assert unotification.user_id == self.u3 | |
119 |
|
124 | |||
120 | NotificationModel().delete(self.u3, |
|
125 | NotificationModel().delete(self.u3, | |
121 | notification.notification_id) |
|
126 | notification.notification_id) | |
122 | Session().commit() |
|
127 | Session().commit() | |
123 |
|
128 | |||
124 | u3notification = UserNotification.query() \ |
|
129 | u3notification = UserNotification.query() \ | |
125 | .filter(UserNotification.notification == |
|
130 | .filter(UserNotification.notification == | |
126 | notification) \ |
|
131 | notification) \ | |
127 | .filter(UserNotification.user_id == self.u3) \ |
|
132 | .filter(UserNotification.user_id == self.u3) \ | |
128 | .scalar() |
|
133 | .scalar() | |
129 |
|
134 | |||
130 | assert u3notification == None |
|
135 | assert u3notification == None | |
131 |
|
136 | |||
132 | # notification object is still there |
|
137 | # notification object is still there | |
133 | assert Notification.query().all() == [notification] |
|
138 | assert Notification.query().all() == [notification] | |
134 |
|
139 | |||
135 | #u1 and u2 still have assignments |
|
140 | #u1 and u2 still have assignments | |
136 | u1notification = UserNotification.query() \ |
|
141 | u1notification = UserNotification.query() \ | |
137 | .filter(UserNotification.notification == |
|
142 | .filter(UserNotification.notification == | |
138 | notification) \ |
|
143 | notification) \ | |
139 | .filter(UserNotification.user_id == self.u1) \ |
|
144 | .filter(UserNotification.user_id == self.u1) \ | |
140 | .scalar() |
|
145 | .scalar() | |
141 | assert u1notification != None |
|
146 | assert u1notification != None | |
142 | u2notification = UserNotification.query() \ |
|
147 | u2notification = UserNotification.query() \ | |
143 | .filter(UserNotification.notification == |
|
148 | .filter(UserNotification.notification == | |
144 | notification) \ |
|
149 | notification) \ | |
145 | .filter(UserNotification.user_id == self.u2) \ |
|
150 | .filter(UserNotification.user_id == self.u2) \ | |
146 | .scalar() |
|
151 | .scalar() | |
147 | assert u2notification != None |
|
152 | assert u2notification != None | |
148 |
|
153 | |||
149 | def test_notification_counter(self): |
|
154 | def test_notification_counter(self): | |
150 | NotificationModel().create(created_by=self.u1, |
|
155 | with test_context(self.app): | |
151 | subject=u'title', body=u'hi there_delete', |
|
156 | NotificationModel().create(created_by=self.u1, | |
152 | recipients=[self.u3, self.u1]) |
|
157 | subject=u'title', body=u'hi there_delete', | |
153 | Session().commit() |
|
158 | recipients=[self.u3, self.u1]) | |
154 |
|
159 | Session().commit() | ||
155 | assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0 |
|
|||
156 | assert NotificationModel().get_unread_cnt_for_user(self.u2) == 0 |
|
|||
157 | assert NotificationModel().get_unread_cnt_for_user(self.u3) == 1 |
|
|||
158 |
|
160 | |||
159 | notification = NotificationModel().create(created_by=self.u1, |
|
161 | assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0 | |
160 | subject=u'title', body=u'hi there3', |
|
162 | assert NotificationModel().get_unread_cnt_for_user(self.u2) == 0 | |
161 | recipients=[self.u3, self.u1, self.u2]) |
|
163 | assert NotificationModel().get_unread_cnt_for_user(self.u3) == 1 | |
162 | Session().commit() |
|
|||
163 |
|
164 | |||
164 | assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0 |
|
165 | notification = NotificationModel().create(created_by=self.u1, | |
165 | assert NotificationModel().get_unread_cnt_for_user(self.u2) == 1 |
|
166 | subject=u'title', body=u'hi there3', | |
166 | assert NotificationModel().get_unread_cnt_for_user(self.u3) == 2 |
|
167 | recipients=[self.u3, self.u1, self.u2]) | |
|
168 | Session().commit() | |||
|
169 | ||||
|
170 | assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0 | |||
|
171 | assert NotificationModel().get_unread_cnt_for_user(self.u2) == 1 | |||
|
172 | assert NotificationModel().get_unread_cnt_for_user(self.u3) == 2 | |||
167 |
|
173 | |||
168 | @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()))))) |
|
174 | @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()))))) | |
169 | def test_dump_html_mails(self): |
|
175 | def test_dump_html_mails(self): | |
@@ -184,92 +190,93 b' class TestNotifications(TestController):' | |||||
184 | l.append(html_body) |
|
190 | l.append(html_body) | |
185 | l.append('<hr/>\n') |
|
191 | l.append('<hr/>\n') | |
186 |
|
192 | |||
187 | with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email): |
|
193 | with test_context(self.app): | |
188 | pr_kwargs = dict( |
|
194 | with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email): | |
189 |
pr_ |
|
195 | pr_kwargs = dict( | |
190 |
|
|
196 | pr_nice_id='#7', | |
191 |
pr_title |
|
197 | pr_title='The Title', | |
192 | pr_url='http://pr.org/7', |
|
198 | pr_title_short='The Title', | |
193 |
|
|
199 | pr_url='http://pr.org/7', | |
194 |
pr_target_ |
|
200 | pr_target_repo='http://mainline.com/repo', | |
195 | pr_source_repo='https://dev.org/repo', |
|
201 | pr_target_branch='trunk', | |
196 |
pr_source_ |
|
202 | pr_source_repo='https://dev.org/repo', | |
197 | pr_owner=User.get(self.u2), |
|
203 | pr_source_branch='devbranch', | |
198 |
pr_owner |
|
204 | pr_owner=User.get(self.u2), | |
199 | ) |
|
205 | pr_owner_username='u2' | |
|
206 | ) | |||
200 |
|
207 | |||
201 | for type_, body, kwargs in [ |
|
208 | for type_, body, kwargs in [ | |
202 | (Notification.TYPE_CHANGESET_COMMENT, |
|
209 | (Notification.TYPE_CHANGESET_COMMENT, | |
203 | u'This is the new comment.\n\n - and here it ends indented.', |
|
210 | u'This is the new comment.\n\n - and here it ends indented.', | |
204 | dict( |
|
211 | dict( | |
205 | short_id='cafe1234', |
|
212 | short_id='cafe1234', | |
206 | raw_id='cafe1234c0ffeecafe', |
|
213 | raw_id='cafe1234c0ffeecafe', | |
207 | branch='brunch', |
|
214 | branch='brunch', | |
208 | cs_comment_user='Opinionated User (jsmith)', |
|
215 | cs_comment_user='Opinionated User (jsmith)', | |
209 | cs_comment_url='http://comment.org', |
|
216 | cs_comment_url='http://comment.org', | |
210 | is_mention=[False, True], |
|
217 | is_mention=[False, True], | |
211 | message='This changeset did something clever which is hard to explain', |
|
218 | message='This changeset did something clever which is hard to explain', | |
212 | message_short='This changeset did something cl...', |
|
219 | message_short='This changeset did something cl...', | |
213 | status_change=[None, 'Approved'], |
|
220 | status_change=[None, 'Approved'], | |
214 | cs_target_repo='repo_target', |
|
221 | cs_target_repo='repo_target', | |
215 | cs_url='http://changeset.com', |
|
222 | cs_url='http://changeset.com', | |
216 | cs_author=User.get(self.u2))), |
|
223 | cs_author=User.get(self.u2))), | |
217 | (Notification.TYPE_MESSAGE, |
|
224 | (Notification.TYPE_MESSAGE, | |
218 | u'This is the body of the test message\n - nothing interesting here except indentation.', |
|
225 | u'This is the body of the test message\n - nothing interesting here except indentation.', | |
219 | dict()), |
|
226 | dict()), | |
220 | #(Notification.TYPE_MENTION, '$body', None), # not used |
|
227 | #(Notification.TYPE_MENTION, '$body', None), # not used | |
221 | (Notification.TYPE_REGISTRATION, |
|
228 | (Notification.TYPE_REGISTRATION, | |
222 | u'Registration body', |
|
229 | u'Registration body', | |
223 | dict( |
|
230 | dict( | |
224 | new_username='newbie', |
|
231 | new_username='newbie', | |
225 | registered_user_url='http://newbie.org', |
|
232 | registered_user_url='http://newbie.org', | |
226 | new_email='new@email.com', |
|
233 | new_email='new@email.com', | |
227 | new_full_name='New Full Name')), |
|
234 | new_full_name='New Full Name')), | |
228 | (Notification.TYPE_PULL_REQUEST, |
|
235 | (Notification.TYPE_PULL_REQUEST, | |
229 | u'This PR is awesome because it does stuff\n - please approve indented!', |
|
236 | u'This PR is awesome because it does stuff\n - please approve indented!', | |
230 | dict( |
|
237 | dict( | |
231 | pr_user_created='Requesting User (root)', # pr_owner should perhaps be used for @mention in description ... |
|
238 | pr_user_created='Requesting User (root)', # pr_owner should perhaps be used for @mention in description ... | |
232 | is_mention=[False, True], |
|
239 | is_mention=[False, True], | |
233 | pr_revisions=[('123abc'*7, "Introduce one and two\n\nand that's it"), ('567fed'*7, 'Make one plus two equal tree')], |
|
240 | pr_revisions=[('123abc'*7, "Introduce one and two\n\nand that's it"), ('567fed'*7, 'Make one plus two equal tree')], | |
234 | org_repo_name='repo_org', |
|
241 | org_repo_name='repo_org', | |
235 | **pr_kwargs)), |
|
242 | **pr_kwargs)), | |
236 | (Notification.TYPE_PULL_REQUEST_COMMENT, |
|
243 | (Notification.TYPE_PULL_REQUEST_COMMENT, | |
237 | u'Me too!\n\n - and indented on second line', |
|
244 | u'Me too!\n\n - and indented on second line', | |
238 | dict( |
|
245 | dict( | |
239 | closing_pr=[False, True], |
|
246 | closing_pr=[False, True], | |
240 | is_mention=[False, True], |
|
247 | is_mention=[False, True], | |
241 | pr_comment_user='Opinionated User (jsmith)', |
|
248 | pr_comment_user='Opinionated User (jsmith)', | |
242 | pr_comment_url='http://pr.org/comment', |
|
249 | pr_comment_url='http://pr.org/comment', | |
243 | status_change=[None, 'Under Review'], |
|
250 | status_change=[None, 'Under Review'], | |
244 | **pr_kwargs)), |
|
251 | **pr_kwargs)), | |
245 | ]: |
|
252 | ]: | |
246 | kwargs['repo_name'] = u'repo/name' |
|
253 | kwargs['repo_name'] = u'repo/name' | |
247 | params = [(type_, type_, body, kwargs)] |
|
254 | params = [(type_, type_, body, kwargs)] | |
248 | for param_name in ['is_mention', 'status_change', 'closing_pr']: # TODO: inline/general |
|
255 | for param_name in ['is_mention', 'status_change', 'closing_pr']: # TODO: inline/general | |
249 | if not isinstance(kwargs.get(param_name), list): |
|
256 | if not isinstance(kwargs.get(param_name), list): | |
250 | continue |
|
257 | continue | |
251 | new_params = [] |
|
258 | new_params = [] | |
252 | for v in kwargs[param_name]: |
|
259 | for v in kwargs[param_name]: | |
253 | for desc, type_, body, kwargs in params: |
|
260 | for desc, type_, body, kwargs in params: | |
254 | kwargs = dict(kwargs) |
|
261 | kwargs = dict(kwargs) | |
255 | kwargs[param_name] = v |
|
262 | kwargs[param_name] = v | |
256 | new_params.append(('%s, %s=%r' % (desc, param_name, v), type_, body, kwargs)) |
|
263 | new_params.append(('%s, %s=%r' % (desc, param_name, v), type_, body, kwargs)) | |
257 | params = new_params |
|
264 | params = new_params | |
258 |
|
265 | |||
259 | for desc, type_, body, kwargs in params: |
|
266 | for desc, type_, body, kwargs in params: | |
260 | # desc is used as "global" variable |
|
267 | # desc is used as "global" variable | |
261 | notification = NotificationModel().create(created_by=self.u1, |
|
268 | notification = NotificationModel().create(created_by=self.u1, | |
262 | subject=u'unused', body=body, email_kwargs=kwargs, |
|
269 | subject=u'unused', body=body, email_kwargs=kwargs, | |
263 | recipients=[self.u2], type_=type_) |
|
270 | recipients=[self.u2], type_=type_) | |
264 |
|
271 | |||
265 | # Email type TYPE_PASSWORD_RESET has no corresponding notification type - test it directly: |
|
272 | # Email type TYPE_PASSWORD_RESET has no corresponding notification type - test it directly: | |
266 | desc = 'TYPE_PASSWORD_RESET' |
|
273 | desc = 'TYPE_PASSWORD_RESET' | |
267 | kwargs = dict(user='John Doe', reset_token='decbf64715098db5b0bd23eab44bd792670ab746', reset_url='http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746') |
|
274 | kwargs = dict(user='John Doe', reset_token='decbf64715098db5b0bd23eab44bd792670ab746', reset_url='http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746') | |
268 | kallithea.lib.celerylib.tasks.send_email(['john@doe.com'], |
|
275 | kallithea.lib.celerylib.tasks.send_email(['john@doe.com'], | |
269 | "Password reset link", |
|
276 | "Password reset link", | |
270 | EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'txt', **kwargs), |
|
277 | EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'txt', **kwargs), | |
271 | EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'html', **kwargs), |
|
278 | EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'html', **kwargs), | |
272 | author=User.get(self.u1)) |
|
279 | author=User.get(self.u1)) | |
273 |
|
280 | |||
274 | 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' % \ |
|
281 | 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' % \ | |
275 | re.sub(r'<(/?(?:!doctype|html|head|title|meta|body)\b[^>]*)>', r'<!--\1-->', ''.join(l)) |
|
282 | re.sub(r'<(/?(?:!doctype|html|head|title|meta|body)\b[^>]*)>', r'<!--\1-->', ''.join(l)) |
@@ -31,6 +31,7 b' import mock' | |||||
31 | from kallithea.tests.base import * |
|
31 | from kallithea.tests.base import * | |
32 | from kallithea.lib.utils2 import AttributeDict |
|
32 | from kallithea.lib.utils2 import AttributeDict | |
33 | from kallithea.model.db import Repository |
|
33 | from kallithea.model.db import Repository | |
|
34 | from kallithea.tests.test_context import test_context | |||
34 |
|
35 | |||
35 | proto = 'http' |
|
36 | proto = 'http' | |
36 | TEST_URLS = [ |
|
37 | TEST_URLS = [ | |
@@ -154,9 +155,10 b' class TestLibs(TestController):' | |||||
154 | def test_age(self, age_args, expected): |
|
155 | def test_age(self, age_args, expected): | |
155 | from kallithea.lib.utils2 import age |
|
156 | from kallithea.lib.utils2 import age | |
156 | from dateutil import relativedelta |
|
157 | from dateutil import relativedelta | |
157 | n = datetime.datetime(year=2012, month=5, day=17) |
|
158 | with test_context(self.app): | |
158 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) |
|
159 | n = datetime.datetime(year=2012, month=5, day=17) | |
159 | assert age(n + delt(**age_args), now=n) == expected |
|
160 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) | |
|
161 | assert age(n + delt(**age_args), now=n) == expected | |||
160 |
|
162 | |||
161 | @parametrize('age_args,expected', [ |
|
163 | @parametrize('age_args,expected', [ | |
162 | (dict(), u'just now'), |
|
164 | (dict(), u'just now'), | |
@@ -178,9 +180,10 b' class TestLibs(TestController):' | |||||
178 | def test_age_short(self, age_args, expected): |
|
180 | def test_age_short(self, age_args, expected): | |
179 | from kallithea.lib.utils2 import age |
|
181 | from kallithea.lib.utils2 import age | |
180 | from dateutil import relativedelta |
|
182 | from dateutil import relativedelta | |
181 | n = datetime.datetime(year=2012, month=5, day=17) |
|
183 | with test_context(self.app): | |
182 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) |
|
184 | n = datetime.datetime(year=2012, month=5, day=17) | |
183 | assert age(n + delt(**age_args), show_short_version=True, now=n) == expected |
|
185 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) | |
|
186 | assert age(n + delt(**age_args), show_short_version=True, now=n) == expected | |||
184 |
|
187 | |||
185 | @parametrize('age_args,expected', [ |
|
188 | @parametrize('age_args,expected', [ | |
186 | (dict(), u'just now'), |
|
189 | (dict(), u'just now'), | |
@@ -196,9 +199,10 b' class TestLibs(TestController):' | |||||
196 | def test_age_in_future(self, age_args, expected): |
|
199 | def test_age_in_future(self, age_args, expected): | |
197 | from kallithea.lib.utils2 import age |
|
200 | from kallithea.lib.utils2 import age | |
198 | from dateutil import relativedelta |
|
201 | from dateutil import relativedelta | |
199 | n = datetime.datetime(year=2012, month=5, day=17) |
|
202 | with test_context(self.app): | |
200 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) |
|
203 | n = datetime.datetime(year=2012, month=5, day=17) | |
201 | assert age(n + delt(**age_args), now=n) == expected |
|
204 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) | |
|
205 | assert age(n + delt(**age_args), now=n) == expected | |||
202 |
|
206 | |||
203 | def test_tag_extractor(self): |
|
207 | def test_tag_extractor(self): | |
204 | sample = ( |
|
208 | sample = ( |
General Comments 0
You need to be logged in to leave comments.
Login now