diff options
| author | Robert | 2017-07-26 08:13:43 +0200 |
|---|---|---|
| committer | Robert | 2017-07-26 14:35:42 +0200 |
| commit | d385a6da731dc6b3ec0ec5bec1e94eb2dcff5efb (patch) | |
| tree | 0e2113f5292a7929d3a3e34d06121302b0f70949 /spec | |
| parent | fe46c7832fa1e0c6af450f0937aea3534c0f6b01 (diff) | |
| download | chouette-core-d385a6da731dc6b3ec0ec5bec1e94eb2dcff5efb.tar.bz2 | |
Refs: #3507@4h; Finished Basic WorkbenchImportWorker
- speced but missing error treatment
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/models/api/v1/api_key_spec.rb | 31 | ||||
| -rw-r--r-- | spec/models/organisation_spec.rb | 2 | ||||
| -rw-r--r-- | spec/services/file_service_spec.rb | 25 | ||||
| -rw-r--r-- | spec/workers/workbench_import_worker_spec.rb | 62 |
4 files changed, 74 insertions, 46 deletions
diff --git a/spec/models/api/v1/api_key_spec.rb b/spec/models/api/v1/api_key_spec.rb index 8a34c9221..5f39a65e4 100644 --- a/spec/models/api/v1/api_key_spec.rb +++ b/spec/models/api/v1/api_key_spec.rb @@ -1,11 +1,34 @@ describe Api::V1::ApiKey, :type => :model do - let!(:referential){create(:referential)} - subject { Api::V1::ApiKey.create( :name => "test", :referential => referential)} - it "test" do - expect(subject).to be_valid + let(:referential){ create :referential } + + subject { described_class.create( :name => "test", :referential => referential)} + + it "validity test" do + expect_it.to be_valid expect(subject.referential).to eq(referential) + end + + context 'Creation' do + let( :name ){ SecureRandom.urlsafe_base64 } + + it 'can be created from a referential with a name, iff needed' do + # 1st time create a new record + expect{ described_class.from(referential, name: name) }.to change{ described_class.count }.by(1) + expect( described_class.last.attributes.values_at(*%w{referential_id name}) ).to eq([ + referential.id, name + ]) + + # 2nd time get the same record + expect{ described_class.from(referential, name: name) }.not_to change{ described_class.count } + expect( described_class.last.attributes.values_at(*%w{referential_id name}) ).to eq([ + referential.id, name + ]) + end + it 'cannot be created without a referential' do + expect{ described_class.from(nil, name:name) rescue nil }.not_to change{ described_class.count } + end end end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 1cce7e846..b16324a56 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -1,5 +1,3 @@ -require 'spec_helper' - describe Organisation, :type => :model do it { should validate_presence_of(:name) } it { should validate_uniqueness_of(:code) } diff --git a/spec/services/file_service_spec.rb b/spec/services/file_service_spec.rb index 90e164408..4426ee145 100644 --- a/spec/services/file_service_spec.rb +++ b/spec/services/file_service_spec.rb @@ -1,16 +1,17 @@ -RSpec.describe FileService do +# 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 ) +# 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 +# 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 ) +# 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 +# expect(described_class.unique_filename('xxx/yyy')).to eq('xxx/yyy_2') +# end +# end diff --git a/spec/workers/workbench_import_worker_spec.rb b/spec/workers/workbench_import_worker_spec.rb index d227c7610..68e429b60 100644 --- a/spec/workers/workbench_import_worker_spec.rb +++ b/spec/workers/workbench_import_worker_spec.rb @@ -2,54 +2,60 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do let( :worker ) { described_class.new } let( :import ){ build_stubbed :import, token_download: download_token, file: File.open(zip_file) } + let( :workbench ){ import.workbench } let( :referential ){ import.referential } + let( :api_key ){ build_stubbed :api_key, referential: referential, token: "#{referential.id}-#{SecureRandom.hex}" } + let( :params ){ {referential_id: referential.id, workbench_id: workbench.id} } # http://www.example.com/workbenches/:workbench_id/imports/:id/download - let( :url ){ "#{File.join(host, path)}?token=#{download_token}" } let( :host ){ Rails.configuration.front_end_host } let( :path ){ download_workbench_import_path(workbench, import) } - let( :result ){ import.file.read } + let( :downloaded_zip ){ double("downloaded zip") } let( :download_token ){ SecureRandom.urlsafe_base64 } + let( :upload_path ) { '/api/v1/netex_imports.json' } + + let( :entry_group_streams ) do + 2.times.map{ |i| double( "entry group stream #{i}" ) } + end + let( :entry_groups ) do + 2.times.map do | i | + {"entry_group_name#{i}" => entry_group_streams[i] } + end + end + + let( :zip_service ){ double("zip service") } + before do # That should be `build_stubbed's` job, no? allow(Import).to receive(:find).with(import.id).and_return(import) + allow(Api::V1::ApiKey).to receive(:from).and_return(api_key) + allow(ZipService).to receive(:new).with(downloaded_zip).and_return zip_service + expect(zip_service).to receive(:entry_group_streams).and_return(entry_groups) end - context 'multireferential zipfile' do + context 'multireferential zipfile' do let( :zip_file ){ File.join(fixture_path, 'multiref.zip') } it 'downloads a zip file' do - default_headers = {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'} - stub_request(:get, url) - .with(headers: default_headers) - .to_return(body: result) - - worker.perform import.id - - require 'pry' - binding.pry - expect( File.read(worker.downloaded) ).to eq( result ) - expect( worker ).not_to be_single_entry - end - end - - context 'unireferential zipfile' do - let( :zip_file ){ File.join(fixture_path, 'uniref.zip') } - - it 'downloads a zip file' do - default_headers = {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'} - stub_request(:get, url) - .with(headers: default_headers) - .to_return(body: result) - + expect(HTTPService).to receive(:get_resource) + .with(host: host, path: path, params: {token: download_token}) + .and_return( downloaded_zip ) + + entry_groups.each do | entry_group_name, entry_group_stream | + expect( HTTPService ).to receive(:post_resource) + .with(host: host, + path: upload_path, + resource_name: 'netex_import', + token: api_key.token, + params: params, + upload: {file: [entry_group_stream, 'application/zip', entry_group_name]}) + end worker.perform import.id - expect( File.read(worker.downloaded) ).to eq( result ) - expect( worker ).to be_single_entry end end |
