Skip to content

Commit

Permalink
Beefed up the AuthHash, pulled name out to the root, specs for #to_hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Sep 22, 2011
1 parent ba4bd3e commit 5733f9a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 50 deletions.
40 changes: 26 additions & 14 deletions lib/omniauth/auth_hash.rb
@@ -1,36 +1,48 @@
require 'hashie/mash'

module OmniAuth
# The AuthHash is a normalized schema returned by all OmniAuth
# strategies. It maps as much user information as the provider
# is able to provide into the InfoHash (stored as the `'info'`
# key).
class AuthHash < Hashie::Mash

# Tells you if this is considered to be a valid
# OmniAuth AuthHash. The requirements for that
# are that it has a provider name, a uid, and a
# valid user_info hash. See UserInfo#valid? for
# valid info hash. See InfoHash#valid? for
# more details there.
def valid?
uid? && provider? && (!user_info? || user_info.valid?)
uid? && provider? && name?
end

def name
return self[:name] if self[:name]
return nil unless info?
return "#{info.first_name} #{info.last_name}".strip if info.first_name? || info.last_name?
return info.nickname if info.nickname?
return info.email if info.email?
nil
end

def name?; !!name end

def regular_writer(key, value)
if key.to_s == 'user_info' && !value.is_a?(UserInfo)
value = UserInfo.new(value)
if key.to_s == 'info' && !value.is_a?(InfoHash)
value = InfoHash.new(value)
end
super
end

class UserInfo < Hashie::Mash
def to_hash
hash = super
hash['name'] ||= name
hash
end

class InfoHash < Hashie::Mash
def valid?
name?
end

def name
return self[:name] if name?
return "#{first_name} #{last_name}".strip if first_name? || last_name?
return nickname if nickname?
return email if email?
nil
end
end
end
end
100 changes: 64 additions & 36 deletions spec/omniauth/auth_hash_spec.rb
Expand Up @@ -2,14 +2,14 @@

describe OmniAuth::AuthHash do
subject{ OmniAuth::AuthHash.new }
it 'should convert a supplied user_info key into a UserInfo object' do
subject.user_info = {:name => 'Awesome'}
subject.user_info.should be_kind_of(OmniAuth::AuthHash::UserInfo)
subject.user_info.name.should == 'Awesome'
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'
end

describe '#valid?' do
subject{ OmniAuth::AuthHash.new(:uid => '123', :provider => 'example', :user_info => {:name => 'Steven'}) }
subject{ OmniAuth::AuthHash.new(:uid => '123', :provider => 'example', :name => 'Steven') }

it 'should be valid with the right parameters' do
subject.should be_valid
Expand All @@ -26,50 +26,78 @@
end

it 'should require a name in the user info hash' do
subject.user_info.name = nil
subject.name = nil
subject.should_not be_valid?
end
end

describe OmniAuth::AuthHash::UserInfo do
describe '#valid?' do
it 'should be valid if there is a name' do
OmniAuth::AuthHash::UserInfo.new(:name => 'Awesome').should be_valid
end
end

describe '#name' do
subject{ OmniAuth::AuthHash::UserInfo.new(
:name => 'Phillip J. Fry',
describe '#name' do
subject{ OmniAuth::AuthHash.new(
:name => 'Phillip J. Fry',
:info => {
:first_name => 'Phillip',
:last_name => 'Fry',
:nickname => 'meatbag',
:email => 'fry@planetexpress.com'
)}
})}

it 'should default to the name key' do
subject.name.should == 'Phillip J. Fry'
end
it 'should default to the name key' do
subject.name.should == 'Phillip J. Fry'
end

it 'should fall back to go to first_name last_name concatenation' do
subject.name = nil
subject.name.should == 'Phillip Fry'
end
it 'should fall back to go to first_name last_name concatenation' do
subject.name = nil
subject.name.should == 'Phillip Fry'
end

it 'should display only a first or last name if only that is available' do
subject.name = nil
subject.first_name = nil
subject.name.should == 'Fry'
end
it 'should display only a first or last name if only that is available' do
subject.name = nil
subject.info.first_name = nil
subject.name.should == 'Fry'
end

it 'should display the nickname if no name, first, or last is available' do
%w(name first_name last_name).each{|k| subject[k] = nil}
subject.name.should == 'meatbag'
end
it 'should display the nickname if no name, first, or last is available' do
subject.name = nil
%w(first_name last_name).each{|k| subject.info[k] = nil}
subject.name.should == 'meatbag'
end

it 'should display the email if no name, first, last, or nick is available' do
subject.name = nil
%w(first_name last_name nickname).each{|k| subject.info[k] = nil}
subject.name.should == 'fry@planetexpress.com'
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

it 'should display the email if no name, first, last, or nick is available' do
%w(name first_name last_name nickname).each{|k| subject[k] = nil}
subject.name.should == 'fry@planetexpress.com'
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.name = nil
subject.info = {:first_name => 'Bob', :last_name => 'Examplar'}
hash['name'].should == 'Bob Examplar'
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
end
end
end
Expand Down

0 comments on commit 5733f9a

Please sign in to comment.