2013-02-25 19:05:37 -05:00
|
|
|
require 'helper'
|
2011-05-17 10:53:32 -04:00
|
|
|
|
|
|
|
describe OmniAuth::AuthHash do
|
2014-01-15 23:00:46 -05:00
|
|
|
subject { OmniAuth::AuthHash.new }
|
|
|
|
it 'converts a supplied info key into an InfoHash object' do
|
2011-09-22 13:56:36 -04:00
|
|
|
subject.info = {:first_name => 'Awesome'}
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject.info).to be_kind_of(OmniAuth::AuthHash::InfoHash)
|
|
|
|
expect(subject.info.first_name).to eq('Awesome')
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
|
2016-05-23 20:01:42 -04:00
|
|
|
it 'does not try to parse `string` as InfoHash' do
|
2017-02-12 03:20:08 -05:00
|
|
|
subject.weird_field = {:info => 'string'}
|
2016-05-23 20:01:42 -04:00
|
|
|
expect(subject.weird_field.info).to eq 'string'
|
|
|
|
end
|
|
|
|
|
2020-03-02 15:09:14 -05:00
|
|
|
it 'has a subkey_class' do
|
|
|
|
expect(OmniAuth::AuthHash.subkey_class).to eq Hashie::Mash
|
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
describe '#valid?' do
|
|
|
|
subject { OmniAuth::AuthHash.new(:uid => '123', :provider => 'example', :info => {:name => 'Steven'}) }
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'is valid with the right parameters' do
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject).to be_valid
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'requires a uid' do
|
2011-05-17 10:53:32 -04:00
|
|
|
subject.uid = nil
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject).not_to be_valid
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'requires a provider' do
|
2011-05-17 10:53:32 -04:00
|
|
|
subject.provider = nil
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject).not_to be_valid
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'requires a name in the user info hash' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
2014-06-03 13:37:46 -04:00
|
|
|
expect(subject).not_to be_valid
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
describe '#name' do
|
|
|
|
subject do
|
2014-08-18 07:21:39 -04:00
|
|
|
OmniAuth::AuthHash.new(:info => {
|
|
|
|
:name => 'Phillip J. Fry',
|
|
|
|
:first_name => 'Phillip',
|
|
|
|
:last_name => 'Fry',
|
|
|
|
:nickname => 'meatbag',
|
2015-12-19 06:10:33 -05:00
|
|
|
:email => 'fry@planetexpress.com'
|
2014-08-18 07:21:39 -04:00
|
|
|
})
|
2014-01-15 23:00:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to the name key' do
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject.info.name).to eq('Phillip J. Fry')
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'falls back to go to first_name last_name concatenation' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject.info.name).to eq('Phillip Fry')
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'displays only a first or last name if only that is available' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
2011-09-22 13:56:36 -04:00
|
|
|
subject.info.first_name = nil
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject.info.name).to eq('Fry')
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'displays the nickname if no name, first, or last is available' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
2017-09-28 13:07:45 -04:00
|
|
|
%w[first_name last_name].each { |k| subject.info[k] = nil }
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject.info.name).to eq('meatbag')
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'displays the email if no name, first, last, or nick is available' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
2017-09-28 13:07:45 -04:00
|
|
|
%w[first_name last_name nickname].each { |k| subject.info[k] = nil }
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject.info.name).to eq('fry@planetexpress.com')
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
describe '#to_hash' do
|
2014-01-15 23:00:57 -05:00
|
|
|
subject { OmniAuth::AuthHash.new(:uid => '123', :provider => 'test', :name => 'Example User') }
|
2014-01-15 23:00:46 -05:00
|
|
|
let(:hash) { subject.to_hash }
|
2011-09-22 13:56:36 -04:00
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'is a plain old hash' do
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(hash.class).to eq(::Hash)
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'has string keys' do
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(hash.keys).to be_include('uid')
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'converts an info hash as well' do
|
2014-01-15 23:00:57 -05:00
|
|
|
subject.info = {:first_name => 'Example', :last_name => 'User'}
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(subject.info.class).to eq(OmniAuth::AuthHash::InfoHash)
|
|
|
|
expect(subject.to_hash['info'].class).to eq(::Hash)
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:00:46 -05:00
|
|
|
it 'supplies the calculated name in the converted hash' do
|
2014-01-15 23:00:57 -05:00
|
|
|
subject.info = {:first_name => 'Examplar', :last_name => 'User'}
|
|
|
|
expect(hash['info']['name']).to eq('Examplar User')
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
2011-10-15 04:15:45 -04:00
|
|
|
|
2012-10-10 04:32:55 -04:00
|
|
|
it "does not pollute the URL hash with 'name' etc" do
|
2014-01-15 23:00:46 -05:00
|
|
|
subject.info = {'urls' => {'Homepage' => 'http://homepage.com'}}
|
|
|
|
expect(subject.to_hash['info']['urls']).to eq('Homepage' => 'http://homepage.com')
|
2011-10-15 04:15:45 -04:00
|
|
|
end
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe OmniAuth::AuthHash::InfoHash do
|
2014-01-15 23:00:46 -05:00
|
|
|
describe '#valid?' do
|
|
|
|
it 'is valid if there is a name' do
|
2012-10-10 04:32:55 -04:00
|
|
|
expect(OmniAuth::AuthHash::InfoHash.new(:name => 'Awesome')).to be_valid
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
end
|
2017-02-03 16:36:11 -05:00
|
|
|
|
2020-03-02 15:09:14 -05:00
|
|
|
it 'has a subkey_class' do
|
|
|
|
expect(OmniAuth::AuthHash::InfoHash.subkey_class).to eq Hashie::Mash
|
|
|
|
end
|
|
|
|
|
2017-02-03 16:36:11 -05:00
|
|
|
require 'hashie/version'
|
|
|
|
if Gem::Version.new(Hashie::VERSION) >= Gem::Version.new('3.5.1')
|
|
|
|
context 'with Hashie 3.5.1+' do
|
|
|
|
around(:each) do |example|
|
|
|
|
original_logger = Hashie.logger
|
|
|
|
example.run
|
|
|
|
Hashie.logger = original_logger
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not log anything in Hashie 3.5.1+' do
|
|
|
|
logger = double('Logger')
|
|
|
|
expect(logger).not_to receive(:warn)
|
|
|
|
|
|
|
|
Hashie.logger = logger
|
|
|
|
|
|
|
|
subject.name = 'test'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
end
|