Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discarded draft attribution in timeline entries #1755

Merged
merged 4 commits into from Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions app/services/delete_draft_edition_service.rb
@@ -1,32 +1,32 @@
# frozen_string_literal: true

class DeleteDraftEditionService < ApplicationService
def initialize(document, user)
@document = document
def initialize(edition, user)
@edition = edition
@user = user
end

def call
edition = document.current_edition

raise "Trying to delete a document without a current edition" unless edition
raise "Trying to delete a live document" if edition.live?
raise "Only current editions can be deleted" unless edition.current?
raise "Trying to delete a live edition" if edition.live?

begin
delete_assets(edition.assets)
discard_draft(edition)
rescue GdsApi::BaseError
document.current_edition.update!(revision_synced: false)
edition.update!(revision_synced: false)
raise
end

reset_live_edition if document.live_edition
discard_path_reservations(edition) if edition.first?
document.reload_current_edition
end

private

attr_reader :document, :user
attr_reader :edition, :user
delegate :document, to: :edition

def discard_path_reservations(edition)
paths = edition.revisions.map(&:base_path).uniq.compact
Expand All @@ -41,18 +41,19 @@ def discard_path_reservations(edition)

def reset_live_edition
document.live_edition.update!(current: true)
document.reload_live_edition
end

def discard_draft(edition)
begin
GdsApi.publishing_api.discard_draft(document.content_id)
GdsApi.publishing_api.discard_draft(edition.content_id)
rescue GdsApi::HTTPNotFound
Rails.logger.warn("No draft to discard for content id #{document.content_id}")
Rails.logger.warn("No draft to discard for content id #{edition.content_id}")
rescue GdsApi::HTTPUnprocessableEntity => e
no_draft_message = "There is not a draft edition of this document to discard"

if e.error_details.respond_to?(:dig) && e.error_details.dig("error", "message") == no_draft_message
Rails.logger.warn("No draft to discard for content id #{document.content_id}")
Rails.logger.warn("No draft to discard for content id #{edition.content_id}")
else
raise
end
Expand Down
55 changes: 55 additions & 0 deletions spec/interactors/editions/destroy_interactor_spec.rb
@@ -0,0 +1,55 @@
# frozen_string_literal: true

RSpec.describe Editions::DestroyInteractor do
describe ".call" do
let(:edition) { create(:edition) }
let(:user) { create :user }

let(:params) do
ActionController::Parameters.new(document: edition.document.to_param)
end

before do
stub_publishing_api_unreserve_path(edition.base_path)
stub_publishing_api_discard_draft(edition.content_id)
end

it "discards an edition" do
result = Editions::DestroyInteractor.call(params: params, user: user)
expect(result).to be_success
expect(result.edition).to be_discarded
end

it "delegates to the DeleteDraftEditionService" do
expect(DeleteDraftEditionService)
.to receive(:call)
.with(edition.document, user)
Editions::DestroyInteractor.call(params: params, user: user)
end

it "creates a timeline entry" do
expect { Editions::DestroyInteractor.call(params: params, user: user) }
.to change { TimelineEntry.where(entry_type: :draft_discarded).count }
.by(1)
end

context "when the Publishing API is down" do
before { stub_publishing_api_isnt_available }

it "fails with an api_error flag" do
result = Editions::DestroyInteractor.call(params: params, user: user)
expect(result).to be_failure
expect(result.api_error).to be(true)
end
end

context "when the edition isn't editable" do
let(:edition) { create(:edition, :published) }

it "raises a state error" do
expect { Editions::DestroyInteractor.call(params: params, user: user) }
.to raise_error(EditionAssertions::StateError)
end
end
end
end
46 changes: 23 additions & 23 deletions spec/services/delete_draft_edition_service_spec.rb
Expand Up @@ -4,25 +4,25 @@
let(:user) { create :user }

describe ".call" do
it "raises an exception if there is not a current edition" do
document = create :document
it "raises an exception if the edition is not current" do
edition = create :edition, current: false

expect { DeleteDraftEditionService.call(document, user) }
.to raise_error "Trying to delete a document without a current edition"
expect { DeleteDraftEditionService.call(edition, user) }
.to raise_error "Only current editions can be deleted"
end

it "raises an exception if the current edition is live" do
document = create :document, :with_live_edition

expect { DeleteDraftEditionService.call(document, user) }
.to raise_error "Trying to delete a live document"
expect { DeleteDraftEditionService.call(document.current_edition, user) }
.to raise_error "Trying to delete a live edition"
end

it "attempts to delete the document preview" do
document = create :document, :with_current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
request = stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document, user)
DeleteDraftEditionService.call(document.current_edition, user)
expect(request).to have_been_requested
end

Expand All @@ -37,7 +37,7 @@
stub_publishing_api_unreserve_path(edition.base_path)
delete_request = stub_asset_manager_deletes_any_asset

DeleteDraftEditionService.call(edition.document, user)
DeleteDraftEditionService.call(edition, user)

expect(delete_request).to have_been_requested.at_least_once
expect(image_revision.reload.assets.map(&:state).uniq).to eq(%w[absent])
Expand All @@ -61,7 +61,7 @@
PreviewDraftEditionService::Payload::PUBLISHING_APP,
)

DeleteDraftEditionService.call(edition.document, user)
DeleteDraftEditionService.call(edition, user)

expect(unreserve_request1).to have_been_requested
expect(unreserve_request2).to have_been_requested
Expand All @@ -70,15 +70,15 @@
it "does not delete path reservations for published documents" do
document = create :document, :with_current_and_live_editions
stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document, user)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to eq document.live_edition
end

it "sets the current edition to nil if there is no live edition" do
document = create :document, :with_current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document, user)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to be_nil
end

Expand All @@ -87,7 +87,7 @@
live_edition = document.live_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document, user)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to eq(live_edition)
end

Expand All @@ -96,15 +96,15 @@
edition = document.current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
stub_publishing_api_discard_draft(document.content_id)
DeleteDraftEditionService.call(document, user)
DeleteDraftEditionService.call(document.current_edition, user)
expect(edition.status).to be_discarded
end

it "copes if the document preview does not exist" do
document = create :document, :with_current_edition
stub_publishing_api_unreserve_path(document.current_edition.base_path)
stub_any_publishing_api_call_to_return_not_found
DeleteDraftEditionService.call(document, user)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to be_nil
end

Expand All @@ -120,7 +120,7 @@
stub_any_publishing_api_discard_draft
.to_return(status: 422, body: discard_draft_error.to_json)

DeleteDraftEditionService.call(document, user)
DeleteDraftEditionService.call(document.current_edition, user)
expect(document.reload.current_edition).to be_nil
end

Expand All @@ -136,7 +136,7 @@
stub_any_publishing_api_discard_draft
.to_return(status: 422, body: discard_draft_error.to_json)

expect { DeleteDraftEditionService.call(document, user) }
expect { DeleteDraftEditionService.call(document.current_edition, user) }
.to raise_error(GdsApi::HTTPUnprocessableEntity)
end

Expand All @@ -149,7 +149,7 @@

stub_publishing_api_unreserve_path(edition.base_path)
stub_publishing_api_discard_draft(edition.content_id)
DeleteDraftEditionService.call(edition.document, user)
DeleteDraftEditionService.call(edition, user)

expect(edition.reload.status).to be_discarded
expect(image_revision.reload.assets.map(&:state).uniq).to eq(%w[absent])
Expand All @@ -161,7 +161,7 @@

stub_publishing_api_unreserve_path_not_found(edition.base_path)
stub_publishing_api_discard_draft(edition.content_id)
DeleteDraftEditionService.call(edition.document, user)
DeleteDraftEditionService.call(edition, user)

expect(edition.reload.status).to be_discarded
end
Expand All @@ -170,7 +170,7 @@
edition = create :edition, base_path: nil

stub_publishing_api_discard_draft(edition.content_id)
DeleteDraftEditionService.call(edition.document, user)
DeleteDraftEditionService.call(edition, user)

expect(edition.reload.status).to be_discarded
end
Expand All @@ -188,7 +188,7 @@

stub_publishing_api_unreserve_path(edition.base_path)
stub_publishing_api_discard_draft(edition.content_id)
DeleteDraftEditionService.call(edition.document, user)
DeleteDraftEditionService.call(edition, user)

expect(edition.reload.status).to be_discarded
expect(image_revision.reload.assets.map(&:state).uniq).to eq(%w[absent])
Expand All @@ -201,7 +201,7 @@
stub_publishing_api_discard_draft(edition.content_id)
stub_publishing_api_unreserve_path_invalid(edition.base_path)

expect { DeleteDraftEditionService.call(edition.document, user) }
expect { DeleteDraftEditionService.call(edition, user) }
.to raise_error(GdsApi::BaseError)

expect(edition.reload.revision_synced?).to be true
Expand All @@ -211,7 +211,7 @@
edition = create :edition
stub_publishing_api_isnt_available

expect { DeleteDraftEditionService.call(edition.document, user) }
expect { DeleteDraftEditionService.call(edition, user) }
.to raise_error(GdsApi::BaseError)

expect(edition.reload.revision_synced?).to be false
Expand All @@ -226,7 +226,7 @@

stub_asset_manager_isnt_available

expect { DeleteDraftEditionService.call(edition.document, user) }
expect { DeleteDraftEditionService.call(edition, user) }
.to raise_error(GdsApi::BaseError)

expect(edition.reload.revision_synced?).to be false
Expand Down