aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/models/organisation_spec.rb2
-rw-r--r--spec/models/user_spec.rb2
-rw-r--r--spec/support/webmock/helpers.rb18
-rw-r--r--spec/workers/workbench_import_worker_spec.rb22
4 files changed, 36 insertions, 8 deletions
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