def test_verifyFailIfDiscoveryRedirects(self):
        realm = 'http://*.example.com/'
        return_to = 'http://www.example.com/foo'

        def vrfy(disco_url):
            raise trustroot.RealmVerificationRedirected(
                disco_url, "http://redirected.invalid")

        with LogCapture() as logbook:
            self.assertFalse(trustroot.verifyReturnTo(realm, return_to, _vrfy=vrfy))
        logbook.check(('openid.server.trustroot', 'INFO', StringComparison('Attempting to verify .*')))
 def test_get_file_dict_allow_raw_file(self):
     log_printer = ListLogPrinter()
     with LogCapture() as capture:
         file_dict = get_file_dict([self.unreadable_path], log_printer,
                                   True)
     self.assertNotEqual(file_dict, {})
     self.assertEqual(file_dict[self.unreadable_path], None)
     capture.check(
         ('root', 'DEBUG', StringComparison(r'(?s).*Files that will be '
                                            r'checked(?s).*'))
     )
 def testBadResponse(self):
     self.consumer.return_messages = [
         Message(self.endpoint.preferredNamespace())
     ]
     with LogCapture() as logbook:
         self.assertIsNone(
             self.consumer._negotiateAssociation(self.endpoint))
     logbook.check(
         ('openid.consumer.consumer', 'ERROR',
          StringComparison('Server error when requesting an association .*')
          ))
 def match_regex(self, regex_pattern, name=None, level=None):
     matches = []
     for r in self.records:
         if name and r.name != name:
             continue
         if level and r.levelname != level:
             continue
         if r.getMessage() != StringComparison(regex_pattern):
             continue
         matches.append(r)
     return matches
 def test_exeption(self):
     mltprsm = MultiprocessingManager()
     mltprsm.append(exeption_raising_func, {'arg': 0})
     exp = IOError(StringComparison(
         "The function 'exeption_raising_func' of "
         "'slidelint.tests.modules.linter.test_modules' module"
         " raised an Exception:\n"
         "integer division or modulo by zero\n"
         ".*"))
     with ShouldRaise(exp):
         [i for i in mltprsm]
    def test_verifyFailWithDiscoveryCalled(self):
        realm = 'http://*.example.com/'
        return_to = 'http://www.example.com/foo'

        def vrfy(disco_url):
            self.assertEqual(disco_url, 'http://www.example.com/')
            return ['http://something-else.invalid/']

        with LogCapture() as logbook:
            self.assertFalse(trustroot.verifyReturnTo(realm, return_to, _vrfy=vrfy))
        logbook.check(('openid.server.trustroot', 'INFO', StringComparison('Failed to validate return_to .*')))
Example #7
0
 def test_untrimmed(self):
     """Should not trim"""
     req = '{"jsonrpc": "2.0", "method": "go", "params": {"foo": "%s"}}' % (
         "foo" * 100, )
     with LogCapture() as capture:
         DummyClient().log_request(req, trim_log_values=False)
     capture.check((
         "jsonrpcclient.client.request",
         "INFO",
         StringComparison(r".*" + "foo" * 100 + ".*"),
     ))
 def test_bad_args(self):
     mltprsm = MultiprocessingManager()
     mltprsm.append(exeption_raising_func, {'x': 0})
     exp = IOError(StringComparison(
         "The function 'exeption_raising_func' of "
         "'slidelint.tests.modules.linter.test_modules' module"
         " raised an Exception:\n"
         "exeption_raising_func\(\) got an unexpected keyword"
         " argument 'x'\n"
         ".*"))
     with ShouldRaise(exp):
         [i for i in mltprsm]
Example #9
0
    def testEmptySessionType(self):
        msg = Message(self.endpoint.preferredNamespace())
        msg.setArg(OPENID_NS, 'error', 'Unsupported type')
        msg.setArg(OPENID_NS, 'error_code', 'unsupported-type')
        msg.setArg(OPENID_NS, 'assoc_type', 'new-assoc-type')
        # not set: msg.setArg(OPENID_NS, 'session_type', None)

        self.consumer.return_messages = [msg]
        with LogCapture() as logbook:
            self.assertIsNone(self.consumer._negotiateAssociation(self.endpoint))
        logbook.check(
            ('openid.consumer.consumer', 'ERROR', StringComparison('Server error when requesting an association .*')))
Example #10
0
 def test_register_alerting_configured(self, mock_import, mock_add):
     manager = self._init_tool(['check', 'config_path', '-v'])
     manager.settings['alerting'] = {
         'monitoring1': {
             'module': 'monitoring1',
             'class': 'TestMonitoringPlugin',
             'config': {
                 'k1': 'v1',
                 'k2': 'v2'
             }
         },
         'dummy': {
             'module': 'test',
             'class': 'DummyAlertingPlugin'
         }
     }
     with LogCapture() as log:
         manager._register_alerting()
     mock_import.assert_has_calls([
         mock.call('ipamanager.alerting.monitoring1'),
         mock.call('ipamanager.alerting.test')
     ],
                                  any_order=True)
     plugin1 = mock_import(
         'ipamanager.alerting.monitoring1').TestMonitoringPlugin
     plugin1.assert_called_with({'k1': 'v1', 'k2': 'v2'})
     plugin2 = mock_import('ipamanager.alerting.test').DummyAlertingPlugin
     plugin2.assert_called_with({})
     instances = {p.return_value for p in (plugin1, plugin2)}
     mock_add.assert_has_calls([mock.call(i) for i in instances],
                               any_order=True)
     assert set(manager.alerting_plugins) == instances
     log.check_present(
         ('FreeIPAManager', 'DEBUG', 'Registering 2 alerting plugins'),
         ('FreeIPAManager', 'DEBUG',
          StringComparison("Registered plugin .*TestMonitoringPlugin.*")),
         ('FreeIPAManager', 'DEBUG',
          StringComparison("Registered plugin .*DummyAlertingPlugin.*")),
         order_matters=False)
Example #11
0
    def test_sane_repr(self):
        self.assertEqual(sane_repr(None, 1024), str("None"))
        self.assertEqual(sane_repr(10562, 1024), str("10562"))
        self.assertEqual(sane_repr('short', 1024),
                         StringComparison("u?'short'"))
        self.assertEqual(sane_repr(ValueError('A message'), 1024),
                         StringComparison(r"ValueError\(u?'A message',?\)"))

        # Letter "g" on the end is present if unicode prefix is absent
        self.assertEqual(sane_repr('something longer', 10),
                         StringComparison(r"u?'something? \[truncated\]..."))

        self.assertEqual(
            sane_repr('ěščřž'.encode('utf-8'), 1024),
            StringComparison(
                "b?'\\\\xc4\\\\x9b\\\\xc5\\\\xa1\\\\xc4\\\\x8d\\\\xc5\\\\x99\\\\xc5\\\\xbe'"
            ))
        if six.PY2:  # pragma: no cover
            self.assertEqual(sane_repr('ěščřž', 1024),
                             str("u'\\u011b\\u0161\\u010d\\u0159\\u017e'"))
        else:  # pragma: no cover
            self.assertEqual(sane_repr('ěščřž', 1024), str("'ěščřž'"))
Example #12
0
 def test_model_pedantic(self):
     stan_file = os.path.join(DATAFILES_PATH, 'bernoulli_pedantic.stan')
     with LogCapture(level=logging.WARNING) as log:
         logging.getLogger()
         model = CmdStanModel(model_name='bern', stan_file=stan_file)
         model.compile(force=True, stanc_options={'warn-pedantic': True})
     log.check_present(
         (
             'cmdstanpy',
             'WARNING',
             StringComparison(r'(?s).*The parameter theta has no priors.*'),
         )
     )
def test_entity_mention_tokenization():
    comm = read_test_comm()
    assert validate_communication(comm)
    assert validate_entity_mention_ids(comm)

    comm.entityMentionSetList[0].mentionList[0].tokens.tokenizationId = (
        concrete.UUID(uuidString='BAD_TOKENIZATION_UUID'))

    with LogCapture() as log_capture:
        assert not validate_entity_mention_tokenization_ids(comm)
    log_capture.check(
        ('root', 'ERROR',
         StringComparison(r'.*invalid tokenizationId.*BAD_TOKENIZATION_UUID')))
Example #14
0
 def test_get_file_dict_forbid_raw_file(self):
     log_printer = ListLogPrinter()
     with LogCapture() as capture:
         file_dict = get_file_dict([self.unreadable_path], log_printer,
                                   False)
     self.assertEqual(file_dict, {})
     capture.check(('root', 'WARNING',
                    "Failed to read file '{}'. It seems to contain "
                    'non-unicode characters. Leaving it out.'.format(
                        self.unreadable_path)),
                   ('root', 'DEBUG',
                    StringComparison(r'(?s).*Files that will be '
                                     r'checked(?s).*')))
Example #15
0
    def test_connection(self):
        """
        Run a unit test on the connection method
        Note: a valid configuration file is required.
              Specify the path to the file in the environment variable POSTGRES_CFG.
        """
        db = self.get_test_database()
        valid_cfg = db.get_configuration()

        # test valid config
        self.assertFalse(db.is_connected())
        connection = db.get_connection()
        self.assertIsNotNone(connection)
        self.assertTrue(db.is_connected())
        db.close_connection()
        self.assertFalse(db.is_connected())

        # test erroneous connection config one argument at a time
        with LogCapture() as log_cap:
            for idx in list(range(len(PostgresDb.KEYS))):

                if idx == port_idx:
                    # TODO figure out way to test connection timeout, skip for now
                    continue

                db[PostgresDb.KEYS[idx]] = values[idx]
                connection = db.get_connection()
                self.assertIsNone(connection)

                db[PostgresDb.KEYS[idx]] = valid_cfg[PostgresDb.KEYS[idx]]

            auth_failed_regex = r'.*password authentication failed for user.*'
            log_cap.check(
                ('root', 'WARNING', StringComparison(auth_failed_regex)),
                ('root', 'WARNING', StringComparison(auth_failed_regex)),
                ('root', 'WARNING', StringComparison(r'.*does not exist.*')),
                ('root', 'WARNING',
                 StringComparison(r'could not translate host name.*')))
Example #16
0
    def testNotAllowed(self):
        """
        Test the case where an unsupported-type response specifies a
        preferred (assoc_type, session_type) combination that is not
        allowed by the consumer's SessionNegotiator.
        """
        allowed_types = []

        negotiator = association.SessionNegotiator(allowed_types)
        self.consumer.negotiator = negotiator

        msg = Message(self.endpoint.preferredNamespace())
        msg.setArg(OPENID_NS, 'error', 'Unsupported type')
        msg.setArg(OPENID_NS, 'error_code', 'unsupported-type')
        msg.setArg(OPENID_NS, 'assoc_type', 'not-allowed')
        msg.setArg(OPENID_NS, 'session_type', 'not-allowed')

        self.consumer.return_messages = [msg]
        with LogCapture() as logbook:
            self.assertIsNone(self.consumer._negotiateAssociation(self.endpoint))
        unsupported_msg = StringComparison('Server sent unsupported session/association type: .*')
        logbook.check(('openid.consumer.consumer', 'WARNING', StringComparison('Unsupported association type .*')),
                      ('openid.consumer.consumer', 'WARNING', unsupported_msg))
def test_entity_mention_ids():
    comm = read_test_comm()
    assert validate_communication(comm)
    assert validate_entity_mention_ids(comm)

    comm.entitySetList[0].entityList[0].mentionIdList[0] = concrete.UUID(
        uuidString='BAD_ENTITY_MENTION_UUID')

    with LogCapture() as log_capture:
        assert not validate_entity_mention_ids(comm)
    log_capture.check(
        ('root', 'ERROR',
         StringComparison(
             r'.*invalid entityMentionId.*BAD_ENTITY_MENTION_UUID')))
Example #18
0
    def assertEmailNotSent(self, function, reason):
        with LogCapture(emails.logger.name) as log_capture:
            function(self.course_run)

        assert len(mail.outbox) == 0

        if reason:
            log_capture.check((
                emails.logger.name,
                'INFO',
                StringComparison(
                    'Not sending notification email for template course_metadata/email/.* because '
                    + reason),
            ))
    def testUnsupportedWithRetryAndFail(self):
        """
        Test the case where an unsupported-typ response triggers a
        retry, but the retry fails and None is returned instead.
        """
        msg = Message(self.endpoint.preferredNamespace())
        msg.setArg(OPENID_NS, 'error', 'Unsupported type')
        msg.setArg(OPENID_NS, 'error_code', 'unsupported-type')
        msg.setArg(OPENID_NS, 'assoc_type', 'HMAC-SHA1')
        msg.setArg(OPENID_NS, 'session_type', 'DH-SHA1')

        self.consumer.return_messages = [
            msg, Message(self.endpoint.preferredNamespace())
        ]

        with LogCapture() as logbook:
            self.assertIsNone(
                self.consumer._negotiateAssociation(self.endpoint))
        refused_msg = StringComparison('Server %s refused its .*' %
                                       self.endpoint.server_url)
        logbook.check(('openid.consumer.consumer', 'ERROR',
                       StringComparison('Unsupported association type .*')),
                      ('openid.consumer.consumer', 'ERROR', refused_msg))
Example #20
0
    def testUnsupportedWithRetry(self):
        msg = Message(self.endpoint.preferredNamespace())
        msg.setArg(OPENID_NS, 'error', 'Unsupported type')
        msg.setArg(OPENID_NS, 'error_code', 'unsupported-type')
        msg.setArg(OPENID_NS, 'assoc_type', 'HMAC-SHA1')
        msg.setArg(OPENID_NS, 'session_type', 'DH-SHA1')

        assoc = association.Association('handle', b'secret', 'issued', 10000, 'HMAC-SHA1')

        self.consumer.return_messages = [msg, assoc]
        with LogCapture() as logbook:
            self.assertIsNone(self.consumer._negotiateAssociation(self.endpoint))
        logbook.check(
            ('openid.consumer.consumer', 'ERROR', StringComparison('Server error when requesting an association .*')))
 def testBadResponse(self):
     """
     Test the case where the response to an associate request is a
     server error or is otherwise undecipherable.
     """
     self.consumer.return_messages = [
         Message(self.endpoint.preferredNamespace())
     ]
     with LogCapture() as logbook:
         self.assertEqual(
             self.consumer._negotiateAssociation(self.endpoint), None)
     logbook.check(
         ('openid.consumer.consumer', 'ERROR',
          StringComparison('Server error when requesting an association .*')
          ))
Example #22
0
    def test_fetch_from_properties_lemma_not_found(self):
        self.page_mock.title_str = "RE:Aas"
        self.page_mock.text = """{{REDaten
|BAND=I,1
|WP=Aal_wp_link
|WS=Aal_ws_link
}}
text.
{{REAutor|OFF}}"""
        task = SCANTask(None, self.logger)
        task.re_page = RePage(self.page_mock)
        with LogCapture() as log_catcher:
            task._process_from_article_list()
            log_catcher.check(mock.ANY, ("Test", "ERROR",
                                         StringComparison("No available Lemma in Registers for issue I,1 "
                                                          ".* Reason is:.*")))
    def test_handle_pass(self, mock_response):
        """ Test using mock response from setup, using threshold it will clear"""

        mock_response.return_value = self.test_response

        with LogCapture(self.LOGGER_NAME) as log:
            call_command('populate_sdn_fallback_data_and_metadata',
                         '--threshold=0.0001')

            log.check((
                self.LOGGER_NAME, 'INFO',
                StringComparison(
                    r'(?s)SDNFallback: IMPORT SUCCESS: Imported SDN CSV\. Metadata id.*'
                )
            ), (self.LOGGER_NAME, 'INFO',
                "SDNFallback: DOWNLOAD SUCCESS: Successfully downloaded the SDN CSV."
                ))
Example #24
0
    def testEmptySessionType(self):
        """
        Test the case where the session type (session_type) returned
        in an unsupported-type response is absent.
        """
        msg = Message(self.endpoint.preferredNamespace())
        msg.setArg(OPENID_NS, 'error', 'Unsupported type')
        msg.setArg(OPENID_NS, 'error_code', 'unsupported-type')
        msg.setArg(OPENID_NS, 'assoc_type', 'new-assoc-type')
        # not set: msg.setArg(OPENID_NS, 'session_type', None)

        self.consumer.return_messages = [msg]
        with LogCapture() as logbook:
            self.assertIsNone(self.consumer._negotiateAssociation(self.endpoint))
        no_fallback_msg = 'Server responded with unsupported association session but did not supply a fallback.'
        logbook.check(('openid.consumer.consumer', 'WARNING', StringComparison('Unsupported association type .*')),
                      ('openid.consumer.consumer', 'WARNING', no_fallback_msg))
Example #25
0
    def testUnsupportedWithRetry(self):
        """
        Test the case where an unsupported-type response triggers a
        retry to get an association with the new preferred type.
        """
        msg = Message(self.endpoint.preferredNamespace())
        msg.setArg(OPENID_NS, 'error', 'Unsupported type')
        msg.setArg(OPENID_NS, 'error_code', 'unsupported-type')
        msg.setArg(OPENID_NS, 'assoc_type', 'HMAC-SHA1')
        msg.setArg(OPENID_NS, 'session_type', 'DH-SHA1')

        assoc = association.Association('handle', b'secret', 'issued', 10000, 'HMAC-SHA1')

        self.consumer.return_messages = [msg, assoc]
        with LogCapture() as logbook:
            self.assertEqual(self.consumer._negotiateAssociation(self.endpoint), assoc)
        logbook.check(('openid.consumer.consumer', 'WARNING', StringComparison('Unsupported association type .*')))
Example #26
0
    def testNotAllowed(self):
        allowed_types = []

        negotiator = association.SessionNegotiator(allowed_types)
        self.consumer.negotiator = negotiator

        msg = Message(self.endpoint.preferredNamespace())
        msg.setArg(OPENID_NS, 'error', 'Unsupported type')
        msg.setArg(OPENID_NS, 'error_code', 'unsupported-type')
        msg.setArg(OPENID_NS, 'assoc_type', 'not-allowed')
        msg.setArg(OPENID_NS, 'session_type', 'not-allowed')

        self.consumer.return_messages = [msg]
        with LogCapture() as logbook:
            self.assertIsNone(self.consumer._negotiateAssociation(self.endpoint))
        logbook.check(
            ('openid.consumer.consumer', 'ERROR', StringComparison('Server error when requesting an association .*')))
Example #27
0
def test_read_csv():
    """
    Tests whether the monkey patching of ('pandas.io.parsers', 'read_csv') works
    """
    test_code = cleandoc("""
        import os
        import pandas as pd
        from mlinspect.utils import get_project_root
        
        train_file = os.path.join(str(get_project_root()), "example_pipelines", "adult_complex", "adult_train.csv")
        raw_data = pd.read_csv(train_file, na_values='?', index_col=0)
        assert len(raw_data) == 22792
        """)

    inspector_result = _pipeline_executor.singleton.run(python_code=test_code, track_code_references=True,
                                                        inspections=[RowLineage(2)])

    extracted_node: DagNode = list(inspector_result.dag.nodes)[0]
    expected_node = DagNode(0,
                            BasicCodeLocation("<string-source>", 6),
                            OperatorContext(OperatorType.DATA_SOURCE, FunctionInfo('pandas.io.parsers', 'read_csv')),
                            DagNodeDetails(StringComparison(r".*\.csv"),
                                           ['age', 'workclass', 'fnlwgt', 'education', 'education-num',
                                            'marital-status',
                                            'occupation', 'relationship', 'race', 'sex', 'capital-gain', 'capital-loss',
                                            'hours-per-week', 'native-country', 'income-per-year']),
                            OptionalCodeInfo(CodeReference(6, 11, 6, 62),
                                             "pd.read_csv(train_file, na_values='?', index_col=0)"))
    compare(extracted_node, expected_node)

    inspection_results_data_source = inspector_result.dag_node_to_inspection_results[extracted_node]
    lineage_output = inspection_results_data_source[RowLineage(2)]
    expected_lineage_df = DataFrame([[46, 'Private', 128645, 'Some-college', 10, 'Divorced', 'Prof-specialty',
                                      'Not-in-family', 'White', 'Female', 0, 0, 40, 'United-States', '<=50K',
                                      {LineageId(0, 0)}],
                                     [29, 'Local-gov', 115585, 'Some-college', 10, 'Never-married', 'Handlers-cleaners',
                                      'Not-in-family', 'White', 'Male', 0, 0, 50, 'United-States', '<=50K',
                                      {LineageId(0, 1)}]],
                                    columns=['age', 'workclass', 'fnlwgt', 'education', 'education-num',
                                             'marital-status', 'occupation', 'relationship', 'race', 'sex',
                                             'capital-gain', 'capital-loss', 'hours-per-week', 'native-country',
                                             'income-per-year', 'mlinspect_lineage'])

    pandas.testing.assert_frame_equal(lineage_output.reset_index(drop=True), expected_lineage_df.reset_index(drop=True))
Example #28
0
    def run_command(self,
                    courses=None,
                    orgs=None,
                    allow_for=None,
                    commit=True,
                    fails=None,
                    log=None):
        command_args = ['--partner=' + self.partner.short_code]
        if commit:
            command_args.append('--commit')
        if courses is None and orgs is None:
            courses = [self.course]
        if courses:
            command_args += ['--course=' + str(c.uuid) for c in courses]
        if orgs:
            command_args += ['--org=' + str(o.key) for o in orgs]
        if allow_for:
            command_args += [
                '--allow-for=' + type_slug_run_slug_tuple
                for type_slug_run_slug_tuple in allow_for
            ]

        with LogCapture(logger.name) as log_capture:
            if fails:
                fails = fails if isinstance(fails, list) else [fails]
                keys = sorted('{key} ({id})'.format(key=fail.key, id=fail.id)
                              for fail in fails)
                msg = 'Could not backpopulate a course type for the following courses: {course_keys}'.format(
                    course_keys=', '.join(keys))
                with self.assertRaisesMessage(CommandError, msg):
                    self.call_command(*command_args)
            else:
                self.call_command(*command_args)

        if log:
            log_capture.check_present(
                (logger.name, 'INFO', StringComparison(log)))

        # As a convenience, refresh our built in courses and runs
        for obj in (self.course, self.audit_run, self.verified_run,
                    self.course2, self.c2_audit_run):
            if obj.id:
                obj.refresh_from_db()
Example #29
0
    def test_openID2MismatchedDoesDisco(self):
        mismatched = discover.OpenIDServiceEndpoint()
        mismatched.identity = 'nothing special, but different'
        mismatched.local_id = 'green cheese'

        op_endpoint = 'Phone Home'
        sentinel = discover.OpenIDServiceEndpoint()
        sentinel.claimed_id = 'monkeysoft'
        self.consumer._discoverAndVerify = const(sentinel)
        msg = message.Message.fromOpenIDArgs(
            {'ns': message.OPENID2_NS,
             'identity': 'sour grapes',
             'claimed_id': 'monkeysoft',
             'op_endpoint': op_endpoint})
        with LogCapture() as logbook:
            result = self.consumer._verifyDiscoveryResults(msg, mismatched)
        self.assertEqual(result, sentinel)
        logbook.check(('openid.consumer.consumer', 'ERROR', StringComparison('Error attempting to use .*')),
                      ('openid.consumer.consumer', 'INFO', 'Attempting discovery to verify endpoint'))
Example #30
0
    def test_failing_action(self):
        class FailingTestAction(ResultAction):
            def apply(self, *args, **kwargs):
                raise RuntimeError("YEAH THAT'S A FAILING BEAR")

        ACTIONS.append(FailingTestAction)

        self.section.append(
            Setting('default_actions', 'YBear: FailingTestAction'))
        with LogCapture() as capture:
            ret = autoapply_actions(self.results, {}, {}, self.section,
                                    self.log_printer)
        self.assertEqual(ret, self.results)
        capture.check(
            ('root', 'ERROR', "Failed to execute action 'FailingTestAction' "
             "with error: YEAH THAT'S A FAILING BEAR."),
            ('root', 'INFO',
             StringComparison(r"(?s).*YEAH THAT'S A FAILING BEAR.*")),
            ('root', 'DEBUG', '-> for result ' + repr(self.resultY) + '.'))
        ACTIONS.pop()