diff options
| -rw-r--r-- | app/models/api/v1/api_key.rb | 22 | ||||
| -rw-r--r-- | db/migrate/20170817122914_add_organisation_to_api_keys.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 8 | ||||
| -rw-r--r-- | spec/factories/api_keys.rb | 4 | ||||
| -rw-r--r-- | spec/models/api/v1/api_key_spec.rb | 39 | ||||
| -rw-r--r-- | spec/support/api_key.rb | 2 | ||||
| -rw-r--r-- | spec/support/referential.rb | 1 |
7 files changed, 46 insertions, 35 deletions
diff --git a/app/models/api/v1/api_key.rb b/app/models/api/v1/api_key.rb index e1a7ab5a4..e1cc2cc3b 100644 --- a/app/models/api/v1/api_key.rb +++ b/app/models/api/v1/api_key.rb @@ -3,19 +3,27 @@ module Api class ApiKey < ::ActiveRecord::Base before_create :generate_access_token belongs_to :referential, :class_name => '::Referential' - validates_presence_of :referential + belongs_to :organisation, :class_name => '::Organisation' + + validates_presence_of :organisation class << self def from(referential, name:) find_or_create_by!(name: name, referential: referential) end - def model_name - ActiveModel::Name.new Api::V1, self.name.demodulize - end + def referential_from_token(token) array = token.split('-') - return nil unless array.size==2 - ::Referential.find( array.first) + if !array.first.empty? && array.size > 1 + ::Referential.find array.first + end + end + + def organisation_from_token(token) + array = token.split('-') + if !array[1].empty? && array.size > 1 + ::Organisation.find array[1] + end end end @@ -28,7 +36,7 @@ module Api private def generate_access_token begin - self.token = "#{referential_id}-#{SecureRandom.hex}" + self.token = "#{referential_id}-#{organisation_id}-#{SecureRandom.hex}" end while self.class.exists?(:token => self.token) end end diff --git a/db/migrate/20170817122914_add_organisation_to_api_keys.rb b/db/migrate/20170817122914_add_organisation_to_api_keys.rb new file mode 100644 index 000000000..14c742c87 --- /dev/null +++ b/db/migrate/20170817122914_add_organisation_to_api_keys.rb @@ -0,0 +1,5 @@ +class AddOrganisationToApiKeys < ActiveRecord::Migration + def change + add_reference :api_keys, :organisation, index: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 8816bab2f..e9344e56c 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: 20170816104020) do +ActiveRecord::Schema.define(version: 20170817122914) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -72,13 +72,16 @@ ActiveRecord::Schema.define(version: 20170816104020) do add_index "access_points", ["objectid"], name: "access_points_objectid_key", unique: true, using: :btree create_table "api_keys", id: :bigserial, force: :cascade do |t| - t.integer "referential_id", limit: 8 + t.integer "referential_id", limit: 8 t.string "token" t.string "name" t.datetime "created_at" t.datetime "updated_at" + t.integer "organisation_id" end + add_index "api_keys", ["organisation_id"], name: "index_api_keys_on_organisation_id", using: :btree + create_table "calendars", id: :bigserial, force: :cascade do |t| t.string "name" t.string "short_name" @@ -854,6 +857,7 @@ ActiveRecord::Schema.define(version: 20170816104020) do add_index "workbenches", ["stop_area_referential_id"], name: "index_workbenches_on_stop_area_referential_id", using: :btree add_foreign_key "access_links", "access_points", name: "aclk_acpt_fkey" + add_foreign_key "api_keys", "organisations" add_foreign_key "group_of_lines_lines", "group_of_lines", name: "groupofline_group_fkey", on_delete: :cascade add_foreign_key "journey_frequencies", "timebands", on_delete: :nullify add_foreign_key "journey_frequencies", "vehicle_journeys", on_delete: :nullify diff --git a/spec/factories/api_keys.rb b/spec/factories/api_keys.rb index bd31edecc..963938c64 100644 --- a/spec/factories/api_keys.rb +++ b/spec/factories/api_keys.rb @@ -1,6 +1,8 @@ FactoryGirl.define do factory :api_key, class: Api::V1::ApiKey do - token { "#{referential.id}-#{SecureRandom.hex}" } + name { SecureRandom.urlsafe_base64 } + token { "#{referential_id}-#{organisation_id}-#{SecureRandom.hex}" } referential + organisation end end diff --git a/spec/models/api/v1/api_key_spec.rb b/spec/models/api/v1/api_key_spec.rb index 5f39a65e4..b700429d3 100644 --- a/spec/models/api/v1/api_key_spec.rb +++ b/spec/models/api/v1/api_key_spec.rb @@ -1,34 +1,25 @@ -describe Api::V1::ApiKey, :type => :model do +require 'rails_helper' - let(:referential){ create :referential } +RSpec.describe Api::V1::ApiKey, type: :model do + subject { create(:api_key) } - subject { described_class.create( :name => "test", :referential => referential)} + it { should validate_presence_of :organisation } - it "validity test" do - expect_it.to be_valid - expect(subject.referential).to eq(referential) + it 'should have a valid factory' do + expect(build(:api_key)).to be_valid end - context 'Creation' do - let( :name ){ SecureRandom.urlsafe_base64 } - - it 'can be created from a referential with a name, iff needed' do - # 1st time create a new record - expect{ described_class.from(referential, name: name) }.to change{ described_class.count }.by(1) - expect( described_class.last.attributes.values_at(*%w{referential_id name}) ).to eq([ - referential.id, name - ]) - - # 2nd time get the same record - expect{ described_class.from(referential, name: name) }.not_to change{ described_class.count } - expect( described_class.last.attributes.values_at(*%w{referential_id name}) ).to eq([ - referential.id, name - ]) + describe '#referential_from_token' do + it 'should return referential' do + referential = Api::V1::ApiKey.referential_from_token(subject.token) + expect(referential).to eq(subject.referential) end + end - it 'cannot be created without a referential' do - expect{ described_class.from(nil, name:name) rescue nil }.not_to change{ described_class.count } + describe '#organisation_from_token' do + it 'should return organisation' do + organisation = Api::V1::ApiKey.organisation_from_token(subject.token) + expect(organisation).to eq(subject.organisation) end end end - diff --git a/spec/support/api_key.rb b/spec/support/api_key.rb index 561e1f796..5aaa31217 100644 --- a/spec/support/api_key.rb +++ b/spec/support/api_key.rb @@ -5,7 +5,7 @@ module ApiKeyHelper end def get_api_key - Api::V1::ApiKey.first_or_create( :referential_id => referential.id, :name => "test") + Api::V1::ApiKey.first_or_create(referential: referential, organisation: organisation) end def config_formatted_request_with_authorization( format) request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( get_api_key.token) diff --git a/spec/support/referential.rb b/spec/support/referential.rb index 57b510f69..c431856b8 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -12,6 +12,7 @@ module ReferentialHelper base.class_eval do extend ClassMethods alias_method :referential, :first_referential + alias_method :organisation, :first_organisation end end |
