WpItem::Infos specs

This commit is contained in:
erwanlr
2013-03-25 15:24:46 +01:00
parent 276952ffb9
commit 5bb9aa29fa
4 changed files with 123 additions and 12 deletions

View File

@@ -1,6 +1,8 @@
# encoding: UTF-8 # encoding: UTF-8
class WpItem class WpItem
# @uri is used instead of #uri to avoid the presence of the :path into it
module Infos module Infos
# @return [ Boolean ] # @return [ Boolean ]
@@ -8,26 +10,17 @@ class WpItem
Browser.instance.get(readme_url).code == 200 ? true : false Browser.instance.get(readme_url).code == 200 ? true : false
end end
# @return [ String ] # @return [ String ] The url to the readme file
def readme_url def readme_url
@uri.merge('readme.txt').to_s @uri.merge('readme.txt').to_s
end end
# @return [ String ]
def wordpress_url
end
def wordpress_org_item?
end
# @return [ Boolean ] # @return [ Boolean ]
def has_changelog? def has_changelog?
Browser.instance.get(changelog_url).code == 200 ? true : false Browser.instance.get(changelog_url).code == 200 ? true : false
end end
# @return [ String ] # @return [ String ] The url to the changelog file
def changelog_url def changelog_url
@uri.merge('changelog.txt').to_s @uri.merge('changelog.txt').to_s
end end
@@ -43,16 +36,20 @@ class WpItem
# however can also be found in their specific plugin dir. # however can also be found in their specific plugin dir.
# http://www.exploit-db.com/ghdb/3714/ # http://www.exploit-db.com/ghdb/3714/
# #
# Only the first 700 bytes are checked to avoid the download
# of the whole file which can be very huge (like 2 Go)
#
# @return [ Boolean ] # @return [ Boolean ]
def has_error_log? def has_error_log?
response_body = Browser.instance.get(error_log_url, headers: {'range' => 'bytes=0-700'}).body response_body = Browser.instance.get(error_log_url, headers: {'range' => 'bytes=0-700'}).body
response_body[%r{PHP Fatal error}i] ? true : false response_body[%r{PHP Fatal error}i] ? true : false
end end
# @return [ String ] # @return [ String ] The url to the error_log file
def error_log_url def error_log_url
@uri.merge('error_log').to_s @uri.merge('error_log').to_s
end end
end end
end end

View File

@@ -5,6 +5,11 @@ require 'spec_helper'
describe WpItem do describe WpItem do
it_behaves_like 'WpItem::Existable' it_behaves_like 'WpItem::Existable'
it_behaves_like 'WpItem::Findable#Found_From=' it_behaves_like 'WpItem::Findable#Found_From='
it_behaves_like 'WpItem::Infos' do
let(:readme_url) { uri.merge('readme.txt').to_s }
let(:changelog_url) { uri.merge('changelog.txt').to_s }
let(:error_log_url) { uri.merge('error_log').to_s }
end
subject(:wp_item) { WpItem.new(uri, options) } subject(:wp_item) { WpItem.new(uri, options) }
let(:uri) { URI.parse('http://example.com') } let(:uri) { URI.parse('http://example.com') }

View File

@@ -0,0 +1,109 @@
# encoding: UTF-8
shared_examples 'WpItem::Infos' do
# 3 expected urls have to be set in the described class (or subject)
# e.g :
# let(:readme_url) { }
# let(:changelog_url) { }
# let(:error_log_url) { }
describe '#readme_url' do
it 'returns the correct url' do
subject.readme_url.should == readme_url
end
end
describe '#has_readme?' do
after :each do
stub_request(:get, subject.readme_url).to_return(status: @status)
subject.has_readme?.should === @expected
end
it 'returns true on a 200' do
@status = 200
@expected = true
end
it 'returns false otherwise' do
@status = 404
@expected = false
end
end
describe '#changelog_url' do
it 'returns the correct url' do
subject.changelog_url.should == changelog_url
end
end
describe '#has_changelog?' do
after :each do
stub_request(:get, subject.changelog_url).to_return(status: @status)
subject.has_changelog?.should === @expected
end
it 'returns true on a 200' do
@status = 200
@expected = true
end
it 'returns false otherwise' do
@status = 404
@expected = false
end
end
describe '#has_directory_listing?' do
after do
stub_request(:get, subject.uri.to_s).to_return(@stub_return)
subject.has_directory_listing?.should === @expected
end
context 'when the body contains <title>Index of' do
it 'returns true' do
@stub_return = { status: 200, body: '<title>Index of asdf</title>' }
@expected = true
end
end
it 'returns false otherwise' do
@stub_return = { status: 200, body: '<title>My Wordpress Site</title>' }
@expected = false
end
it 'returns false on a 404' do
@stub_return = { status: 404 }
@expected = false
end
end
describe '#error_log_url' do
it 'returns the correct url' do
subject.error_log_url.should == error_log_url
end
end
describe '#has_error_log?' do
after do
stub_request(:get, subject.error_log_url).to_return(@stub_return)
subject.has_error_log?.should === @expected
end
it 'returns true if the pattern is detected' do
@stub_return = { status: 200, body: File.new( MODELS_FIXTURES + '/wp_item/error_log') }
@expected = true
end
it 'returns false otherwise' do
@stub_return = { status: 200, body: 'yolo' }
@expected = false
end
it 'returns false on a 404' do
@stub_return = { status: 404 }
@expected = false
end
end
end