You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2016/01/27 18:42:54 UTC

[7/8] allura git commit: [#8044] ticket:891 Add url parameter to notification API, change notification API test

[#8044] ticket:891 Add url parameter to notification API, change notification API test


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/62cbf932
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/62cbf932
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/62cbf932

Branch: refs/heads/master
Commit: 62cbf93216e98f421ed0573ef13186ffaeafd8ed
Parents: af8bd6e
Author: Denis Kotov <de...@gmail.com>
Authored: Sat Jan 23 14:48:42 2016 +0200
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Jan 27 12:37:38 2016 -0500

----------------------------------------------------------------------
 Allura/allura/controllers/rest.py           | 17 +++++++----------
 Allura/allura/lib/plugin.py                 |  8 ++++----
 Allura/allura/tests/functional/test_rest.py | 13 +++++++++++--
 3 files changed, 22 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/62cbf932/Allura/allura/controllers/rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/rest.py b/Allura/allura/controllers/rest.py
index ef09398..70a9ffd 100644
--- a/Allura/allura/controllers/rest.py
+++ b/Allura/allura/controllers/rest.py
@@ -90,17 +90,14 @@ class RestController(object):
         return summary
 
     @expose('json:')
-    def notification(self, cookie='', **kw):
+    def notification(self, cookie='', url='', **kw):
         c.api_token = self._authenticate_request()
-        if c.api_token:
-            r = g.theme._get_site_notification(
-                user=c.api_token.user,
-                site_notification_cookie_value=cookie
-            )
-        else:
-            r = g.theme._get_site_notification(
-                site_notification_cookie_value=cookie
-            )
+        user = c.api_token.user if c.api_token else c.user
+        r = g.theme._get_site_notification(
+            url=url,
+            user=user,
+            site_notification_cookie_value=cookie
+        )
         if r:
             return dict(notification=r[0], cookie=r[1])
         return {}

http://git-wip-us.apache.org/repos/asf/allura/blob/62cbf932/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 6ae6eb0..dffd1fa 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -1310,20 +1310,19 @@ class ThemeProvider(object):
         else:
             return app.icon_url(size)
 
-    def _get_site_notification(self, user=None, site_notification_cookie_value=''):
-        from pylons import request
+    def _get_site_notification(self, url='', user=None, site_notification_cookie_value=''):
         from allura.model.notification import SiteNotification
         note = SiteNotification.current()
         if note is None:
             return None
-        if note.user_role and (not user or user.is_anonymous()):
+        if note.user_role and user.is_anonymous():
             return None
         if note.user_role:
             projects = user.my_projects_by_role_name(note.user_role)
             if len(projects) == 0 or len(projects) == 1 and projects[0].is_user_project:
                 return None
 
-        if note.page_regex and re.search(note.page_regex, request.path_qs) is None:
+        if note.page_regex and re.search(note.page_regex, url) is None:
             return None
         if note.page_tool_type and (c.app is None or c.app.config.tool_name.lower() != note.page_tool_type.lower()):
             return None
@@ -1345,6 +1344,7 @@ class ThemeProvider(object):
         from pylons import request, response
 
         r = self._get_site_notification(
+            request.path_qs,
             c.user,
             request.cookies.get('site-notification', '')
         )

http://git-wip-us.apache.org/repos/asf/allura/blob/62cbf932/Allura/allura/tests/functional/test_rest.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_rest.py b/Allura/allura/tests/functional/test_rest.py
index 0ff0526..3ec58f6 100644
--- a/Allura/allura/tests/functional/test_rest.py
+++ b/Allura/allura/tests/functional/test_rest.py
@@ -387,17 +387,26 @@ class TestRestHome(TestRestApiBase):
 
     @mock.patch('allura.lib.plugin.ThemeProvider._get_site_notification')
     def test_notification(self, _get_site_notification):
+        user = M.User.by_username('test-admin')
         note = M.SiteNotification()
         cookie = '{}-1-False'.format(note._id)
         g.theme._get_site_notification = mock.Mock(return_value=(note, cookie))
 
-        r = self.app.get('/rest/notification')
+        r = self.app.get('/rest/notification?url=test_url&cookie=test_cookie')
+        g.theme._get_site_notification.assert_called_once_with(
+            url='test_url',
+            site_notification_cookie_value='test_cookie',
+            user=user
+        )
 
         assert r.status_int == 200
-        print r.json
         assert r.json['cookie'] == cookie
         assert r.json['notification'] == note.__json__()
 
+        g.theme._get_site_notification = mock.Mock(return_value=None)
+        r = self.app.get('/rest/notification')
+        assert r.json == {}
+
 
 
 class TestDoap(TestRestApiBase):