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
end
end
end

View File

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

View File

@@ -22,13 +22,13 @@ class WpUser < WpItem
if response.code == 301 # login in location?
location = response.headers_hash['Location']
@login = WpUser::Existable.login_from_author_pattern(location)
@display_name = WpUser::Existable.display_name_from_body(
@login = Existable.login_from_author_pattern(location)
@display_name = Existable.display_name_from_body(
Browser.instance.get(location).body
)
elsif response.code == 200 # login in body?
@login = WpUser::Existable.login_from_body(response.body)
@display_name = WpUser::Existable.display_name_from_body(response.body)
@login = Existable.login_from_body(response.body)
@display_name = Existable.display_name_from_body(response.body)
end
end
private :load_from_response
@@ -55,16 +55,22 @@ class WpUser < WpItem
login
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
#
# @return [ String ] The display_name
def self.display_name_from_body(body)
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
# &amp; are not decoded with Nokogiri
title_tag.sub!('&amp;', '&')
return title_tag[%r{([^|«]+) }, 1]
name = title_tag[%r{([^|«]+) }, 1]
return name.strip if name
end
end

View File

@@ -33,6 +33,34 @@ describe WpUser do
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
it 'bases the comparaison on the :id' do
wp_user.id = 1

View File

@@ -69,6 +69,13 @@ shared_examples 'WpUser::Existable' do
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 unencoded extra chars' do
it 'returns the display_name w/o extra chars' do
@@ -91,7 +98,7 @@ shared_examples 'WpUser::Existable' do
end
end
it 'decodes entities in display_name' do
it 'decodes entities' do
@body = '<title>user &amp; nickname &#124; Wordpress-3.5.1</title>'
@expected = 'user & nickname'
end