aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert2017-08-01 14:44:43 +0200
committerRobert2017-08-01 16:17:16 +0200
commitee75bd1e579ab366eb6cac938f50e7786536472b (patch)
tree6b23231644522b945f29e57fc72b743ab41c93c6
parentf899be88e2b476bae67b5b5bfe2f4a1f7458b24e (diff)
downloadchouette-core-ee75bd1e579ab366eb6cac938f50e7786536472b.tar.bz2
Refs: #3507@2h; CR Step 4
- Added tests for HTTPService (regression) - Removed some dead code (Workbench model spec, Import factory) - Changed front_end_host to rails_host in config
-rw-r--r--app/services/http_service.rb2
-rw-r--r--app/workers/workbench_import_worker.rb4
-rw-r--r--config/environments/development.rb4
-rw-r--r--config/environments/test.rb2
-rw-r--r--spec/models/import_spec.rb1
-rw-r--r--spec/models/workbench_import_spec.rb4
-rw-r--r--spec/services/http_service_spec.rb74
-rw-r--r--spec/workers/workbench_import_worker_spec.rb9
8 files changed, 83 insertions, 17 deletions
diff --git a/app/services/http_service.rb b/app/services/http_service.rb
index bdd4ca8d5..ae7d0e413 100644
--- a/app/services/http_service.rb
+++ b/app/services/http_service.rb
@@ -26,7 +26,7 @@ module HTTPService extend self
# token: '13-74009c36638f587c9eafb1ce46e95585',
# 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)
+ def post_resource(host:, path:, token: nil, params: {}, upload: nil)
Faraday.new(url: host) do |c|
c.headers['Authorization'] = "Token token=#{token.inspect}" if token
c.request :multipart
diff --git a/app/workers/workbench_import_worker.rb b/app/workers/workbench_import_worker.rb
index 4f9a53c14..57c0f5f4e 100644
--- a/app/workers/workbench_import_worker.rb
+++ b/app/workers/workbench_import_worker.rb
@@ -93,7 +93,7 @@ class WorkbenchImportWorker
# =========
def export_host
- Rails.application.config.front_end_host
+ Rails.application.config.rails_host
end
def export_path
api_v1_netex_imports_path(format: :json)
@@ -103,7 +103,7 @@ class WorkbenchImportWorker
end
def import_host
- Rails.application.config.front_end_host
+ Rails.application.config.rails_host
end
def import_path
@__import_path__ ||= download_workbench_import_path(@import.workbench, @import)
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 42523a761..ab4a29a3e 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_host = ENV.fetch('FRONT_END_URL', 'http://localhost:3000')
+ config.iev_url = ENV.fetch('IEV_URL', 'http://localhost:8080')
+ config.rails_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 9b6191546..b3312be4a 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -62,7 +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"
+ config.rails_host = "http://www.example.com"
# file to data for demo
config.demo_data = "tmp/demo.zip"
diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb
index 6546b14ab..c56858b44 100644
--- a/spec/models/import_spec.rb
+++ b/spec/models/import_spec.rb
@@ -1,5 +1,4 @@
RSpec.describe Import, :type => :model do
- subject{ build_stubbed :import }
it { should belong_to(:referential) }
it { should belong_to(:workbench) }
diff --git a/spec/models/workbench_import_spec.rb b/spec/models/workbench_import_spec.rb
deleted file mode 100644
index 24f3a2f85..000000000
--- a/spec/models/workbench_import_spec.rb
+++ /dev/null
@@ -1,4 +0,0 @@
-RSpec.describe WorkbenchImport do
- it { should validate_presence_of(:referential) }
- it { should validate_presence_of(:workbench) }
-end
diff --git a/spec/services/http_service_spec.rb b/spec/services/http_service_spec.rb
new file mode 100644
index 000000000..8c8af480c
--- /dev/null
+++ b/spec/services/http_service_spec.rb
@@ -0,0 +1,74 @@
+RSpec.describe HTTPService do
+
+ subject{ described_class }
+
+ %i{host params path result}.each do |param|
+ let(param){ double(param) }
+ end
+ let( :token ){ SecureRandom.hex }
+
+ let( :faraday_connection ){ double('faraday_connection') }
+ let( :headers ){ {} }
+
+
+ context 'get_resource' do
+ let( :params ){ double('params') }
+
+ it 'sets authorization and returns result' do
+ expect(Faraday).to receive(:new).with(url: host).and_yield(faraday_connection)
+ expect(faraday_connection).to receive(:adapter).with(Faraday.default_adapter)
+ expect(faraday_connection).to receive(:headers).and_return headers
+ expect(faraday_connection).to receive(:get).with(path, params).and_return(result)
+
+ expect(subject.get_resource(host: host, path: path, token: token, params: params)).to eq(result)
+ expect(headers['Authorization']).to eq( "Token token=#{token.inspect}" )
+ end
+ end
+
+ context 'post_resource' do
+ %i{as_name mime_type name upload_io value}.each do | param |
+ let( param ){ double(param) }
+ end
+
+ let( :upload_list ){ [value, mime_type, as_name] }
+
+ it 'sets authorization and posts data' do
+ expect(Faraday::UploadIO).to receive(:new).with(*upload_list).and_return upload_io
+ expect(params).to receive(:update).with(name => upload_io)
+
+ expect(Faraday).to receive(:new).with(url: host).and_yield(faraday_connection)
+ expect(faraday_connection).to receive(:adapter).with(Faraday.default_adapter)
+ expect(faraday_connection).to receive(:headers).and_return headers
+ expect(faraday_connection).to receive(:request).with(:multipart)
+ expect(faraday_connection).to receive(:request).with(:url_encoded)
+
+ expect(faraday_connection).to receive(:post).with(path, params).and_return(result)
+
+ expect(subject.post_resource(
+ host: host,
+ path: path,
+ token: token,
+ params: params,
+ upload: {name => upload_list} )).to eq(result)
+ expect(headers['Authorization']).to eq( "Token token=#{token.inspect}" )
+ end
+
+ end
+
+ context 'get_json_resource' do
+
+ let( :content ){ SecureRandom.hex }
+
+ it 'delegates an parses the response' do
+ expect_it.to receive(:get_resource)
+ .with(host: host, path: path, token: token, params: params)
+ .and_return(double(body: {content: content}.to_json, status: 200))
+
+ expect( subject.get_json_resource(
+ host: host,
+ path: path,
+ token: token,
+ params: params) ).to eq('content' => content)
+ end
+ end
+end
diff --git a/spec/workers/workbench_import_worker_spec.rb b/spec/workers/workbench_import_worker_spec.rb
index 4706595ce..b719cbb98 100644
--- a/spec/workers/workbench_import_worker_spec.rb
+++ b/spec/workers/workbench_import_worker_spec.rb
@@ -13,7 +13,7 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do
end
# http://www.example.com/workbenches/:workbench_id/imports/:id/download
- let( :host ){ Rails.configuration.front_end_host }
+ let( :host ){ Rails.configuration.rails_host }
let( :path ){ download_workbench_import_path(workbench, import) }
let( :downloaded_zip ){ double("downloaded zip") }
@@ -35,7 +35,7 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do
let( :zip_service ){ double("zip service") }
let( :zip_file ){ open_fixture('multiple_references_import.zip') }
- let( :post_response_ok ){ response(status: 201, body: "{}") }
+ let( :post_response_ok ){ double(status: 201, body: "{}") }
before do
# Silence Logger
@@ -76,7 +76,7 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do
context 'multireferential zipfile with error' do
let( :entry_count ){ 3 }
- let( :post_response_failure ){ response(status: 406, body: {error: 'What was you thinking'}) }
+ let( :post_response_failure ){ double(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)
@@ -116,7 +116,4 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do
upload: {file: [entry_group_stream, 'application/zip', entry_group_name]})
.and_return(response)
end
- def response(**opts)
- double(**opts)
- end
end