diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 907b0fddc..cb600b004 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -29,13 +29,24 @@ class OrganisationsController < ApplicationController end def users - @pagy, @users = pagy(filtered_users(@organisation.users.sorted_by_organisation_and_role, search_term)) - @searched = search_term.presence - @total_count = @organisation.users.size - if current_user.support? - render "users", layout: "application" - else - render "users/index" + organisation_users = @organisation.users.sorted_by_organisation_and_role + unpaginated_filtered_users = filtered_collection(organisation_users, search_term) + + respond_to do |format| + format.html do + @pagy, @users = pagy(unpaginated_filtered_users) + @searched = search_term.presence + @total_count = @organisation.users.size + + if current_user.support? + render "users", layout: "application" + else + render "users/index" + end + end + format.csv do + send_data unpaginated_filtered_users.to_csv, filename: "users-#{@organisation.name}-#{Time.zone.now}.csv" + end end end @@ -83,11 +94,18 @@ class OrganisationsController < ApplicationController organisation_logs = CaseLog.all.where(owning_organisation_id: @organisation.id) unpaginated_filtered_logs = filtered_case_logs(filtered_collection(organisation_logs, search_term)) - @pagy, @case_logs = pagy(unpaginated_filtered_logs) - @searched = search_term.presence - @total_count = organisation_logs.size - - render "logs", layout: "application" + respond_to do |format| + format.html do + @pagy, @case_logs = pagy(unpaginated_filtered_logs) + @searched = search_term.presence + @total_count = organisation_logs.size + render "logs", layout: "application" + end + + format.csv do + send_data unpaginated_filtered_logs.to_csv, filename: "logs-#{@organisation.name}-#{Time.zone.now}.csv" + end + end else redirect_to(case_logs_path) end diff --git a/app/views/case_logs/_log_list.html.erb b/app/views/case_logs/_log_list.html.erb index c08bfa067..43a0bc701 100644 --- a/app/views/case_logs/_log_list.html.erb +++ b/app/views/case_logs/_log_list.html.erb @@ -2,7 +2,7 @@ <%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular], id: title.dasherize) do |caption| %> <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "logs", path: request.path)) %> - <%= govuk_link_to "Download (CSV)", "/logs.csv", type: "text/csv" %> + <%= govuk_link_to "Download (CSV)", "#{request.path}.csv", type: "text/csv" %> <% end %> <%= table.head do |head| %> <%= head.row do |row| %> diff --git a/app/views/users/_user_list.html.erb b/app/views/users/_user_list.html.erb index 7876d77dc..786e3c571 100644 --- a/app/views/users/_user_list.html.erb +++ b/app/views/users/_user_list.html.erb @@ -4,7 +4,7 @@ <%= render(SearchResultCaptionComponent.new(searched:, count: pagy.count, item_label:, total_count:, item: "users", path: request.path)) %> <% if current_user.support? %> <% query = searched.present? ? "?search=#{searched}" : nil %> - <%= govuk_link_to "Download (CSV)", "/users.csv#{query}", type: "text/csv" %> + <%= govuk_link_to "Download (CSV)", "#{request.path}.csv#{query}", type: "text/csv" %> <% end %> <% end %> <%= table.head do |head| %> diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 3d388ee0f..e17f140f5 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -1020,4 +1020,65 @@ RSpec.describe OrganisationsController, type: :request do end end end + + context "when the user is a support user" do + let(:user) { FactoryBot.create(:user, :support) } + + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + end + + context "when they view the logs tab" do + before do + get "/organisations/#{organisation.id}/logs" + end + + it "has a CSV download button with the correct path" do + expect(page).to have_link("Download (CSV)", href: "/organisations/#{organisation.id}/logs.csv") + end + + context "when you download the CSV" do + let(:headers) { { "Accept" => "text/csv" } } + let(:other_organisation) { FactoryBot.create(:organisation) } + + before do + FactoryBot.create_list(:case_log, 3, owning_organisation: organisation) + FactoryBot.create_list(:case_log, 2, owning_organisation: other_organisation) + end + + it "only includes logs from that organisation" do + get "/organisations/#{organisation.id}/logs", headers:, params: {} + csv = CSV.parse(response.body) + expect(csv.count).to eq(4) + end + end + end + + context "when they view the users tab" do + before do + get "/organisations/#{organisation.id}/users" + end + + it "has a CSV download button with the correct path" do + expect(page).to have_link("Download (CSV)", href: "/organisations/#{organisation.id}/users.csv") + end + + context "when you download the CSV" do + let(:headers) { { "Accept" => "text/csv" } } + let(:other_organisation) { FactoryBot.create(:organisation) } + + before do + FactoryBot.create_list(:user, 3, organisation:) + FactoryBot.create_list(:user, 2, organisation: other_organisation) + end + + it "only includes users from that organisation" do + get "/organisations/#{other_organisation.id}/users", headers:, params: {} + csv = CSV.parse(response.body) + expect(csv.count).to eq(3) + end + end + end + end end