diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/requests/api/v1/netex_import_spec.rb | 65 | ||||
| -rw-r--r-- | spec/services/file_service_spec.rb | 17 |
2 files changed, 46 insertions, 36 deletions
diff --git a/spec/requests/api/v1/netex_import_spec.rb b/spec/requests/api/v1/netex_import_spec.rb index ab1e7f6ae..fd5f6d497 100644 --- a/spec/requests/api/v1/netex_import_spec.rb +++ b/spec/requests/api/v1/netex_import_spec.rb @@ -3,13 +3,15 @@ RSpec.describe "NetexImport", type: :request do describe 'POST netex_imports' do let( :referential ){ create :referential } + let( :workbench ){ referential.workbench } + let( :file_path ){ fixtures_path 'single_reference_import.zip' } let( :file ){ fixture_file_upload( file_path ) } let( :post_request ) do -> (attributes) do - post "/api/v1/netex_imports.json", + post api_v1_netex_imports_path(format: :json), attributes, authorization end @@ -19,54 +21,79 @@ RSpec.describe "NetexImport", type: :request do { name: 'hello world', file: file, - referential_id: referential.id, - workbench_id: referential.workbench_id + workbench_id: workbench.id } end - let( :illegal_attributes ) do - { referential_id: referential.id } - end context 'with correct credentials and correct request' do let( :authorization ){ authorization_token_header( get_api_key.token ) } - it 'succeeds' do post_request.(netex_import: legal_attributes) expect( response ).to be_success - expect( json_response_body ).to eq({'id' => NetexImport.last.id, 'type' => 'NetexImport'}) + expect( json_response_body ).to eq( + 'id' => NetexImport.last.id, + 'referential_id' => Referential.last.id, + 'workbench_id' => workbench.id + ) end it 'creates a NetexImport object in the DB' do expect{ post_request.(netex_import: legal_attributes) }.to change{NetexImport.count}.by(1) end + + it 'creates a correct Referential' do + legal_attributes # force object creation for correct to change behavior + expect{post_request.(netex_import: legal_attributes)}.to change{Referential.count}.by(1) + Referential.last.tap do | ref | + expect( ref.workbench_id ).to eq(workbench.id) + expect( ref.organisation_id ).to eq(workbench.organisation_id) + end + end end + context 'with incorrect credentials and correct request' do let( :authorization ){ authorization_token_header( "#{referential.id}-incorrect_token") } - it 'does not succeed' do - post_request.(netex_import: legal_attributes) + it 'does not create any DB object and does not succeed' do + legal_attributes # force object creation for correct to change behavior + expect{ post_request.(netex_import: legal_attributes) }.not_to change{Referential.count} expect( response.status ).to eq(401) end - it 'does not create an Import object' do - expect{ post_request.(netex_import: legal_attributes) }.not_to change{Import.count} - end end context 'with correct credentials and incorrect request' do let( :authorization ){ authorization_token_header( get_api_key.token ) } - it 'does not succeed' do - post_request.(netex_import: illegal_attributes) - expect( response.status ).to eq(406) - expect( json_response_body['errors']['file'] ).not_to be_empty + shared_examples_for 'illegal attributes' do |bad_attribute, illegal_value=nil, referential_count: 0| + context "missing #{bad_attribute}" do + let!( :illegal_attributes ){ legal_attributes.merge( bad_attribute => illegal_value ) } + it 'does not succeed' do + post_request.(netex_import: illegal_attributes) + expect( response.status ).to eq(406) + expect( json_response_body['errors'][bad_attribute.to_s] ).not_to be_empty + end + + it 'does not create an Import object' do + expect{ post_request.(netex_import: illegal_attributes) }.not_to change{Import.count} + end + + it 'might create a referential' do + expect{ post_request.(netex_import: illegal_attributes) }.to change{Referential.count}.by(referential_count) + end + end end - it 'does not create an Import object' do - expect{ post_request.(netex_import: illegal_attributes) }.not_to change{Import.count} + it_behaves_like 'illegal attributes', :file, referential_count: 1 + it_behaves_like 'illegal attributes', :workbench_id + context 'name already taken' do + before do + create :referential, name: 'already taken' + end + it_behaves_like 'illegal attributes', :name, 'already taken' end end end diff --git a/spec/services/file_service_spec.rb b/spec/services/file_service_spec.rb deleted file mode 100644 index 4426ee145..000000000 --- a/spec/services/file_service_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -# TODO: Delete me after stable implementation of #1726 -# RSpec.describe FileService do - -# it 'computes a unique filename' do -# expect( File ).to receive(:exists?).with('xxx/yyy_0').and_return( false ) - -# expect(described_class.unique_filename('xxx/yyy')).to eq('xxx/yyy_0') -# end - -# it 'handles duplicate names by means of a counter' do -# expect( File ).to receive(:exists?).with('xxx/yyy_0').and_return( true ) -# expect( File ).to receive(:exists?).with('xxx/yyy_1').and_return( true ) -# expect( File ).to receive(:exists?).with('xxx/yyy_2').and_return( false ) - -# expect(described_class.unique_filename('xxx/yyy')).to eq('xxx/yyy_2') -# end -# end |
