diff options
| author | Robert | 2017-07-31 22:03:41 +0200 |
|---|---|---|
| committer | Robert | 2017-07-31 22:03:41 +0200 |
| commit | 24b079038f4fa6af2c0f6399334c7243ed5e70bc (patch) | |
| tree | bb3504a9de090f5f7763c488d1f39715ef0b908b | |
| parent | f0b4a3f06740290ddd344048f85307aa03885f23 (diff) | |
| parent | 8b748a668c9be125bdff3cf0e2bd0f023fbe7dc5 (diff) | |
| download | chouette-core-24b079038f4fa6af2c0f6399334c7243ed5e70bc.tar.bz2 | |
Merge branch '3507_1726_impl_workbench_import' into 4176_1726_neteximp_autocreate_ref
25 files changed, 183 insertions, 89 deletions
diff --git a/app/models/import.rb b/app/models/import.rb index d3aa6d21b..c932ecdd9 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -6,7 +6,7 @@ class Import < ActiveRecord::Base belongs_to :parent, class_name: to_s extend Enumerize - enumerize :status, in: %i(new downloading analyzing pending successful failed running aborted canceled) + enumerize :status, in: %i(new pending successful failed running aborted canceled) validates :file, presence: true validates_presence_of :referential, :workbench diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 7e571e78d..f697122aa 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -27,10 +27,9 @@ class Organisation < ActiveRecord::Base conf = Rails.application.config.try(:stif_portail_api) raise 'Rails.application.config.stif_portail_api configuration is not defined' unless conf - HTTPService.get_resource( + HTTPService.get_json_resource( host: conf[:url], path: '/api/v1/organizations', - parse_json: true, token: conf[:key]) end diff --git a/app/models/user.rb b/app/models/user.rb index 1dc5975e1..7fd8b8cee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -66,10 +66,9 @@ class User < ActiveRecord::Base conf = Rails.application.config.try(:stif_portail_api) raise 'Rails.application.config.stif_portail_api settings is not defined' unless conf - HTTPService.get_resource( + HTTPService.get_json_resource( host: conf[:url], path: '/api/v1/users', - parse_json: true, token: conf[:key]) end diff --git a/app/services/http_service.rb b/app/services/http_service.rb index b01e11d6f..bdd4ca8d5 100644 --- a/app/services/http_service.rb +++ b/app/services/http_service.rb @@ -2,25 +2,29 @@ module HTTPService extend self Timeout = Faraday::TimeoutError - def get_resource(host:, path:, token: nil, params: {}, parse_json: false) + def get_resource(host:, path:, token: nil, params: {}) Faraday.new(url: host) do |c| c.headers['Authorization'] = "Token token=#{token.inspect}" if token c.adapter Faraday.default_adapter - resp = c.get path, params - if resp.status == 200 - return parse_json ? JSON.parse(resp.body) : resp.body - else - raise "Error on api request status : #{resp.status} => #{resp.body}" - end + return c.get path, params + end + end + + def get_json_resource(host:, path:, token: nil, params: {}) + # Stupid Ruby!!! (I mean I just **need** Pattern Matching, maybe I need to write it myself :O) + resp = get_resource(host: host, path: path, token: token, params: params) + if resp.status == 200 + return JSON.parse(resp.body) + else + raise "Error on api request status : #{resp.status} => #{resp.body}" end end # host: 'http://localhost:3000', # path: '/api/v1/netex_imports.json', - # resource_name: 'netex_import', # token: '13-74009c36638f587c9eafb1ce46e95585', - # params: {referential_id: 13, workbench_id: 1}, + # params: { netex_import: {referential_id: 13, workbench_id: 1}}, # upload: {file: [StringIO.new('howdy'), 'application/zip', 'greeting']}) def post_resource(host:, path:, resource_name:, token: nil, params: {}, upload: nil) Faraday.new(url: host) do |c| @@ -35,7 +39,7 @@ module HTTPService extend self params.update( name => Faraday::UploadIO.new(value, mime_type, as_name ) ) end - c.post path, resource_name => params + return c.post path, params end end end diff --git a/app/services/retry_service.rb b/app/services/retry_service.rb index 55e2585ef..21b1def36 100644 --- a/app/services/retry_service.rb +++ b/app/services/retry_service.rb @@ -1,37 +1,52 @@ +require 'result' + class RetryService Retry = Class.new(RuntimeError) - def initialize( delays:, rescue_from: [], &blk ) + # @param@ delays: + # An array of delays that are used to retry after a sleep of the indicated + # value in case of failed exceutions. + # Once this array is exhausted the executen fails permanently + # + # @param@ rescue_from: + # During execution all the excpetions from this array +plus RetryService::Retry+ are rescued from and + # trigger just another retry after a `sleep` as indicated above. + # + # @param@ block: + # This optional code is excuted before each retry, it is passed the result of the failed attempt, thus + # an `Exception` and the number of execution already tried. + def initialize( delays: [], rescue_from: [], &blk ) @intervals = delays @registered_exceptions = Array(rescue_from) << Retry @failure_callback = blk end + # @param@ blk: + # The code to be executed it will be retried goverened by the `delay` passed into the initializer + # as described there in case it fails with one of the predefined exceptions or `RetryService::Retry` + # + # Eventually it will return a `Result` object. def execute &blk - status, result = execute_protected blk - return [status, result] if status == :ok + result = execute_protected blk + return result if result.ok? @intervals.each_with_index do | interval, retry_count | - @failure_callback.try(:call, result, retry_count.succ) sleep interval - status, result = execute_protected blk - return [status, result] if status == :ok + @failure_callback.try(:call, result.value, retry_count + 1) + result = execute_protected blk + return result if result.ok? end - [status, result] - end - - def register_failure_callback &blk - @failure_callback = blk + result end private def execute_protected blk - [:ok, blk.()] + Result.ok(blk.()) rescue Exception => e if @registered_exceptions.any?{ |re| e.is_a? re } - [:error, e] + Result.error(e) else raise end diff --git a/app/workers/workbench_import_worker.rb b/app/workers/workbench_import_worker.rb index 3f3d40a00..4f9a53c14 100644 --- a/app/workers/workbench_import_worker.rb +++ b/app/workers/workbench_import_worker.rb @@ -9,7 +9,7 @@ class WorkbenchImportWorker # ======= def perform(import_id) - @import = Import.find(import_id) + @import = WorkbenchImport.find(import_id) @response = nil @import.update_attributes(status: 'running') downloaded = download @@ -18,11 +18,11 @@ class WorkbenchImportWorker end def download - logger.warn "HTTP GET #{import_url}" + logger.info "HTTP GET #{import_url}" @zipfile_data = HTTPService.get_resource( host: import_host, path: import_path, - params: {token: @import.token_download}) + params: {token: @import.token_download}).body end def execute_post eg_name, eg_stream @@ -30,14 +30,13 @@ class WorkbenchImportWorker HTTPService.post_resource( host: export_host, path: export_path, - resource_name: 'netex_import', token: token(eg_name), params: params, upload: {file: [eg_stream, 'application/zip', eg_name]}) end def log_failure reason, count - logger.info "HTTP POST failed with #{reason}, count = #{count}, response=#{@response}" + logger.warn "HTTP POST failed with #{reason}, count = #{count}, response=#{@response}" end def try_again @@ -61,9 +60,12 @@ class WorkbenchImportWorker def upload_entry_group key_pair, element_count @import.update_attributes( current_step: element_count.succ ) - retry_service = RetryService.new(delays: RETRY_DELAYS, rescue_from: HTTPService::Timeout, &method(:log_failure)) - status, _ = retry_service.execute(&upload_entry_group_proc(key_pair)) - raise StopIteration unless status == :ok + retry_service = RetryService.new( + delays: RETRY_DELAYS, + rescue_from: [HTTPService::Timeout], + &method(:log_failure)) + status = retry_service.execute(&upload_entry_group_proc(key_pair)) + raise StopIteration unless status.ok? end def upload_entry_group_proc key_pair @@ -94,7 +96,7 @@ class WorkbenchImportWorker Rails.application.config.front_end_host end def export_path - '/api/v1/netex_imports.json' + api_v1_netex_imports_path(format: :json) end def export_url @__export_url__ ||= File.join(export_host, export_path) @@ -104,13 +106,13 @@ class WorkbenchImportWorker Rails.application.config.front_end_host end def import_path - @__import_path__ ||= File.join(download_workbench_import_path(@import.workbench, @import)) + @__import_path__ ||= download_workbench_import_path(@import.workbench, @import) end def import_url @__import_url__ ||= File.join(import_host, import_path) end def params - @__params__ ||= { referential_id: @import.referential_id, workbench_id: @import.workbench_id } + @__params__ ||= { netex_import: { referential_id: @import.referential_id, workbench_id: @import.workbench_id } } end end diff --git a/config/application.rb b/config/application.rb index e2f6f8e7b..05a9752b6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,7 +15,6 @@ module ChouetteIhm # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. config.autoload_paths << config.root.join('lib') - config.autoload_paths << config.root.join('concerns') # custom exception pages config.exceptions_app = self.routes diff --git a/config/environments/development.rb b/config/environments/development.rb index 1384d0c00..42523a761 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -91,7 +91,5 @@ Rails.application.configure do # link to validation specification pages config.validation_spec = "http://www.chouette.mobi/neptune-validation/v21/" - # Local zip decompression dir - # config.i18n.available_locales = [:fr, :en] end diff --git a/config/initializers/workbench_import.rb b/config/initializers/workbench_import.rb index 1e405c9ca..89ddd72ef 100644 --- a/config/initializers/workbench_import.rb +++ b/config/initializers/workbench_import.rb @@ -1,5 +1,5 @@ WorkbenchImportWorker.config do | config | config.dir = ENV.fetch('WORKBENCH_IMPORT_DIR'){ Rails.root.join 'tmp/workbench_import' } - FileUtils.mkdir_p config.dir rescue nil + FileUtils.mkdir_p config.dir end diff --git a/hello_world b/hello_world deleted file mode 100644 index 3b18e512d..000000000 --- a/hello_world +++ /dev/null @@ -1 +0,0 @@ -hello world diff --git a/lib/result.rb b/lib/result.rb new file mode 100644 index 000000000..96e03d323 --- /dev/null +++ b/lib/result.rb @@ -0,0 +1,37 @@ +# A value wrapper adding status information to any value +# Status can be :ok or :error, we are thusly implementing +# what is expressed in Elixir/Erlang as result tuples and +# in Haskell as `Data.Either` +class Result + + attr_reader :status, :value + + class << self + def ok value + make :ok, value + end + def error value + make :error, value + end + + def new *args + raise NoMethodError, "No default constructor for #{self}" + end + + private + def make status, value + allocate.tap do | o | + o.instance_exec do + @status = status + @value = value + end + end + end + end + + def ok?; status == :ok end + + def == other + other.kind_of?(self.class) && other.status == status && other.value == value + end +end diff --git a/spec/concerns/configurable_spec.rb b/spec/concerns/configurable_spec.rb index 822f572c1..330241b72 100644 --- a/spec/concerns/configurable_spec.rb +++ b/spec/concerns/configurable_spec.rb @@ -14,7 +14,9 @@ RSpec.describe Configurable do subject.config.something = something expect( subject.config.something ).to eq(something) + # Instances delegate to the class expect( subject.new.send(:config).something ).to eq(something) + # **All** instances delegate to the class expect( subject.new.send(:config).something ).to eq(something) end @@ -25,7 +27,9 @@ RSpec.describe Configurable do end expect( subject.config.something ).to eq(something) + # Instances delegate to the class expect( subject.new.send(:config).something ).to eq(something) + # **All** instances delegate to the class expect( subject.new.send(:config).something ).to eq(something) end end diff --git a/spec/fixtures/multiref.zip b/spec/fixtures/multiple_references_import.zip Binary files differindex 28ddff198..28ddff198 100644 --- a/spec/fixtures/multiref.zip +++ b/spec/fixtures/multiple_references_import.zip diff --git a/spec/fixtures/singleref.zip b/spec/fixtures/single_reference_import.zip Binary files differindex 4aee23614..4aee23614 100644 --- a/spec/fixtures/singleref.zip +++ b/spec/fixtures/single_reference_import.zip diff --git a/spec/lib/result_spec.rb b/spec/lib/result_spec.rb new file mode 100644 index 000000000..949de163c --- /dev/null +++ b/spec/lib/result_spec.rb @@ -0,0 +1,20 @@ +RSpec.describe Result do + + context 'is a wrapper of a value' do + it { expect( described_class.ok('hello').value ).to eq('hello') } + it { expect( described_class.error('hello').value ).to eq('hello') } + end + + context 'it has status information' do + it { expect( described_class.ok('hello') ).to be_ok } + it { expect( described_class.ok('hello').status ).to eq(:ok) } + + it { expect( described_class.error('hello') ).not_to be_ok } + it { expect( described_class.error('hello').status ).to eq(:error) } + end + + context 'nil is just another value' do + it { expect( described_class.ok(nil) ).to be_ok } + it { expect( described_class.ok(nil).value ).to be_nil } + end +end diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb index 424936c58..6546b14ab 100644 --- a/spec/models/import_spec.rb +++ b/spec/models/import_spec.rb @@ -1,10 +1,13 @@ RSpec.describe Import, :type => :model do subject{ build_stubbed :import } + it { should belong_to(:referential) } it { should belong_to(:workbench) } it { should belong_to(:parent).class_name(described_class.to_s) } - it { should enumerize(:status).in("aborted", "analyzing", "canceled", "downloading", "failed", "new", "pending", "running", "successful") } + it { should enumerize(:status).in("aborted", "canceled", "failed", "new", "pending", "running", "successful") } it { should validate_presence_of(:file) } + it { should validate_presence_of(:referential) } + it { should validate_presence_of(:workbench) } end diff --git a/spec/models/netex_import_spec.rb b/spec/models/netex_import_spec.rb deleted file mode 100644 index 066595f06..000000000 --- a/spec/models/netex_import_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -RSpec.describe NetexImport, type: :model do - it { should validate_presence_of(:referential) } - it { should validate_presence_of(:workbench) } -end diff --git a/spec/requests/api/v1/netex_import_spec.rb b/spec/requests/api/v1/netex_import_spec.rb index c17274ab3..da42f8e19 100644 --- a/spec/requests/api/v1/netex_import_spec.rb +++ b/spec/requests/api/v1/netex_import_spec.rb @@ -6,7 +6,7 @@ RSpec.describe "NetexImport", type: :request do let( :workbench ){ referential.workbench } - let( :file_path ){ fixtures_path 'singleref.zip' } + let( :file_path ){ fixtures_path 'single_reference_import.zip' } let( :file ){ fixture_file_upload( file_path ) } let( :post_request ) do diff --git a/spec/services/retry_service_spec.rb b/spec/services/retry_service_spec.rb index 22957b565..bb3416373 100644 --- a/spec/services/retry_service_spec.rb +++ b/spec/services/retry_service_spec.rb @@ -6,11 +6,11 @@ RSpec.describe RetryService do expect( subject ).not_to receive(:sleep) end - it 'returns a tuple :ok and the result' do - expect( subject.execute { 42 } ).to eq([:ok, 42]) + it 'returns an ok result' do + expect( subject.execute { 42 } ).to eq(Result.ok(42)) end it 'does not fail on nil' do - expect( subject.execute { nil } ).to eq([:ok, nil]) + expect( subject.execute { nil } ).to eq(Result.ok(nil)) end it 'fails wihout retries if raising un unregistered exception' do @@ -26,13 +26,13 @@ RSpec.describe RetryService do end it 'fails after raising a registered exception n times' do result = subject.execute{ raise ArgumentError } - expect( result.first ).to eq(:error) - expect( result.last ).to be_kind_of(ArgumentError) + expect( result.status ).to eq(:error) + expect( result.value ).to be_kind_of(ArgumentError) end it 'fails with an explicit try again (automatically registered exception)' do result = subject.execute{ raise RetryService::Retry } - expect( result.first ).to eq(:error) - expect( result.last ).to be_kind_of(RetryService::Retry) + expect( result.status ).to eq(:error) + expect( result.value ).to be_kind_of(RetryService::Retry) end end @@ -42,12 +42,12 @@ RSpec.describe RetryService do expect( subject ).to receive(:sleep).with(2) end - it 'succeds the second time' do - expect( subject.execute{ succeed_later(ArgumentError){ 42 } } ).to eq([:ok, 42]) + it 'succeeds the second time' do + expect( subject.execute{ succeed_later(ArgumentError){ 42 } } ).to eq(Result.ok(42)) end it 'succeeds the second time with try again (automatically registered exception)' do - expect( subject.execute{ succeed_later(RetryService::Retry){ 42 } } ).to eq([:ok, 42]) + expect( subject.execute{ succeed_later(RetryService::Retry){ 42 } } ).to eq(Result.ok(42)) end end @@ -58,17 +58,26 @@ RSpec.describe RetryService do expect( subject ).to receive(:sleep).with(3) end it 'succeeds the third time with try again (automatically registered exception)' do - expect( subject.execute{ succeed_later(RetryService::Retry, count: 2){ 42 } } ).to eq([:ok, 42]) + result = subject.execute{ succeed_later(RetryService::Retry, count: 2){ 42 } } + expect( result ).to eq( Result.ok(42) ) end end context 'failure callback once' do + subject do + described_class.new delays: [2, 3], rescue_from: [NameError, ArgumentError] do |reason, count| + @reason=reason + @callback_count=count + @failures += 1 + end + end + before do @failures = 0 @count = 0 expect( subject ).to receive(:sleep).with(2) - subject.register_failure_callback { |reason, count| @reason=reason; @callback_count=count; @failures += 1 } end + it 'succeeds the second time and calls the failure_callback once' do subject.execute{ succeed_later(RetryService::Retry){ 42 } } expect( @failures ).to eq(1) @@ -81,13 +90,19 @@ RSpec.describe RetryService do end context 'failure callback twice' do + subject do + described_class.new delays: [2, 3], rescue_from: [NameError, ArgumentError] do |_reason, _count| + @failures += 1 + end + end + before do @failures = 0 @count = 0 expect( subject ).to receive(:sleep).with(2) expect( subject ).to receive(:sleep).with(3) - subject.register_failure_callback { @failures += 1 } end + it 'succeeds the third time and calls the failure_callback twice' do subject.execute{ succeed_later(NameError, count: 2){ 42 } } expect( @failures ).to eq(2) diff --git a/spec/services/zip_service/zip_entry_data_spec.rb b/spec/services/zip_service/zip_entry_data_spec.rb index 6bfaa8cc4..2a7226eb4 100644 --- a/spec/services/zip_service/zip_entry_data_spec.rb +++ b/spec/services/zip_service/zip_entry_data_spec.rb @@ -1,18 +1,15 @@ RSpec.describe ZipService do - subject{ described_class.new(read_fixture('multiref.zip')) } - - let( :ref1_zipdata ){ read_fixture('ref1.zip') } - let( :ref2_zipdata ){ read_fixture('ref2.zip') } + subject{ described_class.new(read_fixture('multiple_references_import.zip')) } it 'can group all entries' do expect( subject.entry_groups.keys ).to eq(%w{ref1 ref2}) end context 'creates correct zip data for each subdir' do - it 'e.g. ref1' do - ref1_stream = subject.entry_group_streams['ref1'] - control_stream = Zip::InputStream.open( ref1_stream ) + it 'e.g. reference1' do + reference1_stream = subject.entry_group_streams['ref1'] + control_stream = Zip::InputStream.open( reference1_stream ) control_entries = described_class.entries(control_stream) expect( control_entries.map{ |e| [e.name, e.get_input_stream.read]}.force ).to eq([ ["multiref/ref1/", ""], @@ -20,9 +17,9 @@ RSpec.describe ZipService do ["multiref/ref1/datum-2", "multi-ref1-datum2\n"] ]) end - it 'e.g. ref2' do - ref2_stream = subject.entry_group_streams['ref2'] - control_stream = Zip::InputStream.open( ref2_stream ) + it 'e.g. reference2' do + reference2_stream = subject.entry_group_streams['ref2'] + control_stream = Zip::InputStream.open( reference2_stream ) control_entries = described_class.entries(control_stream) expect( control_entries.map{ |e| [e.name, e.get_input_stream.read]}.force ).to eq([ ["multiref/ref2/", ""], diff --git a/spec/services/zip_service/zip_entry_dirs_spec.rb b/spec/services/zip_service/zip_entry_dirs_spec.rb index cf927855f..8ca1b0f1a 100644 --- a/spec/services/zip_service/zip_entry_dirs_spec.rb +++ b/spec/services/zip_service/zip_entry_dirs_spec.rb @@ -11,14 +11,14 @@ RSpec.describe ZipService do end context 'single entry' do - let( :zip_file ){ fixtures_path 'multiref.zip' } + let( :zip_file ){ fixtures_path 'multiple_references_import.zip' } let( :expected ){ %w{ref1 ref2} } it_behaves_like 'a correct zip entry reader' end context 'more entries' do - let( :zip_file ){ fixtures_path 'singleref.zip' } + let( :zip_file ){ fixtures_path 'single_reference_import.zip' } let( :expected ){ %w{ref} } it_behaves_like 'a correct zip entry reader' diff --git a/spec/services/zip_service/zip_output_streams_spec.rb b/spec/services/zip_service/zip_output_streams_spec.rb index b99e7bc2d..fbc60ae92 100644 --- a/spec/services/zip_service/zip_output_streams_spec.rb +++ b/spec/services/zip_service/zip_output_streams_spec.rb @@ -1,6 +1,6 @@ RSpec.describe ZipService do - subject{ described_class.new(read_fixture('multiref.zip')) } + subject{ described_class.new(read_fixture('multiple_references_import.zip')) } it 'can write itself to a file' do @@ -15,7 +15,7 @@ RSpec.describe ZipService do expect( ref2_lines ).to eq %w(multiref/ref2/ multiref/ref2/datum-1 multiref/ref2/datum-2) end - it "exposes it's size" do + it "exposes its size" do expect( subject.entry_group_streams.size ).to eq(2) end end diff --git a/spec/support/fixtures_helper.rb b/spec/support/fixtures_helper.rb index 81f6ce838..20963261b 100644 --- a/spec/support/fixtures_helper.rb +++ b/spec/support/fixtures_helper.rb @@ -4,6 +4,9 @@ module Support Rails.root.join( fixture_path, *segments ) end + def open_fixture *segments + File.open(fixtures_path(*segments)) + end def read_fixture *segments File.read(fixtures_path(*segments)) end diff --git a/spec/tasks/reflex_rake_spec.rb b/spec/tasks/reflex_rake_spec.rb index 04c5886aa..6ece223d2 100644 --- a/spec/tasks/reflex_rake_spec.rb +++ b/spec/tasks/reflex_rake_spec.rb @@ -5,7 +5,7 @@ describe 'reflex:sync' do before(:each) do ['getOP', 'getOR'].each do |method| stub_request(:get, "#{Rails.application.config.reflex_api_url}/?format=xml&idRefa=0&method=#{method}"). - to_return(body: File.open("#{fixture_path}/reflex.zip"), status: 200) + to_return(body: open_fixture('reflex.zip'), status: 200) end stop_area_ref = create(:stop_area_referential, name: 'Reflex') @@ -43,7 +43,7 @@ describe 'reflex:sync' do before(:each) do ['getOP', 'getOR'].each do |method| stub_request(:get, "#{Rails.application.config.reflex_api_url}/?format=xml&idRefa=0&method=#{method}"). - to_return(body: File.open("#{fixture_path}/reflex_updated.zip"), status: 200) + to_return(body: open_fixture('reflex_updated.zip'), status: 200) end Stif::ReflexSynchronization.synchronize end diff --git a/spec/workers/workbench_import_worker_spec.rb b/spec/workers/workbench_import_worker_spec.rb index b6057b36a..4706595ce 100644 --- a/spec/workers/workbench_import_worker_spec.rb +++ b/spec/workers/workbench_import_worker_spec.rb @@ -1,22 +1,27 @@ 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( :import ){ build_stubbed :import, token_download: download_token, file: 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} } + let( :params ) do + { netex_import: + { referential_id: referential.id, workbench_id: workbench.id } + } + end # http://www.example.com/workbenches/:workbench_id/imports/:id/download let( :host ){ Rails.configuration.front_end_host } let( :path ){ download_workbench_import_path(workbench, import) } let( :downloaded_zip ){ double("downloaded zip") } + let( :download_zip_response ){ OpenStruct.new( body: downloaded_zip ) } let( :download_token ){ SecureRandom.urlsafe_base64 } - let( :upload_path ) { '/api/v1/netex_imports.json' } + let( :upload_path ) { api_v1_netex_imports_path(format: :json) } let( :entry_group_streams ) do entry_count.times.map{ |i| double( "entry group stream #{i}" ) } @@ -28,9 +33,9 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do end let( :zip_service ){ double("zip service") } - let( :zip_file ){ File.join(fixture_path, 'multiref.zip') } + let( :zip_file ){ open_fixture('multiple_references_import.zip') } - let( :post_response_ok ){ response(status: 201, boody: "{}") } + let( :post_response_ok ){ response(status: 201, body: "{}") } before do # Silence Logger @@ -54,7 +59,7 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do expect(HTTPService).to receive(:get_resource) .with(host: host, path: path, params: {token: download_token}) - .and_return( downloaded_zip ) + .and_return( download_zip_response ) entry_groups.each do | entry_group_name, entry_group_stream | mock_post entry_group_name, entry_group_stream, post_response_ok @@ -71,12 +76,12 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do context 'multireferential zipfile with error' do let( :entry_count ){ 3 } - let( :post_response_failure ){ response(status: 406, boody: {error: 'What was you thinking'}) } + let( :post_response_failure ){ response(status: 406, body: {error: 'What was you thinking'}) } it 'downloads a zip file, cuts it, and uploads some pieces' do expect(HTTPService).to receive(:get_resource) .with(host: host, path: path, params: {token: download_token}) - .and_return( downloaded_zip ) + .and_return( download_zip_response ) # First entry_group succeeds entry_groups[0..0].each do | entry_group_name, entry_group_stream | @@ -106,13 +111,12 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do 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]}) .and_return(response) end def response(**opts) - OpenStruct.new(opts) + double(**opts) end end |
