Fix up browser detector spec and do some refactoring for clarity.

master
Jeff Dallien 2013-07-02 23:13:22 -04:00
parent eda67c7ea8
commit 36ad9f11b2
2 changed files with 34 additions and 16 deletions

View File

@ -4,9 +4,7 @@ module CookieExtractor
class NoCookieFileFoundException < Exception; end class NoCookieFileFoundException < Exception; end
class BrowserDetector class BrowserDetector
attr_reader :cookie_locations COOKIE_LOCATIONS = {
@cookie_locations = {
"chrome" => "~/.config/google-chrome/Default/Cookies", "chrome" => "~/.config/google-chrome/Default/Cookies",
"chromium" => "~/.config/chromium/Default/Cookies", "chromium" => "~/.config/chromium/Default/Cookies",
"firefox" => "~/.mozilla/firefox/*.default/cookies.sqlite" "firefox" => "~/.mozilla/firefox/*.default/cookies.sqlite"
@ -15,12 +13,7 @@ module CookieExtractor
# Returns the extractor of the most recently used browser's cookies # Returns the extractor of the most recently used browser's cookies
# or raise NoCookieFileFoundException if there are no cookies # or raise NoCookieFileFoundException if there are no cookies
def self.guess def self.guess
full_cookie_locations = @cookie_locations.select { |browser, path| most_recently_used_detected_browsers.each { |browser, path|
!Dir.glob(File.expand_path(path)).empty?
}.sort_by { |browser, path|
File.mtime(Dir.glob(File.expand_path(path)).first)
}.reverse.each { |tuple|
browser = tuple.first
begin begin
extractor = self.browser_extractor(browser) extractor = self.browser_extractor(browser)
rescue BrowserNotDetectedException, NoCookieFileFoundException rescue BrowserNotDetectedException, NoCookieFileFoundException
@ -36,7 +29,7 @@ module CookieExtractor
# Open a browser's cookie file using intelligent guesswork # Open a browser's cookie file using intelligent guesswork
def self.browser_extractor(browser) def self.browser_extractor(browser)
raise InvalidBrowserNameException, "Browser must be one of: #{self.supported_browsers.join(', ')}" unless self.supported_browsers.include?(browser) raise InvalidBrowserNameException, "Browser must be one of: #{self.supported_browsers.join(', ')}" unless self.supported_browsers.include?(browser)
paths = Dir.glob(File.expand_path(@cookie_locations[browser])) paths = Dir.glob(File.expand_path(COOKIE_LOCATIONS[browser]))
if paths.length < 1 or not File.exists?(paths.first) if paths.length < 1 or not File.exists?(paths.first)
raise NoCookieFileFoundException, "File #{paths.first} does not exist!" raise NoCookieFileFoundException, "File #{paths.first} does not exist!"
end end
@ -53,7 +46,7 @@ module CookieExtractor
end end
def self.supported_browsers def self.supported_browsers
@cookie_locations.keys COOKIE_LOCATIONS.keys
end end
def self.detect_browser(db_filename) def self.detect_browser(db_filename)
@ -71,5 +64,15 @@ module CookieExtractor
def self.has_table?(db, table_name) def self.has_table?(db, table_name)
db.table_info(table_name).size > 0 db.table_info(table_name).size > 0
end end
def self.most_recently_used_detected_browsers
COOKIE_LOCATIONS.select { |browser, path|
Dir.glob(File.expand_path(path)).any?
}.sort_by { |browser, path|
File.mtime(Dir.glob(File.expand_path(path)).first)
}.reverse
end
private_class_method :most_recently_used_detected_browsers
end end
end end

View File

@ -40,7 +40,9 @@ describe CookieExtractor::BrowserDetector, "determining the correct extractor to
extractor.instance_of?(CookieExtractor::ChromeCookieExtractor).should be_true extractor.instance_of?(CookieExtractor::ChromeCookieExtractor).should be_true
end end
end end
end
describe CookieExtractor::BrowserDetector, "guessing the location of the cookie file" do
describe "when no cookie files are found in the standard locations" do describe "when no cookie files are found in the standard locations" do
before :each do before :each do
Dir.stub!(:glob).and_return([]) Dir.stub!(:glob).and_return([])
@ -54,12 +56,25 @@ describe CookieExtractor::BrowserDetector, "determining the correct extractor to
describe "when multiple cookie files are found in the standard locations" do describe "when multiple cookie files are found in the standard locations" do
before :each do before :each do
Dir.stub!(:glob).and_return(CookieExtractor::BrowserDetector.cookie_locations.values) cookie_locations = CookieExtractor::BrowserDetector::COOKIE_LOCATIONS
Dir.stub!(:glob).and_return([cookie_locations['chrome']],
[],
[cookie_locations['firefox']])
end end
it "should return a ChromeCookieExtractor or FirefoxCookieExtractor" do describe "and chrome was the most recently used" do
lambda { CookieExtractor::BrowserDetector.guess }. before :each do
should be_kind_of(CookieExtractor::Common) File.should_receive(:mtime).twice.and_return(
Time.parse("July 2 2013 00:00:00"),
Time.parse("July 1 2013 00:00:00"))
end
it "should build a ChromeCookieExtractor" do
CookieExtractor::BrowserDetector.
should_receive(:browser_extractor).
once.with("chrome")
CookieExtractor::BrowserDetector.guess
end
end end
end end
end end