diff options
Diffstat (limited to 'spec')
| -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 |
4 files changed, 20 insertions, 26 deletions
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 |
