Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added integration test for attachment deletion
This demonstrates the current behaviour when an attachment is deleted from a draft edition which has never been published. * `Admin::AttachmentsController#destroy` calls `Attachment#destroy`. * `Attachment#destroy` overrides the standard `ActiveRecord::Persistence#destroy` method so that it only marks the attachment as deleted, but it still runs `:destroy` callbacks [1]. * `FileAttachment`, a subclass of `Attachment` has an `after_destroy` callback which will destroy the associated `AttachmentData` only if no other non-deleted attachments are using it [2]. * In this case, because there's only a single edition of the document, there is only one instance of `Attachment` for the relevant `AttachmentData` and thus the latter is destroyed. * This in turn fires an `after_commit, on: :destroy` callback which is added by CarrierWave which eventually results in a call to `Whitehall::AssetManagerStorage::File#delete` which calls `AssetManagerDeleteAssetWorker.perform_async` to queue a job to delete the corresponding asset in Asset Manager. * The fact that the `AttachmentData` has actually been deleted results in an `ActiveRecord::RecordNotFound` exception being raised in `AttachmentsController#attachment_data`. In production this will result in a `404 Not Found` response [3]. * In the case where an `AttachmentData` is associated with more than one edition, the deletion of an `Attachment` will only result in the `Attachment` being marked as deleted; the `AttachmentData` will not be destroyed`. However, requests to the attachment URL will still result in a `404 Not Found`, because `AttachmentVisibility#edition_ids` [4] does not consider deleted attachments and thus `AttachmentVisibility#visible?` will always return `false` and this line [5] in `AttachmentsController#fail` will be invoked. [1]: https://github.com/alphagov/whitehall/blob/32bb13cf3ae3ae9ca71fa14bd4c620c631660419/app/models/attachment.rb#L130-L132 [2]: https://github.com/alphagov/whitehall/blob/32bb13cf3ae3ae9ca71fa14bd4c620c631660419/app/models/file_attachment.rb#L33-L38 [3]: http://guides.rubyonrails.org/v5.0.6/action_controller_overview.html#rescue [4]: https://github.com/alphagov/whitehall/blob/32bb13cf3ae3ae9ca71fa14bd4c620c631660419/app/models/attachment_visibility.rb#L116-L118 [5]: https://github.com/alphagov/whitehall/blob/32bb13cf3ae3ae9ca71fa14bd4c620c631660419/app/controllers/attachments_controller.rb#L31
- Loading branch information