diff --git a/app/controllers/admin/attachments_controller.rb b/app/controllers/admin/attachments_controller.rb index f0effd8e485..9bb37255688 100644 --- a/app/controllers/admin/attachments_controller.rb +++ b/app/controllers/admin/attachments_controller.rb @@ -17,6 +17,7 @@ def new; end def create if save_attachment + Whitehall.attachment_notifier.publish('create', attachment) redirect_to attachable_attachments_path(attachable), notice: "Attachment '#{attachment.title}' uploaded" else render :new diff --git a/lib/whitehall.rb b/lib/whitehall.rb index f74a75494c5..41f8ee14137 100644 --- a/lib/whitehall.rb +++ b/lib/whitehall.rb @@ -153,6 +153,10 @@ def self.edition_services @edition_actions ||= EditionServiceCoordinator.new end + def self.attachment_notifier + @attachment_notifier ||= ActiveSupport::Notifications::Fanout.new + end + def self.organisations_in_tagging_beta @taggable_organisations ||= YAML.load_file(Rails.root + "config/organisations_in_tagging_beta.yml")["organisations_in_tagging_beta"] diff --git a/test/functional/admin/attachments_controller_test.rb b/test/functional/admin/attachments_controller_test.rb index 3cfed01426b..d5e0e016eec 100644 --- a/test/functional/admin/attachments_controller_test.rb +++ b/test/functional/admin/attachments_controller_test.rb @@ -29,6 +29,7 @@ def valid_external_attachment_params setup do login_as :gds_editor @edition = create(:consultation) + Whitehall.attachment_notifier.stubs(:publish) end def self.supported_attachable_types @@ -93,6 +94,20 @@ def self.supported_attachable_types assert_redirected_to admin_edition_attachments_url(attachable) end + test "POST :create publishes event to attachment notifier on success" do + Whitehall.attachment_notifier.expects(:publish).with('create', is_a(Attachment)) + attachable = create(:edition) + + post :create, params: { edition_id: attachable.id, attachment: valid_file_attachment_params } + end + + test "POST :create does not publish event to attachment notifier on failure" do + Whitehall.attachment_notifier.expects(:publish).never + attachable = create(:edition) + + post :create, params: { edition_id: attachable.id, attachment: {} } + end + view_test 'GET :index shows html attachments' do create(:html_attachment, title: 'An HTML attachment', attachable: @edition)