2019-07-01 15:55:37 +02:00
describe ActiveStorage :: DownloadableFile do
2021-06-24 21:01:59 +02:00
let ( :dossier ) { create ( :dossier , :en_construction ) }
2019-07-25 15:57:00 +02:00
2023-01-30 17:42:03 +01:00
subject ( :list ) { ActiveStorage :: DownloadableFile . create_list_from_dossiers ( Dossier . where ( id : dossier . id ) , with_bills : true , with_champs_private : true ) }
2019-07-01 15:55:37 +02:00
2022-04-05 14:20:59 +02:00
describe 'create_list_from_dossiers' do
2019-07-01 15:55:37 +02:00
context 'when no piece_justificative is present' do
2021-03-08 11:35:20 +01:00
it { expect ( list . length ) . to eq 1 }
2021-06-24 21:01:59 +02:00
it { expect ( list . first [ 0 ] . name ) . to eq " pdf_export_for_instructeur " }
2019-07-01 15:55:37 +02:00
end
end
2022-09-05 11:20:30 +02:00
describe '.cleanup_list_from_dossier' do
context 'active_storage.service test' do
before { Rails . application . config . active_storage . service = :test }
it 'returns the list' do
list = [ :a , :b , :c ]
result = ActiveStorage :: DownloadableFile . cleanup_list_from_dossier ( list )
expect ( list ) . to eq ( result )
end
end
context 'active_storage.service local' do
before { Rails . application . config . active_storage . service = :local }
2023-04-26 22:18:40 +02:00
after { Rails . application . config . active_storage . service = :test }
2022-09-05 11:20:30 +02:00
it 'returns the list' do
list = [ :a , :b , :c ]
result = ActiveStorage :: DownloadableFile . cleanup_list_from_dossier ( list )
expect ( list ) . to eq ( result )
end
end
context 'active_storage.service openstack' do
let ( :object_storage_container ) { 'object_storage' }
let ( :available_blob_key ) { 'available' }
let ( :unavailable_blob_key ) { 'broken' }
let ( :active_storage_client ) { double }
2023-09-07 15:13:58 +02:00
let ( :active_storage_service ) { double ( container : object_storage_container ) }
2022-09-05 11:20:30 +02:00
before do
require 'fog/openstack'
Rails . application . config . active_storage . service = :openstack
2023-09-07 15:13:58 +02:00
allow ( ActiveStorage :: DownloadableFile ) . to receive ( :client ) . and_return ( active_storage_client )
2022-09-05 11:20:30 +02:00
end
2023-04-26 22:18:40 +02:00
after { Rails . application . config . active_storage . service = :test }
2022-09-05 11:20:30 +02:00
it 'returns the list' do
available_blob = double ( key : available_blob_key )
unavailable_blob = double ( key : unavailable_blob_key )
[ available_blob , unavailable_blob ] . map do | attachment |
allow ( attachment ) . to receive ( :service ) . and_return ( active_storage_service )
end
expect ( active_storage_client ) . to receive ( :head_object ) . with ( object_storage_container , available_blob_key ) . and_return ( true )
expect ( active_storage_client ) . to receive ( :head_object ) . with ( object_storage_container , unavailable_blob_key ) . and_raise ( Fog :: OpenStack :: Storage :: NotFound . new ( 'Object storage 99.99% availability leave space to 0.01% failure' ) )
list = [
[ instance_double ( ActiveStorage :: Attachment , blob : available_blob ) , 'filename.pdf' ] ,
[ instance_double ( ActiveStorage :: Attachment , blob : unavailable_blob ) , 'filename.pdf' ]
]
result = ActiveStorage :: DownloadableFile . cleanup_list_from_dossier ( list )
expect ( result . size ) . to eq ( 1 )
expect ( result . first . first . blob . key ) . to eq ( available_blob_key )
end
end
end
2019-07-01 15:55:37 +02:00
end