Files
grecia/spec/controllers/admin/emails_download_controller_spec.rb
Javi Martín 90ae03795d Send an empty CSV file for invalid user segments
We were getting an exception in this case, which was OK I guess since
this shouldn't happen if the application is used in a normal way, but we
can simplify the code a little bit if we make the `recipients` code
return an empty list of users.

Note that the behavior of the `AdminNotification#list_of_recipients` and
`Newsletter#list_of_recipient_emails` methods is now slightly different;
previously they returned `nil` when given an invalid segment recipient,
while now they return an empty array. I haven't found a place where this
change is relevant. For example, in both of these models, the `deliver`
method used to raise an exception when given an invalid segment while
now it doesn't, but we always check the user segment is valid before
calling the `deliver` method anyway, so it doesn't really affect the
application.
2025-04-02 13:21:45 +02:00

27 lines
751 B
Ruby

require "rails_helper"
describe Admin::EmailsDownloadController do
before do
admin = create(:administrator, user: create(:user, email: "admin@consul.dev"))
sign_in(admin.user)
end
describe "GET generate_csv" do
it "sends a list of emails in a comma-separated format" do
create(:user, email: "user@consul.dev")
get :generate_csv, params: { users_segment: "all_users" }
expect(response).to be_successful
expect(response.body).to eq "admin@consul.dev,user@consul.dev"
end
it "sends an empty file with an invalid users_segment" do
get :generate_csv, params: { users_segment: "invalid_segment" }
expect(response).to be_successful
expect(response.body).to be_empty
end
end
end