2011-05-17 10:53:32 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe OmniAuth::AuthHash do
|
|
|
|
subject{ OmniAuth::AuthHash.new }
|
2011-09-22 13:56:36 -04:00
|
|
|
it 'should convert a supplied info key into an InfoHash object' do
|
|
|
|
subject.info = {:first_name => 'Awesome'}
|
|
|
|
subject.info.should be_kind_of(OmniAuth::AuthHash::InfoHash)
|
|
|
|
subject.info.first_name.should == 'Awesome'
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#valid?' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject{ OmniAuth::AuthHash.new(:uid => '123', :provider => 'example', :info => {:name => 'Steven'}) }
|
2011-05-17 10:53:32 -04:00
|
|
|
|
|
|
|
it 'should be valid with the right parameters' do
|
|
|
|
subject.should be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should require a uid' do
|
|
|
|
subject.uid = nil
|
|
|
|
subject.should_not be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should require a provider' do
|
|
|
|
subject.provider = nil
|
|
|
|
subject.should_not be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should require a name in the user info hash' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
2011-05-17 10:53:32 -04:00
|
|
|
subject.should_not be_valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-22 13:56:36 -04:00
|
|
|
describe '#name' do
|
|
|
|
subject{ OmniAuth::AuthHash.new(
|
|
|
|
:info => {
|
2011-09-27 13:36:05 -04:00
|
|
|
:name => 'Phillip J. Fry',
|
2011-05-17 10:53:32 -04:00
|
|
|
:first_name => 'Phillip',
|
|
|
|
:last_name => 'Fry',
|
|
|
|
:nickname => 'meatbag',
|
|
|
|
:email => 'fry@planetexpress.com'
|
2011-09-22 13:56:36 -04:00
|
|
|
})}
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2011-09-22 13:56:36 -04:00
|
|
|
it 'should default to the name key' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name.should == 'Phillip J. Fry'
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2011-09-22 13:56:36 -04:00
|
|
|
it 'should fall back to go to first_name last_name concatenation' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
|
|
|
subject.info.name.should == 'Phillip Fry'
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2011-09-22 13:56:36 -04:00
|
|
|
it 'should display 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
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name.should == 'Fry'
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2011-09-22 13:56:36 -04:00
|
|
|
it 'should display the nickname if no name, first, or last is available' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
2011-09-22 13:56:36 -04:00
|
|
|
%w(first_name last_name).each{|k| subject.info[k] = nil}
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name.should == 'meatbag'
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should display the email if no name, first, last, or nick is available' do
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name = nil
|
2011-09-22 13:56:36 -04:00
|
|
|
%w(first_name last_name nickname).each{|k| subject.info[k] = nil}
|
2011-09-27 13:36:05 -04:00
|
|
|
subject.info.name.should == 'fry@planetexpress.com'
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#to_hash' do
|
|
|
|
subject{ OmniAuth::AuthHash.new(:uid => '123', :provider => 'test', :name => 'Bob Example')}
|
|
|
|
let(:hash){ subject.to_hash }
|
|
|
|
|
|
|
|
it 'should be a plain old hash' do
|
|
|
|
hash.class.should == ::Hash
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should have string keys' do
|
|
|
|
hash.keys.should be_include('uid')
|
|
|
|
end
|
2011-05-17 10:53:32 -04:00
|
|
|
|
2011-09-22 13:56:36 -04:00
|
|
|
it 'should convert an info hash as well' do
|
|
|
|
subject.info = {:first_name => 'Bob', :last_name => 'Example'}
|
|
|
|
subject.info.class.should == OmniAuth::AuthHash::InfoHash
|
|
|
|
subject.to_hash['info'].class.should == ::Hash
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should supply the calculated name in the converted hash' do
|
|
|
|
subject.info = {:first_name => 'Bob', :last_name => 'Examplar'}
|
2011-09-27 13:36:05 -04:00
|
|
|
hash['info']['name'].should == 'Bob Examplar'
|
2011-09-22 13:56:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe OmniAuth::AuthHash::InfoHash do
|
|
|
|
describe '#valid?' do
|
|
|
|
it 'should be valid if there is a name' do
|
|
|
|
OmniAuth::AuthHash::InfoHash.new(:name => 'Awesome').should be_valid
|
2011-05-17 10:53:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|