Beispiel #1
0
def publish_website_batch(
    website_names: List[str],
    version: str,
    prepublish: Optional[bool] = False,
    trigger_pipeline: Optional[bool] = True,
) -> bool:
    """ Call api.publish_website for a batch of websites"""
    result = True
    if trigger_pipeline and settings.CONTENT_SYNC_PIPELINE_BACKEND:
        pipeline_api = import_string(
            f"content_sync.pipelines.{settings.CONTENT_SYNC_PIPELINE_BACKEND}.SitePipeline"
        ).get_api()
    else:
        pipeline_api = None
    for name in website_names:
        try:
            backend = import_string(settings.CONTENT_SYNC_BACKEND)(
                Website.objects.get(name=name))
            api.throttle_git_backend_calls(backend)
            api.publish_website(
                name,
                version,
                pipeline_api=pipeline_api,
                prepublish=prepublish,
                trigger_pipeline=trigger_pipeline,
            )
        except:  # pylint:disable=bare-except
            log.exception("Error publishing %s website %s", version, name)
            result = False
    return result
Beispiel #2
0
def test_publish_website(  # pylint:disable=redefined-outer-name,too-many-arguments
    settings,
    mocker,
    mock_api_funcs,
    prepublish,
    prepublish_actions,
    has_api,
    version,
    status,
    trigger,
):
    """Verify that the appropriate backend calls are made by the publish_website function"""
    settings.PREPUBLISH_ACTIONS = prepublish_actions
    website = WebsiteFactory.create()
    setattr(website, f"{version}_publish_status", status)
    if status:
        setattr(website, f"{version}_publish_status_updated_on", now_in_utc())
    website.save()
    build_id = 123456
    pipeline_api = mocker.Mock() if has_api else None
    backend = mock_api_funcs.mock_get_backend.return_value
    pipeline = mock_api_funcs.mock_get_pipeline.return_value
    pipeline.trigger_pipeline_build.return_value = build_id
    api.publish_website(
        website.name,
        version,
        pipeline_api=pipeline_api,
        prepublish=prepublish,
        trigger_pipeline=trigger,
    )
    mock_api_funcs.mock_get_backend.assert_called_once_with(website)
    backend.sync_all_content_to_backend.assert_called_once()
    if version == VERSION_DRAFT:
        backend.merge_backend_draft.assert_called_once()
    else:
        backend.merge_backend_live.assert_called_once()
    website.refresh_from_db()
    if trigger:
        mock_api_funcs.mock_get_pipeline.assert_called_once_with(
            website, api=pipeline_api)
        pipeline.trigger_pipeline_build.assert_called_once_with(version)
        pipeline.unpause_pipeline.assert_called_once_with(version)
        assert getattr(website, f"latest_build_id_{version}") == build_id
    else:
        mock_api_funcs.mock_get_pipeline.assert_not_called()
        pipeline.trigger_pipeline_build.assert_not_called()
        pipeline.unpause_pipeline.assert_not_called()
        assert getattr(website, f"latest_build_id_{version}") is None
    assert getattr(website,
                   f"{version}_publish_status") == PUBLISH_STATUS_NOT_STARTED
    assert getattr(
        website,
        f"has_unpublished_{version}") is (status == PUBLISH_STATUS_NOT_STARTED)
    assert getattr(website, f"{version}_last_published_by") is None
    assert getattr(website, f"{version}_publish_status_updated_on") is not None
    if len(prepublish_actions) > 0 and prepublish:
        mock_api_funcs.mock_import_string.assert_any_call("some.Action")
        mock_api_funcs.mock_import_string.return_value.assert_any_call(
            website, version=version)
Beispiel #3
0
def publish_website_backend_draft(website_name: str):
    """
    Create a new backend preview for the website.
    """
    try:
        api.publish_website(website_name, VERSION_DRAFT)
        return True
    except:  # pylint:disable=bare-except
        log.exception("Error publishing draft site %s", website_name)
        return website_name
Beispiel #4
0
def test_publish_website_error(mock_api_funcs, settings):
    """Verify that the appropriate error handling occurs if publish_website throws an exception"""
    settings.PREPUBLISH_ACTIONS = [["some.Action"]]
    mock_api_funcs.mock_import_string.side_effect = Exception("error")
    website = WebsiteFactory.create()
    with pytest.raises(Exception):
        api.publish_website(website.name, VERSION_LIVE)
    website.refresh_from_db()
    mock_api_funcs.mock_get_backend.assert_not_called()
    assert website.live_publish_status == PUBLISH_STATUS_ERRORED
Beispiel #5
0
def publish_website_backend_live(website_name: str):
    """
    Create a new backend release for the website.
    """
    try:
        api.publish_website(
            website_name,
            VERSION_LIVE,
        )
        return True
    except:  # pylint:disable=bare-except
        log.exception("Error publishing live site %s", website_name)
        return website_name