WpUser additional specs

This commit is contained in:
erwanlr
2013-03-30 19:08:47 +01:00
parent 21f5acf62f
commit a9b25edafe
5 changed files with 53 additions and 6 deletions

View File

@@ -43,6 +43,7 @@ module Typhoeus
body_hash != error_404_hash && body_hash != homepage_hash body_hash != error_404_hash && body_hash != homepage_hash
end end
end end
end end

View File

@@ -7,6 +7,7 @@ class WpUser < WpItem
attr_accessor :id, :login, :display_name, :password attr_accessor :id, :login, :display_name, :password
# @return [ Array<Symbol> ]
def allowed_options; [:id, :login, :display_name, :password] end def allowed_options; [:id, :login, :display_name, :password] end
# @return [ URI ] The uri to the auhor page # @return [ URI ] The uri to the auhor page
@@ -32,11 +33,15 @@ class WpUser < WpItem
end end
# @param [ WpUser ] other # @param [ WpUser ] other
#
# @return [ Boolean ]
def ==(other) def ==(other)
self === other self === other
end end
# @param [ WpUser ] other # @param [ WpUser ] other
#
# @return [ Boolean ]
def ===(other) def ===(other)
id === other.id && login === other.login id === other.id && login === other.login
end end

View File

@@ -22,13 +22,13 @@ class WpUser < WpItem
if response.code == 301 # login in location? if response.code == 301 # login in location?
location = response.headers_hash['Location'] location = response.headers_hash['Location']
@login = WpUser::Existable.login_from_author_pattern(location) @login = Existable.login_from_author_pattern(location)
@display_name = WpUser::Existable.display_name_from_body( @display_name = Existable.display_name_from_body(
Browser.instance.get(location).body Browser.instance.get(location).body
) )
elsif response.code == 200 # login in body? elsif response.code == 200 # login in body?
@login = WpUser::Existable.login_from_body(response.body) @login = Existable.login_from_body(response.body)
@display_name = WpUser::Existable.display_name_from_body(response.body) @display_name = Existable.display_name_from_body(response.body)
end end
end end
private :load_from_response private :load_from_response
@@ -55,16 +55,22 @@ class WpUser < WpItem
login login
end end
# @note Some bodies are encoded in ASCII-8BIT, and Nokogiri doesn't support it
# So it's forced to UTF-8 when this encoding is detected
#
# @param [ String ] body # @param [ String ] body
# #
# @return [ String ] The display_name # @return [ String ] The display_name
def self.display_name_from_body(body) def self.display_name_from_body(body)
if title_tag = body[%r{<title>([^<]+)</title>}i, 1] if title_tag = body[%r{<title>([^<]+)</title>}i, 1]
title_tag.force_encoding('UTF-8') if title_tag.encoding == Encoding::ASCII_8BIT
title_tag = Nokogiri::HTML::DocumentFragment.parse(title_tag).to_s title_tag = Nokogiri::HTML::DocumentFragment.parse(title_tag).to_s
# &amp; are not decoded with Nokogiri # &amp; are not decoded with Nokogiri
title_tag.sub!('&amp;', '&') title_tag.sub!('&amp;', '&')
return title_tag[%r{([^|«]+) }, 1] name = title_tag[%r{([^|«]+) }, 1]
return name.strip if name
end end
end end

View File

@@ -33,6 +33,34 @@ describe WpUser do
end end
end end
describe '#to_s' do
after do
subject.id = 1
subject.to_s.should == @expected
end
it 'returns @id' do
@expected = '1'
end
context 'when @login' do
it 'returns @id | @login' do
subject.login = 'admin'
@expected = '1 | admin'
end
context 'when @display_name' do
it 'returns @id | @login | @display_name' do
subject.login = 'admin'
subject.display_name = 'real name'
@expected = '1 | admin | real name'
end
end
end
end
describe '#<=>' do describe '#<=>' do
it 'bases the comparaison on the :id' do it 'bases the comparaison on the :id' do
wp_user.id = 1 wp_user.id = 1

View File

@@ -69,6 +69,13 @@ shared_examples 'WpUser::Existable' do
end end
end end
context 'when the body is an ASCII-8BIT' do
it 'return the correct display_name' do
@body = '<title>its me | wordpress</title>'.encode('ASCII-8BIT')
@expected = 'its me'
end
end
context 'when pattern is found' do context 'when pattern is found' do
context 'when unencoded extra chars' do context 'when unencoded extra chars' do
it 'returns the display_name w/o extra chars' do it 'returns the display_name w/o extra chars' do
@@ -91,7 +98,7 @@ shared_examples 'WpUser::Existable' do
end end
end end
it 'decodes entities in display_name' do it 'decodes entities' do
@body = '<title>user &amp; nickname &#124; Wordpress-3.5.1</title>' @body = '<title>user &amp; nickname &#124; Wordpress-3.5.1</title>'
@expected = 'user & nickname' @expected = 'user & nickname'
end end