From 36ad9f11b28d45512c3bfe0ff52375c24eaea805 Mon Sep 17 00:00:00 2001 From: Jeff Dallien Date: Tue, 2 Jul 2013 23:13:22 -0400 Subject: [PATCH] Fix up browser detector spec and do some refactoring for clarity. --- lib/cookie_extractor/browser_detector.rb | 25 +++++++++++++----------- spec/browser_detector_spec.rb | 25 +++++++++++++++++++----- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/cookie_extractor/browser_detector.rb b/lib/cookie_extractor/browser_detector.rb index 17cef26..8700344 100644 --- a/lib/cookie_extractor/browser_detector.rb +++ b/lib/cookie_extractor/browser_detector.rb @@ -4,9 +4,7 @@ module CookieExtractor class NoCookieFileFoundException < Exception; end class BrowserDetector - attr_reader :cookie_locations - - @cookie_locations = { + COOKIE_LOCATIONS = { "chrome" => "~/.config/google-chrome/Default/Cookies", "chromium" => "~/.config/chromium/Default/Cookies", "firefox" => "~/.mozilla/firefox/*.default/cookies.sqlite" @@ -15,12 +13,7 @@ module CookieExtractor # Returns the extractor of the most recently used browser's cookies # or raise NoCookieFileFoundException if there are no cookies def self.guess - full_cookie_locations = @cookie_locations.select { |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 + most_recently_used_detected_browsers.each { |browser, path| begin extractor = self.browser_extractor(browser) rescue BrowserNotDetectedException, NoCookieFileFoundException @@ -36,7 +29,7 @@ module CookieExtractor # Open a browser's cookie file using intelligent guesswork def self.browser_extractor(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) raise NoCookieFileFoundException, "File #{paths.first} does not exist!" end @@ -53,7 +46,7 @@ module CookieExtractor end def self.supported_browsers - @cookie_locations.keys + COOKIE_LOCATIONS.keys end def self.detect_browser(db_filename) @@ -71,5 +64,15 @@ module CookieExtractor def self.has_table?(db, table_name) db.table_info(table_name).size > 0 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 diff --git a/spec/browser_detector_spec.rb b/spec/browser_detector_spec.rb index c01cd4c..788a371 100644 --- a/spec/browser_detector_spec.rb +++ b/spec/browser_detector_spec.rb @@ -40,7 +40,9 @@ describe CookieExtractor::BrowserDetector, "determining the correct extractor to extractor.instance_of?(CookieExtractor::ChromeCookieExtractor).should be_true 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 before :each do 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 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 - it "should return a ChromeCookieExtractor or FirefoxCookieExtractor" do - lambda { CookieExtractor::BrowserDetector.guess }. - should be_kind_of(CookieExtractor::Common) + describe "and chrome was the most recently used" do + before :each do + 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