Fix up browser detector spec and do some refactoring for clarity.
This commit is contained in:
parent
eda67c7ea8
commit
36ad9f11b2
2 changed files with 34 additions and 16 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue