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 incoming redelivery to multiple #7898

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions app/controllers/admin_incoming_message_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,34 @@ def bulk_destroy
end

def redeliver

message_ids = params[:url_title].split(",").each(&:strip)
message_ids = params[:url_title].split(',').map(&:strip)
previous_request = @incoming_message.info_request
destination_request = nil

if message_ids.empty?
flash[:error] = "You must supply at least one request to redeliver the message to."
flash[:error] =
'You must supply at least one request to redeliver the message to.'

return redirect_to admin_request_url(previous_request)
end

ActiveRecord::Base.transaction do
message_ids.each do |m|
if m.match(/^[0-9]+$/)
destination_request = InfoRequest.find_by_id(m.to_i)
else
destination_request = InfoRequest.find_by_url_title!(m)
end
destination_request =
if m.match(/^[0-9]+$/)
InfoRequest.find_by_id(m.to_i)
else
InfoRequest.find_by_url_title!(m)
end

if destination_request.nil?
flash[:error] = "Failed to find destination request '" + m + "'"
flash[:error] = "Failed to find destination request '#{m}'"
return redirect_to admin_request_url(previous_request)
end

raw_email_data = @incoming_message.raw_email.data
mail = MailHandler.mail_from_raw_email(raw_email_data)

destination_request.
receive(mail,
raw_email_data,
Expand All @@ -114,12 +118,15 @@ def redeliver
deleted_incoming_message_id: @incoming_message.id
)

flash[:notice] = "Message has been moved to request(s). Showing the last one:"
flash[:notice] =
'Message has been moved to request(s). Showing the last one:'
end

# expire cached files
previous_request.expire
@incoming_message.destroy
end

redirect_to admin_request_url(destination_request)
end

Expand Down
24 changes: 24 additions & 0 deletions spec/controllers/admin_incoming_message_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
FactoryBot.create(:incoming_message, info_request: previous_info_request)
end
let(:destination_info_request) { FactoryBot.create(:info_request) }
let(:destination_info_request_2) { FactoryBot.create(:info_request) }

it 'expires the file cache for the previous request' do
allow(IncomingMessage).to receive(:find).and_return(incoming_message)
Expand Down Expand Up @@ -107,6 +108,29 @@
expect(response).to redirect_to admin_request_url(incoming_message.info_request)
end

context 'when redelivering to multiple requests' do
before do
destination_params =
[destination_info_request, destination_info_request_2].
map(&:id).
join(', ')

post :redeliver,
params: { id: incoming_message.id,
url_title: destination_params }
end

it 'renders a message' do
msg = 'Message has been moved to request(s). Showing the last one:'
expect(flash[:notice]).to eq(msg)
end

it 'redirects to the last given destination request' do
expect(response).
to redirect_to admin_request_path(destination_info_request_2)
end
end

context 'if the request is embargoed', feature: :alaveteli_pro do
before do
incoming_message.info_request.create_embargo
Expand Down
Loading