Beispiel #1
0
    def validate_and_score(self, audit_logs):

        audit_log_validator = AuditLogValidator()

        component = []
        sub_component = []

        validation_errors = []
        score = []

        users = []
        teams = []
        comps = []
        sub_comps = []
        count = []

        for log in audit_logs:
            validation_result = audit_log_validator.validate(log, self.rules)
            score_result, user, team, comp, subcomp = audit_log_validator.score(
                validation_result, 1 / Decimal(len(self.rules)), self.weights)

            score.append(round(score_result, 2))
            users.append(user)
            teams.append(team)
            comps.append(comp)
            sub_comps.append(subcomp)

            if log.has_key("component-attr"):
                component.append(log["component-attr"])
            else:
                component.append('')

            if log.has_key("sub-component-attr"):
                sub_component.append(log["sub-component-attr"])
            else:
                sub_component.append('')

            error = Json.dumps(validation_result, default=lambda x: x.__dict__)
            validation_errors.append(error)

            count.append(1)

            continue

        logs = {
            "score": score,
            "component_name": component,
            "users": users,
            "teams": teams,
            "comps": comps,
            "subcomps": sub_comps,
            "records": count,
            "total_count": len(audit_logs)
        }

        return logs
    def validate_and_score(self, audit_logs):

        audit_log_validator = AuditLogValidator()

        component = []
        sub_component = []

        validation_errors = []
        score = []

        users = []
        teams = []
        comps = []
        sub_comps = []
        actions = []

        for log in audit_logs:
            validation_result = audit_log_validator.validate(log, self.rules)
            score_result, user, team, comp, sub_comp = audit_log_validator.score(validation_result, 1/Decimal(len(self.rules)), self.weights)

            score.append(round(score_result, 2))
            users.append(user)
            teams.append(team)
            comps.append(comp)
            sub_comps.append(sub_comp)

            if log.has_key(constant.COMPONENT_ATTRIBUTE):
                component.append(log[constant.COMPONENT_ATTRIBUTE])
            else:
                component.append('')

            if log.has_key(constant.SUB_COMPONENT_ATTRIBUTE):
                sub_component.append(log[constant.SUB_COMPONENT_ATTRIBUTE])
            else:
                sub_component.append('')

            if log.has_key(constant.ACTION_ATTRIBUTE):
                actions.append(log[constant.ACTION_ATTRIBUTE])
            else:
                actions.append('')

            json_errors = Json.dumps(validation_result, default=lambda x: x.__dict__)
            validation_errors.append(json_errors)

            continue

        logs = {
            "users": users,
            "teams": teams,
            "comps": comps,
            "subcomps": sub_comps,
            "actions": actions
        }

        return logs
    def test_validate_Audit_log_positive_scenario(self):
        validator = AuditLogValidator()

        audit_log = {
            'user-attr': '*****@*****.**',
            'team-attr': 'CORETSA',
            'component-attr': 'component-',
            'sub-component-attr': 'sub-component-'
        }

        rules = get_rules(['*****@*****.**', '*****@*****.**'],
                          ["CORETSA", "TSATEST", "FTest"])

        error = validator.validate(audit_log, rules)

        self.assertEqual(error[0], True, msg='validation status.')
    def test_calculate_score_sub_component_attribute_scenarios(self):
        validator = AuditLogValidator()
        weights = {'sub-component-attr': 3}
        required_attr_error = "must be present"
        single_error = [
            'must match regex pattern <_sre.SRE_Pattern object at 0x7fe107ada418>'
        ]
        property_weight = 0.25

        key = 'sub-component-attr'

        self.assertEqual(
            validator.calculate_score(key, single_error, property_weight,
                                      weights), (0.08333333333333333, 3))
        self.assertEqual(
            validator.calculate_score(key, required_attr_error,
                                      property_weight, weights), (0, 2))
    def test_validate_sub_component_attribute_partially_valid_scenario(self):
        validator = AuditLogValidator()

        audit_log = {
            'user-attr': '*****@*****.**',
            'team-attr': 'CORETSA',
            'component-attr': 'comp',
            'sub-component-attr': 'sub-'
        }

        rules = get_rules(['*****@*****.**', '*****@*****.**'],
                          ["CORETSA", "TSATEST", "FTest"])

        error = validator.validate(audit_log, rules)

        self.assertEqual(error[0], False, msg='validation status.')
        self.assertTrue(len(error[1]) >= 1)
    def test_validate_sub_component_attribute_negative_scenario(self):
        validator = AuditLogValidator()

        audit_log = {
            'user-attr': '*****@*****.**',
            'team-attr': 'CORETSA',
            'component-attr': 'component-'
        }

        rules = get_rules(['*****@*****.**', '*****@*****.**'],
                          ["CORETSA", "TSATEST", "FTest"])

        error = validator.validate(audit_log, rules)

        self.assertEqual(error[0], False, msg='validation status.')
        self.assertEqual(error[1]['sub-component-attr'],
                         'must be present',
                         msg='error description.')
    def test_calculate_score_team_attribute_scenarios(self):
        validator = AuditLogValidator()
        weights = {'team-attr': 3}
        required_attr_error = "must be present"
        single_error = [
            'must match regex pattern <_sre.SRE_Pattern object at 0x7fe107ada418>'
        ]
        property_weight = 0.25

        multiple_error = [
            'must match regex pattern <_sre.SRE_Pattern object at 0x1b80060>',
            "must be one of [u'*****@*****.**', u'*****@*****.**']"
        ]
        key = 'team-attr'

        self.assertEqual(
            validator.calculate_score(key, single_error, property_weight,
                                      weights), (0.08333333333333333, 3))
        self.assertEqual(
            validator.calculate_score(key, multiple_error, property_weight,
                                      weights), (0.16666666666666666, 3))
        self.assertEqual(
            validator.calculate_score(key, required_attr_error,
                                      property_weight, weights), (0, 2))