def test_aggregated_reputation(self):
        with patch('mongo.mongo.Mongo') as mock:
            instance = mock.return_value
            enter = instance.__enter__.return_value
            enter.find_all_events_for_ip.return_value = [
                {'weight': 3, 'timestamp': 4, 'filename': 'ZZZ', 'source': 'SpamCop', 'data': 'AAA'},
                {'weight': 5, 'timestamp': 5, 'filename': 'YYY', 'source': 'SpamCop', 'data': 'BBB'},
                {'weight': 7, 'timestamp': 6, 'filename': 'XXX', 'source': 'AOL', 'data': 'CCC'}
            ]

            result = reputation.aggregate_reputation_per_source('5.5.5.5', 3)

            enter.find_all_events_for_ip.assert_called_with('5.5.5.5', 3, True)

            # Build expected values that should be all parser shortened names = 0, except SpamCop (SCOP) = 8 and AOL = 7.
            expected = []
            for parser in parsers.keys():
                weight = 0
                if parser == 'AOL':
                    weight = 7
                elif parser == 'SpamCop':
                    weight = 8

                expected.append({
                    'short_name': shortened_names[parser],
                    'full_name': parser,
                    'result': weight,
                })

            # Assertions
            self.assertEquals(len(expected), len(result))
            self.assertEqual(expected, result)
def get_reputation_per_source(addr):
    """
        Retrieve aggregated weights per source for a single ip. A timestamp
        can be passed (add `start_date` parameter) so weights are computed after
        provided date.
    """
    try:
        start_date = _get_start_date()
    except:
        return {"error": "Expected start_date to be an integer."}, 400

    return reputation_service.aggregate_reputation_per_source(addr, start_date), 200
def get_reputation_per_source(addr):
    """
        Retrieve aggregated weights per source for a single ip. A timestamp
        can be passed (add `start_date` parameter) so weights are computed after
        provided date.
    """
    try:
        start_date = _get_start_date()
    except:
        return {"error": "Expected start_date to be an integer."}, 400

    return reputation_service.aggregate_reputation_per_source(
        addr, start_date), 200