Browse Source

CSV download only includes users in search result

pull/619/head
baarkerlounger 3 years ago
parent
commit
96a796a51a
  1. 5
      app/controllers/users_controller.rb
  2. 3
      app/views/users/_user_list.html.erb
  3. 47
      spec/requests/users_controller_spec.rb

5
app/controllers/users_controller.rb

@ -11,7 +11,8 @@ class UsersController < ApplicationController
redirect_to users_organisation_path(current_user.organisation) unless current_user.support?
all_users = User.all
@pagy, @users = pagy(filtered_users(all_users, search_term))
filtered_users = filtered_users(all_users, search_term)
@pagy, @users = pagy(filtered_users)
@searched = search_term.presence
@total_count = all_users.size
@ -19,7 +20,7 @@ class UsersController < ApplicationController
format.html
format.csv do
if current_user.support?
send_data User.all.where(active: true).includes(:organisation).to_csv, filename: "users-#{Time.zone.now}.csv"
send_data filtered_users.to_csv, filename: "users-#{Time.zone.now}.csv"
else
head :unauthorized
end

3
app/views/users/_user_list.html.erb

@ -8,7 +8,8 @@
<% end %>
</span>
<% if current_user.support? %>
<%= govuk_link_to "Download (CSV)", "/users.csv", type: "text/csv" %>
<% query = searched.present? ? "?search=#{searched}" : nil %>
<%= govuk_link_to "Download (CSV)", "/users.csv#{query}", type: "text/csv" %>
<% end %>
<% end %>
<%= table.head do |head| %>

47
spec/requests/users_controller_spec.rb

@ -842,7 +842,11 @@ RSpec.describe UsersController, type: :request do
end
it "updates the table caption" do
expect(page).to have_content("1 user found matching ‘Danny’ of 4 total users.")
expect(page).to have_content("1 user found matching ‘#{search_param}’ of 4 total users.")
end
it "includes the search term in the CSV download link" do
expect(page).to have_link("Download (CSV)", href: "/users.csv?search=#{search_param}")
end
end
@ -895,23 +899,40 @@ RSpec.describe UsersController, type: :request do
before do
FactoryBot.create_list(:user, 25)
sign_in user
get "/users", headers:, params: {}
end
it "downloads a CSV file with headers" do
csv = CSV.parse(response.body)
expect(csv.first.second).to eq("email")
expect(csv.second.first).to eq(user.id.to_s)
end
context "when there is no search param" do
before do
get "/users", headers:, params: {}
end
it "downloads a CSV file with headers" do
csv = CSV.parse(response.body)
expect(csv.first.second).to eq("email")
expect(csv.second.first).to eq(user.id.to_s)
end
it "downloads all users" do
csv = CSV.parse(response.body)
expect(csv.count).to eq(27)
end
it "downloads all users" do
csv = CSV.parse(response.body)
expect(csv.count).to eq(27)
it "downloads organisation names rather than ids" do
csv = CSV.parse(response.body)
expect(csv.second[3]).to eq(user.organisation.name.to_s)
end
end
it "downloads organisation names rather than ids" do
csv = CSV.parse(response.body)
expect(csv.second[3]).to eq(user.organisation.name.to_s)
context "when there is a search param" do
before do
FactoryBot.create(:user, name: "Unusual name")
get "/users?search=unusual", headers:, params: {}
end
it "downloads only the matching records" do
csv = CSV.parse(response.body)
expect(csv.count).to eq(2)
end
end
end

Loading…
Cancel
Save