@ -2,7 +2,7 @@ require "rails_helper"
RSpec . describe CollectionResourcesController , type : :request do
let ( :page ) { Capybara :: Node :: Simple . new ( response . body ) }
let ( :storage_service ) { instance_double ( Storage :: S3Service , get_file_metadata : nil ) }
let ( :storage_service ) { instance_double ( Storage :: S3Service , get_file_metadata : nil , delete_file : nil ) }
before do
allow ( Storage :: S3Service ) . to receive ( :new ) . and_return ( storage_service )
@ -183,7 +183,7 @@ RSpec.describe CollectionResourcesController, type: :request do
expect ( page ) . to have_content ( " additional resource " )
expect ( page ) . not_to have_content ( " additional resource 2 " )
expect ( page ) . to have_link ( " additional.pdf " , href : collection_resource_download_path ( collection_resource ) )
expect ( page ) . to have_link ( " Delete " )
expect ( page ) . to have_link ( " Delete " , href : collection_resource_delete_confirmation_path ( collection_resource ) )
end
end
end
@ -277,9 +277,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
get edit_mandatory_collection_resource_path ( year : 2024 , log_type : " sales " , resource_type : " bulk_upload_template " )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -290,9 +290,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
get edit_mandatory_collection_resource_path ( year : 2024 , log_type : " sales " , resource_type : " bulk_upload_template " )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -366,9 +366,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
patch update_mandatory_collection_resource_path , params : params
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -379,9 +379,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
patch update_mandatory_collection_resource_path , params : params
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
end
@ -401,9 +401,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
get confirm_mandatory_collection_resources_release_path ( year : 2025 )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -414,9 +414,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
get confirm_mandatory_collection_resources_release_path ( year : 2025 )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -466,9 +466,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
patch release_mandatory_collection_resources_path ( year : 2024 )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -479,9 +479,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
patch release_mandatory_collection_resources_path ( year : 2024 )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -528,9 +528,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
get new_collection_resource_path ( year : 2025 , log_type : " sales " )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -541,9 +541,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
get new_collection_resource_path ( year : 2025 , log_type : " sales " )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -589,9 +589,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
post collection_resources_path , params : params
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -602,9 +602,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
post collection_resources_path , params : params
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
end
@ -722,9 +722,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
get collection_resource_edit_path ( collection_resource )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -735,9 +735,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
get collection_resource_edit_path ( collection_resource )
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -794,9 +794,9 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
patch collection_resource_update_path ( collection_resource ) , params : params
expect ( response ) . to have_http_status ( :not_foun d )
expect ( response ) . to have_http_status ( :unauthorize d )
end
end
@ -807,9 +807,132 @@ RSpec.describe CollectionResourcesController, type: :request do
sign_in user
end
it " returns page not foun d " do
it " returns page not authorise d " do
patch collection_resource_update_path ( collection_resource ) , params : params
expect ( response ) . to have_http_status ( :not_found )
expect ( response ) . to have_http_status ( :unauthorized )
end
end
end
describe " GET # collection_resource_delete_confirmation " do
let ( :collection_resource ) { create ( :collection_resource , :additional , year : 2025 , log_type : " sales " , short_display_name : " additional resource " , download_filename : " additional.pdf " ) }
context " when user is not signed in " do
it " redirects to the sign in page " do
get collection_resource_delete_confirmation_path ( collection_resource )
expect ( response ) . to redirect_to ( new_user_session_path )
end
end
context " when user is signed in as a data coordinator " do
let ( :user ) { create ( :user , :data_coordinator ) }
before do
sign_in user
end
it " returns page not authorised " do
get collection_resource_delete_confirmation_path ( collection_resource )
expect ( response ) . to have_http_status ( :unauthorized )
end
end
context " when user is signed in as a data provider " do
let ( :user ) { create ( :user , :data_provider ) }
before do
sign_in user
end
it " returns page not authorised " do
get collection_resource_delete_confirmation_path ( collection_resource )
expect ( response ) . to have_http_status ( :unauthorized )
end
end
context " when user is signed in as a support user " do
let ( :user ) { create ( :user , :support ) }
before do
allow ( Time . zone ) . to receive ( :today ) . and_return ( Time . zone . local ( 2025 , 1 , 8 ) )
allow ( user ) . to receive ( :need_two_factor_authentication? ) . and_return ( false )
sign_in user
end
context " and the file exists on S3 " do
it " displays delete confirmation page content " do
get collection_resource_delete_confirmation_path ( collection_resource )
expect ( page ) . to have_content ( " Sales 2025 to 2026 " )
expect ( page ) . to have_content ( " Are you sure you want to delete the additional resource? " )
expect ( page ) . to have_content ( " This file will no longer be available for users to download. " )
expect ( page ) . to have_content ( " You will not be able to undo this action. " )
expect ( page ) . to have_button ( " Delete resource " )
expect ( page ) . to have_link ( " Back " , href : collection_resources_path )
expect ( page ) . to have_link ( " Cancel " , href : collection_resources_path )
end
end
end
end
describe " DELETE # collection_resource_delete " do
let! ( :collection_resource ) { create ( :collection_resource , :additional , year : 2025 , log_type : " sales " , short_display_name : " additional resource " , download_filename : " additional.pdf " ) }
context " when user is not signed in " do
it " redirects to the sign in page " do
delete collection_resource_delete_path ( collection_resource )
expect ( response ) . to redirect_to ( new_user_session_path )
end
end
context " when user is signed in as a data coordinator " do
let ( :user ) { create ( :user , :data_coordinator ) }
before do
sign_in user
end
it " returns page not authorised " do
delete collection_resource_delete_path ( collection_resource )
expect ( response ) . to have_http_status ( :unauthorized )
end
end
context " when user is signed in as a data provider " do
let ( :user ) { create ( :user , :data_provider ) }
before do
sign_in user
end
it " returns page not authorised " do
delete collection_resource_delete_path ( collection_resource )
expect ( response ) . to have_http_status ( :unauthorized )
end
end
context " when user is signed in as a support user " do
let ( :user ) { create ( :user , :support ) }
before do
allow ( storage_service ) . to receive ( :file_exists? ) . and_return ( true )
allow ( Time . zone ) . to receive ( :today ) . and_return ( Time . zone . local ( 2025 , 1 , 8 ) )
allow ( user ) . to receive ( :need_two_factor_authentication? ) . and_return ( false )
sign_in user
end
context " and the file exists on S3 " do
it " displays delete confirmation page content " do
expect ( CollectionResource . visible . count ) . to eq ( 1 )
delete collection_resource_delete_path ( collection_resource )
expect ( CollectionResource . count ) . to eq ( 1 )
expect ( CollectionResource . visible . count ) . to eq ( 0 )
expect ( response ) . to redirect_to ( collection_resources_path )
expect ( storage_service ) . to have_received ( :delete_file ) . with ( collection_resource . download_filename )
follow_redirect!
expect ( page ) . to have_content ( " The sales 2025 to 2026 additional resource has been deleted. " )
end
end
end
end