def test_invoke_lambda_for_ddb_config_with_failed_count(get_event_bus_stubber):
    lambda_event = create_cw_schedule_event()
    create_ddb_table_for_US_en()
    site_list = get_news_sites_for_US_en()

    for site in site_list:
        get_event_bus_stubber.add_response(
            "put_events",
            {
                "Entries": [{
                    "EventId": "fakeeventid"
                }],
                "FailedEntryCount": 1
            },
            {
                "Entries": [{
                    "EventBusName": os.environ["EVENT_BUS_NAME"],
                    "Source": os.environ["INGESTION_NAMESPACE"],
                    "Detail": create_event_data_for_ddb(site),
                    "DetailType": "config",
                }]
            },
        )
    get_event_bus_stubber.activate()

    from lambda_function import publish_config_handler

    assert None == publish_config_handler(lambda_event, None)
def test_search_video_when_api_throws_error(mock_youtube_resource,
                                            get_event_bus_stubber):
    api_key = "fakeapikey"
    ssm_setup(api_key)

    current_datetime = datetime.now(timezone.utc)
    publish_time = current_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")
    test_item = {
        "kind": "youtube#searchResult",
        "id": {
            "kind": "youtube#video",
            "videoId": "fakeId"
        },
        "snippet": {
            "publishTime": publish_time,
            "title": "fakeTitle",
        },
    }

    import googleapiclient.errors
    import mock

    mock_youtube_resource.return_value.search.return_value.list.return_value.execute.side_effect = (
        googleapiclient.errors.HttpError(mock.Mock(status=403),
                                         "Error invoking API".encode("utf-8")))
    get_event_bus_stubber.activate()

    from util.video import search_videos

    search_videos(None)

    get_event_bus_stubber.assert_no_pending_responses()
    get_event_bus_stubber.deactivate()
def test_search_video_with_event_bus_failures(mock_youtube_resource,
                                              get_event_bus_stubber):
    api_key = "fakeapikey"
    ssm_setup(api_key)

    get_event_bus_stubber.add_response(
        "put_events",
        {
            "Entries": [{
                "EventId": "fakeeventid"
            }],
            "FailedEntryCount": 1
        },
        {
            "Entries": [{
                "EventBusName":
                os.environ["EVENT_BUS_NAME"],
                "Source":
                os.environ["VIDEO_NAMESPACE"],
                "Detail":
                json.dumps({
                    "VideoId": "fakeId",
                    "SearchQuery": f'{os.environ["QUERY"]}#None',
                    "Title": "fakeTitle",
                }),
                "DetailType":
                "Video",
            }]
        },
    )

    current_datetime = datetime.now(timezone.utc)
    publish_time = current_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")
    test_item = {
        "kind": "youtube#searchResult",
        "id": {
            "kind": "youtube#video",
            "videoId": "fakeId"
        },
        "snippet": {
            "publishTime": publish_time,
            "title": "fakeTitle",
        },
    }

    mock_youtube_resource.return_value.search.return_value.list.return_value.execute.return_value = {
        "items": [test_item],
        "nextPageToken": None,
    }

    get_event_bus_stubber.activate()
    from util.video import search_videos

    search_videos(None)

    get_event_bus_stubber.assert_no_pending_responses()
    get_event_bus_stubber.deactivate()
def test_search_video_with_10_or_more_iterations(mock_youtube_resource,
                                                 get_event_bus_stubber):
    api_key = "fakeapikey"
    ssm_setup(api_key)

    count = 15
    entries = []
    for loop in range(10):
        entries.append({
            "EventBusName":
            os.environ["EVENT_BUS_NAME"],
            "Source":
            os.environ["VIDEO_NAMESPACE"],
            "Detail":
            json.dumps({
                "VideoId": f"fakeId{loop}",
                "SearchQuery": f'{os.environ["QUERY"]}#None',
                "Title": "fakeTitle",
            }),
            "DetailType":
            "Video",
        })

    get_event_bus_stubber.add_response("put_events", {
        "Entries": [{
            "EventId": "fakeeventid"
        }],
        "FailedEntryCount": 0
    }, {"Entries": entries})

    entries = []
    for loop in range(count - 10):
        entries.append({
            "EventBusName":
            os.environ["EVENT_BUS_NAME"],
            "Source":
            os.environ["VIDEO_NAMESPACE"],
            "Detail":
            json.dumps({
                "VideoId": f"fakeId{loop + 10}",
                "SearchQuery": f'{os.environ["QUERY"]}#None',
                "Title": "fakeTitle",
            }),
            "DetailType":
            "Video",
        })

    get_event_bus_stubber.add_response("put_events", {
        "Entries": [{
            "EventId": "fakeeventid"
        }],
        "FailedEntryCount": 0
    }, {"Entries": entries})

    get_event_bus_stubber.add_response(
        "put_events",
        {
            "Entries": [{
                "EventId": "fakeeventid"
            }],
            "FailedEntryCount": 0
        },
        {
            "Entries": [{
                "EventBusName":
                os.environ["EVENT_BUS_NAME"],
                "Source":
                os.environ["VIDEO_NAMESPACE"],
                "Detail":
                json.dumps({
                    "VideoId": f"fakeId{count}",
                    "SearchQuery": f'{os.environ["QUERY"]}#None',
                    "Title": "fakeTitle",
                }),
                "DetailType":
                "Video",
            }]
        },
    )

    current_datetime = datetime.now(timezone.utc)
    publish_time = current_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")

    items = []
    mock_responses = []

    for loop in range(count):
        items.append({
            "kind": "youtube#searchResult",
            "id": {
                "kind": "youtube#video",
                "videoId": f"fakeId{loop}"
            },
            "snippet": {
                "publishTime": publish_time,
                "title": "fakeTitle",
            },
        })

    mock_responses.append({"items": items, "nextPageToken": "fakePageToken"})
    mock_responses.append({
        "items": [{
            "kind": "youtube#searchResult",
            "id": {
                "kind": "youtube#video",
                "videoId": f"fakeId{count}"
            },
            "snippet": {
                "publishTime": publish_time,
                "title": "fakeTitle",
            },
        }],
        "nextPageToken":
        None,
    })
    mock_youtube_resource.return_value.search.return_value.list.return_value.execute.side_effect = mock_responses

    get_event_bus_stubber.activate()
    from util.video import search_videos

    search_videos(None)

    get_event_bus_stubber.assert_no_pending_responses()
    get_event_bus_stubber.deactivate()