WpUser::Existable specs
This commit is contained in:
@@ -18,7 +18,7 @@ class WpUser < WpItem
|
||||
end
|
||||
end
|
||||
|
||||
# @param [ Wpuser ] other
|
||||
# @param [ WpUser ] other
|
||||
def <=>(other)
|
||||
id <=> other.id
|
||||
end
|
||||
|
||||
@@ -3,13 +3,22 @@
|
||||
class WpUser < WpItem
|
||||
module Existable
|
||||
|
||||
# @param [ Typhoeus::Response ] response
|
||||
# @param [ Hash ] options
|
||||
#
|
||||
# @return [ Boolean ]
|
||||
def exists_from_response?(response, options = {})
|
||||
load_login_from_response(response)
|
||||
load_from_response(response)
|
||||
|
||||
@login ? true : false
|
||||
end
|
||||
|
||||
def load_login_from_response(response)
|
||||
# Load the login and display_name from the response
|
||||
#
|
||||
# @param [ Typhoeus::Response ] response
|
||||
#
|
||||
# @return [ void ]
|
||||
def load_from_response(response)
|
||||
if response.code == 301 # login in location?
|
||||
location = response.headers_hash['Location']
|
||||
|
||||
@@ -22,11 +31,18 @@ class WpUser < WpItem
|
||||
@display_name = WpUser::Existable.display_name_from_body(response.body)
|
||||
end
|
||||
end
|
||||
private :load_from_response
|
||||
|
||||
# @param [ String ] text
|
||||
#
|
||||
# @return [ String ] The login
|
||||
def self.login_from_author_pattern(text)
|
||||
text[%r{/author/([^/\b]+)/?}i, 1]
|
||||
end
|
||||
|
||||
# @param [ String ] body
|
||||
#
|
||||
# @return [ String ] The login
|
||||
def self.login_from_body(body)
|
||||
# Feed URL with Permalinks
|
||||
login = WpUser::Existable.login_from_author_pattern(body)
|
||||
@@ -39,11 +55,16 @@ class WpUser < WpItem
|
||||
login
|
||||
end
|
||||
|
||||
# @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.sub!('|', '|')
|
||||
title_tag = Nokogiri::HTML::DocumentFragment.parse(title_tag).to_s
|
||||
# & are not decoded with Nokogiri
|
||||
title_tag.sub!('&', '&')
|
||||
|
||||
return title_tag[%r{([^|]+) }, 1]
|
||||
return title_tag[%r{([^|«]+) }, 1]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe WpUser do
|
||||
it_behaves_like 'WpUser::Existable'
|
||||
|
||||
subject(:wp_user) { WpUser.new(uri, options) }
|
||||
let(:uri) { URI.parse('http://example.com') }
|
||||
let(:options) { {} }
|
||||
|
||||
36
spec/samples/common/models/wp_user/existable/admin.html
Normal file
36
spec/samples/common/models/wp_user/existable/admin.html
Normal file
@@ -0,0 +1,36 @@
|
||||
<!DOCTYPE html>
|
||||
<!--[if IE 6]>
|
||||
<html id="ie6" dir="ltr" lang="en-US">
|
||||
<![endif]-->
|
||||
<!--[if IE 7]>
|
||||
<html id="ie7" dir="ltr" lang="en-US">
|
||||
<![endif]-->
|
||||
<!--[if IE 8]>
|
||||
<html id="ie8" dir="ltr" lang="en-US">
|
||||
<![endif]-->
|
||||
<!--[if !(IE 6) | !(IE 7) | !(IE 8) ]><!-->
|
||||
<html dir="ltr" lang="en-US">
|
||||
<!--<![endif]-->
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>admin d-name | Wordpress 3.3.2</title>
|
||||
<link rel="profile" href="http://gmpg.org/xfn/11" />
|
||||
<link rel="stylesheet" type="text/css" media="all" href="http://lamp/wordpress-3.3.2/wp-content/themes/twentyeleven/style.css" />
|
||||
<link rel="pingback" href="http://lamp/wordpress-3.3.2/xmlrpc.php" />
|
||||
<!--[if lt IE 9]>
|
||||
<script src="http://lamp/wordpress-3.3.2/wp-content/themes/twentyeleven/js/html5.js" type="text/javascript"></script>
|
||||
<![endif]-->
|
||||
<meta name='robots' content='noindex,nofollow' />
|
||||
<link rel="alternate" type="application/rss+xml" title="Wordpress 3.3.2 » Feed" href="http://lamp/wordpress-3.3.2/?feed=rss2" />
|
||||
<link rel="alternate" type="application/rss+xml" title="Wordpress 3.3.2 » Comments Feed" href="http://lamp/wordpress-3.3.2/?feed=comments-rss2" />
|
||||
<link rel="alternate" type="application/rss+xml" title="Wordpress 3.3.2 » Posts by admin Feed" href="http://lamp/wordpress-3.3.2/?feed=rss2&author=1" />
|
||||
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://lamp/wordpress-3.3.2/xmlrpc.php?rsd" />
|
||||
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://lamp/wordpress-3.3.2/wp-includes/wlwmanifest.xml" />
|
||||
<meta name="generator" content="WordPress 3.3.2" />
|
||||
<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
|
||||
</head>
|
||||
|
||||
<body class="archive author author-admin author-1 single-author two-column right-sidebar">
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,145 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<!--[if IE 6]>
|
||||
<html id="ie6" dir="ltr" lang="en-US">
|
||||
<![endif]-->
|
||||
<!--[if IE 7]>
|
||||
<html id="ie7" dir="ltr" lang="en-US">
|
||||
<![endif]-->
|
||||
<!--[if IE 8]>
|
||||
<html id="ie8" dir="ltr" lang="en-US">
|
||||
<![endif]-->
|
||||
<!--[if !(IE 6) | !(IE 7) | !(IE 8) ]><!-->
|
||||
<html dir="ltr" lang="en-US">
|
||||
<!--<![endif]-->
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>admin | Wordpress 3.3.2</title>
|
||||
<link rel="profile" href="http://gmpg.org/xfn/11" />
|
||||
<link rel="stylesheet" type="text/css" media="all" href="http://lamp/wordpress-3.3.2/wp-content/themes/twentyeleven/style.css" />
|
||||
<link rel="pingback" href="http://lamp/wordpress-3.3.2/xmlrpc.php" />
|
||||
<!--[if lt IE 9]>
|
||||
<script src="http://lamp/wordpress-3.3.2/wp-content/themes/twentyeleven/js/html5.js" type="text/javascript"></script>
|
||||
<![endif]-->
|
||||
<meta name='robots' content='noindex,nofollow' />
|
||||
<link rel="alternate" type="application/rss+xml" title="Wordpress 3.3.2 » Feed" href="http://lamp/wordpress-3.3.2/?feed=rss2" />
|
||||
<link rel="alternate" type="application/rss+xml" title="Wordpress 3.3.2 » Comments Feed" href="http://lamp/wordpress-3.3.2/?feed=comments-rss2" />
|
||||
<link rel="alternate" type="application/rss+xml" title="Wordpress 3.3.2 » Posts by admin Feed" href="http://lamp/wordpress-3.3.2/?feed=rss2&author=1" />
|
||||
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://lamp/wordpress-3.3.2/xmlrpc.php?rsd" />
|
||||
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://lamp/wordpress-3.3.2/wp-includes/wlwmanifest.xml" />
|
||||
<meta name="generator" content="WordPress 3.3.2" />
|
||||
<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
|
||||
</head>
|
||||
|
||||
<body class="archive author author-admin author-1 single-author two-column right-sidebar">
|
||||
<div id="page" class="hfeed">
|
||||
<header id="branding" role="banner">
|
||||
<hgroup>
|
||||
<h1 id="site-title"><span><a href="http://lamp/wordpress-3.3.2/" title="Wordpress 3.3.2" rel="home">Wordpress 3.3.2</a></span></h1>
|
||||
<h2 id="site-description">Just another WordPress site</h2>
|
||||
</hgroup>
|
||||
|
||||
<a href="http://lamp/wordpress-3.3.2/">
|
||||
<img src="http://lamp/wordpress-3.3.2/wp-content/themes/twentyeleven/images/headers/chessboard.jpg" width="1000" height="288" alt="" />
|
||||
</a>
|
||||
|
||||
<form method="get" id="searchform" action="http://lamp/wordpress-3.3.2/">
|
||||
<label for="s" class="assistive-text">Search</label>
|
||||
<input type="text" class="field" name="s" id="s" placeholder="Search" />
|
||||
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />
|
||||
</form>
|
||||
|
||||
<nav id="access" role="navigation">
|
||||
<h3 class="assistive-text">Main menu</h3>
|
||||
<div class="skip-link"><a class="assistive-text" href="#content" title="Skip to primary content">Skip to primary content</a></div>
|
||||
<div class="skip-link"><a class="assistive-text" href="#secondary" title="Skip to secondary content">Skip to secondary content</a></div>
|
||||
<div class="menu"><ul><li ><a href="http://lamp/wordpress-3.3.2/" title="Home">Home</a></li><li class="page_item page-item-2"><a href="http://lamp/wordpress-3.3.2/?page_id=2">Sample Page</a></li></ul></div>
|
||||
</nav><!-- #access -->
|
||||
</header><!-- #branding -->
|
||||
|
||||
|
||||
<div id="main">
|
||||
<section id="primary">
|
||||
<div id="content" role="main">
|
||||
|
||||
|
||||
|
||||
<header class="page-header">
|
||||
<h1 class="page-title author">Author Archives: <span class="vcard"><a class="url fn n" href="http://lamp/wordpress-3.3.2/?author=1" title="admin" rel="me">admin</a></span></h1>
|
||||
</header>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<article id="post-1" class="post-1 post type-post status-publish format-standard hentry category-uncategorized">
|
||||
<header class="entry-header">
|
||||
<h1 class="entry-title"><a href="http://lamp/wordpress-3.3.2/?p=1" title="Permalink to Hello world!" rel="bookmark">Hello world!</a></h1>
|
||||
|
||||
<div class="entry-meta">
|
||||
<span class="sep">Posted on </span><a href="http://lamp/wordpress-3.3.2/?p=1" title="1:05 pm" rel="bookmark"><time class="entry-date" datetime="2012-05-02T13:05:44+00:00" pubdate>May 2, 2012</time></a><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="http://lamp/wordpress-3.3.2/?author=1" title="View all posts by admin" rel="author">admin</a></span></span> </div><!-- .entry-meta -->
|
||||
|
||||
<div class="comments-link">
|
||||
<a href="http://lamp/wordpress-3.3.2/?p=1#comments" title="Comment on Hello world!">2</a> </div>
|
||||
</header><!-- .entry-header -->
|
||||
|
||||
<div class="entry-content">
|
||||
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>
|
||||
</div><!-- .entry-content -->
|
||||
|
||||
<footer class="entry-meta">
|
||||
<span class="cat-links">
|
||||
<span class="entry-utility-prep entry-utility-prep-cat-links">Posted in</span> <a href="http://lamp/wordpress-3.3.2/?cat=1" title="View all posts in Uncategorized" rel="category">Uncategorized</a> </span>
|
||||
|
||||
<span class="sep"> | </span>
|
||||
<span class="comments-link"><a href="http://lamp/wordpress-3.3.2/?p=1#comments" title="Comment on Hello world!"><b>2</b> Replies</a></span>
|
||||
|
||||
</footer><!-- #entry-meta -->
|
||||
</article><!-- #post-1 -->
|
||||
|
||||
|
||||
|
||||
|
||||
</div><!-- #content -->
|
||||
</section><!-- #primary -->
|
||||
|
||||
<div id="secondary" class="widget-area" role="complementary">
|
||||
<aside id="search-2" class="widget widget_search"> <form method="get" id="searchform" action="http://lamp/wordpress-3.3.2/">
|
||||
<label for="s" class="assistive-text">Search</label>
|
||||
<input type="text" class="field" name="s" id="s" placeholder="Search" />
|
||||
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />
|
||||
</form>
|
||||
</aside> <aside id="recent-posts-2" class="widget widget_recent_entries"> <h3 class="widget-title">Recent Posts</h3> <ul>
|
||||
<li><a href="http://lamp/wordpress-3.3.2/?p=1" title="Hello world!">Hello world!</a></li>
|
||||
</ul>
|
||||
</aside><aside id="recent-comments-2" class="widget widget_recent_comments"><h3 class="widget-title">Recent Comments</h3><ul id="recentcomments"><li class="recentcomments">Pwet on <a href="http://lamp/wordpress-3.3.2/?p=1#comment-2">Hello world!</a></li><li class="recentcomments"><a href='http://wordpress.org/' rel='external nofollow' class='url'>Mr WordPress</a> on <a href="http://lamp/wordpress-3.3.2/?p=1#comment-1">Hello world!</a></li></ul></aside><aside id="archives-2" class="widget widget_archive"><h3 class="widget-title">Archives</h3> <ul>
|
||||
<li><a href='http://lamp/wordpress-3.3.2/?m=201205' title='May 2012'>May 2012</a></li>
|
||||
</ul>
|
||||
</aside><aside id="categories-2" class="widget widget_categories"><h3 class="widget-title">Categories</h3> <ul>
|
||||
<li class="cat-item cat-item-1"><a href="http://lamp/wordpress-3.3.2/?cat=1" title="View all posts filed under Uncategorized">Uncategorized</a>
|
||||
</li>
|
||||
</ul>
|
||||
</aside><aside id="meta-2" class="widget widget_meta"><h3 class="widget-title">Meta</h3> <ul>
|
||||
<li><a href="http://lamp/wordpress-3.3.2/wp-login.php">Log in</a></li>
|
||||
<li><a href="http://lamp/wordpress-3.3.2/?feed=rss2" title="Syndicate this site using RSS 2.0">Entries <abbr title="Really Simple Syndication">RSS</abbr></a></li>
|
||||
<li><a href="http://lamp/wordpress-3.3.2/?feed=comments-rss2" title="The latest comments to all posts in RSS">Comments <abbr title="Really Simple Syndication">RSS</abbr></a></li>
|
||||
<li><a href="http://wordpress.org/" title="Powered by WordPress, state-of-the-art semantic personal publishing platform.">WordPress.org</a></li>
|
||||
</ul>
|
||||
</aside> </div><!-- #secondary .widget-area -->
|
||||
|
||||
</div><!-- #main -->
|
||||
|
||||
<footer id="colophon" role="contentinfo">
|
||||
|
||||
|
||||
|
||||
<div id="site-generator">
|
||||
<a href="http://wordpress.org/" title="Semantic Personal Publishing Platform" rel="generator">Proudly powered by WordPress</a>
|
||||
</div>
|
||||
</footer><!-- #colophon -->
|
||||
</div><!-- #page -->
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
161
spec/shared_examples/wp_user_existable.rb
Normal file
161
spec/shared_examples/wp_user_existable.rb
Normal file
@@ -0,0 +1,161 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
shared_examples 'WpUser::Existable' do
|
||||
let(:mod) { WpUser::Existable }
|
||||
let(:fixtures_dir) { MODELS_FIXTURES + '/wp_user/existable' }
|
||||
|
||||
describe '::login_from_author_pattern' do
|
||||
after do
|
||||
mod.login_from_author_pattern(@text).should == @expected
|
||||
end
|
||||
|
||||
context 'when no trailing slash' do
|
||||
it 'returns the correct login' do
|
||||
@text = '/aurhor/neo'
|
||||
@expeced = 'neo'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when trailing slash' do
|
||||
it 'returns the correct login' do
|
||||
@text = '/author/admin/'
|
||||
@expected = 'admin'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when pattern not found' do
|
||||
it 'returns nil' do
|
||||
@text = 'im not from this world'
|
||||
@expected = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '::login_from_body' do
|
||||
after { mod.login_from_body(body).should == @expected }
|
||||
|
||||
context 'when the author pattern is in the body' do
|
||||
let(:body) { '/author/admin' }
|
||||
|
||||
it 'returns it' do
|
||||
mod.stub(:login_from_body).with(body).and_return('admin')
|
||||
@expected = 'admin'
|
||||
end
|
||||
end
|
||||
|
||||
context 'otherwise' do
|
||||
let(:body) { '<body class="archive author author-user2 author-1 custom-font-enabled single-author">' }
|
||||
|
||||
it 'gets the login from the body class' do
|
||||
@expected = 'user2'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '::display_name_from_body' do
|
||||
after { mod.display_name_from_body(@body).should == @expected }
|
||||
|
||||
context 'when pattern not found' do
|
||||
it 'returns nil' do
|
||||
@body = 'im not there'
|
||||
@expected = nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the title tag is empty' do
|
||||
it 'returns nil' do
|
||||
@body = '<title></title>'
|
||||
@expected = nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when pattern is found' do
|
||||
context 'when unencoded extra chars' do
|
||||
it 'returns the display_name w/o extra chars' do
|
||||
@body = '<title>admin display | Wordpress-3.5.1</title>'
|
||||
@expected = 'admin display'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when encoded extra chars' do
|
||||
it 'returns the display_name w/o extra chars' do
|
||||
@body = '<title>user user | Wordpress-3.5.1</title>'
|
||||
@expected = 'user user'
|
||||
end
|
||||
|
||||
context 'when custom extra chars' do
|
||||
it 'detects them' do
|
||||
@body = '<title>admin « Wiener</title>'
|
||||
@expected = 'admin'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'decodes entities in display_name' do
|
||||
@body = '<title>user & nickname | Wordpress-3.5.1</title>'
|
||||
@expected = 'user & nickname'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#load_from_response' do
|
||||
after do
|
||||
response = Typhoeus::Response.new(@resp_opt || resp_opt)
|
||||
subject.send(:load_from_response, response)
|
||||
|
||||
subject.login.should == @login
|
||||
subject.display_name.should == @display_name
|
||||
end
|
||||
|
||||
context 'with a 301' do
|
||||
let(:location) { 'http://lamp/wordpress-3.5.1/author/admin/' }
|
||||
let(:resp_opt) { { code: 301, headers: { 'Location' => location } } }
|
||||
|
||||
it 'loads the correct values' do
|
||||
stub_request(:get, location).to_return(body: '<title>admin name | wp</title>')
|
||||
|
||||
@login = 'admin'
|
||||
@display_name = 'admin name'
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a 200' do
|
||||
let(:resp_opt) { { code: 200, body: File.new(fixtures_dir + '/admin.html').read } }
|
||||
|
||||
it 'loads the correct values' do
|
||||
@login = 'admin'
|
||||
@display_name = 'admin d-name'
|
||||
end
|
||||
end
|
||||
|
||||
context 'otherwise' do
|
||||
it 'does not do anything' do
|
||||
@resp_opt = { code: 404 }
|
||||
@login = nil
|
||||
@display_name = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#exists_from_response?' do
|
||||
after do
|
||||
response = Typhoeus::Response.new(@resp_opt || resp_opt)
|
||||
subject.exists_from_response?(response).should == @expected
|
||||
end
|
||||
|
||||
context 'login not found' do
|
||||
it 'returns false' do
|
||||
@resp_opt = { code: 404 }
|
||||
@expected = false
|
||||
end
|
||||
end
|
||||
|
||||
context 'login found' do
|
||||
it 'returns true' do
|
||||
@resp_opt = { code: 200, body: File.new(fixtures_dir + '/admin.html').read }
|
||||
@expected = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user