def test_get_submitted_drafts_returns_submitted_draft_services_only(mock_data_client):
    mock_data_client.find_draft_services_iter.return_value = iter((
        {"id": 1, "status": "not-submitted"},
        {"id": 2, "status": "submitted"},
    ))
    result = get_submitted_drafts(mock_data_client, 12345, 'digital-biscuits-and-cakes')
    assert (result == [{"id": 2, "status": "submitted"}])
def process_submitted_drafts(client, supplier_id, user):
    submitted_drafts = get_submitted_drafts(client, FRAMEWORK_SLUG, supplier_id)
    supplier_has_submitted_services = False

    for draft in submitted_drafts:
        draft_result = check_service_essentials(draft)
        if draft_result == PASS:
            supplier_has_submitted_services = True
        else:
            # Update the draft to be 'failed'
            print("  Service essentials failed for draft {} in lot '{}'".format(draft['id'], draft['lot']))
            client.update_draft_service_status(draft['id'], "failed", user)
    print("  Supplier has valid service: {}".format("YES" if supplier_has_submitted_services else "NO"))
    return supplier_has_submitted_services
def test_get_submitted_drafts_calls_with_correct_arguments(mock_data_client):
    mock_data_client.find_draft_services_iter.return_value = iter([])
    get_submitted_drafts(mock_data_client, 'digital-biscuits-and-cakes', 12345)
    mock_data_client.find_draft_services_iter.assert_called_with(12345, framework='digital-biscuits-and-cakes')
        if dry_run:
            print(u"    > not updating document URLs {}".format(document_updates))
        else:
            client.update_service(service_id, document_updates, 'Moving documents to live bucket')
            print("    > document URLs updated")
    except Exception as e:
        print("ERROR MAKING DRAFT '{}' LIVE: {}".format(draft_service['id'], e.message))


if __name__ == '__main__':
    arguments = docopt(__doc__)

    STAGE = arguments['<stage>']
    api_url = get_api_endpoint_from_stage(STAGE)

    client = DataAPIClient(api_url, arguments['<api_token>'])
    DRAFT_BUCKET = S3(arguments['<draft_bucket>'])
    DOCUMENTS_BUCKET = S3(arguments['<documents_bucket>'])
    DRY_RUN = arguments['--dry-run']
    FRAMEWORK_SLUG = arguments['<framework_slug>']
    copy_document = document_copier(DRAFT_BUCKET, DOCUMENTS_BUCKET, DRY_RUN)

    suppliers = find_suppliers_on_framework(client, FRAMEWORK_SLUG)

    for supplier in suppliers:
        print(u"Migrating drafts for supplier {} - {}".format(supplier['supplierId'], supplier['supplierName']))
        draft_services = get_submitted_drafts(client, FRAMEWORK_SLUG, supplier['supplierId'])

        for draft_service in draft_services:
            make_draft_service_live(client, copy_document, draft_service, FRAMEWORK_SLUG, DRY_RUN)