Changeset 14785


Ignore:
Timestamp:
Jul 11, 2015, 2:53:25 PM (9 years ago)
Author:
Steffen Hoffmann
Message:

TagsPlugin: Catch inappropriate macro arguments for ListTagged, refs #12292.

Thanks for the patch including unit tests by Ryan J. Ollos, applied here
with minor modifications.

Location:
tagsplugin/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tagsplugin/trunk/changelog

    r14153 r14785  
    88 * #11950: The tag TracLink doesn't allow trailing arguments
    99   by supporting the 'realm' attribute in tag query expressions
     10 * #12292: AssertionError: Page 5 out of range
    1011
    1112 new features
  • tagsplugin/trunk/tractags/macros.py

    r14159 r14785  
    33# Copyright (C) 2006 Alec Thomas <alec@swapoff.org>
    44# Copyright (C) 2011 Itamar Ostricher <itamarost@gmail.com>
    5 # Copyright (C) 2011-2014 Steffen Hoffmann <hoff.st@web.de>
     5# Copyright (C) 2011-2015 Steffen Hoffmann <hoff.st@web.de>
     6# Copyright (C) 2015 Ryan J Ollos <ryan.j.ollos@gmail.com>
    67#
    78# This software is licensed as described in the file COPYING, which
     
    1516
    1617from trac.config import BoolOption, ListOption, Option
    17 from trac.core import Component, implements
     18from trac.core import Component, TracError, implements
    1819from trac.resource import Resource, get_resource_description, \
    1920                          get_resource_url, render_resource_link
     
    357358                continue
    358359            if ul:
    359                 # Found new tag for cloud; now add previously prepared one. 
     360                # Found new tag for cloud; now add previously prepared one.
    360361                ul('\n', li)
    361362            else:
     
    372373    def _paginate(self, req, results, realms):
    373374        query = req.args.get('q', None)
    374         current_page = as_int(req.args.get('listtagged_page'), 1)
    375         items_per_page = as_int(req.args.get('listtagged_per_page'), None)
    376         if items_per_page is None:
     375        current_page = as_int(req.args.get('listtagged_page'), 1, min=1)
     376        items_per_page = as_int(req.args.get('listtagged_per_page'),
     377                                self.items_per_page)
     378        if items_per_page < 1:
    377379            items_per_page = self.items_per_page
    378         result = Paginator(results, current_page - 1, items_per_page)
     380        try:
     381            result = Paginator(results, current_page - 1, items_per_page)
     382        except (AssertionError, TracError), e:
     383            self.log.warn("ListTagged macro: %s", e)
     384            current_page = 1
     385            result = Paginator(results, current_page - 1, items_per_page)
    379386
    380387        pagedata = []
  • tagsplugin/trunk/tractags/tests/macros.py

    r14154 r14785  
    44# Copyright (C) 2012-2014 Steffen Hoffmann <hoff.st@web.de>
    55# Copyright (C) 2014 Jun Omae <jun66j5@gmail.com>
     6# Copyright (C) 2015 Ryan J Ollos <ryan.j.ollos@gmail.com>
    67#
    78# This software is licensed as described in the file COPYING, which
     
    5051                       % self.db.like(), ('TAGS_%',))
    5152
     53    def _insert_tags(self, tagspace, name, tags):
     54        cursor = self.db.cursor()
     55        args = [(tagspace, name, tag) for tag in tags]
     56        cursor.executemany("INSERT INTO tags (tagspace,name,tag) "
     57                           "VALUES (%s,%s,%s)", args)
     58
    5259
    5360class TagTemplateProviderTestCase(_BaseTestCase):
     
    6572
    6673class ListTaggedMacroTestCase(_BaseTestCase):
    67    
     74
    6875    def setUp(self):
    6976        _BaseTestCase.setUp(self)
     
    8289                        str(self.tag_twm.expand_macro(formatter,
    8390                                                      'ListTagged', '')))
     91
     92    def _test_listtagged_paginate(self, page, per_page=2):
     93        self._insert_tags('wiki', 'InterTrac', ('blah',))
     94        self._insert_tags('wiki', 'InterWiki', ('blah',))
     95        self._insert_tags('wiki', 'WikiStart', ('blah',))
     96        self.req.args['listtagged_per_page'] = per_page
     97        self.req.args['listtagged_page'] = page
     98        context = Mock(env=self.env, href=Href('/'), req=self.req)
     99        formatter = Mock(context=context, req=self.req)
     100        result = \
     101            unicode(self.tag_twm.expand_macro(formatter, 'ListTagged', 'blah'))
     102        return result
     103
     104    def test_listtagged_paginate_page1(self):
     105        """Paginate results for page 1 has two items."""
     106        result = self._test_listtagged_paginate(1)
     107        self.assertTrue('InterTrac' in result)
     108        self.assertTrue('InterWiki' in result)
     109        self.assertFalse('WikiStart' in result)
     110
     111    def test_listtagged_paginate_page2(self):
     112        """Paginate results for page 2 has one item."""
     113        result = self._test_listtagged_paginate(2)
     114        self.assertFalse('InterTrac' in result)
     115        self.assertFalse('InterWiki' in result)
     116        self.assertTrue('WikiStart' in result)
     117
     118    def test_listtagged_paginate_page_out_of_range(self):
     119        """Out of range page defaults to 1."""
     120        result = self._test_listtagged_paginate(3)
     121        self.assertTrue('InterTrac' in result)
     122        self.assertTrue('InterWiki' in result)
     123        self.assertFalse('WikiStart' in result)
     124
     125    def test_listtagged_paginate_page_invalid(self):
     126        """Invalid page default to 1."""
     127        result = self._test_listtagged_paginate(-1)
     128        self.assertTrue('InterTrac' in result)
     129        self.assertTrue('InterWiki' in result)
     130        self.assertFalse('WikiStart' in result)
     131
     132    def test_listtagged_paginate_per_page_invalid(self):
     133        """Invalid per_page defaults to items_per_page (100)."""
     134        result = self._test_listtagged_paginate(2, -1)
     135        self.assertTrue('InterTrac' in result)
     136        self.assertTrue('InterWiki' in result)
     137        self.assertTrue('WikiStart' in result)
    84138
    85139
     
    102156    def _expand_macro(self, content):
    103157        return self.tag_twm.expand_macro(self.formatter, 'TagCloud', content)
    104 
    105     def _insert_tags(self, tagspace, name, tags):
    106         cursor = self.db.cursor()
    107         args = [(tagspace, name, tag) for tag in tags]
    108         cursor.executemany("INSERT INTO tags (tagspace,name,tag) "
    109                            "VALUES (%s,%s,%s)", args)
    110158
    111159    # Tests
Note: See TracChangeset for help on using the changeset viewer.