aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/api/v1/netex_imports_controller.rb26
-rw-r--r--app/controllers/imports_controller.rb2
-rw-r--r--app/models/netex_import.rb1
-rw-r--r--app/views/api/v1/netex_imports/create.json.rabl3
-rw-r--r--config/routes.rb2
-rw-r--r--spec/controllers/api/v1/lines_controller_spec.rb2
-rw-r--r--spec/controllers/api/v1/netex_import_controller_spec.rb12
-rw-r--r--spec/factories/imports.rb5
-rw-r--r--spec/models/api/v1/api_key_spec.rb2
-rw-r--r--spec/models/api/v1/netex_import_spec.rb15
-rw-r--r--spec/requests/api/v1/netex_import_spec.rb71
-rw-r--r--spec/routing/api/v1/import_routes_spec.rb10
-rw-r--r--spec/support/api_key.rb4
-rw-r--r--spec/support/json_helper.rb11
14 files changed, 123 insertions, 43 deletions
diff --git a/app/controllers/api/v1/netex_imports_controller.rb b/app/controllers/api/v1/netex_imports_controller.rb
new file mode 100644
index 000000000..16a7cef69
--- /dev/null
+++ b/app/controllers/api/v1/netex_imports_controller.rb
@@ -0,0 +1,26 @@
+module Api
+ module V1
+ class NetexImportsController < ChouetteController
+
+ def create
+ respond_to do | format |
+ format.json do
+ @import = NetexImport.create(netex_import_params)
+ unless @import.valid?
+ render json: {errors: @import.errors}, status: 406
+ end
+ end
+ end
+ end
+
+
+ private
+
+ def netex_import_params
+ params
+ .require('netex_import')
+ .permit(:file, :name, :referential_id)
+ end
+ end
+ end
+end
diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb
index 01a46f064..70c5c1a0d 100644
--- a/app/controllers/imports_controller.rb
+++ b/app/controllers/imports_controller.rb
@@ -23,8 +23,6 @@ class ImportsController < BreadcrumbController
end
def create
- require 'pry'
- binding.pry
create! { workbench_import_path(parent, resource) }
end
diff --git a/app/models/netex_import.rb b/app/models/netex_import.rb
index de5b84537..40eaca523 100644
--- a/app/models/netex_import.rb
+++ b/app/models/netex_import.rb
@@ -1,5 +1,6 @@
require 'net/http'
class NetexImport < Import
+ mount_uploader :file, ImportUploader
after_commit :launch_java_import
def launch_java_import
diff --git a/app/views/api/v1/netex_imports/create.json.rabl b/app/views/api/v1/netex_imports/create.json.rabl
new file mode 100644
index 000000000..1361cdb80
--- /dev/null
+++ b/app/views/api/v1/netex_imports/create.json.rabl
@@ -0,0 +1,3 @@
+
+object @import
+attributes :id, :type
diff --git a/config/routes.rb b/config/routes.rb
index 0bfde2e25..686c19eff 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -37,7 +37,7 @@ ChouetteIhm::Application.routes.draw do
resources :connection_links, only: [:index, :show]
resources :companies, only: [:index, :show]
resources :group_of_lines, only: [:index, :show]
- resources :imports, only: :create
+ resources :netex_imports, only: :create
resources :journey_patterns, only: :show
resources :lines, only: [:index, :show] do
resources :journey_patterns, only: [:index, :show]
diff --git a/spec/controllers/api/v1/lines_controller_spec.rb b/spec/controllers/api/v1/lines_controller_spec.rb
index d57eed766..30c2f1a50 100644
--- a/spec/controllers/api/v1/lines_controller_spec.rb
+++ b/spec/controllers/api/v1/lines_controller_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe Api::V1::LinesController, :type => :controller do
let!(:line) { referential.lines.first || create(:line) }
- it_behaves_like "api key protected controller" do
+ it_behaves_like "api key protected controller", :wip do
let(:data){line}
end
describe "GET #index" do
diff --git a/spec/controllers/api/v1/netex_import_controller_spec.rb b/spec/controllers/api/v1/netex_import_controller_spec.rb
deleted file mode 100644
index b6b90f7d1..000000000
--- a/spec/controllers/api/v1/netex_import_controller_spec.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-RSpec.describe Api::V1::NetexImportController, type: :controller do
- let( :netex_import ){ build_stubbed :netex_import }
-
- it_behaves_like "api key protected controller" do
- let(:data){ netex_import }
- end
-
- describe "POST #create" do
-
- end
-
-end
diff --git a/spec/factories/imports.rb b/spec/factories/imports.rb
index fc8668606..eea1894a5 100644
--- a/spec/factories/imports.rb
+++ b/spec/factories/imports.rb
@@ -9,5 +9,10 @@ FactoryGirl.define do
status :new
started_at nil
ended_at nil
+
+ factory :netex_import do
+ type 'NetexImport'
+ file {Rails.root.join('spec', 'fixtures', 'terminated_job.json')}
+ end
end
end
diff --git a/spec/models/api/v1/api_key_spec.rb b/spec/models/api/v1/api_key_spec.rb
index eb8826c0e..8a34c9221 100644
--- a/spec/models/api/v1/api_key_spec.rb
+++ b/spec/models/api/v1/api_key_spec.rb
@@ -1,5 +1,3 @@
-require 'spec_helper'
-
describe Api::V1::ApiKey, :type => :model do
let!(:referential){create(:referential)}
subject { Api::V1::ApiKey.create( :name => "test", :referential => referential)}
diff --git a/spec/models/api/v1/netex_import_spec.rb b/spec/models/api/v1/netex_import_spec.rb
deleted file mode 100644
index f1fddc4ba..000000000
--- a/spec/models/api/v1/netex_import_spec.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-RSpec.describe Api::V1::NetexImportController, type: :controller do
-
- context 'POST create' do
-
- let( :netex_import ){ build_stubbed(:netex_import) }
-
- it 'creates a NetexImport record' do
- expect_any_instance_of( ImportController ).to receive(:create).with(
- params: netex_import.attributes
- )
-
- end
- end
-
-end
diff --git a/spec/requests/api/v1/netex_import_spec.rb b/spec/requests/api/v1/netex_import_spec.rb
new file mode 100644
index 000000000..b7a5c94ee
--- /dev/null
+++ b/spec/requests/api/v1/netex_import_spec.rb
@@ -0,0 +1,71 @@
+RSpec.describe "NetexImport", type: :request do
+
+ describe 'POST netex_imports' do
+
+ let( :referential ){ create :referential }
+
+ let( :file_path ){'spec/fixtures/neptune.zip'}
+ let( :file ){ fixture_file_upload( file_path ) }
+
+ let( :post_request ) do
+ -> do
+ post "/api/v1/netex_imports.json",
+ attributes,
+ authorization
+ end
+ end
+
+ let( :attributes ){ {
+ netex_import: {
+ name: 'hello world',
+ file: file,
+ referential_id: referential.id}
+ } }
+
+ context 'with correct credentials and correct request' do
+ let( :authorization ){ authorization_token_header( get_api_key.token ) }
+
+
+ it 'succeeds' do
+ post_request.()
+ expect( response ).to be_success
+ expect( JSON.parse(response.body) ).to eq({'id' => NetexImport.last.id, 'type' => 'NetexImport'})
+ end
+
+ it 'creates a NetexImport object in the DB' do
+ expect(&post_request).to change{NetexImport.count}.by(1)
+ end
+ end
+
+ context 'with incorrect credentials and correct request' do
+ let( :authorization ){ authorization_token_header( "#{referential.id}-incorrect_token") }
+
+ it 'succeeds not' do
+ post_request.()
+ expect( response.status ).to eq(401)
+ end
+
+ it 'does not create an Import object' do
+ expect(&post_request).not_to change{Import.count}
+ end
+ end
+
+ context 'with correct credentials and incorrect request' do
+ let( :authorization ){ authorization_token_header( get_api_key.token ) }
+ let( :attributes ){ {
+ netex_import: {
+ referential_id: referential.id}
+ } }
+
+ it 'succeeds not' do
+ post_request.()
+ expect( response.status ).to eq(406)
+ expect( json_response_body['errors']['file'] ).not_to be_empty
+ end
+
+ it 'does not create an Import object' do
+ expect(&post_request).not_to change{Import.count}
+ end
+ end
+ end
+end
diff --git a/spec/routing/api/v1/import_routes_spec.rb b/spec/routing/api/v1/import_routes_spec.rb
deleted file mode 100644
index 7acc0e82d..000000000
--- a/spec/routing/api/v1/import_routes_spec.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-RSpec.describe Api::V1::ImportsController do
- describe "routing" do
- it {
- expect(post: '/api/v1/imports').to route_to(
- controller: 'api/v1/imports',
- action: 'create'
- )
- }
- end
-end
diff --git a/spec/support/api_key.rb b/spec/support/api_key.rb
index 9353fac15..561e1f796 100644
--- a/spec/support/api_key.rb
+++ b/spec/support/api_key.rb
@@ -1,5 +1,9 @@
module ApiKeyHelper
+ def authorization_token_header(key)
+ {'Authorization' => "Token token=#{key}"}
+ end
+
def get_api_key
Api::V1::ApiKey.first_or_create( :referential_id => referential.id, :name => "test")
end
diff --git a/spec/support/json_helper.rb b/spec/support/json_helper.rb
new file mode 100644
index 000000000..a383981a0
--- /dev/null
+++ b/spec/support/json_helper.rb
@@ -0,0 +1,11 @@
+module Support
+ module JsonHelper
+ def json_response_body
+ JSON.parse(response.body)
+ end
+ end
+end
+
+RSpec.configure do | config |
+ config.include Support::JsonHelper, type: :request
+end