From ded817a7d0802cc2aafcdbe4bb256698f6868afd Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 21 Jul 2017 15:59:34 +0200 Subject: Refs: #3507@4.5h spec setup and refactoring, using Faraday in worker - Setting up correct headers for the Webmock request (Oh Boy) - Refactoring all Faraday requests into `lib/af83/http_fetcher.rb` - Implementing the Download --- spec/models/organisation_spec.rb | 2 +- spec/models/user_spec.rb | 2 +- spec/support/webmock/helpers.rb | 18 ++++++++++++++++++ spec/workers/workbench_import_worker_spec.rb | 22 ++++++++++++++++------ 4 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 spec/support/webmock/helpers.rb (limited to 'spec') diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 527f71015..1cce7e846 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -17,7 +17,7 @@ describe Organisation, :type => :model do let(:conf) { Rails.application.config.stif_portail_api } before :each do stub_request(:get, "#{conf[:url]}/api/v1/organizations"). - with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }). + with(stub_headers(authorization_token: conf[:key])). to_return(body: File.open(File.join(Rails.root, 'spec', 'fixtures', 'organizations.json')), status: 200) end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6f98e5ce7..c21e7e6d0 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -69,7 +69,7 @@ describe User, :type => :model do let(:conf) { Rails.application.config.stif_portail_api } before :each do stub_request(:get, "#{conf[:url]}/api/v1/users"). - with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }). + with(stub_headers(authorization_token: conf[:key])). to_return(body: File.open(File.join(Rails.root, 'spec', 'fixtures', 'users.json')), status: 200) end diff --git a/spec/support/webmock/helpers.rb b/spec/support/webmock/helpers.rb new file mode 100644 index 000000000..fc6c77850 --- /dev/null +++ b/spec/support/webmock/helpers.rb @@ -0,0 +1,18 @@ +module Support + module Webmock + module Helpers + def stub_headers(*args) + {headers: make_headers(*args)} + end + + def make_headers(headers={}, authorization_token:) + headers.merge('Authorization' => "Token token=#{authorization_token.inspect}") + end + end + end +end + +RSpec.configure do | conf | + conf.include Support::Webmock::Helpers, type: :model + conf.include Support::Webmock::Helpers, type: :worker +end diff --git a/spec/workers/workbench_import_worker_spec.rb b/spec/workers/workbench_import_worker_spec.rb index fa48b8252..a1f8659e7 100644 --- a/spec/workers/workbench_import_worker_spec.rb +++ b/spec/workers/workbench_import_worker_spec.rb @@ -1,24 +1,34 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do let( :worker ) { described_class.new } - let( :import ){ build_stubbed :import } + let( :import ){ build_stubbed :import, token_download: download_token } let( :workbench ){ import.workbench } let( :referential ){ import.referential } - let( :api_key ){ build_stubbed :api_key, referential: referential } - # /workbenches/:workbench_id/imports/:id/download + # 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( :download_token ){ SecureRandom.urlsafe_base64 } before do # That should be `build_stubbed's` job, no? allow(Import).to receive(:find).with(import.id).and_return(import) end - xit 'downloads a zip file' do - stub_request(:get, path) - .with(headers: authorization_token_header(api_key)) + + + 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 + expect( worker.downloaded ).to eq( result ) end + end -- cgit v1.2.3