Skip to content

Commit 07cba7e

Browse files
committed
Allow publishers to delete a document preview link
Add a destroy_bypass_id action and DELETE route that removes a draft's auth bypass token via EditionAuthBypassRevoker. Like update_bypass_id it loads the edition and requires :update permission on it.
1 parent 6dc40d2 commit 07cba7e

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

app/controllers/admin/editions_controller.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Admin::EditionsController < Admin::BaseController
66
before_action :clean_edition_parameters, only: %i[create update]
77
before_action :clear_scheduled_publication_if_not_activated, only: %i[create update]
88
before_action :clear_response_form_file_cache, only: %i[create update]
9-
before_action :find_edition, only: %i[show edit update revise diff confirm_destroy destroy update_bypass_id features]
9+
before_action :find_edition, only: %i[show edit update revise diff confirm_destroy destroy update_bypass_id destroy_bypass_id features]
1010
before_action :prevent_modification_of_unmodifiable_edition, only: %i[update]
1111
before_action :delete_absent_edition_organisations, only: %i[create update]
1212
before_action :build_national_exclusion_params, only: %i[create update]
@@ -33,7 +33,7 @@ def enforce_permissions!
3333
enforce_permission!(:create, edition_class || Edition)
3434
when "create"
3535
enforce_permission!(:create, @edition) if @edition.persisted?
36-
when "edit", "update", "revise", "diff", "update_bypass_id", "features"
36+
when "edit", "update", "revise", "diff", "update_bypass_id", "destroy_bypass_id", "features"
3737
enforce_permission!(:update, @edition)
3838
when "destroy", "confirm_destroy"
3939
enforce_permission!(:delete, @edition)
@@ -196,6 +196,12 @@ def update_bypass_id
196196
redirect_to admin_edition_path(@edition), notice: "New document preview link generated"
197197
end
198198

199+
def destroy_bypass_id
200+
EditionAuthBypassRevoker.new(edition: @edition, current_user:, updater:).call
201+
202+
redirect_to admin_edition_path(@edition), notice: "Document preview link deleted"
203+
end
204+
199205
private
200206

201207
def display_filter_error_message

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })
239239
get :audit_trail, to: "edition_audit_trail#index"
240240
get :document_history, to: "edition_document_history#index"
241241
patch :update_bypass_id
242+
delete :destroy_bypass_id
242243
get :confirm_destroy
243244
get :edit_access_limited, to: "edition_access_limited#edit"
244245
patch :update_access_limited, to: "edition_access_limited#update"

test/functional/admin/editions_controller_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,29 @@ class Admin::EditionsControllerTest < ActionController::TestCase
369369
assert_not flash["html_safe"]
370370
end
371371

372+
test "update_bypass_id generates a new preview token and redirects with a notice" do
373+
edition = create(:draft_publication)
374+
previous_auth_bypass_id = edition.auth_bypass_id
375+
376+
patch :update_bypass_id, params: { id: edition }
377+
378+
assert_not_nil edition.reload.auth_bypass_id
379+
assert_not_equal previous_auth_bypass_id, edition.auth_bypass_id
380+
assert_redirected_to admin_publication_path(edition)
381+
assert_equal "New document preview link generated", flash[:notice]
382+
end
383+
384+
test "destroy_bypass_id deletes the preview token and redirects with a notice" do
385+
edition = create(:draft_publication)
386+
assert_not_nil edition.auth_bypass_id
387+
388+
delete :destroy_bypass_id, params: { id: edition }
389+
390+
assert_nil edition.reload.auth_bypass_id
391+
assert_redirected_to admin_publication_path(edition)
392+
assert_equal "Document preview link deleted", flash[:notice]
393+
end
394+
372395
private
373396

374397
def stub_edition_filter(attributes = {})

0 commit comments

Comments
 (0)