diff options
| -rw-r--r-- | app/models/organisation.rb | 19 | ||||
| -rw-r--r-- | app/models/user.rb | 16 | ||||
| -rw-r--r-- | app/workers/workbench_import_worker.rb | 30 | ||||
| -rw-r--r-- | config/environments/development.rb | 4 | ||||
| -rw-r--r-- | config/environments/test.rb | 1 | ||||
| -rw-r--r-- | lib/af83/http_fetcher.rb | 21 | ||||
| -rw-r--r-- | spec/models/organisation_spec.rb | 2 | ||||
| -rw-r--r-- | spec/models/user_spec.rb | 2 | ||||
| -rw-r--r-- | spec/support/webmock/helpers.rb | 18 | ||||
| -rw-r--r-- | spec/workers/workbench_import_worker_spec.rb | 22 | 
10 files changed, 86 insertions, 49 deletions
diff --git a/app/models/organisation.rb b/app/models/organisation.rb index d0742bda6..a3c5da1af 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*-  class Organisation < ActiveRecord::Base    include DataFormatEnumerations @@ -26,19 +25,13 @@ class Organisation < ActiveRecord::Base    def self.portail_api_request      conf = Rails.application.config.try(:stif_portail_api) -    raise 'Rails.application.config.stif_portail_api settings is not defined' unless conf +    raise 'Rails.application.config.stif_portail_api configuration is not defined' unless conf -    conn = Faraday.new(:url => conf[:url]) do |c| -      c.headers['Authorization'] = "Token token=\"#{conf[:key]}\"" -      c.adapter  Faraday.default_adapter -    end - -    resp = conn.get '/api/v1/organizations' -    if resp.status == 200 -      JSON.parse resp.body -    else -      raise "Error on api request status : #{resp.status} => #{resp.body}" -    end +    AF83::HTTPFetcher.get_resource( +      host: conf[:url], +      path: '/api/v1/organizations', +      parse_json: true, +      token: conf[:key])    end    def self.sync_update code, name, scope diff --git a/app/models/user.rb b/app/models/user.rb index 64d66883f..fbffd96be 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -66,17 +66,11 @@ 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 -    conn = Faraday.new(:url => conf[:url]) do |c| -      c.headers['Authorization'] = %{Token token="#{conf[:key]}"} -      c.adapter  Faraday.default_adapter -    end - -    resp = conn.get '/api/v1/users' -    if resp.status == 200 -      JSON.parse resp.body -    else -      raise "Error on api request status : #{resp.status} => #{resp.body}" -    end +    AF83::HTTPFetcher.get_resource( +      host: conf[:url], +      path: '/api/v1/users', +      parse_json: true, +      token: conf[:key])    end    def self.portail_sync diff --git a/app/workers/workbench_import_worker.rb b/app/workers/workbench_import_worker.rb index b1c4fa30f..1def3cb86 100644 --- a/app/workers/workbench_import_worker.rb +++ b/app/workers/workbench_import_worker.rb @@ -1,5 +1,7 @@  class WorkbenchImportWorker    include Sidekiq::Worker +  include Rails.application.routes.url_helpers +    attr_reader :import, :downloaded    def perform(import_id) @@ -9,26 +11,24 @@ class WorkbenchImportWorker    end    def download -    logger.warn  "Call iev get #{Rails.configuration.fe_url}/boiv_iev/referentials/importer/new?id=#{id}" -    begin -      Net::HTTP.get(URI("#{Rails.configuration.front_end_url}/boiv_iev/referentials/importer/new?id=#{id}")) -    rescue Exception => e -      logger.error "IEV server error : e.message" -      logger.error e.backtrace.inspect -    end -     -     -      require 'pry' -      binding.pry -      42 - +    logger.warn  "HTTP GET #{import_url}" +    @downloaded = AF83::HTTPFetcher.get_resource( +      host: import_host, +      path: import_path, +      params: {token: import.token_download})    end +  def import_host +    @__import_host__ ||= Rails.application.config.front_end_host +  end +  def import_path +    @__import_path__ ||= File.join(download_workbench_import_path(import.workbench, import))  +  end    def import_uri -     @__import_uri__ ||= URI(import_url)  +    @__import_uri__ ||= URI(import_url)     end    def import_url -    @__import_url__ ||= File.join(download_workbench_import_path(import.workbench, import))  +    @__import_url__ ||= File.join(import_host, import_path)    end  end diff --git a/config/environments/development.rb b/config/environments/development.rb index 648a9a82c..42523a761 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -82,8 +82,8 @@ Rails.application.configure do    config.portal_url = "http://stif-boiv-staging.af83.priv"    # IEV url -  config.iev_url = ENV.fetch('IEV_URL', 'http://localhost:8080') -  config.front_end_url  = ENV.fetch('FRONT_END_URL', 'http://localhost:3000') +  config.iev_url        = ENV.fetch('IEV_URL', 'http://localhost:8080') +  config.front_end_host = ENV.fetch('FRONT_END_URL', 'http://localhost:3000')    # file to data for demo    config.demo_data = "tmp/demo.zip" diff --git a/config/environments/test.rb b/config/environments/test.rb index a6db12006..9b6191546 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -62,6 +62,7 @@ Rails.application.configure do    # Reflex api url    config.reflex_api_url = "https://195.46.215.128/ws/reflex/V1/service=getData" +  config.front_end_host  = "http://www.example.com"    # file to data for demo    config.demo_data = "tmp/demo.zip" diff --git a/lib/af83/http_fetcher.rb b/lib/af83/http_fetcher.rb new file mode 100644 index 000000000..514dd1dc9 --- /dev/null +++ b/lib/af83/http_fetcher.rb @@ -0,0 +1,21 @@ +module AF83 +  class HTTPFetcher +    def self.get_resource(*args) +      new.get_resource(*args) +    end + +    def get_resource(host:, path:, token: nil, params: {}, parse_json: false)  +      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 +      end +    end +  end +end 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  | 
