def test_parse_date_range():
    utc_now = datetime.utcnow()
    utc_start_time, utc_end_time = parse_date_range('2 days', utc=True)
    # testing UTC date time and range of 2 days
    assert utc_now.replace(microsecond=0) == utc_end_time.replace(microsecond=0)
    assert abs(utc_start_time - utc_end_time).days == 2

    local_now = datetime.now()
    local_start_time, local_end_time = parse_date_range('73 minutes', utc=False)
    # testing local datetime and range of 73 minutes
    assert local_now.replace(microsecond=0) == local_end_time.replace(microsecond=0)
    assert abs(local_start_time - local_end_time).seconds / 60 == 73
示例#2
0
def test_fetch_incidents_command():
    """
    Given:
    - FraudWatch client.
    - Last run parameters.

    When:
     - Fetching incidents.

    Then:
     - Ensure that on first call to fetch_incidents, only day ago or later are fetched.
     - Ensure on another calls to fetch_incidents, only relevant incidents are fetched.
     - Ensure that the incidents returned are as expected.
    """
    now = datetime.now(timezone.utc)
    five_minutes_before = now - timedelta(minutes=5)
    one_hour_before = now - timedelta(hours=1)
    two_hours_before = now - timedelta(hours=2)
    two_days_before = now - timedelta(days=2)

    mock_response = command_tests_data['fetch-incidents']['response']
    mock_response['incidents'][0]['date_opened'] = five_minutes_before.isoformat()
    mock_response['incidents'][1]['date_opened'] = one_hour_before.isoformat()
    mock_response['incidents'][2]['date_opened'] = two_hours_before.isoformat()
    mock_response['incidents'][3]['date_opened'] = two_days_before.isoformat()

    client.incidents_list = Mock()
    client.incidents_list.side_effect = [
        mock_response,
        {'pagination': {}, 'incidents': []},
        mock_response,
        {'pagination': {}, 'incidents': []},
        mock_response,
        {'pagination': {}, 'incidents': []}
    ]

    incidents, next_run = fetch_incidents_command(client, {'max_fetch': 2, 'first_fetch': '5 days'}, {})
    assert incidents == [
        {'name': f'''{mock_response['incidents'][2].get('brand')}:{mock_response['incidents'][2].get('identifier')}''',
         'type': 'FraudWatch Incident',
         'occurred': mock_response['incidents'][2].get('date_opened'),
         'rawJSON': json.dumps(mock_response['incidents'][2])
         },
        {'name': f'''{mock_response['incidents'][1].get('brand')}:{mock_response['incidents'][1].get('identifier')}''',
         'type': 'FraudWatch Incident',
         'occurred': mock_response['incidents'][1].get('date_opened'),
         'rawJSON': json.dumps(mock_response['incidents'][1])
         }
    ]
    assert next_run == {'last_fetch_time': one_hour_before.isoformat()}
    incidents, next_run = fetch_incidents_command(client, {'max_fetch': 2}, next_run)
    assert incidents == [
        {'name': f'''{mock_response['incidents'][0].get('brand')}:{mock_response['incidents'][0].get('identifier')}''',
         'type': 'FraudWatch Incident',
         'occurred': mock_response['incidents'][0].get('date_opened'),
         'rawJSON': json.dumps(mock_response['incidents'][0])
         }
    ]
    assert next_run == {'last_fetch_time': five_minutes_before.isoformat()}
    incidents, next_run = fetch_incidents_command(client, {'max_fetch': 2}, next_run)
    assert incidents == []
    assert next_run == {'last_fetch_time': five_minutes_before.isoformat()}