aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-05-30 11:40:45 +0200
committerZog2018-05-30 11:40:45 +0200
commit4be2635b5b869b9c9e0fd9a970f3de517284dac8 (patch)
treec0a68844bf69f8bc6d020df25b85da1e45da0836
parentdc8218759eff1ca2d492c96618628f1681e6dfdb (diff)
downloadchouette-core-7225-new-api-endpoints.tar.bz2
Refs #7225; New api endpoints for IEV7225-new-api-endpoints
-rw-r--r--app/controllers/api/v1/internals/netex_exports_controller.rb51
-rw-r--r--config/routes.rb2
-rw-r--r--spec/controllers/api/v1/internals/netex_exports_controller_spec.rb80
3 files changed, 133 insertions, 0 deletions
diff --git a/app/controllers/api/v1/internals/netex_exports_controller.rb b/app/controllers/api/v1/internals/netex_exports_controller.rb
new file mode 100644
index 000000000..8e69af635
--- /dev/null
+++ b/app/controllers/api/v1/internals/netex_exports_controller.rb
@@ -0,0 +1,51 @@
+module Api
+ module V1
+ module Internals
+ class NetexExportsController < Api::V1::Internals::ApplicationController
+ include ControlFlow
+ skip_before_action :require_token, only: :upload
+ before_action :find_netex_export
+
+ def upload
+ if authenticate_token
+ @netex_export.file = params[:file]
+ @netex_export.save!
+ @netex_export.successful!
+ @netex_export.notify_parent
+ render json: {
+ status: "ok",
+ message:"File successfully uploaded for #{@netex_export.type} (id: #{@netex_export.id})"
+ }
+ else
+ @netex_export.failed!
+ @netex_export.notify_parent
+ render_unauthorized("Access denied")
+ end
+ end
+
+ def notify_parent
+ if @netex_export.notify_parent
+ render json: {
+ status: "ok",
+ message:"#{@netex_export.parent_type} (id: #{@netex_export.parent_id}) successfully notified at #{l(@netex_export.notified_parent_at)}"
+ }
+ else
+ render json: {status: "error", message: @netex_export.errors.full_messages }
+ end
+ end
+
+ private
+
+ def find_netex_export
+ @netex_export = Export::Netex.find(params[:id])
+ rescue ActiveRecord::RecordNotFound
+ render json: {
+ status: "error",
+ message: "Record not found"
+ }, status: 404
+ finish_action!
+ end
+ end
+ end
+ end
+end
diff --git a/config/routes.rb b/config/routes.rb
index cde1701f8..56b97753c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -209,6 +209,8 @@ ChouetteIhm::Application.routes.draw do
namespace :internals do
get 'compliance_check_sets/:id/notify_parent', to: 'compliance_check_sets#notify_parent'
get 'netex_imports/:id/notify_parent', to: 'netex_imports#notify_parent'
+ get 'netex_exports/:id/notify_parent', to: 'netex_exports#notify_parent'
+ put 'netex_exports/:id/upload', to: 'netex_exports#upload'
end
end
end
diff --git a/spec/controllers/api/v1/internals/netex_exports_controller_spec.rb b/spec/controllers/api/v1/internals/netex_exports_controller_spec.rb
new file mode 100644
index 000000000..5a44ca547
--- /dev/null
+++ b/spec/controllers/api/v1/internals/netex_exports_controller_spec.rb
@@ -0,0 +1,80 @@
+RSpec.describe Api::V1::Internals::NetexExportsController, type: :controller do
+ let(:export_1) { create :netex_export }
+ let(:export_2) { create :netex_export, status: "successful" }
+
+ describe "GET #notify_parent" do
+ context 'unauthenticated' do
+ include_context 'iboo wrong authorisation internal api'
+
+ it 'should not be successful' do
+ get :notify_parent, id: export_1.id, format: :json
+ expect(response).to have_http_status 401
+ end
+ end
+
+ context 'authenticated' do
+ include_context 'iboo authenticated internal api'
+
+ describe "with existing record" do
+
+ before(:each) do
+ get :notify_parent, id: export_2.id, format: :json
+ end
+
+ it 'should be successful' do
+ expect(response).to have_http_status 200
+ end
+
+ it "calls #notify_parent on the export" do
+ expect(export_2.reload.notified_parent_at).not_to be_nil
+ end
+ end
+
+ describe "with non existing record" do
+ it "should throw an error" do
+ get :notify_parent, id: 47, format: :json
+ expect(response).to have_http_status 404
+ end
+ end
+ end
+ end
+
+ describe "POST #upload" do
+ let(:file){ fixture_file_upload('multiple_references_import.zip') }
+ context 'unauthenticated' do
+ include_context 'iboo wrong authorisation internal api'
+
+ it 'should not be successful' do
+ post :upload, id: export_1.id, format: :json, file: file
+ expect(response).to have_http_status 401
+ expect(export_1.reload.failed?).to be_truthy
+ expect(export_1.reload.notified_parent_at).not_to be_nil
+ end
+ end
+
+ context 'authenticated' do
+ include_context 'iboo authenticated internal api'
+
+ describe "with existing record" do
+
+ before(:each) do
+ post :upload, id: export_2.id, format: :json, file: file
+ end
+
+ it 'should be successful' do
+ expect(response).to have_http_status 200
+ expect(export_2.reload.file).not_to be_nil
+ expect(export_2.reload.successful?).to be_truthy
+ expect(export_2.reload.notified_parent_at).not_to be_nil
+ end
+ end
+
+ describe "with non existing record" do
+ it "should throw an error" do
+ post :upload, id: 42, format: :json, file: file
+ expect(response).to have_http_status 404
+ end
+ end
+ end
+ end
+end