示例#1
0
    def testCookieErrorToLog(self):
        from zope.testing.loggingsupport import InstalledHandler
        cookies = {
            'HTTP_COOKIE':
                'foo=bar; path=/; spam="eggs"; ldap/OU="Williams"'
        }
        handler = InstalledHandler('eventlog')
        try:
            req = self._createRequest(extra_env=cookies)
        finally:
            handler.uninstall()

        self.assertEqual(len(handler.records), 1)
        self.assertEqual(handler.records[0].getMessage(),
                         'Illegal key value: ldap/OU')

        self.assertFalse(req.cookies.has_key('foo'))
        self.assertFalse(req.has_key('foo'))

        self.assertFalse(req.cookies.has_key('spam'))
        self.assertFalse(req.has_key('spam'))

        self.assertFalse(req.cookies.has_key('ldap/OU'))
        self.assertFalse(req.has_key('ldap/OU'))

        # Reserved key
        self.assertFalse(req.cookies.has_key('path'))
示例#2
0
 def setUp(self):
     """Set up a class ready for testing"""
     super(ReviewUgQnViewTest, self).setUp()
     self.loghandlers = dict(
         sqlalchemy=InstalledHandler('sqlalchemy.engine'),
         sync=InstalledHandler('tutorweb.quizdb.browser.sync')
     )
    def testCookieErrorToLog(self):
        from zope.testing.loggingsupport import InstalledHandler
        cookies = {
            'HTTP_COOKIE':
                'foo=bar; path=/; spam="eggs"; ldap/OU="Williams"'
        }
        handler = InstalledHandler('eventlog')
        try:
            req = self._createRequest(extra_env=cookies)
        finally:
            handler.uninstall()

        self.assertEqual(len(handler.records), 1)

        message = handler.records[0].getMessage()
        self.assertTrue(message.startswith('Illegal key'))
        self.assertIn('ldap/OU', message)

        self.assertNotIn('foo', req.cookies)
        self.assertNotIn('foo', req)

        self.assertNotIn('spam', req.cookies)
        self.assertNotIn('spam', req)

        self.assertNotIn('ldap/OU', req.cookies)
        self.assertNotIn('ldap/OU', req)

        # Reserved key
        self.assertNotIn('path', req.cookies)
示例#4
0
    def test_detect_delete_trigger(self):
        # Cause the delete trigger to be deleted by opening a newt.db
        # connection:
        import newt.db
        self.ex("drop table object_state")
        newt.db.storage(self.dsn).close()

        # Verify the trigger is there:
        from newt.db._util import trigger_exists
        from newt.db._adapter import DELETE_TRIGGER
        self.assertTrue(trigger_exists(self.cursor, DELETE_TRIGGER))

        from zope.testing.loggingsupport import InstalledHandler
        handler = InstalledHandler('newt.db.updater')

        self.assertEqual(1, updater.main([self.dsn]))
        self.assertEqual(
            "newt.db.updater ERROR\n"
            "  The Newt DB delete trigger exists.\n"
            "It is incompatible with the updater.\n"
            "Use -T to remove it.", str(handler))
        handler.clear()

        # Now run the updater with the -T option, which causes the
        # trigger to be deleted.  (We also use -g, which causes it to
        # to GC and nothing else, which makes it stop, so we don't
        # have to run in a thread.)
        self.assertEqual(0, updater.main([self.dsn, '-Tg']))
        self.assertEqual("", str(handler))

        # And the trigger is gone.
        self.assertFalse(trigger_exists(self.cursor, DELETE_TRIGGER))
示例#5
0
 def setUp(self):
     from Products.LongRequestLogger.patch import do_patch
     from Products.LongRequestLogger.dumper import logger_name
     from zope.testing.loggingsupport import InstalledHandler
     self.setTestEnvironment()
     do_patch()
     self.loghandler = InstalledHandler(logger_name)
     self.requests = []
    def setUp(self):
        """Set up a class ready for testing"""
        super(SyncClassSubscriptionsTest, self).setUp()
        portal = self.layer['portal']

        self.loghandlers = dict(
            sqlalchemy=InstalledHandler('sqlalchemy.engine'),
            sync=InstalledHandler('tutorweb.quizdb.browser.sync'))
 def setUp(self):
     from Products.LongRequestLogger import do_patch, monitor, dumper
     from zope.testing.loggingsupport import InstalledHandler
     dumper.config = dict(logfile=os.devnull)
     log = dumper.getLogger()
     self.monitor = monitor.Monitor(log, **dumper.get_configuration())
     do_patch(self.monitor)
     self.loghandler = InstalledHandler(log.name)
     self.requests = []
示例#8
0
    def setUp(self):
        super(FunctionalTestCase, self).setUp()
        self.loghandlers = dict(
            sqlalchemy=InstalledHandler('sqlalchemy.engine'),
            sync=InstalledHandler('tutorweb.quizdb.browser.sync'),
            syncm=InstalledHandler('tutorweb.quizdb.sync'),
        )

        self.objectPublish(self.layer['portal']['dept1']['tut1']['lec1'])
        self.objectPublish(self.layer['portal']['dept1']['tut1']['lec2'])
class Futures(RemoteLibrary):
    def start_futures_logging(self):
        self._v_record = InstalledHandler('experimental.futures',
                                          level=logging.DEBUG)

    def get_futures_log(self):
        return unicode(self._v_record)

    def stop_futures_logging(self):
        self._v_record.uninstall()
示例#10
0
    def test_search_for_factory_updates_search_set(self):
        from zope.testing.loggingsupport import InstalledHandler

        with warnings.catch_warnings(record=True):
            INT.register_legacy_search_module(__name__)
            # The cache is initialized lazily
            assert_that(__name__, is_in(INT.LEGACY_FACTORY_SEARCH_MODULES))

            assert_that(INT.find_factory_for_class_name('testfunctions'),
                        is_(none()))

            # And we have been examined and removed
            assert_that(__name__,
                        is_not(is_in(INT.LEGACY_FACTORY_SEARCH_MODULES)))
            assert_that(INT.LEGACY_FACTORY_SEARCH_MODULES, is_(set()))

            # Now we're going to fiddle with our public classes and try again.
            # This will force re-registration to occur. Note we do this before
            # we make ourself public, so that we can assert it's lazy
            INT.register_legacy_search_module(__name__)

            TestFunctions.__external_can_create__ = True
            handler = InstalledHandler(INT.__name__)
            try:
                assert_that(INT.find_factory_for_class_name('testfunctions'),
                            equal_to(TestFunctions))

                # Now lets register ourself again, to trigger the logged warnings.
                assert_that(__name__,
                            is_not(is_in(INT.LEGACY_FACTORY_SEARCH_MODULES)))
                assert_that(INT.LEGACY_FACTORY_SEARCH_MODULES, is_(set()))

                # Now we're going to fiddle with our public classes and try again.
                # This will force re-registration to occur. Note we do this before
                # we make ourself public, so that we can assert it's lazy
                INT.register_legacy_search_module(__name__)

                assert_that(INT.find_factory_for_class_name('testfunctions'),
                            equal_to(TestFunctions))
                # case doesn't matter
                assert_that(INT.find_factory_for_class_name('TeStfUnctIons'),
                            equal_to(TestFunctions))

                assert_that(
                    str(handler),
                    contains_string(
                        "Found duplicate registration for legacy search path.")
                )

                assert_that(INT.legacy_factories.count_legacy_classes_found(),
                            greater_than_or_equal_to)

            finally:
                del TestFunctions.__external_can_create__
                handler.uninstall()
示例#11
0
class Futures(RemoteLibrary):

    def start_futures_logging(self):
        self._v_record = InstalledHandler('experimental.futures',
                                          level=logging.DEBUG)

    def get_futures_log(self):
        return unicode(self._v_record)

    def stop_futures_logging(self):
        self._v_record.uninstall()
    def test_log_info(self):
        from zope.server import zlogintegration
        from zope.testing.loggingsupport import InstalledHandler

        handler = InstalledHandler('zope.server')
        try:
            zlogintegration.log_info(None, "Some Info")
        finally:
            handler.uninstall()

        self.assertEqual(1, len(handler.records))
        self.assertEqual("Some Info", str(handler.records[0].getMessage()))
示例#13
0
    def test_log_info(self):
        from zope.server import zlogintegration
        from zope.testing.loggingsupport import InstalledHandler

        handler = InstalledHandler('zope.server')
        try:
            zlogintegration.log_info(None, "Some Info")
        finally:
            handler.uninstall()

        self.assertEqual(1, len(handler.records))
        self.assertEqual("Some Info", str(handler.records[0].getMessage()))
    def test_ActionExecutor(self):

        self.createIFTTTAction()

        # configure easyform actions
        self.assertTrue('ifttt' in get_actions(self.ff1))
        ifttt_trigger = get_actions(self.ff1)['ifttt']
        ifttt_trigger.ifttt_event_name = u'ifttt applet'
        ifttt_trigger.payload_fields = ['replyto', 'topic', 'comments']

        request = FakeRequest(
            add_auth=True,
            method='POST',
            topic='test subject',
            replyto='*****@*****.**',
            comments='test comments'
        )

        self.assertFalse(ifttt_trigger.onSuccess(request.form, request))

        # set secret key
        api.portal.set_registry_record(
            name='ifttt.ifttt_secret_key',
            value=u'secret',
        )

        # inspect logs
        handler = InstalledHandler('collective.ifttt.requests')

        # execute action
        self.assertTrue(ifttt_trigger.onSuccess(request.form, request))

        messages = [record.getMessage() for record in handler.records]
        self.assertGreater(len(messages), 0)
        self.assertTrue(messages[0].startswith('Dispatched requests.post'))
示例#15
0
def setUp(test):
    test.globs['root'] = ZopeTestCase.base.app()
    # As task will be run in different threads, we cannot rely on print
    # results. We need to log calls to prove correctness.
    log_info = InstalledHandler('z3c.taskqueue')
    test.globs['log_info'] = log_info
    # We pass the ZPublisher conflict logger to prove that no conflict
    # happened.
    conflict_logger = InstalledHandler('ZPublisher.Conflict')
    test.globs['conflict_logger'] = conflict_logger
    # Make sure ZPublisher conflict error log level is setup.
    _configure_conflict_error_log_level()
    test.origArgs = service.TaskService.processorArguments
    service.TaskService.processorArguments = {'waitTime': 0.0}
    # Make tests predictable
    random.seed(27)
示例#16
0
    def setUp(self):
        """Set up a class ready for testing"""
        portal = self.layer['portal']
        login(portal, MANAGER_ID)

        self.loghandlers = dict(
            sqlalchemy=InstalledHandler('sqlalchemy.engine'),
            sync=InstalledHandler('tutorweb.quizdb.browser.sync'))

        # Create an extra test tutorial
        portal['dept1'].invokeFactory(
            type_name="tw_tutorial",
            id="tut_extra",
            title="Unittest D1 tutExtra",
        )
        self.extra_lec = self.createTestLecture(qnCount=1)
 def setUp(self):
     from Products.LongRequestLogger import do_patch, monitor, dumper
     from zope.testing.loggingsupport import InstalledHandler
     dumper.config = dict(logfile=os.devnull)
     log = dumper.getLogger()
     self.monitor = monitor.Monitor(log, **dumper.get_configuration())
     do_patch(self.monitor)
     self.loghandler = InstalledHandler(log.name)
     self.requests = []
示例#18
0
def setUp(test):
    root = placefulSetUp(site=True)
    test.globs['root'] = root
    log_info = InstalledHandler('z3c.taskqueue')
    test.globs['log_info'] = log_info
    test.origArgs = service.TaskService.processorArguments
    service.TaskService.processorArguments = {'waitTime': 0.0}
    # Make tests predictable
    random.seed(27)
示例#19
0
    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        self.ipn = queryAdapter(self.portal, IIPN)
        self.log = InstalledHandler('niteoweb.ipn.core')
        eventtesting.setUp()

        # create a test member and a test product group
        api.user.create(email='*****@*****.**')
        api.group.create(groupname='ipn_1')
        api.group.add_user(username='******', groupname='ipn_1')
示例#20
0
    def test_database_events(self):
        from Zope2.App.startup import startup
        from zope.component import provideHandler
        from zope.processlifetime import IDatabaseOpened
        from zope.processlifetime import IDatabaseOpenedWithRoot

        handler = InstalledHandler('Zope2.App.test_startup')
        provideHandler(logevent, [IDatabaseOpenedWithRoot])
        provideHandler(logevent, [IDatabaseOpened])
        startup()
        self.assertEqual(str(handler), logged)
示例#21
0
    def test_remove_words_in_already_corrupted_index(self):
        from zope.testing.loggingsupport import InstalledHandler
        handler = InstalledHandler('nti.zope_catalog.index')
        self.addCleanup(handler.uninstall)

        index = self.index
        index.index_doc(1, ('aizen', ))
        index.index_doc(2, ['ichigo'])
        index.index_doc(3, ['Kuchiki', 'rukia'])
        del index._rev_index[3]
        index.remove_words(('Kuchiki', ))
        assert_that(handler.records[0].msg, is_("Your index is corrupted: %s"))
    def testConsumeFromQueue(self):
        self.assertEqual(len(self.task_queue), 0)
        taskqueue.add("/", queue=self.queue)
        taskqueue.add("/Plone", queue=self.queue)
        transaction.commit()
        self.assertEqual(len(self.task_queue), 2)

        handler = InstalledHandler("collective.taskqueue")
        runAsyncTest(self._testConsumeFromQueue)
        messages = [record.getMessage() for record in handler.records]
        self.assertEqual(messages[-2:],
                         ["http://nohost:/", "http://nohost:/Plone"])
示例#23
0
    def setUp(self):
        """Set up a class ready for testing"""
        portal = self.layer['portal']
        login(portal, MANAGER_ID)

        if 'classa' not in portal:
            portal.invokeFactory(
                type_name="tw_class",
                id="classa",
                title="Unittest ClassA",
                students=[USER_A_ID, USER_C_ID, USER_B_ID],
                lectures=[],
            )
        else:
            portal['classa'].lectures = []

        self.loghandlers = dict(
            sqlalchemy=InstalledHandler('sqlalchemy.engine'),
            sync=InstalledHandler('tutorweb.quizdb.browser.sync'))

        self.objectPublish(portal['dept1']['tut1']['lec1'])
        self.objectPublish(portal['dept1']['tut1']['lec2'])
示例#24
0
    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        self.request = self.layer['request']
        self.ipn = queryAdapter(self.portal, IIPN)
        self.log = InstalledHandler('niteoweb.ipn.core')
        eventtesting.setUp()

        # set a very long valid_to date for the test_user
        # in order to skip this one in our tests below
        test_user = api.user.get(username=TEST_USER_ID)
        test_user.setMemberProperties(
            mapping={'valid_to': DateTime('2020/01/01')})
示例#25
0
    def test_update_index_with_error(self):
        from zope.testing.loggingsupport import InstalledHandler
        handler = InstalledHandler('nti.zope_catalog.catalog')
        self.addCleanup(handler.uninstall)

        cat = self._makeOne()
        cat._PERSISTENCE_EXCEPTIONS = AttributeError

        cat.updateIndex(42, ignore_persistence_exceptions=True)

        assert_that(handler.records, has_length(1))
        assert_that(handler.records[0].msg,
                    is_("Error indexing object %s(%s); %s"))
    def testConsume100FromQueue(self):
        self.assertEqual(len(self.task_queue), 0)
        expected_result = []
        for i in range(100):
            taskqueue.add("/{0:02d}".format(i), queue=self.queue)
            expected_result.append("http://nohost:/{0:02d}".format(i))
        transaction.commit()
        self.assertEqual(len(self.task_queue), 100)

        handler = InstalledHandler("collective.taskqueue")
        runAsyncTest(self._testConsumeFromQueue)
        messages = [record.getMessage() for record in handler.records]

        self.assertEqual(sorted(messages[-100:]), expected_result)
示例#27
0
    def test_jsonifier(self):
        from zope.testing.loggingsupport import InstalledHandler
        handler = InstalledHandler('newt.db.jsonpickle')
        from ..jsonpickle import Jsonifier

        jsonifier = Jsonifier()

        p, tid = self.conn._storage.load(z64)
        class_name, ghost_pickle, state = jsonifier('0', p)
        self.assertEqual('persistent.mapping.PersistentMapping', class_name)
        self.assertEqual('{"data": {}}', state)
        self.assertTrue(
            p.startswith(ghost_pickle) and ghost_pickle[-1:] == b'.'
            and b'persistent.mapping' in ghost_pickle)

        # custon skip_class
        jsonifier2 = Jsonifier(skip_class=lambda c: 1)
        self.assertEqual((None, None, None), jsonifier2('0', p))

        # empty records are skipped:
        self.assertEqual((None, None, None), jsonifier('0', ''))

        # BTrees are skipped by default
        from BTrees.OOBTree import BTree
        self.root.x = BTree()
        self.conn.transaction_manager.commit()
        p, tid = self.conn._storage.load(self.root.x._p_oid)
        self.assertEqual((None, None, None), jsonifier('0', p))

        # errors are logged, and return Nones:
        self.assertEqual(handler.records, [])
        self.assertEqual((None, None, None), jsonifier('foo', b'badness'))
        self.assertEqual(
            [r.getMessage().replace("b'", "'") for r in handler.records],
            ["Failed pickle load, oid: 'foo', pickle starts: 'badness'"])

        handler.uninstall()
示例#28
0
    def testBrokenMailerErrorsAreEaten(self):
        from zope.testing.loggingsupport import InstalledHandler
        from zope.sendmail.delivery import DirectMailDelivery
        mailer = BrokenMailerStub()
        delivery = DirectMailDelivery(mailer)
        fromaddr = 'Jim <*****@*****.**'
        toaddrs = ('Guido <*****@*****.**>',
                   'Steve <steve@examplecom>')
        opt_headers = ('From: Jim <*****@*****.**>\n'
                       'To: some-zope-coders:;\n'
                       'Date: Mon, 19 May 2003 10:17:36 -0400\n'
                       'Message-Id: <*****@*****.**>\n')
        message =     ('Subject: example\n'
                       '\n'
                       'This is just an example\n')

        msgid = delivery.send(fromaddr, toaddrs, opt_headers + message)
        log_handler = InstalledHandler('MailDataManager')
        try:
            transaction.commit()
        finally:
            # Clean up after ourselves
            log_handler.uninstall()
            transaction.abort()
示例#29
0
    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        self.ipn = queryAdapter(self.portal, IIPN)
        self.log = InstalledHandler('niteoweb.ipn.core')
        eventtesting.setUp()

        # create test product groups and set their validity
        api.group.create(groupname='ipn_1')
        group = api.group.get(groupname='ipn_1')
        group.setGroupProperties(mapping={'validity': 31})

        api.group.create(groupname='ipn_2')
        group = api.group.get(groupname='ipn_2')
        group.setGroupProperties(mapping={'validity': 365})
示例#30
0
    def test_warnings_are_shown(self):
        import warnings
        import logging
        from zope.testing.loggingsupport import InstalledHandler

        handler = InstalledHandler("py.warnings", level=logging.WARNING)
        self.addCleanup(handler.uninstall)

        logging.captureWarnings(True)
        self.addCleanup(logging.captureWarnings, False)

        msg = "This should be visible by default"
        warnings.warn(msg, DeprecationWarning)

        self.assertEqual(1, len(handler.records))
        self.assertIn('DeprecationWarning', handler.records[0].getMessage())
        self.assertIn(msg, handler.records[0].getMessage())
示例#31
0
    def setUp(self):
        """Prepare testing environment."""
        self.portal = self.layer['portal']

        self.registry = getUtility(IRegistry)
        self.settings = self.registry.forInterface(IAweberSettings)
        self.settings.app_id = u'appid'
        self.settings.authorization_code = u'authorizationcode'
        self.settings.consumer_key = u'consumerkey'
        self.settings.consumer_secret = u'consumersecret'
        self.settings.access_token = u'accesstoken'
        self.settings.access_secret = u'accesssecret'
        transaction.commit()

        self.browser = Browser(self.portal)
        self.login_as_admin()

        # install a testing log handler
        self.logger = InstalledHandler('niteoweb.aweber')
示例#32
0
def test_suite():
    suite = unittest.TestSuite()
    my_dir = os.path.dirname(__file__)
    docs = os.path.join('..', '..', '..', 'docs')
    registerConfiguration = PLONEBLUEPRINTS_FUNCTIONAL_TESTING \
        .baseResolutionOrder[1].registerConfiguration
    for filename in os.listdir(os.path.join(my_dir, docs)):
        path = os.path.join(docs, filename)
        globs = {
            'registerConfiguration': registerConfiguration,
            'ITransmogrifier': ITransmogrifier,
            'api': api,
            'logger': InstalledHandler('logger', level=logging.INFO)
        }
        flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
        suite.addTests([
            layered(doctest.DocFileSuite(path, globs=globs, optionflags=flags),
                    layer=PLONEBLUEPRINTS_FUNCTIONAL_TESTING)
        ])
    return suite
    def test_ActionExecutor(self):

        element = IftttTriggerAction()
        context = self.portal.restrictedTraverse('')

        api.portal.set_registry_record(
            name='ifttt.ifttt_secret_key',
            value=u'secret',
        )
        element.ifttt_event_name = 'ifttt_applet'

        for option in payload_options:
            # inspect logs
            handler = InstalledHandler('collective.ifttt.requests')

            element.payload_option = option.value
            ex = getMultiAdapter((context, element, DummyEvent(self.folder)),
                                 IExecutable)
            self.assertTrue(ex())
            messages = [record.getMessage() for record in handler.records]
            self.assertGreater(len(messages), 0)
            self.assertTrue(messages[0].startswith('Dispatched requests.post'))
"""Tests for HerokuappAccess tween."""

from pyramid import testing
from zope.testing.loggingsupport import InstalledHandler

import mock
import unittest

tweens_handler = InstalledHandler('pyramid_heroku.herokuapp_access')


class TestHerokuappAccessTween(unittest.TestCase):
    def setUp(self):
        tweens_handler.clear()
        self.config = testing.setUp()
        self.handler = mock.Mock()
        self.request = testing.DummyRequest()
        self.request.registry.settings = {
            'pyramid_heroku.herokuapp_whitelist': [
                '1.2.3.4',
            ]
        }

    def tearDown(self):
        tweens_handler.clear()
        testing.tearDown()

    def test_whitelisted_ip(self):
        from pyramid_heroku.herokuapp_access import HerokuappAccess
        self.request.client_addr = '1.2.3.4'
        self.request.headers = {
示例#35
0
class TestLongRequestLogger(unittest.TestCase):
    def setUp(self):
        from Products.LongRequestLogger.patch import do_patch
        from Products.LongRequestLogger.dumper import logger_name
        from zope.testing.loggingsupport import InstalledHandler
        self.setTestEnvironment()
        do_patch()
        self.loghandler = InstalledHandler(logger_name)
        self.requests = []

    def tearDown(self):
        from Products.LongRequestLogger.patch import do_unpatch
        do_unpatch()
        self.restoreTestEnvironment()
        self.loghandler.uninstall()
        for request in self.requests:
            request.response.stdout.close()
            request.clear()

    def setTestEnvironment(self):
        self.old_env = {}
        for var, value in config_env_variables.items():
            self.old_env[var] = os.environ.pop(var, None)
            if value:
                os.environ[var] = value

    def restoreTestEnvironment(self):
        for var, value in self.old_env.items():
            os.environ.pop(var, None)
            if value is not None:
                os.environ[var] = value

    def makeRequest(self, path='/', **kw):
        # create fake request and response for convenience
        from ZPublisher.HTTPRequest import HTTPRequest
        from ZPublisher.HTTPResponse import HTTPResponse
        stdin = StringIO()
        stdout = StringIO()
        # minimal environment needed
        env = dict(SERVER_NAME='localhost',
                   SERVER_PORT='80',
                   REQUEST_METHOD='GET',
                   SCRIPT_NAME=path)
        response = HTTPResponse(stdout=stdout)
        request = HTTPRequest(stdin, env, response)
        self.requests.append(request)
        return request

    def testDumperFormat(self):
        from Products.LongRequestLogger.dumper import Dumper
        dumper = Dumper()
        check_dump(dumper.format_thread())

    def testDumperRequestExtraction(self):
        # The dumper extract requests by looking for the frame that contains
        # call_object and then looking for the 'request' variable inside it
        from ZPublisher.Publish import call_object
        from Products.LongRequestLogger.dumper import Dumper

        def callable():
            dumper = Dumper()
            frame = sys._current_frames()[dumper.thread_id]
            return dumper.extract_request(frame)

        request = self.makeRequest('/foo')
        retrieved_request = call_object(callable, (), request)
        self.assertTrue(request is retrieved_request)

    def testRequestFormating(self):
        from Products.LongRequestLogger.dumper import Dumper
        dumper = Dumper()
        request = self.makeRequest('/foo/bar')
        check_request_formating(dumper.format_request(request))

    def testDumperLog(self):
        from Products.LongRequestLogger.dumper import Dumper
        dumper = Dumper()
        # check the dumper will log what we expect when called
        dumper()
        check_log(str(self.loghandler))

    def testMonitorStopBeforeTimeout(self):
        from Products.LongRequestLogger.monitor import Monitor
        m = Monitor()
        # sleep just a little to let the other thread start
        s = Sleeper(0.01)
        s.sleep()
        self.assertTrue(m.isAlive())
        m.stop()
        self.assertFalse(m.isAlive())
        # unless this test is so slow that there were 2 seconds interval
        # between starting the monitor and stopping it, there should be no
        # logged messages
        self.assertFalse(self.loghandler.records)

    def testMonitorStopAfterTimeout(self):
        from Products.LongRequestLogger.monitor import Monitor
        m = Monitor()
        s = Sleeper(m.dumper.timeout + 0.5)
        # sleep a little more than the timeout to be on the safe side
        s.sleep()
        m.stop()
        check_monitor_log(str(self.loghandler))

    def testMonitorStopAfterTimeoutAndTwoIntervals(self):
        from Products.LongRequestLogger.monitor import Monitor
        m = Monitor()
        s = Sleeper(m.dumper.timeout + 2 * m.dumper.interval + 0.5)
        # sleep a little more than timeout + intervals to be on the safe
        # side
        s.sleep()
        m.stop()
        check_monitor_2_intervals_log(str(self.loghandler))

    def testMonitorConfigurationDisabled(self):
        from Products.LongRequestLogger.monitor import Monitor
        from Products.LongRequestLogger.dumper import DEFAULT_TIMEOUT
        from Products.LongRequestLogger.dumper import DEFAULT_INTERVAL
        os.environ['longrequestlogger_file'] = ''
        m = Monitor()
        s = Sleeper(DEFAULT_TIMEOUT + 2 * DEFAULT_INTERVAL + 0.5)
        # sleep a little more than timeout + intervals
        s.sleep()
        # the thread shouldn't run disabled
        self.assertFalse(m.isAlive())
        # stopping shouldn't break nonetheless
        m.stop()
        self.assertFalse(m.isAlive())
        # and there should be no records
        self.assertFalse(self.loghandler.records)

    def testMonitorWithEnvironmentConfiguration(self):
        from Products.LongRequestLogger.monitor import Monitor
        os.environ['longrequestlogger_timeout'] = '3.5'
        os.environ['longrequestlogger_interval'] = '2'
        m = Monitor()
        s = Sleeper(m.dumper.timeout + m.dumper.interval + 0.5)
        # sleep a little more than the timeout to be on the safe side
        s.sleep()
        m.stop()
        check_monitor_environment_log(str(self.loghandler))

    def testIsPatched(self):
        import ZPublisher.Publish
        import Products.LongRequestLogger
        self.assertEquals(ZPublisher.Publish.publish_module_standard,
                          Products.LongRequestLogger.patch.wrapper)

    def testPublish(self):
        from ZPublisher.Publish import publish_module_standard
        # Before publishing, there should be no slow query records.
        self.assertFalse(self.loghandler.records)
        # Request taking (timeout + interval + margin) 3.5 seconds...
        request = self.makeRequest('/', interval=3.5)
        request['interval'] = 3.5
        publish_module_standard('Products.LongRequestLogger.tests.common',
                                request=request,
                                response=request.response,
                                debug=True)
        # ...should generate query log records like these
        check_publishing_1_interval_log(str(self.loghandler))
示例#36
0
 def start_futures_logging(self):
     self._v_record = InstalledHandler('experimental.futures',
                                       level=logging.DEBUG)
示例#37
0
class TestUseCases(IntegrationTestCase):
    """Test runtime flow through the disable_member() action for most common
    use cases.
    """

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        self.ipn = queryAdapter(self.portal, IIPN)
        self.log = InstalledHandler('niteoweb.ipn.core')
        eventtesting.setUp()

        # create a test member and a test product group
        api.user.create(email='*****@*****.**')
        api.group.create(groupname='ipn_1')
        api.group.add_user(username='******', groupname='ipn_1')

    def tearDown(self):
        """Clean up after yourself."""
        self.log.clear()
        eventtesting.clearEvents()

    @mock.patch('niteoweb.ipn.core.ipn.DateTime')
    def test_disable_enabled_member(self, DT):
        """Test default execution of the disable_member() action."""
        DT.return_value = DateTime('2012/01/01')

        self.ipn.disable_member(
            email='*****@*****.**',
            product_id='1',
            trans_type='CANCEL',
        )

        # test member is in Disabled group
        self.assertIn(
            'disabled',
            [g.id for g in api.group.get_groups(username='******')]
        )

        # test member is in no other group
        self.assertItemsEqual(
            ['disabled', 'AuthenticatedUsers', ],
            [g.id for g in api.group.get_groups(username='******')]
        )

        # test member does not have the Member role
        self.assertNotIn(
            'Member',
            api.user.get_roles(username='******'),
        )

        # test event emitted
        events = list(set(eventtesting.getEvents(IMemberDisabledEvent)))
        self.assertEquals(len(events), 1)
        self.assertEquals(events[0].username, '*****@*****.**')

        # test member history
        self.assert_member_history(
            username='******',
            history=['2012/01/01 00:00:00|disable_member|1|CANCEL|'
                     'removed from groups: ipn_1, ']
        )

        # test log output
        self.assertEqual(len(self.log.records), 5)
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "START disable_member:CANCEL for '*****@*****.**'.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Adding member '*****@*****.**' to Disabled group.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Removing member '*****@*****.**' from group 'ipn_1'.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Revoking member '*****@*****.**' the Member role.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "END disable_member:CANCEL for '*****@*****.**'.",
        )

    @mock.patch('niteoweb.ipn.core.ipn.DateTime')
    def test_disable_disabled_member(self, DT):
        """Test disabling an already disabled member. This should not happen
        often, but it still could."""
        DT.return_value = DateTime('2012/01/01')

        # first disable a member
        self.test_disable_enabled_member()

        # now let's say a month goes by and we get another notification from
        # IPN that this member is disabled
        DT.return_value = DateTime('2012/02/01')
        self.ipn.disable_member(
            email='*****@*****.**',
            product_id='1',
            trans_type='CANCEL',
        )

        # test event emitted
        events = list(set(eventtesting.getEvents(IMemberDisabledEvent)))
        self.assertEquals(len(events), 2)
        self.assertEquals(events[0].username, '*****@*****.**')
        self.assertEquals(events[1].username, '*****@*****.**')

        # test member history
        self.assert_member_history(
            username='******',
            history=[
                '2012/01/01 00:00:00|disable_member|1|CANCEL|'
                'removed from groups: ipn_1, ',
                '2012/02/01 00:00:00|disable_member|1|CANCEL|',
            ]
        )

        # test log output
        for record in self.log.records:
            self.assertNotIn(
                "Adding member '*****@*****.**' to Disabled group.",
                record.getMessage(),
            )

            self.assertNotIn(
                "Revoking member '*****@*****.**' the Member role.",
                record.getMessage(),
            )
class TestLongRequestLogger(unittest.TestCase):

    def setUp(self):
        from Products.LongRequestLogger import do_patch, monitor, dumper
        from zope.testing.loggingsupport import InstalledHandler
        dumper.config = dict(logfile=os.devnull)
        log = dumper.getLogger()
        self.monitor = monitor.Monitor(log, **dumper.get_configuration())
        do_patch(self.monitor)
        self.loghandler = InstalledHandler(log.name)
        self.requests = []

    def tearDown(self):
        from Products.LongRequestLogger import do_unpatch
        do_unpatch()
        self.monitor.stop()
        self.loghandler.uninstall()
        for request in self.requests:
            request.response.stdout.close()
            request.clear()

    def makeRequest(self, path='/', **kw):
        # create fake request and response for convenience
        from ZPublisher.HTTPRequest import HTTPRequest
        from ZPublisher.HTTPResponse import HTTPResponse
        stdin = StringIO()
        stdout = StringIO()
        # minimal environment needed
        env = dict(SERVER_NAME='localhost',
                   SERVER_PORT='80',
                   REQUEST_METHOD='GET',
                   SCRIPT_NAME=path)
        response = HTTPResponse(stdout=stdout)
        request = HTTPRequest(stdin, env, response)
        self.requests.append(request)
        return request

    def testDumperFormat(self):
        from Products.LongRequestLogger.dumper import Dumper
        dumper = Dumper()
        check_dump(dumper.format_thread())

    def testDumperRequestExtraction(self):
        # The dumper extract requests by looking for the frame that contains
        # call_object and then looking for the 'request' variable inside it
        from ZPublisher.Publish import call_object
        from Products.LongRequestLogger.dumper import Dumper
        def callable():
            dumper = Dumper()
            frame = sys._current_frames()[dumper.thread_id]
            return dumper.extract_request(frame)

        request = self.makeRequest('/foo')
        retrieved_request = call_object(callable, (), request)
        self.assertTrue(request is retrieved_request)

    def testRequestFormating(self):
        from Products.LongRequestLogger.dumper import Dumper
        dumper = Dumper()
        request = self.makeRequest('/foo/bar')
        check_request_formating(dumper.format_request(request))

    def testPublish(self):
        from ZPublisher.Publish import publish_module_standard
        # Before publishing, there should be no slow query records.
        self.assertFalse(self.loghandler.records)
        # Request taking (timeout + interval + margin) 3.5 seconds...
        request = self.makeRequest('/', interval=3.5)
        request['interval'] = 3.5
        publish_module_standard('Products.LongRequestLogger.tests.common',
                                request=request,
                                response=request.response,
                                debug=True)
        # ...should generate query log records like these
        check_publishing_1_interval_log(str(self.loghandler))
示例#39
0
 def setUp(self):
     Test_ro.setUp(self)
     from zope.testing.loggingsupport import InstalledHandler
     self.log_handler = handler = InstalledHandler('zope.interface.ro')
     self.addCleanup(handler.uninstall)
示例#40
0
class FunctionalTestAweber(FunctionalTestCase):
    """Functional test of Aweber."""

    def setUp(self):
        """Prepare testing environment."""
        self.portal = self.layer['portal']

        self.registry = getUtility(IRegistry)
        self.settings = self.registry.forInterface(IAweberSettings)
        self.settings.app_id = u'appid'
        self.settings.authorization_code = u'authorizationcode'
        self.settings.consumer_key = u'consumerkey'
        self.settings.consumer_secret = u'consumersecret'
        self.settings.access_token = u'accesstoken'
        self.settings.access_secret = u'accesssecret'
        transaction.commit()

        self.browser = Browser(self.portal)
        self.login_as_admin()

        # install a testing log handler
        self.logger = InstalledHandler('niteoweb.aweber')

    def tearDown(self):
        """Clean up after yourself."""

        # logout
        self.browser.open(self.portal.absolute_url() + '/logout')

        # reset our mocked logger
        self.logger.uninstall()
        self.logger.clear()

    def test_controlpanel_view(self):
        """Test for control panel view."""
        self.browser.open(self.portal.absolute_url() + "/@@aweber-settings")
        self.assertEqual(
            self.browser.url,
            'http://nohost/plone/@@aweber-settings',
        )
        self.assertIn(
            '<h1 class="documentFirstHeading">Aweber settings</h1>',
            self.browser.contents,
        )

    def test_save(self):
        """Test save button."""
        self.browser.open(self.portal.absolute_url() + "/@@aweber-settings")
        self.browser.getControl(name='form.widgets.app_id').value = \
            u'temp_app_id'
        self.browser.getControl(name="form.buttons.save").click()

        # test if value is saved
        self.assertEqual(
            self.settings.app_id,
            u'temp_app_id'
        )

    def test_cancel(self):
        """Test cancel button."""
        self.browser.open(self.portal.absolute_url() + "/@@aweber-settings")
        self.browser.getControl(name='form.widgets.app_id').value = \
            u'temp_app_id'
        self.browser.getControl(name="form.buttons.cancel").click()

        # test if value is unchanged
        self.assertEqual(
            self.settings.app_id,
            u'appid'
        )

    def test_get_auth(self):
        """Test get authorization code button."""
        self.browser.open(self.portal.absolute_url() + "/@@aweber-settings")
        self.browser.getControl(name="form.buttons.get_auth").click()

        url = "https://auth.aweber.com/1.0/oauth/authorize_app/{0}".format(
            self.browser.getControl(name="form.widgets.app_id").value
        )
        message = "Visit '{0}' and copy authorization code " \
            "to Authorization Code field".format(url)
        self.assertIn(message, self.browser.contents)

    @patch("niteoweb.aweber.controlpanel.parse_auth_code")
    @patch("niteoweb.aweber.controlpanel.set_list_names")
    def test_parse_auth(self, set_list_names, parse_auth_code):
        """Test get authorization code button."""
        self.browser.open(self.portal.absolute_url() + "/@@aweber-settings")
        self.browser.getControl(name="form.buttons.parse_auth").click()
        assert set_list_names.called
        assert parse_auth_code.called

    @patch("niteoweb.aweber.controlpanel.set_list_names")
    def test_update_lists(self, set_list_names):
        """Test update lists button."""
        self.browser.open(self.portal.absolute_url() + "/@@aweber-settings")
        self.browser.getControl(name="form.buttons.update_lists").click()
        assert set_list_names.called

    @patch("niteoweb.aweber.aweberapi.subscribe_to_aweber_mailinglist")
    def test_subscribe_user(self, subscribe):
        """Test subscribe user button."""
        self.browser.open(self.portal.absolute_url() + "/@@aweber-settings")
        self.browser.getControl(name="form.buttons.subscribe_user").click()
        assert subscribe.called
示例#41
0
 def start_futures_logging(self):
     self._v_record = InstalledHandler('collective.futures',
                                       level=logging.DEBUG)
示例#42
0
from sqlalchemy import Column
from sqlalchemy import Enum
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import Unicode
from sqlalchemy.orm import backref
from sqlalchemy.orm import relationship
from zope.testing.loggingsupport import InstalledHandler

import json
import mock
import transaction
import unittest

handler = InstalledHandler('pyramid_bimt.task')


class FooTaskModel(Base, BaseMixin):
    """A dummy celery task backend."""

    __tablename__ = 'foo_tasks'

    user_id = Column(Integer, ForeignKey('users.id'))
    user = relationship(
        User,
        backref=backref('tasks'),
    )

    task_id = Column(
        String,
示例#43
0
class TestValidity(IntegrationTestCase):
    """Test @@validity view."""

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        self.request = self.layer['request']
        self.ipn = queryAdapter(self.portal, IIPN)
        self.log = InstalledHandler('niteoweb.ipn.core')
        eventtesting.setUp()

        # set a very long valid_to date for the test_user
        # in order to skip this one in our tests below
        test_user = api.user.get(username=TEST_USER_ID)
        test_user.setMemberProperties(
            mapping={'valid_to': DateTime('2020/01/01')})

    def tearDown(self):
        """Clean up after yourself."""
        self.log.clear()
        eventtesting.clearEvents()

    def test_wrong_secret(self):
        """Test secret is required to access @@validity."""
        view = self.portal.restrictedTraverse('validity')
        err_msg = "Wrong secret. Please configure it in control panel."

        # empty secret
        self.assertEquals(view.render(), err_msg)

        # wrong secret
        self.request['secret'] = 'wrong secret'
        self.assertEquals(view.render(), err_msg)

    @mock.patch('niteoweb.ipn.core.ipn.DateTime')
    def test_dry_run(self, DT):
        """Test that member is not disabled if dry-run is set to True."""
        DT.return_value = DateTime('2012/01/01')

        # first, let's create a member and enable it
        api.group.create(groupname='ipn_1')
        group = api.group.get(groupname='ipn_1')
        group.setGroupProperties(mapping={'validity': 31})
        self.ipn.enable_member(
            email='*****@*****.**',
            product_id='1',
            trans_type='SALE',
            fullname='New Member',
            affiliate='*****@*****.**',
        )

        # run @@validity
        DT.return_value = DateTime('2012/02/02')
        self.request['secret'] = 'secret'
        self.request['dry-run'] = True
        view = self.portal.restrictedTraverse('validity')
        view.render()

        # test member still has the Member role
        self.assertIn('Member', api.user.get_roles(username='******'))

        # test member is not in Disabled group
        self.assertNotIn(
            'disabled',
            [g.id for g in api.group.get_groups(username='******')]
        )

    def test_skip_disabled_members(self):
        """Test that disabled members are skipped."""
        # add test member to disabled group
        api.group.add_user(groupname='disabled', username=TEST_USER_ID)

        self.request['secret'] = 'secret'
        view = self.portal.restrictedTraverse('validity')
        view.render()

        self.assertEqual(self.log.records, [])

    @mock.patch('niteoweb.ipn.core.ipn.DateTime')
    def test_validity(self, DT):
        """Integration test of @@validity view."""
        DT.return_value = DateTime('2012/01/01')

        # first, let's create a member and enable it
        api.group.create(groupname='ipn_1')
        group = api.group.get(groupname='ipn_1')
        group.setGroupProperties(mapping={'validity': 31})
        self.ipn.enable_member(
            email='*****@*****.**',
            product_id='1',
            trans_type='SALE',
            fullname='New Member',
            affiliate='*****@*****.**',
        )

        # clear after yourself before you start testing
        self.log.clear()
        eventtesting.clearEvents()

        # all is prepared, let's run @@validity as anonymous user, a
        # month after the initial sale
        logout()
        DT.return_value = DateTime('2012/02/02')
        self.request['secret'] = 'secret'
        view = self.portal.restrictedTraverse('validity')
        html = view.render()

        # test member is in Disabled group
        self.assertIn(
            'disabled',
            [g.id for g in api.group.get_groups(username='******')]
        )

        # test member is in no other group
        # TODO: for some reason api.group.remove_user does not work here
        # if I pass in "user=member" --> it is not removed from group???
        self.assertItemsEqual(
            ['disabled', 'AuthenticatedUsers', ],
            [g.id for g in api.group.get_groups(username='******')]
        )

        # test member does not have the Member role
        self.assertNotIn('Member', api.user.get_roles(username='******'))

        # test event emitted
        events = list(set(eventtesting.getEvents(IMemberDisabledEvent)))
        self.assertEquals(len(events), 1)
        self.assertEquals(events[0].username, '*****@*****.**')

        # test member history
        self.assert_member_history(
            username='******',
            history=['2012/01/01 00:00:00|enable_member|1|SALE|',
                     '2012/02/02 00:00:00|disable_member|1|cronjob|'
                     'removed from groups: ipn_1, ']
        )

        # test HTML output
        self.assertEquals(
            html.split('\n'),
            [
                "START validity check.",
                "Disabling member '*****@*****.**' (2012/02/01).",
            ]
        )

        # test log output
        self.assertEqual(len(self.log.records), 5)
        self.assert_log_record(
            'INFO',
            'Anonymous User',
            "START disable_member:cronjob for '*****@*****.**'.",
        )
        self.assert_log_record(
            'INFO',
            'Anonymous User',
            "Adding member '*****@*****.**' to Disabled group.",
        )
        self.assert_log_record(
            'INFO',
            'Anonymous User',
            "Removing member '*****@*****.**' from group 'ipn_1'.",
        )
        self.assert_log_record(
            'INFO',
            'Anonymous User',
            "Revoking member '*****@*****.**' the Member role.",
        )
        self.assert_log_record(
            'INFO',
            'Anonymous User',
            "END disable_member:cronjob for '*****@*****.**'.",
        )
示例#44
0
class TestEnableMember(IntegrationTestCase):
    """Test runtime flow through the enable_member() action for most common
    use cases.
    """

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        self.ipn = queryAdapter(self.portal, IIPN)
        self.log = InstalledHandler('niteoweb.ipn.core')
        eventtesting.setUp()

        # create test product groups and set their validity
        api.group.create(groupname='ipn_1')
        group = api.group.get(groupname='ipn_1')
        group.setGroupProperties(mapping={'validity': 31})

        api.group.create(groupname='ipn_2')
        group = api.group.get(groupname='ipn_2')
        group.setGroupProperties(mapping={'validity': 365})

    def tearDown(self):
        """Clean up after yourself."""
        self.log.clear()
        eventtesting.clearEvents()

    @mock.patch('niteoweb.ipn.core.ipn.DateTime')
    def test_enable_new_member(self, DT):
        """Test creating a new member with enable_member() action."""
        DT.return_value = DateTime('2012/01/01')

        self.ipn.enable_member(
            email='*****@*****.**',
            product_id='1',
            trans_type='SALE',
            fullname='New Member',
            affiliate='*****@*****.**',
            note='Some test note!'
        )

        # test member exists
        member = api.user.get(username='******')
        self.assertTrue(member)

        # test member properties set correctly
        self.assertEqual(member.getProperty('product_id'), '1')
        self.assertEqual(member.getProperty('fullname'), 'New Member')
        self.assertEqual(member.getProperty('affiliate'), '*****@*****.**')

        # test member is in product group
        self.assertIn(
            '*****@*****.**',
            [user.id for user in api.user.get_users(groupname='ipn_1')]
        )

        # test member valid_to
        self.assertEqual(
            api.user.get(username='******').getProperty('valid_to'),
            DateTime('2012/02/01')
        )

        # test event emitted
        events = list(set(eventtesting.getEvents(IMemberEnabledEvent)))
        self.assertEquals(len(events), 1)
        self.assertEquals(events[0].username, '*****@*****.**')

        # test member history
        self.assert_member_history(
            username='******',
            history=[
                '2012/01/01 00:00:00|enable_member|1|SALE|Some test note!'
            ]
        )

        # test log output
        self.assertEqual(len(self.log.records), 5)
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "START enable_member:SALE for '*****@*****.**'.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Creating a new member: [email protected]",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Added member '*****@*****.**' to product group 'ipn_1'.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Member's ([email protected]) valid_to date set to 2012/02/01.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "END enable_member:SALE for '*****@*****.**'.",
        )

    @mock.patch('niteoweb.ipn.core.ipn.DateTime')
    def test_enable_enabled_member(self, DT):
        """Test enabling an already enabled member, meaning extending its
        validity period."""
        DT.return_value = DateTime('2012/01/01')

        # first create a valid member
        self.test_enable_new_member()

        # now let's say a month goes by and the member pays the recurring fee
        DT.return_value = DateTime('2012/02/01')
        self.ipn.enable_member(
            email='*****@*****.**',
            product_id='2',
            trans_type='RECUR',
            note='Some test note!'
        )

        # assert member product_id & valid_to
        self.assertEqual(
            api.user.get(username='******').getProperty('product_id'),
            '2',
        )
        self.assertEqual(
            api.user.get(username='******').getProperty('valid_to'),
            DateTime('2013/01/31')
        )

        # test event emitted
        events = list(set(eventtesting.getEvents(IMemberEnabledEvent)))
        self.assertEquals(len(events), 2)
        self.assertEquals(events[0].username, '*****@*****.**')
        self.assertEquals(events[1].username, '*****@*****.**')

        # test member history
        self.assert_member_history(
            username='******',
            history=[
                '2012/01/01 00:00:00|enable_member|1|SALE|Some test note!',
                '2012/02/01 00:00:00|enable_member|2|RECUR|Some test note!',
            ],
        )

        # test log output
        self.assertEqual(len(self.log.records), 4)
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "START enable_member:RECUR for '*****@*****.**'.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Added member '*****@*****.**' to product group 'ipn_2'.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Member's ([email protected]) valid_to date set to 2013/01/31.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "END enable_member:RECUR for '*****@*****.**'.",
        )

    @mock.patch('niteoweb.ipn.core.ipn.DateTime')
    def test_enable_disabled_member(self, DT):
        """Test enabling a previously disabled member."""
        DT.return_value = DateTime('2012/01/01')

        # first create a disabled member
        api.user.create(email='*****@*****.**')
        api.group.add_user(groupname='disabled', username='******')
        api.user.revoke_roles(
            username='******',
            roles=['Member', ]
        )

        self.ipn.enable_member(
            email='*****@*****.**',
            product_id='2',
            trans_type='UNCANCEL',
            note='Some test note!'
        )

        # test member is no longer in Disabled group
        self.assertNotIn(
            'disabled',
            [g.id for g in api.group.get_groups(username='******')]
        )

        # test member has Member role
        self.assertIn(
            'Member',
            api.user.get_roles(username='******'),
        )

        # assert member product_id & valid_to
        self.assertEqual(
            api.user.get(
                username='******').getProperty('product_id'),
            '2',
        )
        self.assertEqual(
            api.user.get(username='******').getProperty('valid_to'),
            DateTime('2012/12/31')
        )

        # test event emitted
        events = list(set(eventtesting.getEvents(IMemberEnabledEvent)))
        self.assertEquals(len(events), 1)
        self.assertEquals(events[0].username, '*****@*****.**')

        # test member history
        self.assert_member_history(
            username='******',
            history=[
                '2012/01/01 00:00:00|enable_member|2|UNCANCEL|Some test note!'
            ]
        )

        # test log output
        self.assertEqual(len(self.log.records), 6)
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "START enable_member:UNCANCEL for '*****@*****.**'.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Removing member '*****@*****.**' from Disabled group.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Granting member '*****@*****.**' the Member role.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Added member '*****@*****.**' to product group 'ipn_2'.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "Member's ([email protected]) valid_to date set to 2012/12/31.",
        )
        self.assert_log_record(
            'INFO',
            'test_user_1_',
            "END enable_member:UNCANCEL for '*****@*****.**'.",
        )
示例#45
0
 def setUp(self):
     super(TestLogActivityMonitor, self).setUp()
     self.handler = InstalledHandler('nti.zodb.activitylog')
     self.addCleanup(self.handler.uninstall)
示例#46
0
 def setUp(self):
     """Set up a class ready for testing"""
     self.loghandlers = dict(
         errorlog=InstalledHandler('tutorweb.quizdb.browser')
     )