示例#1
0
class MilestoneModuleTestCase(unittest.TestCase):
    def setUp(self):
        self.env = EnvironmentStub(default_data=True)
        self.mmodule = MilestoneModule(self.env)
        self.terms = ['MilestoneAlpha', 'MilestoneBeta', 'MilestoneGamma']
        for term in self.terms + [' '.join(self.terms)]:
            m = Milestone(self.env)
            m.name = term
            m.due = datetime_now(utc)
            m.description = u"""\
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim \
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate \
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat \
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id \
est laborum."""
            m.insert()

    def tearDown(self):
        self.env.reset_db()

    def test_invalid_post_request_raises_exception(self):
        req = MockRequest(self.env,
                          method='POST',
                          action=None,
                          args={'id': 'milestone1'})

        self.assertRaises(HTTPBadRequest,
                          MilestoneModule(self.env).process_request, req)

    def test_milestone_redirects_to_roadmap(self):
        """The path /milestone redirects to /roadmap."""
        def test_milestone_redirect(authname=None):
            req = MockRequest(self.env,
                              method='GET',
                              path_info='/milestone',
                              authname=authname)
            with self.assertRaises(RequestDone):
                self.mmodule.process_request(req)
            self.assertEqual('http://example.org/trac.cgi/roadmap',
                             req.headers_sent['Location'])
            self.assertEqual('302 Found', req._status)
            return req

        # Redirects for user with MILESTONE_VIEW
        req = test_milestone_redirect()
        self.assertIn('MILESTONE_VIEW', req.perm)

        # Redirects for user without MILESTONE_VIEW
        perm_sys = PermissionSystem(self.env)
        perm_sys.revoke_permission('anonymous', 'MILESTONE_VIEW')
        req = test_milestone_redirect('user1')
        self.assertNotIn('MILESTONE_VIEW', req.perm)

    def test_get_search_filters(self):
        req = MockRequest(self.env)
        filters = self.mmodule.get_search_filters(req)
        filters = list(filters)
        self.assertEqual(1, len(filters))
        self.assertEqual(2, len(filters[0]))
        self.assertEqual('milestone', filters[0][0])
        self.assertEqual('Milestones', filters[0][1])

    def test_get_search_results_milestone_not_in_filters(self):
        req = MockRequest(self.env)
        results = self.mmodule.get_search_results(req, self.terms, [])
        self.assertEqual([], list(results))

    def test_get_search_results_matches_all_terms(self):
        req = MockRequest(self.env)
        milestone = Milestone(self.env, ' '.join(self.terms))
        results = self.mmodule.get_search_results(req, self.terms,
                                                  ['milestone'])
        results = list(results)
        self.assertEqual(1, len(results))
        self.assertEqual(5, len(results[0]))
        self.assertEqual(
            '/trac.cgi/milestone/' + milestone.name.replace(' ', '%20'),
            results[0][0])
        self.assertEqual('Milestone ' + milestone.name, results[0][1])
        self.assertEqual(milestone.due, results[0][2])
        self.assertEqual('', results[0][3])
        shorten_desc = u"""\
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, \
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo \
consequat. Duis a ..."""
        self.assertEqual(shorten_desc, results[0][4])

    def test_default_group_by_default(self):
        """Default `default_group_by` is `component`."""
        req = MockRequest(self.env, args={'id': 'milestone1'})

        data = self.mmodule.process_request(req)[1]

        self.assertIn('grouped_by', data)
        self.assertEqual('component', data['grouped_by'])

    def test_default_group_by(self):
        """Option `default_group_by` is set in configuration."""
        self.env.config.set('milestone', 'default_group_by', 'priority')
        req = MockRequest(self.env, args={'id': 'milestone1'})

        data = self.mmodule.process_request(req)[1]

        self.assertIn('grouped_by', data)
        self.assertEqual('priority', data['grouped_by'])

    def test_get_search_results_matches_ignorecase(self):
        req = MockRequest(self.env)

        def search(terms):
            return list(
                self.mmodule.get_search_results(req, terms, ['milestone']))

        results = search(self.terms)
        self.assertEqual(results, search([t.lower() for t in self.terms]))
        self.assertEqual(results, search([t.upper() for t in self.terms]))
示例#2
0
class MilestoneModuleTestCase(unittest.TestCase):
    def setUp(self):
        self.env = EnvironmentStub(default_data=True)
        self.mmodule = MilestoneModule(self.env)
        self.terms = ['MilestoneAlpha', 'MilestoneBeta', 'MilestoneGamma']
        for term in self.terms + [' '.join(self.terms)]:
            m = Milestone(self.env)
            m.name = term
            m.due = datetime_now(utc)
            m.description = random_sentence()
            m.insert()

    def tearDown(self):
        self.env.reset_db()

    def test_invalid_post_request_raises_exception(self):
        req = MockRequest(self.env, method='POST', action=None)

        self.assertRaises(HTTPBadRequest,
                          MilestoneModule(self.env).process_request, req)

    def test_get_search_filters(self):
        req = MockRequest(self.env)
        filters = self.mmodule.get_search_filters(req)
        filters = list(filters)
        self.assertEqual(1, len(filters))
        self.assertEqual(2, len(filters[0]))
        self.assertEqual('milestone', filters[0][0])
        self.assertEqual('Milestones', filters[0][1])

    def test_get_search_results_milestone_not_in_filters(self):
        req = MockRequest(self.env)
        results = self.mmodule.get_search_results(req, self.terms, [])
        self.assertEqual([], list(results))

    def test_get_search_results_matches_all_terms(self):
        req = MockRequest(self.env)
        milestone = Milestone(self.env, ' '.join(self.terms))
        results = self.mmodule.get_search_results(req, self.terms,
                                                  ['milestone'])
        results = list(results)
        self.assertEqual(1, len(results))
        self.assertEqual(5, len(results[0]))
        self.assertEqual(
            '/trac.cgi/milestone/' + milestone.name.replace(' ', '%20'),
            results[0][0])
        self.assertEqual('Milestone ' + milestone.name, results[0][1])
        self.assertEqual(milestone.due, results[0][2])
        self.assertEqual('', results[0][3])
        self.assertEqual(milestone.description, results[0][4])

    def test_default_group_by_default(self):
        """Default `default_group_by` is `component`."""
        req = MockRequest(self.env, args={'id': 'milestone1'})

        data = self.mmodule.process_request(req)[1]

        self.assertIn('grouped_by', data)
        self.assertEqual('component', data['grouped_by'])

    def test_default_group_by(self):
        """Option `default_group_by` is set in configuration."""
        self.env.config.set('milestone', 'default_group_by', 'priority')
        req = MockRequest(self.env, args={'id': 'milestone1'})

        data = self.mmodule.process_request(req)[1]

        self.assertIn('grouped_by', data)
        self.assertEqual('priority', data['grouped_by'])

    def test_get_search_results_matches_ignorecase(self):
        req = MockRequest(self.env)

        def search(terms):
            return list(
                self.mmodule.get_search_results(req, terms, ['milestone']))

        results = search(self.terms)
        self.assertEqual(results, search([t.lower() for t in self.terms]))
        self.assertEqual(results, search([t.upper() for t in self.terms]))