diff --git a/app/views/links/index.html.erb b/app/views/links/index.html.erb index 3840ad8cb..dd24ae1e6 100644 --- a/app/views/links/index.html.erb +++ b/app/views/links/index.html.erb @@ -1,4 +1,5 @@ <% content_for :page_title, "Broken Links" %> +<%= render partial: "shared/flash" %> <%= render "govuk_publishing_components/components/heading", { text: "Broken Links (showing top 200 of #{@total_broken_links})", diff --git a/app/views/local_authorities/show.html.erb b/app/views/local_authorities/show.html.erb index 373bf5b57..bce773743 100644 --- a/app/views/local_authorities/show.html.erb +++ b/app/views/local_authorities/show.html.erb @@ -1,4 +1,5 @@ <% content_for :page_title, @authority.name %> +<%= render partial: "shared/flash" %> <%= render "govuk_publishing_components/components/heading", { text: @authority.name, diff --git a/spec/controllers/links_controller_spec.rb b/spec/controllers/links_controller_spec.rb deleted file mode 100644 index b57f6c43e..000000000 --- a/spec/controllers/links_controller_spec.rb +++ /dev/null @@ -1,72 +0,0 @@ -RSpec.describe LinksController, type: :controller do - before do - login_as_gds_editor - @local_authority = create(:local_authority) - @service = create(:service) - @interaction = create(:interaction) - @service_interaction = create(:service_interaction, service: @service, interaction: @interaction) - end - - describe "GET destroy" do - it "deletes link and redirects" do - get :destroy, params: { local_authority_slug: @local_authority.slug, service_slug: @service.slug, interaction_slug: @interaction.slug } - expect(response).to have_http_status(302) - expect(flash[:success]).to match("Link has been deleted") - end - end - - describe "GET edit" do - it "retrieves HTTP success" do - get :edit, params: { local_authority_slug: @local_authority.slug, service_slug: @service.slug, interaction_slug: @interaction.slug } - expect(response).to have_http_status(200) - end - - it "handles a URL passed in via flash" do - flash_hash = ActionDispatch::Flash::FlashHash.new - flash_hash[:link_url] = "https://www.example.com" - session["flash"] = flash_hash.to_session_value - - get :edit, params: { local_authority_slug: @local_authority.slug, service_slug: @service.slug, interaction_slug: @interaction.slug } - expect(response).to have_http_status(200) - end - end - - describe "POST edit" do - it "updates valid links" do - post :update, params: { local_authority_slug: @local_authority.slug, service_slug: @service.slug, interaction_slug: @interaction.slug, url: "http://www.example.com/new" } - expect(response).to have_http_status(302) - expect(Link.last.url).to eq("http://www.example.com/new") - expect(flash[:danger]).to be nil - end - - it "catches invalid links" do - post :update, params: { local_authority_slug: @local_authority.slug, service_slug: @service.slug, interaction_slug: @interaction.slug, url: "ftp://who" } - expect(response).to have_http_status(302) - expect(flash[:danger]).not_to be nil - end - end - - describe "GET homepage_links_status_csv" do - it "retrieves HTTP success" do - get :homepage_links_status_csv - expect(response).to have_http_status(200) - expect(response.headers["Content-Type"]).to eq("text/csv") - end - end - - describe "GET links_status_csv" do - it "retrieves HTTP success" do - get :links_status_csv - expect(response).to have_http_status(200) - expect(response.headers["Content-Type"]).to eq("text/csv") - end - end - - describe "GET bad_links_url_and_status_csv" do - it "retrieves HTTP success" do - get :bad_links_url_and_status_csv - expect(response).to have_http_status(200) - expect(response.headers["Content-Type"]).to eq("text/csv") - end - end -end diff --git a/spec/controllers/local_authorities_controller_spec.rb b/spec/controllers/local_authorities_controller_spec.rb deleted file mode 100644 index b60d56125..000000000 --- a/spec/controllers/local_authorities_controller_spec.rb +++ /dev/null @@ -1,137 +0,0 @@ -RSpec.describe LocalAuthoritiesController, type: :controller do - describe "GET #index" do - context "when there is sufficient data" do - it "returns http succcess" do - login_as_gds_editor - create(:local_authority) - get :index - expect(response).to have_http_status(200) - end - end - end - - describe "GET #show" do - before do - @local_authority = create(:local_authority, name: "Angus") - @service = create(:service, label: "Service 1", lgsl_code: 1) - end - - it "returns http success" do - login_as_gds_editor - get :show, params: { local_authority_slug: @local_authority.slug, service_slug: @service.slug } - expect(response).to have_http_status(200) - end - end - - describe "GET #edit_url" do - before do - @local_authority = create(:local_authority, name: "Angus") - @service = create(:service, label: "Service 1", lgsl_code: 1) - end - - it "returns http success" do - login_as_gds_editor - get :edit_url, params: { local_authority_slug: @local_authority.slug } - expect(response).to have_http_status(200) - end - end - - describe "POST #update" do - before do - @local_authority = create(:local_authority, name: "Angus") - @service = create(:service, label: "Service 1", lgsl_code: 1) - end - - it "returns http redirect" do - login_as_gds_editor - post :update, params: { local_authority_slug: @local_authority.slug, homepage_url: "http://www.example.com/new-homepage" } - expect(response).to have_http_status(302) - @local_authority.reload - expect(@local_authority.homepage_url).to eq("http://www.example.com/new-homepage") - end - end - - describe "GET bad_homepage_url_and_status_csv" do - it "retrieves HTTP success" do - login_as_gds_editor - get :bad_homepage_url_and_status_csv - expect(response).to have_http_status(200) - expect(response.headers["Content-Type"]).to eq("text/csv") - end - end - - describe "GET download_links_csv" do - before do - @local_authority = create(:local_authority) - end - - it "retrieves HTTP success" do - login_as_gds_editor - get( - :download_links_csv, - params: { - local_authority_slug: @local_authority.slug, - links_status_checkbox: %w[ok broken caution missing pending], - }, - ) - expect(response).to have_http_status(200) - expect(response.headers["Content-Type"]).to eq("text/csv") - end - end - - describe "POST upload_links_csv" do - context "with a valid CSV" do - before { @local_authority = create(:local_authority, gss: "S1") } - let(:path) { Rails.root.join("spec/lib/local-links-manager/import/fixtures/imported_links.csv") } - let(:csv) { Rack::Test::UploadedFile.new(path, "text/csv", true) } - let(:url_regex) { /http:\/\/.+\/local_authorities\/#{@local_authority.slug}/ } - - it "retrieves HTTP found" do - login_as_gds_editor - post(:upload_links_csv, params: { local_authority_slug: @local_authority.slug, csv: }) - - expect(response.status).to eq(302) - expect(response.location).to match(url_regex) - expect(response.headers["Content-Type"]).to eq("text/html; charset=utf-8") - end - end - - context "with errors in the CSV" do - before do - login_as_gds_editor - interaction = create(:interaction, lgil_code: 1) - 6.times do |i| - service = create(:service, lgsl_code: i + 1) - create(:service_interaction, service:, interaction:) - end - end - - let(:local_authority) { create(:local_authority, gss: "S1") } - let(:fixture_path) { "spec/lib/local-links-manager/import/fixtures/" } - - it "shows the all error message if all lines are broken" do - csv = Rack::Test::UploadedFile.new(Rails.root.join(fixture_path, "imported_links_all_errors.csv"), "text/csv", true) - post(:upload_links_csv, params: { local_authority_slug: local_authority.slug, csv: }) - expect(flash[:danger]).to eq("Errors on all lines. Ensure a New URL column exists, with all rows either blank or a valid URL") - end - - it "shows the many error message if many lines are broken" do - csv = Rack::Test::UploadedFile.new(Rails.root.join(fixture_path, "imported_links_many_errors.csv"), "text/csv", true) - post(:upload_links_csv, params: { local_authority_slug: local_authority.slug, csv: }) - expect(flash[:danger].first).to eq("74 Errors detected. Please ensure a valid entry in the New URL column for lines (showing first 50):") - end - - it "shows the few error message if few lines are broken" do - csv = Rack::Test::UploadedFile.new(Rails.root.join(fixture_path, "imported_links_few_errors.csv"), "text/csv", true) - post(:upload_links_csv, params: { local_authority_slug: local_authority.slug, csv: }) - expect(flash[:danger].first).to eq("2 Errors detected. Please ensure a valid entry in the New URL column for lines:") - end - - it "shows the nothing to import info if it didn't import anything" do - csv = Rack::Test::UploadedFile.new(Rails.root.join(fixture_path, "imported_links_nothing_to_import.csv"), "text/csv", true) - post(:upload_links_csv, params: { local_authority_slug: local_authority.slug, csv: }) - expect(flash[:info]).to eq("No records updated. (If you were expecting updates, check the format of the uploaded file)") - end - end - end -end diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb deleted file mode 100644 index 968d11f3a..000000000 --- a/spec/controllers/services_controller_spec.rb +++ /dev/null @@ -1,41 +0,0 @@ -RSpec.describe ServicesController, type: :controller do - before { login_as_gds_editor } - - describe "GET #index" do - it "returns http succcess" do - create(:service) - get :index - expect(response).to have_http_status(200) - end - end - - describe "GET #show" do - it "returns http success" do - service = create(:service) - get :show, params: { service_slug: service.slug } - expect(response).to have_http_status(200) - end - end - - describe "GET #download_links_form" do - it "returns a success response" do - service = create(:service) - get :download_links_form, params: { service_slug: service.slug } - expect(response).to be_successful - end - end - - describe "POST #download_links_csv" do - let(:service) { create(:service) } - let(:exported_data) { "some_data" } - - before do - allow_any_instance_of(LocalLinksManager::Export::ServiceLinksExporter).to receive(:export_links).and_return(exported_data) - end - - it "returns a success response" do - post :download_links_csv, params: { service_slug: service.slug } - expect(response).to be_successful - end - end -end diff --git a/spec/requests/broken_links_page_spec.rb b/spec/requests/broken_links_page_spec.rb index f648f1427..5aea544be 100644 --- a/spec/requests/broken_links_page_spec.rb +++ b/spec/requests/broken_links_page_spec.rb @@ -1,3 +1,51 @@ RSpec.describe "Broken links page" do it_behaves_like "redirects non-GDS Editors to services page", "/" end + +describe "Broken links page" do + before do + login_as_gds_editor + @local_authority = create(:local_authority, name: "North Midlands") + @service = create(:service, label: "Aardvark Wardens") + @interaction = create(:interaction, label: "Reporting") + @service_interaction = create(:service_interaction, service: @service, interaction: @interaction) + end + + context "GET edit" do + it "GET edit handles URL passed in via flash" do + get "/local_authorities/north-midlands/services/aardvark-wardens/reporting/edit" + expect(response).to have_http_status(:ok) + + flash_hash = ActionDispatch::Flash::FlashHash.new + flash_hash[:link_url] = "https://www.example.com" + session["flash"] = flash_hash.to_session_value + + get "/local_authorities/north-midlands/services/aardvark-wardens/reporting/edit" + expect(response).to have_http_status(:ok) + end + end + + context "GET homepage_links_status_csv" do + it "returns a 200 response" do + get "/check_homepage_links_status.csv" + expect(response).to have_http_status(:ok) + expect(response.headers["Content-Type"]).to eq("text/csv") + end + end + + context "GET links_status_csv" do + it "returns a 200 response" do + get "/check_links_status.csv" + expect(response).to have_http_status(:ok) + expect(response.headers["Content-Type"]).to eq("text/csv") + end + end + + context "GET bad_links_url_and_status_csv" do + it "returns a 200 response" do + get "/bad_links_url_status.csv" + expect(response).to have_http_status(:ok) + expect(response.headers["Content-Type"]).to eq("text/csv") + end + end +end diff --git a/spec/requests/council_page_spec.rb b/spec/requests/council_page_spec.rb index 2a02bd3c4..d0ccbb635 100644 --- a/spec/requests/council_page_spec.rb +++ b/spec/requests/council_page_spec.rb @@ -7,6 +7,26 @@ it_behaves_like "redirects non-GDS Editors to services page", "/local_authorities/north-midlands/download_links_form" it_behaves_like "redirects non-GDS Editors to services page", "/local_authorities/north-midlands/upload_links_form" + describe "GET #index" do + context "when there is sufficient data" do + it "returns http succcess" do + login_as_gds_editor + + get "/local_authorities" + expect(response).to have_http_status(:ok) + end + end + end + + describe "GET #show" do + it "returns http success" do + login_as_gds_editor + + get "/local_authorities/north-midlands" + expect(response).to have_http_status(:ok) + end + end + describe "PATCH local_authorities/:local_authority_slug" do context "as a GDS Editor" do before { login_as_gds_editor } @@ -31,14 +51,25 @@ end end + describe "GET bad_homepage_url_and_status_csv" do + it "retrieves HTTP success" do + login_as_gds_editor + + get "/bad_homepage_url_status.csv" + expect(response).to have_http_status(:ok) + expect(response.headers["Content-Type"]).to eq("text/csv") + end + end + describe "POST local_authorities/:local_authority_slug/download_links_csv" do context "as a GDS Editor" do before { login_as_gds_editor } it "returns 200 OK" do - post "/local_authorities/north-midlands/download_links_csv", params: { links_status_checkbox: %w[ok] } + post "/local_authorities/north-midlands/download_links_csv", params: { links_status_checkbox: %w[ok broken] } expect(response).to have_http_status(:ok) + expect(response.headers["Content-Type"]).to eq("text/csv") end end @@ -75,5 +106,22 @@ end end end + + context "with a valid CSV" do + before { @local_authority = create(:local_authority, gss: "S1") } + let(:path) { Rails.root.join("spec/lib/local-links-manager/import/fixtures/imported_links.csv") } + let(:csv) { Rack::Test::UploadedFile.new(path, "text/csv", true) } + let(:url_regex) { /http:\/\/.+\/local_authorities\/north-midlands/ } + + it "retrieves HTTP found" do + login_as_gds_editor + + post "/local_authorities/north-midlands/upload_links_csv", params: { csv: } + + expect(response.status).to eq(302) + expect(response.location).to match(url_regex) + expect(response.headers["Content-Type"]).to eq("text/html; charset=utf-8") + end + end end end diff --git a/spec/requests/edit_link_page_spec.rb b/spec/requests/edit_link_page_spec.rb index cdf490efe..8e55ce05d 100644 --- a/spec/requests/edit_link_page_spec.rb +++ b/spec/requests/edit_link_page_spec.rb @@ -26,8 +26,17 @@ it "updates the link" do put path, params: { url: } + expect(Link.last.url).to eq("http://www.example.com/new") + expect(flash[:danger]).to be nil expect(link.reload.url).to eq(url) end + + it "catches invalid links" do + put path, params: { url: "ftp://who" } + + expect(response).to have_http_status(:found) + expect(flash[:danger]).not_to be nil + end end context "as a department user from the owning department" do diff --git a/spec/requests/services_page_spec.rb b/spec/requests/services_page_spec.rb index 331a1054e..018bc142c 100644 --- a/spec/requests/services_page_spec.rb +++ b/spec/requests/services_page_spec.rb @@ -21,16 +21,39 @@ allow_any_instance_of(LocalLinksManager::Export::ServiceLinksExporter).to receive(:export_links).and_return(exported_data) end - context "as a GDS Editor" do - before { login_as_gds_editor } + before { login_as_gds_editor } - it "returns 200 OK" do - post "/services/aardvark-wardens/download_links_csv" + context "GET #index" do + it "returns http success for services index page" do + get "/services" + expect(response).to have_http_status(200) + end + end + context "Get #show" do + it "returns http success" do + get "/services/aardvark-wardens" expect(response).to have_http_status(:ok) end end + context "GET #download_links_form and POST #download_links_csv" do + let(:exported_data) { "some_data" } + + before do + allow_any_instance_of(LocalLinksManager::Export::ServiceLinksExporter).to receive(:export_links).and_return(exported_data) + end + + it "returns a success response" do + create(:service) + get "/services/aardvark-wardens/download_links_form" + expect(response).to be_successful + + post "/services/aardvark-wardens/download_links_csv" + expect(response).to be_successful + end + end + context "as a department user from the owning department" do before { login_as_department_user(organisation_slug: owning_department) } @@ -56,9 +79,13 @@ context "as a GDS Editor" do before { login_as_gds_editor } + it "returns a success response" do + get "/services/aardvark-wardens/upload_links_form" + expect(response).to be_successful + end + it "returns 302 Found" do post "/services/aardvark-wardens/upload_links_csv" - expect(response).to have_http_status(:found) end end diff --git a/spec/system/edit_link_page_spec.rb b/spec/system/edit_link_page_spec.rb index 006c7814b..8b560868f 100644 --- a/spec/system/edit_link_page_spec.rb +++ b/spec/system/edit_link_page_spec.rb @@ -23,6 +23,8 @@ visit "/local_authorities/north-midlands/services/aardvark-wardens/reporting/edit" expect(page).to have_button("Delete") + click_on "Delete" + expect(page).to have_content("Link has been deleted") end end end diff --git a/spec/system/upload_links_csv_spec.rb b/spec/system/upload_links_csv_spec.rb new file mode 100644 index 000000000..557fb141d --- /dev/null +++ b/spec/system/upload_links_csv_spec.rb @@ -0,0 +1,52 @@ +RSpec.describe "Council page" do + let!(:local_authority) { create(:district_council, slug: "north-midlands", gss: "S1") } + let(:fixture_path) { "spec/lib/local-links-manager/import/fixtures/" } + + context "with errors in the CSV" do + before do + login_as_gds_editor + interaction = create(:interaction, lgil_code: 1) + 6.times do |i| + service = create(:service, lgsl_code: i + 1) + create(:service_interaction, service:, interaction:) + end + end + + it "shows the all error message if all lines are broken" do + visit "/local_authorities/north-midlands/upload_links_form" + + attach_file(Rails.root.join(fixture_path, "imported_links_all_errors.csv")) + expect(page).to have_button("Upload Links") + click_button "Upload Links" + + expect(page).to have_content("Errors on all lines. Ensure a New URL column exists, with all rows either blank or a valid URL") + end + + it "shows the many error message if many lines are broken" do + visit "/local_authorities/north-midlands/upload_links_form" + + attach_file(Rails.root.join(fixture_path, "imported_links_many_errors.csv")) + expect(page).to have_button("Upload Links") + click_button "Upload Links" + expect(page).to have_content("74 Errors detected. Please ensure a valid entry in the New URL column for lines (showing first 50):") + end + + it "shows the few error message if few lines are broken" do + visit "/local_authorities/north-midlands/upload_links_form" + + attach_file(Rails.root.join(fixture_path, "imported_links_few_errors.csv")) + expect(page).to have_button("Upload Links") + click_button "Upload Links" + expect(page).to have_content("2 Errors detected. Please ensure a valid entry in the New URL column for lines:") + end + + it "shows the nothing to import info if it didn't import anything" do + visit "/local_authorities/north-midlands/upload_links_form" + + attach_file(Rails.root.join(fixture_path, "imported_links_nothing_to_import.csv")) + expect(page).to have_button("Upload Links") + click_button "Upload Links" + expect(page).to have_content("No records updated. (If you were expecting updates, check the format of the uploaded file)") + end + end +end