You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by ke...@apache.org on 2019/11/18 21:46:25 UTC

[allura] 10/11: [#8340] more misc coverage

This is an automated email from the ASF dual-hosted git repository.

kentontaylor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 5099080db69184f039f945b67921df3e87f2c198
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Fri Nov 15 12:42:51 2019 -0500

    [#8340] more misc coverage
---
 Allura/allura/lib/macro.py                  |  2 +-
 Allura/allura/lib/stats.py                  | 85 -----------------------------
 Allura/allura/tests/functional/test_root.py |  5 ++
 Allura/allura/tests/test_globals.py         | 11 ++++
 Allura/allura/tests/test_mail_util.py       | 16 ++++++
 Allura/allura/tests/test_utils.py           | 18 +++++-
 6 files changed, 50 insertions(+), 87 deletions(-)

diff --git a/Allura/allura/lib/macro.py b/Allura/allura/lib/macro.py
index b01eaad..4dc1f54 100644
--- a/Allura/allura/lib/macro.py
+++ b/Allura/allura/lib/macro.py
@@ -312,7 +312,7 @@ def project_screenshots():
     from allura.lib.widgets.project_list import ProjectScreenshots
     ps = ProjectScreenshots()
     g.resource_manager.register(ps)
-    response = ps.display(project=c.project)
+    response = ps.display(project=c.project, h=h)
     return response
 
 
diff --git a/Allura/allura/lib/stats.py b/Allura/allura/lib/stats.py
deleted file mode 100644
index d5244b1..0000000
--- a/Allura/allura/lib/stats.py
+++ /dev/null
@@ -1,85 +0,0 @@
-#       Licensed to the Apache Software Foundation (ASF) under one
-#       or more contributor license agreements.  See the NOTICE file
-#       distributed with this work for additional information
-#       regarding copyright ownership.  The ASF licenses this file
-#       to you under the Apache License, Version 2.0 (the
-#       "License"); you may not use this file except in compliance
-#       with the License.  You may obtain a copy of the License at
-#
-#         http://www.apache.org/licenses/LICENSE-2.0
-#
-#       Unless required by applicable law or agreed to in writing,
-#       software distributed under the License is distributed on an
-#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-#       KIND, either express or implied.  See the License for the
-#       specific language governing permissions and limitations
-#       under the License.
-
-from __future__ import with_statement
-from time import time
-from contextlib import contextmanager
-from tg import request
-
-
-class StatsRecord(object):
-
-    def __init__(self, request, active):
-        self.timers = dict(
-            mongo=0,
-            template=0,
-            total=0)
-        self.url = request.environ['PATH_INFO']
-        self.active = active
-        # Avoid double-timing things
-        self._now_timing = set()
-
-    def __repr__(self):
-        stats = ' '.join(
-            ('%s=%.0fms' % (k, v * 1000))
-            for k, v in sorted(self.timers.iteritems()))
-        return '%s: %s' % (self.url, stats)
-
-    def asdict(self):
-        return dict(
-            url=self.url,
-            timers=self.timers)
-
-    @contextmanager
-    def timing(self, name):
-        if self.active and name not in self._now_timing:
-            self._now_timing.add(name)
-            self.timers.setdefault(name, 0)
-            begin = time()
-            try:
-                yield
-            finally:
-                end = time()
-                self.timers[name] += end - begin
-                self._now_timing.remove(name)
-        else:
-            yield
-
-
-class timing(object):
-
-    '''Decorator to time a method call'''
-
-    def __init__(self, timer):
-        self.timer = timer
-
-    def __call__(self, func):
-        def inner(*l, **kw):
-            try:
-                stats = request.environ['sf.stats']
-            except TypeError:
-                return func(*l, **kw)
-            with stats.timing(self.timer):
-                return func(*l, **kw)
-        inner.__name__ = func.__name__
-        return inner
-
-    def decorate(self, obj, names):
-        names = names.split()
-        for name in names:
-            setattr(obj, name,
-                    self(getattr(obj, name)))
diff --git a/Allura/allura/tests/functional/test_root.py b/Allura/allura/tests/functional/test_root.py
index ca078fb..e6a6bc7 100644
--- a/Allura/allura/tests/functional/test_root.py
+++ b/Allura/allura/tests/functional/test_root.py
@@ -199,6 +199,11 @@ class TestRootController(TestController):
                          NeighborhoodController.index.__wrapped__)
             set_transaction_name.assert_called_with('foo')
 
+    def test_error_page(self):
+        # hard to force a real error (esp. with middleware debugging being different for tests) but we can hit direct:
+        r = self.app.get('/error/document')
+        r.mustcontain("We're sorry but we weren't able to process")
+
 
 class TestRootWithSSLPattern(TestController):
     def setUp(self):
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index 7176654..f7b2be4 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -667,6 +667,17 @@ def test_project_blog_posts_macro():
         assert_in('by <em>Test Admin</em>', r)
 
 
+def test_project_screenshots_macro():
+    with h.push_context('test', neighborhood='Projects'):
+        M.ProjectFile(project_id=c.project._id, category='screenshot', caption='caption', filename='test_file.jpg')
+        ThreadLocalORMSession.flush_all()
+
+        r = g.markdown_wiki.convert('[[project_screenshots]]')
+
+        assert_in('href="/p/test/screenshot/test_file.jpg"', r)
+        assert_in('src="/p/test/screenshot/test_file.jpg/thumb"', r)
+
+
 def get_project_names(r):
     """
     Extracts a list of project names from a wiki page HTML.
diff --git a/Allura/allura/tests/test_mail_util.py b/Allura/allura/tests/test_mail_util.py
index 8568191..7b75f16 100644
--- a/Allura/allura/tests/test_mail_util.py
+++ b/Allura/allura/tests/test_mail_util.py
@@ -27,6 +27,7 @@ from ming.orm import ThreadLocalORMSession
 from tg import config as tg_config
 
 from alluratest.controller import setup_basic_test, setup_global_objects
+from allura.command.smtp_server import MailServer
 from allura.lib.utils import ConfigProxy
 from allura.app import Application
 from allura.lib.mail_util import (
@@ -323,3 +324,18 @@ def test_parse_message_id():
         'de31888f6be2d87dc377d9e713876bb514548625.patches@libjpeg-turbo.p.domain.net',
         'de31888f6be2d87dc377d9e713876bb514548625.patches@libjpeg-turbo.p.domain.net',
     ])
+
+
+class TestMailServer(object):
+
+    def setUp(self):
+        setup_basic_test()
+
+    @mock.patch('allura.command.base.log', autospec=True)
+    def test(self, log):
+        listen_port = ('0.0.0.0', 8825)
+        mailserver = MailServer(listen_port, None)
+        mailserver.process_message('127.0.0.1', 'foo@bar.com', ['1234@tickets.test.p.localhost'],
+                                   u'this is the email body with headers and everything Ο'.encode('utf-8'))
+        assert_equal([], log.exception.call_args_list)
+        log.info.assert_called_with('Msg passed along')
diff --git a/Allura/allura/tests/test_utils.py b/Allura/allura/tests/test_utils.py
index 8887cef..12017e2 100644
--- a/Allura/allura/tests/test_utils.py
+++ b/Allura/allura/tests/test_utils.py
@@ -410,4 +410,20 @@ def test_is_nofollow_url():
 def test_close_ipv4_addrs():
     assert utils.close_ipv4_addrs('1.2.3.4', '1.2.3.4')
     assert utils.close_ipv4_addrs('1.2.3.4', '1.2.3.255')
-    assert not utils.close_ipv4_addrs('1.2.3.4', '1.2.4.4')
\ No newline at end of file
+    assert not utils.close_ipv4_addrs('1.2.3.4', '1.2.4.4')
+
+
+def test_lsub_utf8():
+    assert_equal(b'asdf',
+                 utils.lsub_utf8(h.really_unicode('asdf').encode('utf-8'), 40))
+    assert_equal(b'as\xf0\x9f\x98\x84\xc2\xb6\xc2\xba\xc2\xb6',
+                 utils.lsub_utf8(h.really_unicode(u'as😄¶º¶').encode('utf-8'), 40))
+    assert_equal(b'as\xf0\x9f\x98\x84',
+                 utils.lsub_utf8(h.really_unicode(u'as😄¶º¶').encode('utf-8'), 6))
+    # these would truncate the smiley:
+    assert_equal(b'as',
+                 utils.lsub_utf8(h.really_unicode(u'as😄¶º¶').encode('utf-8'), 5))
+    assert_equal(b'as',
+                 utils.lsub_utf8(h.really_unicode(u'as😄¶º¶').encode('utf-8'), 4))
+    assert_equal(b'as',
+                 utils.lsub_utf8(h.really_unicode(u'as😄¶º¶').encode('utf-8'), 3))