def test_bulk_search_task_download_sync_timeout(bulk_search_task: BulkSearchTask):
    bulk_search_task.state = BulkSearchTaskState.IN_PROGRESS  # download will never be ready
    bs_update_json = copy.deepcopy(bs_status_json)
    bs_update_json['results'][0]['state'] = 'IN_PROGRESS'
    bs_status_url = f'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/tasks/'
    responses.add(responses.POST, bs_status_url, json=bs_update_json, status=200)

    with pytest.raises(TimeoutError):
        bulk_search_task.download_sync(timeout=0.2)
def test_bulk_search_task_download_sync_failed(bulk_search_task: BulkSearchTask):
    bulk_search_task.state = BulkSearchTaskState.IN_PROGRESS
    bs_update_json = copy.deepcopy(bs_status_json)
    bs_update_json['results'][0]['state'] = 'CANCELLED'
    bs_status_url = f'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/tasks/'
    responses.add(responses.POST, bs_status_url, json=bs_update_json, status=200)

    with pytest.raises(BulkSearchFailedError) as err:
        bulk_search_task.download_sync()
    assert err.value.failed_state == BulkSearchTaskState.CANCELLED
def test_bulk_search_task_download_not_ready(bulk_search_task: BulkSearchTask):
    task_uuid = bulk_search_task.uuid
    bs_download_url = f'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/task/{task_uuid}/'
    error_message = "Not ready yet"
    json_returned = {"message": ("%s" % error_message)}
    responses.add(responses.GET, bs_download_url, json=json_returned, status=202)

    with pytest.raises(ResponseNotReady) as err:
        bulk_search_task.download()
    assert str(err.value) == error_message
async def test_bulk_search_task_download_async_timeout(bulk_search_task: BulkSearchTask):
    with responses.RequestsMock() as response_context:  # pytest.mark.asyncio is not compatible with responses decorator
        bulk_search_task.state = BulkSearchTaskState.IN_PROGRESS  # download will never be ready
        bs_update_json = copy.deepcopy(bs_status_json)
        bs_update_json['results'][0]['state'] = 'IN_PROGRESS'
        bs_status_url = f'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/tasks/'
        response_context.add(responses.POST, bs_status_url, json=bs_update_json, status=200)

        loop = asyncio.get_event_loop()
        task = loop.create_task(bulk_search_task.download_async(timeout=0.2))
        with pytest.raises(TimeoutError):
            await task
def test_bulk_search_task_download_sync(bulk_search_task: BulkSearchTask):
    bs_status_url = 'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/tasks/'
    responses.add(responses.POST, bs_status_url, json=bs_status_json, status=200)
    bulk_search_task.state = BulkSearchTaskState.IN_PROGRESS  # download is not ready yet
    task_uuid = bulk_search_task.uuid
    bs_download_url = f'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/task/{task_uuid}/'
    expected_result = "bulk search download"
    responses.add(responses.GET, bs_download_url, json=expected_result, status=200)

    download_result = bulk_search_task.download_sync()

    assert download_result == expected_result
def test_bulk_search_task_update(bulk_search_task: BulkSearchTask):
    assert bulk_search_task.queue_position is None
    assert bulk_search_task.state == BulkSearchTaskState.DONE
    bs_status_url = 'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/tasks/'
    bs_update_json = copy.deepcopy(bs_status_json)
    bs_update_json['results'][0]['queue_position'] = 42
    responses.add(responses.POST, bs_status_url, json=bs_update_json, status=200)

    bulk_search_task.update()

    assert bulk_search_task.queue_position is 42
    assert bulk_search_task.state == BulkSearchTaskState.DONE  # field not modified
def test_bulk_search_task_download(bulk_search_task: BulkSearchTask):
    task_uuid = bulk_search_task.uuid
    bs_download_url = f'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/task/{task_uuid}/'
    expected_result = "bulk search download"
    responses.add(responses.GET, bs_download_url, json=expected_result, status=200)

    download_result = bulk_search_task.download()

    assert download_result == expected_result
def test_bulk_search_task_download_async(bulk_search_task: BulkSearchTask):
    task_uuid = bulk_search_task.uuid
    bs_download_url = f'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/task/{task_uuid}/'
    expected_result = "bulk search download"
    responses.add(responses.GET, bs_download_url, json=expected_result, status=200)

    loop = asyncio.get_event_loop()
    download_result = loop.run_until_complete(bulk_search_task.download_async())

    assert download_result == expected_result
def test_bulk_search_task_download_zip_json_output(bulk_search_task: BulkSearchTask):
    task_uuid = bulk_search_task.uuid
    bs_download_url = f'https://datalake.cert.orangecyberdefense.com/api/v2/mrti/bulk-search/task/{task_uuid}/'
    expected_result = 'zip json'

    def bs_download_callback(request):
        assert request.headers['Accept'] == 'application/zip'
        headers = {'Content-Type': 'application/zip'}
        return 200, headers, expected_result

    responses.add_callback(
        responses.GET,
        bs_download_url,
        callback=bs_download_callback,
        content_type='application/zip',
    )

    download_result = bulk_search_task.download(Output.JSON_ZIP)

    assert download_result == expected_result
def test_bulk_search_task_download_invalid_output(bulk_search_task: BulkSearchTask):
    with pytest.raises(ValueError) as err:
        bulk_search_task.download(Output.MISP)
    assert str(err.value) == f'MISP output type is not supported. Outputs supported are: CSV, CSV_ZIP, JSON, JSON_ZIP,'\
                             ' STIX, STIX_ZIP'