class Application: def __init__(self, config_file): logging.getLogger('application').info("Config file: %s" % config_file) Application._init(config_file) self.config = ConfigProcessor(config_file) @staticmethod def _init(config_file): default_config = """!Config targets: - &chrome_default browser: chrome profile: Default rules: - url_pattern: "*example.com/*" pattern_type: ant target: *chrome_default defaults: target: *ff1 """ if not path.exists(config_file): os.makedirs(os.path.dirname(config_file), exist_ok=True) with open(config_file, "w") as f: f.write(default_config) def run(self, url): target = self.config.target(url) logging.getLogger('history').info("open: %s\t\turl: %s" % (target, url)) command_to_execute = browser(target, url).command() logging.getLogger('application').info(command_to_execute) return os.system(command_to_execute)
class HostsRuleTest(unittest.TestCase): def setUp(self): config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test-hosts-browser-config.yml') self.config = ConfigProcessor(config_file) def test_by_www_host(self): result = self.config.target('http://w3.host2.com') self.assertEqual(result.profile, 'Default') def test_by_host(self): result = self.config.target('http://console.aws.amazon.com') self.assertEqual(result.profile, 'Profile 1') def test_by_ip(self): result = self.config.target('http://192.168.0.100:5000') self.assertEqual(result.profile, 'Profile 2') def test_by_ip_and_port_match(self): result = self.config.target('http://192.168.0.101:4000') self.assertEqual(result.profile, 'Profile 2') def test_by_ip_and_port_not_match(self): result = self.config.target('http://192.168.0.101:3000') self.assertEqual(result.profile, 'Default') def test_by_host_and_port(self): result = self.config.target('http://console.aws.amazon.com:3000/') self.assertEqual(result.profile, 'Profile 1') def test_by_host_and_port_match(self): result = self.config.target('http://host3.com:3000') self.assertEqual(result.profile, 'Profile 1') def test_by_host_and_port_not_match(self): result = self.config.target('http://www.host3.com:4000') self.assertEqual(result.profile, 'Default') def test_by_my_hosts(self): result = self.config.target('http://www.myhost2.com') self.assertEqual(result.profile, 'Default')
class TestNullBrowserDispatcher(unittest.TestCase): def setUp(self): self.config = ConfigProcessor('config/test-browser-config.yml') def test_start_null(self): url = 'http://www.bad-site.com/' target = self.config.target(url) command = browser(target, url).command() self.assertEqual( command, 'echo "Url http://www.bad-site.com/ was not opened" >> /dev/null &' )
class TestFirefoxBrowserDispatcher(unittest.TestCase): def setUp(self): self.config = ConfigProcessor('config/test-browser-config.yml') def test_firefox_default(self): url = 'http://fox.example.com' target = self.config.target(url) with self.assertRaises(Exception) as context: browser(target, url).command() self.assertTrue( 'Unsupported Browser: firefox' in str(context.exception))
class Test(unittest.TestCase): def setUp(self): config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test-browser-config.yml') self.config = ConfigProcessor(config_file) def test_firefox_default(self): result = self.config.target('http://fox.example.com') self.assertEqual(result.browser, 'firefox') self.assertEqual(result.profile, 'Default') self.assertFalse(result.incognito) def test_chrome_profile1(self): result = self.config.target('http://github.com/johndoe') self.assertEqual(result.browser, 'chrome') self.assertEqual(result.profile, 'Profile 1') self.assertFalse(result.incognito) def test_default_profile(self): result = self.config.target('http://anything.example.com') self.assertEqual(result.browser, 'firefox') self.assertEqual(result.profile, 'Default') self.assertFalse(result.incognito) def test_chrome_incognito(self): result = self.config.target('http://secret.example.com') self.assertEqual(result.browser, 'chrome') self.assertEqual(result.profile, 'Default') self.assertTrue(result.incognito) def test_chrome_guest(self): result = self.config.target('http://my-guest.example.com') self.assertEqual(result.browser, 'chrome') self.assertEqual(result.profile, 'Default') self.assertTrue(result.guest) def test_chrome_guest_and_incognito(self): result = self.config.target( 'http://my-guest-not-incognito.example.com') self.assertEqual(result.browser, 'chrome') self.assertEqual(result.profile, 'Default') self.assertTrue(result.guest) self.assertTrue(result.incognito)
def setUp(self): config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test-browser-config.yml') self.config = ConfigProcessor(config_file)
def setUp(self): self.config = ConfigProcessor('config/test-browser-config.yml')
class TestChromeBrowserDispatcher(unittest.TestCase): def setUp(self): self.config = ConfigProcessor('config/test-browser-config.yml') def test_chrome_profile1(self): url = 'http://github.com/johndoe' target = self.config.target(url) command = browser(target, url).command() self.assertEqual( command, '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory="Profile 1" "http://github.com/johndoe" >> /dev/null &' ) def test_default_profile(self): url = 'http://anything.example.com' target = self.config.target(url) with self.assertRaises(Exception) as context: browser(target, url).command() self.assertTrue( 'Unsupported Browser: firefox' in str(context.exception)) def test_chrome_incognito(self): url = 'http://secret.example.com' target = self.config.target(url) command = browser(target, url).command() self.assertEqual( command, '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory="Default" --incognito "http://secret.example.com" >> /dev/null &' ) def test_chrome_guest(self): url = 'http://my-guest.example.com' target = self.config.target(url) command = browser(target, url).command() self.assertEqual( command, '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --guest "http://my-guest.example.com" >> /dev/null &' ) def test_chrome_guest_overwrite_incognito(self): url = 'http://my-guest-not-incognito.example.com' target = self.config.target(url) command = browser(target, url).command() self.assertEqual( command, '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --guest "http://my-guest-not-incognito.example.com" >> /dev/null &' ) def test_strip_trailing_slash(self): url = 'https://gitlab.com/exampleuser/' target = self.config.target(url) command = browser(target, url).command() self.assertEqual( command, '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory="Profile 2" "https://gitlab.com/exampleuser/" >> /dev/null &' ) def test_http_schema(self): url = 'http://schema.example.com/johndoe' target = self.config.target(url) command = browser(target, url).command() self.assertEqual( command, '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory="Profile 1" "http://schema.example.com/johndoe" >> /dev/null &' ) def test_https_schema(self): url = 'https://schema.example.com/johndoe/' target = self.config.target(url) command = browser(target, url).command() self.assertEqual( command, '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory="Profile 2" "https://schema.example.com/johndoe/" >> /dev/null &' )
def __init__(self, config_file): logging.getLogger('application').info("Config file: %s" % config_file) Application._init(config_file) self.config = ConfigProcessor(config_file)