diff options
| -rw-r--r-- | app/controllers/api/v1/internals/application_controller.rb | 19 | ||||
| -rw-r--r-- | app/controllers/api/v1/internals/compliance_check_sets_controller.rb | 11 | ||||
| -rw-r--r-- | app/models/compliance_check_set.rb | 2 | ||||
| -rw-r--r-- | config/locales/compliance_check_sets.en.yml | 2 | ||||
| -rw-r--r-- | db/schema.rb | 43 | ||||
| -rw-r--r-- | spec/controllers/api/v1/compliance_check_sets_controller_spec.rb | 37 | ||||
| -rw-r--r-- | spec/controllers/api/v1/internals/compliance_check_sets_controller_spec.rb | 52 | ||||
| -rw-r--r-- | spec/controllers/api/v1/internals/netex_imports_controller_spec.rb | 42 | ||||
| -rw-r--r-- | spec/controllers/compliance_check_sets_controller_spec.rb | 22 | ||||
| -rw-r--r-- | spec/support/shared_context.rb | 16 | 
10 files changed, 147 insertions, 99 deletions
| diff --git a/app/controllers/api/v1/internals/application_controller.rb b/app/controllers/api/v1/internals/application_controller.rb index 77b74f5f6..ab9daf4f7 100644 --- a/app/controllers/api/v1/internals/application_controller.rb +++ b/app/controllers/api/v1/internals/application_controller.rb @@ -4,11 +4,24 @@ module Api        class ApplicationController < ActionController::Base          respond_to :json          layout false -        before_action :authenticate +        before_action :require_token + +        def require_token +          authenticate_token || render_unauthorized("Access denied") +        end + +        protected + +        def render_unauthorized(message) +          errors = { errors: [ { detail: message } ] } +          render json: errors, status: :unauthorized +        end          private -        def authenticate -          authenticate_with_http_token { |token| Rails.application.secrets.api_token == token } +        def authenticate_token +          authenticate_with_http_token do |token| +            return true if Rails.application.secrets.api_token == token +          end          end        end      end diff --git a/app/controllers/api/v1/internals/compliance_check_sets_controller.rb b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb index db92c3fad..08965989a 100644 --- a/app/controllers/api/v1/internals/compliance_check_sets_controller.rb +++ b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb @@ -19,7 +19,9 @@ module Api          def notify_parent            find_compliance_check_set -          if  @compliance_check_set.notify_parent && @compliance_check_set.parent +          check_parent +           +          if  @compliance_check_set.notify_parent              render json: {                status: "ok",                message:"#{@compliance_check_set.parent_type} (id: #{@compliance_check_set.parent_id}) successfully notified at #{l(@compliance_check_set.notified_parent_at)}" @@ -31,6 +33,13 @@ module Api          private +        def check_parent +          unless @compliance_check_set.parent +            render json: {status: "error", message: I18n.t('compliance_check_sets.errors.no_parent') } +            finish_action! +          end +        end +          def find_compliance_check_set            @compliance_check_set = ComplianceCheckSet.find(params[:id])          rescue ActiveRecord::RecordNotFound diff --git a/app/models/compliance_check_set.rb b/app/models/compliance_check_set.rb index 7c55561af..020100f4a 100644 --- a/app/models/compliance_check_set.rb +++ b/app/models/compliance_check_set.rb @@ -23,8 +23,6 @@ class ComplianceCheckSet < ActiveRecord::Base      if parent        # parent.child_change        update(notified_parent_at: DateTime.now) -    else -      errors.add(:base, I18n.t('compliance_check_sets.errors.no_parent'))      end    end diff --git a/config/locales/compliance_check_sets.en.yml b/config/locales/compliance_check_sets.en.yml index 8023da0f7..49323c91f 100644 --- a/config/locales/compliance_check_sets.en.yml +++ b/config/locales/compliance_check_sets.en.yml @@ -23,7 +23,7 @@ en:        table_explanation: "These controls apply to all imported data and condition the construction of your organization's offer."        metrics: "%{ok_count} ok, %{error_count} errors, %{warning_count} warnings, %{uncheck_count} n/a"      errors: -      no_parent: "The compliance check set doesn't any parent" +      no_parent: "The compliance check set doesn't have any parent"    activerecord:      attributes:        compliance_check_set: diff --git a/db/schema.rb b/db/schema.rb index c94b43da1..4a04dac26 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@  #  # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171128112629) do +ActiveRecord::Schema.define(version: 20171130180144) do    # These are extensions that must be enabled in order to support this database    enable_extension "plpgsql" @@ -280,22 +280,6 @@ ActiveRecord::Schema.define(version: 20171128112629) do    add_index "connection_links", ["objectid"], name: "connection_links_objectid_key", unique: true, using: :btree -  create_table "delayed_jobs", id: :bigserial, force: :cascade do |t| -    t.integer  "priority",               default: 0 -    t.integer  "attempts",               default: 0 -    t.text     "handler" -    t.text     "last_error" -    t.datetime "run_at" -    t.datetime "locked_at" -    t.datetime "failed_at" -    t.string   "locked_by",  limit: 255 -    t.string   "queue",      limit: 255 -    t.datetime "created_at" -    t.datetime "updated_at" -  end - -  add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree -    create_table "exports", id: :bigserial, force: :cascade do |t|      t.integer  "referential_id",  limit: 8      t.string   "status" @@ -415,7 +399,7 @@ ActiveRecord::Schema.define(version: 20171128112629) do      t.datetime "started_at"      t.datetime "ended_at"      t.string   "token_download" -    t.string   "type",                  limit: 255 +    t.string   "type"      t.integer  "parent_id",             limit: 8      t.string   "parent_type"      t.datetime "notified_parent_at" @@ -558,11 +542,6 @@ ActiveRecord::Schema.define(version: 20171128112629) do    add_index "networks", ["objectid"], name: "networks_objectid_key", unique: true, using: :btree    add_index "networks", ["registration_number"], name: "networks_registration_number_key", using: :btree -  create_table "object_id_factories", id: :bigserial, force: :cascade do |t| -    t.datetime "created_at", null: false -    t.datetime "updated_at", null: false -  end -    create_table "organisations", id: :bigserial, force: :cascade do |t|      t.string   "name"      t.datetime "created_at" @@ -729,7 +708,7 @@ ActiveRecord::Schema.define(version: 20171128112629) do    create_table "stop_areas", id: :bigserial, force: :cascade do |t|      t.integer  "parent_id",                       limit: 8 -    t.string   "objectid",                                                              null: false +    t.string   "objectid",                                                            null: false      t.integer  "object_version",                  limit: 8      t.string   "name"      t.string   "comment" @@ -737,8 +716,8 @@ ActiveRecord::Schema.define(version: 20171128112629) do      t.string   "registration_number"      t.string   "nearest_topic_name"      t.integer  "fare_code" -    t.decimal  "longitude",                                   precision: 19, scale: 16 -    t.decimal  "latitude",                                    precision: 19, scale: 16 +    t.decimal  "longitude",                                 precision: 19, scale: 16 +    t.decimal  "latitude",                                  precision: 19, scale: 16      t.string   "long_lat_type"      t.string   "country_code"      t.string   "street_name" @@ -756,7 +735,7 @@ ActiveRecord::Schema.define(version: 20171128112629) do      t.datetime "deleted_at"      t.datetime "created_at"      t.datetime "updated_at" -    t.string   "stif_type",                       limit: 255 +    t.string   "stif_type"    end    add_index "stop_areas", ["name"], name: "index_stop_areas_on_name", using: :btree @@ -826,17 +805,17 @@ ActiveRecord::Schema.define(version: 20171128112629) do    add_index "time_table_periods", ["time_table_id"], name: "index_time_table_periods_on_time_table_id", using: :btree    create_table "time_tables", id: :bigserial, force: :cascade do |t| -    t.string   "objectid",                                null: false -    t.integer  "object_version",  limit: 8,   default: 1 +    t.string   "objectid",                              null: false +    t.integer  "object_version",  limit: 8, default: 1      t.string   "version"      t.string   "comment" -    t.integer  "int_day_types",               default: 0 +    t.integer  "int_day_types",             default: 0      t.date     "start_date"      t.date     "end_date"      t.integer  "calendar_id",     limit: 8      t.datetime "created_at"      t.datetime "updated_at" -    t.string   "color",           limit: 255 +    t.string   "color"      t.integer  "created_from_id", limit: 8      t.string   "checksum"      t.text     "checksum_source" @@ -991,9 +970,7 @@ ActiveRecord::Schema.define(version: 20171128112629) do    add_foreign_key "compliance_controls", "compliance_control_blocks"    add_foreign_key "compliance_controls", "compliance_control_sets"    add_foreign_key "group_of_lines_lines", "group_of_lines", name: "groupofline_group_fkey", on_delete: :cascade -  add_foreign_key "journey_frequencies", "timebands", name: "journey_frequencies_timeband_id_fk", on_delete: :nullify    add_foreign_key "journey_frequencies", "timebands", on_delete: :nullify -  add_foreign_key "journey_frequencies", "vehicle_journeys", name: "journey_frequencies_vehicle_journey_id_fk", on_delete: :nullify    add_foreign_key "journey_frequencies", "vehicle_journeys", on_delete: :nullify    add_foreign_key "journey_patterns", "routes", name: "jp_route_fkey", on_delete: :cascade    add_foreign_key "journey_patterns", "stop_points", column: "arrival_stop_point_id", name: "arrival_point_fkey", on_delete: :nullify diff --git a/spec/controllers/api/v1/compliance_check_sets_controller_spec.rb b/spec/controllers/api/v1/compliance_check_sets_controller_spec.rb deleted file mode 100644 index 1c3784807..000000000 --- a/spec/controllers/api/v1/compliance_check_sets_controller_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -RSpec.describe Api::V1::ComplianceCheckSetsController, type: :controller do -  include_context 'iboo authenticated api user' - -  describe "POST #validate" do -    let(:check_set) { create(:compliance_check_set) } - -    it "calls #update_status on the ComplianceCheckSet" do -      expect_any_instance_of(ComplianceCheckSet).to receive(:update_status) - -      patch :validated, id: check_set.id -    end - -    context "responds with" do -      render_views - -      it "object JSON on #update_status true" do -        allow_any_instance_of( -          ComplianceCheckSet -        ).to receive(:update_status).and_return(true) - -        patch :validated, id: check_set.id - -        expect(JSON.parse(response.body)['id']).to eq(check_set.id) -      end - -      it "error JSON on #update_status false" do -        allow_any_instance_of( -          ComplianceCheckSet -        ).to receive(:update_status).and_return(false) - -        patch :validated, id: check_set.id - -        expect(response.body).to include('error') -      end -    end -  end -end diff --git a/spec/controllers/api/v1/internals/compliance_check_sets_controller_spec.rb b/spec/controllers/api/v1/internals/compliance_check_sets_controller_spec.rb new file mode 100644 index 000000000..90401611c --- /dev/null +++ b/spec/controllers/api/v1/internals/compliance_check_sets_controller_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' + +RSpec.describe Api::V1::Internals::ComplianceCheckSetsController, type: :controller do +  let(:check_set_1) { create :compliance_check_set } +  let(:check_set_2) { create :compliance_check_set } + +  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: check_set_1.id +        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: check_set_2.id +        end + +        it 'should be successful' do +          expect(response).to have_http_status 200 +        end + +        describe "that has a parent" do   +          xit "calls #notify_parent on the import" do +            expect(check_set_2.reload.notified_parent_at).not_to be_nil +          end +        end + +        describe "that does not have a parent" do +          xit "should not be successful" do +            expect(response.body).to include("error") +          end +        end +         +      end + +      describe "with non existing record" do +        it "should throw an error" do +          get :notify_parent, id: 47 +          expect(response.body).to include("error") +        end +      end +    end +  end +end diff --git a/spec/controllers/api/v1/internals/netex_imports_controller_spec.rb b/spec/controllers/api/v1/internals/netex_imports_controller_spec.rb new file mode 100644 index 000000000..1fb898c06 --- /dev/null +++ b/spec/controllers/api/v1/internals/netex_imports_controller_spec.rb @@ -0,0 +1,42 @@ +RSpec.describe Api::V1::Internals::NetexImportsController, type: :controller do +  let(:import_1) { create :netex_import } +  let(:import_2) { create :netex_import } + +  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: import_1.id +        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: import_2.id +        end + +        it 'should be successful' do +          expect(response).to have_http_status 200 +        end +         +        it "calls #notify_parent on the import" do +          expect(import_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 +          expect(response.body).to include("error") +        end +      end +    end +  end +end + diff --git a/spec/controllers/compliance_check_sets_controller_spec.rb b/spec/controllers/compliance_check_sets_controller_spec.rb deleted file mode 100644 index 3ddb1dad1..000000000 --- a/spec/controllers/compliance_check_sets_controller_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'rails_helper' - -RSpec.describe ComplianceCheckSetsController, type: :controller do -  login_user - -  let(:compliance_check_set) { create :compliance_check_set } - -  describe "GET executed" do -    it 'should be successful' do -      get :executed, workbench_id: compliance_check_set.workbench.id, id: compliance_check_set.id -      expect(response).to be_success -    end -  end - -  describe "GET index" do -    it 'should be successful' do -      get :index, workbench_id: compliance_check_set.workbench.id, id: compliance_check_set.id -      expect(response).to be_success -    end -  end - -end diff --git a/spec/support/shared_context.rb b/spec/support/shared_context.rb index e9b0025a2..5e472eb85 100644 --- a/spec/support/shared_context.rb +++ b/spec/support/shared_context.rb @@ -13,3 +13,19 @@ shared_context 'iboo wrong authorisation api user' do      request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials('fake code', api_key.token)    end  end + +shared_context 'iboo authenticated internal api' do +  let(:api_key) { Rails.application.secrets.api_token } + +  before do +    request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(api_key) +  end +end + +shared_context 'iboo wrong authorisation internal api' do +  let(:api_key) { "false_api_token" } + +  before do +    request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(api_key) +  end +end | 
