Пример #1
0
 def _process_request(self, req):
     mod = PreferencesModule(self.env)
     self.assertTrue(mod.match_request(req))
     try:
         data = mod.process_request(req)[1]
     except RequestDone:
         return None
     else:
         return data
Пример #2
0
    def test_change_to_invalid_session_key_raises_exception(self):
        """Changing session key with invalid session key raises
        TracValueError.
        """
        req = MockRequest(self.env,
                          method='POST',
                          path_info='/prefs/advanced',
                          args={'newsid': 'a' * 23 + '$'})
        module = PreferencesModule(self.env)

        self.assertTrue(module.match_request(req))
        with self.assertRaises(TracValueError) as cm:
            module.process_request(req)
        self.assertEqual("Session ID must be alphanumeric.", str(cm.exception))
Пример #3
0
    def _test_auto_preview_timeout(self, timeout):
        req = MockRequest(self.env,
                          method='POST',
                          path_info='/prefs/userinterface',
                          cookie='trac_session=1234567890abcdef;',
                          args={'ui.auto_preview_timeout': timeout})
        module = PreferencesModule(self.env)

        self.assertTrue(module.match_request(req))
        with self.assertRaises(RequestDone) as cm:
            module.process_request(req)

        self.assertEqual("Your preferences have been saved.",
                         req.session['chrome.notices.0'])
        return req
Пример #4
0
    def test_load_invalid_session_key_raises_exception(self):
        """Loading session key with invalid session key raises
        TracValueError.
        """
        req = MockRequest(self.env, method='POST',
                          path_info='/prefs/advanced',
                          args={'loadsid': 'a' * 23 + '$', 'restore': True})
        module = PreferencesModule(self.env)

        self.assertTrue(module.match_request(req))
        try:
            module.process_request(req)
        except TracValueError as e:
            self.assertEqual("Session ID must be alphanumeric.", unicode(e))
        else:
            self.fail("TracValueError not raised.")
Пример #5
0
    def test_load_session_key(self):
        """Load session key."""
        old_sid = 'a' * 24
        new_sid, name, email = self._insert_session()
        req = MockRequest(self.env, method='POST',
                          path_info='/prefs/advanced',
                          cookie='trac_session=%s;' % old_sid,
                          args={'loadsid': new_sid, 'restore': True})
        module = PreferencesModule(self.env)

        self.assertEqual(old_sid, req.session.sid)
        self.assertIsNone(req.session.get('name'))
        self.assertIsNone(req.session.get('email'))
        self.assertTrue(module.match_request(req))
        module.process_request(req)
        self.assertIn("The session has been loaded.", req.chrome['notices'])
        self.assertEqual(new_sid, req.session.sid)
        self.assertEqual(name, req.session.get('name'))
        self.assertEqual(email, req.session.get('email'))
Пример #6
0
    def test_render_child_preferences_panels(self):
        create_file(
            os.path.join(self.templates_dir, self.child1_template_name),
            self.child_template)
        create_file(
            os.path.join(self.templates_dir, self.child2_template_name),
            self.child_template)

        req = MockRequest(self.env, path_info='/prefs/panel1')
        mod = PreferencesModule(self.env)

        self.assertTrue(mod.match_request(req))
        resp = mod.process_request(req)

        self.assertEqual('prefs_panel_1.html', resp[0])
        self.assertEqual(2, len(resp[1]['children']))
        self.assertEqual(('child1', 'Child 1', Markup(u'<h2>Child 1</h2>')),
                         resp[1]['children'][0])
        self.assertEqual(('child2', 'Child 2', Markup(u'<h2>Child 2</h2>')),
                         resp[1]['children'][1])
Пример #7
0
    def test_change_session_key(self):
        """Change session key."""
        old_sid, name, email = self._insert_session()
        new_sid = 'a' * 24
        req = MockRequest(self.env, method='POST',
                          path_info='/prefs/advanced',
                          cookie='trac_session=%s;' % old_sid,
                          args={'newsid': new_sid})
        module = PreferencesModule(self.env)

        self.assertEqual(old_sid, req.session.sid)
        self.assertEqual(name, req.session.get('name'))
        self.assertEqual(email, req.session.get('email'))
        self.assertTrue(module.match_request(req))
        self.assertRaises(RequestDone, module.process_request, req)
        self.assertIn("Your preferences have been saved.",
                      req.chrome['notices'])
        self.assertEqual(new_sid, req.session.sid)
        self.assertEqual(name, req.session.get('name'))
        self.assertEqual(email, req.session.get('email'))
Пример #8
0
class NotificationPreferencesTestCase(unittest.TestCase):

    def setUp(self):
        self.env = EnvironmentStub()
        self.prefs_mod = PreferencesModule(self.env)
        ids = self._add_subscriptions([
            {'sid': 'foo', 'class_': 'TicketOwnerSubscriber'},
            {'sid': 'foo', 'class_': 'CarbonCopySubscriber'},
            {'sid': 'foo', 'class_': 'TicketReporterSubscriber'},
            {'sid': 'bar', 'class_': 'CarbonCopySubscriber'},
        ])
        self.foo_rule0 = ids[0]
        self.foo_rule1 = ids[1]
        self.foo_rule2 = ids[2]
        self.bar_rule0 = ids[3]

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

    def _add_subscription(self, **kwargs):
        props = {'sid': None, 'authenticated': 1, 'distributor': 'email',
                 'format': 'text/plain', 'adverb': 'always',
                 'class': 'TicketOwnerSubscriber'}
        props.update((k.rstrip('_'),
                      (v or None) if isinstance(v, basestring) else v)
                     for k, v in kwargs.iteritems())
        assert props['sid'] is not None
        return Subscription.add(self.env, props)

    def _add_subscriptions(self, rows):
        with self.env.db_transaction:
            return [self._add_subscription(**row) for row in rows]

    def _get_rules(self, sid, authenticated=1):
        return self.env.db_query("""
            SELECT distributor, format, priority, adverb, class
            FROM notify_subscription
            WHERE sid=%s AND authenticated=1
            ORDER BY distributor, priority
            """, (sid,))

    def _request(self, **kwargs):
        kwargs.setdefault('method', 'POST')
        kwargs.setdefault('path_info', '/prefs/notification')
        return MockRequest(self.env, **kwargs)

    def _process(self, req):
        self.assertTrue(self.prefs_mod.match_request(req))
        return self.prefs_mod.process_request(req)

    def test_add_rule(self):
        args = {'action': 'add-rule_email', 'new-adverb-email': 'never',
                'format-email': '', 'new-rule-email': 'TicketOwnerSubscriber'}
        req = self._request(authname='baz', args=args)
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', None, 1, 'never', 'TicketOwnerSubscriber')],
            self._get_rules('baz'))

        args = {'action': 'add-rule_email', 'new-adverb-email': 'always',
                'format-email': 'text/plain',
                'new-rule-email': 'CarbonCopySubscriber'}
        req = self._request(authname='baz', args=args)
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', None, 1, 'never', 'TicketOwnerSubscriber'),
             ('email', 'text/plain', 2, 'always', 'CarbonCopySubscriber')],
            self._get_rules('baz'))

    def test_delete_rule(self):
        req = self._request(authname='foo',
                            args={'action': 'delete-rule_%d' % self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', 'text/plain', 1, 'always', 'TicketOwnerSubscriber'),
             ('email', 'text/plain', 2, 'always', 'TicketReporterSubscriber')],
            self._get_rules('foo'))

        # try to delete non-existent rule
        rules = self._get_rules('foo')
        req = self._request(authname='foo',
                            args={'action': 'delete-rule_%d' % self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))

        # try to delete non-owned rules
        rules = self._get_rules('bar')
        req = self._request(authname='foo',
                            args={'action': 'delete-rule_%d' % self.bar_rule0})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('bar'))

    def test_move_rule(self):
        # move to last
        req = self._request(authname='foo',
                            args={'action': 'move-rule_%d-3' % self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', 'text/plain', 1, 'always', 'TicketOwnerSubscriber'),
             ('email', 'text/plain', 2, 'always', 'TicketReporterSubscriber'),
             ('email', 'text/plain', 3, 'always', 'CarbonCopySubscriber')],
            self._get_rules('foo'))
        self.assertEqual(
            [('email', 'text/plain', 1, 'always', 'CarbonCopySubscriber')],
            self._get_rules('bar'))

        # move to first
        req = self._request(authname='foo',
                            args={'action': 'move-rule_%d-1' % self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', 'text/plain', 1, 'always', 'CarbonCopySubscriber'),
             ('email', 'text/plain', 2, 'always', 'TicketOwnerSubscriber'),
             ('email', 'text/plain', 3, 'always', 'TicketReporterSubscriber')],
            self._get_rules('foo'))

        # move to same position
        rules = self._get_rules('foo')
        req = self._request(authname='foo',
                            args={'action': 'move-rule_%d-1' % self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))

        # try to move to out of range
        rules = self._get_rules('foo')
        req = self._request(authname='foo', args={'action': 'move-rule_%d-42' %
                                                            self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))

        # try to move non-owned rules
        rules = self._get_rules('foo')
        req = self._request(authname='bar',
                            args={'action': 'move-rule_%d-3' % self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))

        rules = self._get_rules('foo')

        # try to move non-existent rule
        req = self._request(authname='foo', args={'action': 'move-rule_42-1'})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))

        # invalid id
        req = self._request(authname='foo',
                            args={'action': 'move-rule_0-1'})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))
        req = self._request(authname='foo',
                            args={'action': 'move-rule_a-1'})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))

        # invalid priority
        req = self._request(authname='foo',
                            args={'action': 'move-rule_%d-0' % self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))
        req = self._request(authname='foo',
                            args={'action': 'move-rule_%d-a' % self.foo_rule1})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(rules, self._get_rules('foo'))

    def test_set_format(self):
        # set text/html
        req = self._request(authname='foo', args={'action': 'set-format_email',
                                                  'format-email': 'text/html'})
        self.assertNotIn('notification.format.email', req.session)
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', 'text/html', 1, 'always', 'TicketOwnerSubscriber'),
             ('email', 'text/html', 2, 'always', 'CarbonCopySubscriber'),
             ('email', 'text/html', 3, 'always', 'TicketReporterSubscriber')],
            self._get_rules('foo'))
        self.assertEqual(
            [('email', 'text/plain', 1, 'always', 'CarbonCopySubscriber')],
            self._get_rules('bar'))
        self.assertEqual('text/html', req.session['notification.format.email'])

        # set default format
        req = self._request(authname='foo', args={'action': 'set-format_email',
                                                  'format-email': ''})
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', None, 1, 'always', 'TicketOwnerSubscriber'),
             ('email', None, 2, 'always', 'CarbonCopySubscriber'),
             ('email', None, 3, 'always', 'TicketReporterSubscriber')],
            self._get_rules('foo'))
        self.assertNotIn('notification.format.email', req.session)

    def test_replace(self):
        arg_list = [
            ('action', 'replace_all'),
            ('format-email', 'text/plain'),
            ('adverb-email', 'always'),
            ('adverb-email', 'never'),
            ('adverb-email', 'always'),
            ('adverb-email', 'always'),
            ('class-email', 'TicketOwnerSubscriber'),
            ('class-email', 'CarbonCopySubscriber'),
            ('class-email', 'TicketReporterSubscriber'),
            ('class-email', 'TicketUpdaterSubscriber'),
        ]
        req = self._request(authname='foo', arg_list=arg_list)
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', 'text/plain', 1, 'always', 'TicketOwnerSubscriber'),
             ('email', 'text/plain', 2, 'never', 'CarbonCopySubscriber'),
             ('email', 'text/plain', 3, 'always', 'TicketReporterSubscriber'),
             ('email', 'text/plain', 4, 'always', 'TicketUpdaterSubscriber')],
            self._get_rules('foo'))
        self.assertEqual(
            [('email', 'text/plain', 1, 'always', 'CarbonCopySubscriber')],
            self._get_rules('bar'))

        arg_list = [
            ('action', 'replace_all'), ('format-email', ''),
            ('adverb-email', 'always'),
            ('class-email', 'TicketOwnerSubscriber'),
        ]
        req = self._request(authname='foo', arg_list=arg_list)
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual(
            [('email', None, 1, 'always', 'TicketOwnerSubscriber')],
            self._get_rules('foo'))

        arg_list = [('action', 'replace_all'), ('format-email', '')]
        req = self._request(authname='foo', arg_list=arg_list)
        self.assertRaises(RequestDone, self._process, req)
        self.assertEqual([], self._get_rules('foo'))