def test_disabled_when_configured(self): spy = MasterSpy(EndlessFake()) self.context.inject(requests, spy) self.context.set_env(HTTPS_VERIFY_CERTS="FAlSe") HttpClient().get("http://spam") args, kwargs = spy.last_call_to("get") expect(kwargs).to(have_keys(verify=False))
def test_last_call_to_returns_last_call_to_given_func(self): stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) planted_args = (4, 5) planted_kwargs = {"foo": 77, "bar": "soap"} spy.func(spam=1, eggs=2) spy.func(*planted_args, **planted_kwargs) expect(spy.last_call_to("func")).to( equal((planted_args, planted_kwargs)))
def test_last_call_to_returns_last_call_to_given_func(self): stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) planted_args = (4, 5) planted_kwargs = {'foo': 77, 'bar': 'soap'} spy.func(spam=1, eggs=2) spy.func(*planted_args, **planted_kwargs) expect(spy.last_call_to('func')).to( equal((planted_args, planted_kwargs)))
def test_enabled_in_session_by_default(self): requests_stub = EndlessFake() self.context.inject(requests, requests_stub) spy = MasterSpy(EndlessFake()) requests_stub.Session = lambda *a, **k: spy client = HttpClient() client.enable_cookies() client.get("http://spam") args, kwargs = spy.last_call_to("send") expect(kwargs).to(have_keys(verify=True))
def test_disabled_in_session_when_configured(self): self.context.set_env(HTTPS_VERIFY_CERTS="FAlSe") requests_stub = EndlessFake() self.context.inject(requests, requests_stub) spy = MasterSpy(EndlessFake()) requests_stub.Session = lambda *a, **k: spy client = HttpClient() client.enable_cookies() client.get("http://spam") args, kwargs = spy.last_call_to("send") expect(kwargs).to(have_keys(verify=False))
class TestRequestsPassthrough(TestCase): def setUp(self): self.context = open_dependency_context(supply_env=True) self.requests_spy = MasterSpy(FakeResponse()) self.context.inject(requests, self.requests_spy) def tearDown(self): self.context.close() def test_attribute_error_on_unknown_method(self): def attempt(): HttpClient().this_method_does_not_exist expect(attempt).to(raise_ex(AttributeError)) def check_request(self, method): url = "http://nothing" kwargs = {"spam": 42, "eggs": 0} getattr(HttpClient(), method)(url, **kwargs) req_args, req_kwargs = self.requests_spy.last_call_to(method) expect(req_args).to(equal((url, ))) for k, v in kwargs.items(): expect(req_kwargs).to(contain_key_with_value(k, v)) def check_response(self, method): actual = getattr(HttpClient(), method)("http://something") spy = self.requests_spy.attribute_spies[method] expect(actual).to(equal(spy.return_value_spies[-1])) def test_delete_request(self): self.check_request("delete") def test_delete_response(self): self.check_response("delete") def test_get_request(self): self.check_request("get") def test_get_response(self): self.check_response("get") def test_head_request(self): self.check_request("head") def test_head_response(self): self.check_response("head") def test_options_request(self): self.check_request("options") def test_options_response(self): self.check_response("options") def test_patch_request(self): self.check_response("patch") def test_post_request(self): self.check_request("post") def test_post_response(self): self.check_response("post") def test_put_request(self): self.check_request("put") def test_put_response(self): self.check_response("put")
class TestLaunchBrowserStack(TestCase): def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.set_env( BROWSER_AVAILABILITY_TIMEOUT=0, BROWSER_AVAILABILITY_THROTTLE=0, BROWSERSTACK_URL="https://browserstack.not:43/really", BROWSERSTACK_OS="CP/M", BROWSERSTACK_OS_VERSION="1.7", USE_BROWSER="NCSA-Mosaic", USE_BROWSER_VERSION="1.3", BROWSERSTACK_SCREEN_RESOLUTION="kumquat", BROWSER_AVALABILITY_TIMEOUT="0", BROWSERSTACK_SET_LOCAL="fAlSe", BROWSERSTACK_SET_DEBUG="TrUe", BROWSER_LOCATION="BrowserStack", BROWSERSTACK_USERNAME="******", BROWSERSTACK_ACCESS_KEY="gdf0i9/38md-p00", ) self.webdriver_spy = MasterSpy(EndlessFake()) self.context.inject(webdriver, self.webdriver_spy) def tearDown(self): self.context.close() def test_complains_if_environment_lacks_username(self): self.context.unset_env("BROWSERSTACK_USERNAME") expect(Browser).to(raise_error(InvalidConfiguration)) def test_complains_if_environment_lacks_access_key(self): self.context.unset_env("BROWSERSTACK_ACCESS_KEY") expect(Browser).to(raise_error(InvalidConfiguration)) def parse_command_executor(self): args, kwargs = self.webdriver_spy.last_call_to("Remote") expect(kwargs.keys()).to(contain("command_executor")) return urlsplit(kwargs["command_executor"]) def retrieve_desired_capabilities(self): args, kwargs = self.webdriver_spy.last_call_to("Remote") expect(kwargs.keys()).to(contain("desired_capabilities")) return kwargs["desired_capabilities"] def test_uses_configured_browserstack_url(self): expected = "https://a.b.c/1" self.context.set_env(BROWSERSTACK_URL=expected) Browser() parsed = self.parse_command_executor() scheme, netloc, path, query, fragment = tuple(parsed) without_credentials = netloc.split("@")[-1] actual = urlunsplit( (scheme, without_credentials, path, query, fragment)) expect(actual).to(equal(expected)) def test_uses_urlencoded_configured_username(self): expected = "*****@*****.**" self.context.set_env(BROWSERSTACK_USERNAME=expected) Browser() parsed = self.parse_command_executor() expect(parsed.username).to(equal(quote_plus(expected))) def test_uses_urlencoded_configured_access_key(self): expected = "PleaseOhPlease" self.context.set_env(BROWSERSTACK_ACCESS_KEY=expected) Browser() parsed = self.parse_command_executor() expect(parsed.password).to(equal(expected)) def test_uses_configured_os(self): expected = "OS/irus" self.context.set_env(BROWSERSTACK_OS=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("os", expected)) def test_uses_configured_os_version(self): expected = "14.7.1" self.context.set_env(BROWSERSTACK_OS_VERSION=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("os_version", expected)) def test_uses_configured_screen_resolution(self): expected = "8x10" self.context.set_env(BROWSERSTACK_SCREEN_RESOLUTION=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("resolution", expected)) def test_uses_configured_browser_type(self): expected = "smoking" self.context.set_env(USE_BROWSER=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("browser", expected)) def test_uses_configured_browser_version(self): expected = "nineteen" self.context.set_env(USE_BROWSER_VERSION=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("browser_version", expected)) def test_does_not_specify_browser_version_if_not_configured(self): self.context.unset_env("USE_BROWSER_VERSION") Browser() expect(self.retrieve_desired_capabilities().keys()).not_to( contain("browser_version")) def test_uses_configured_browserstack_local(self): expected = False self.context.set_env(BROWSERSTACK_SET_LOCAL=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("browserstack.local", expected)) def test_uses_configured_browserstack_debug(self): expected = True Browser() self.context.set_env(BROWSERSTACK_SET_DEBUG=expected) expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("browserstack.debug", expected)) def test_sets_full_screen(self): remote_spy = MasterSpy(EndlessFake()) self.webdriver_spy.Remote = lambda *a, **k: remote_spy Browser() expect(remote_spy.attribute_spies.keys()).to( contain("fullscreen_window")) calls = remote_spy.attribute_spies["fullscreen_window"].call_history assert calls, "fullscreen_window was not called" def test_detects_busy_signal(self): def busy(*args, **kwargs): raise WebDriverException( "All parallel tests are currently in use, " "including the queued tests. Please wait to " "finish or upgrade your plan to add more sessions.") self.webdriver_spy.Remote = busy expect(Browser).to(raise_ex(AllBrowsersBusy))
def test_last_call_to_raises_function_not_called(self): stub = EmptyFake() stub.func = lambda: None spy = MasterSpy(stub) expect(lambda: spy.last_call_to("func")).to( complain(FunctionNotCalled))
def test_enabled_by_default(self): spy = MasterSpy(EndlessFake()) self.context.inject(requests, spy) HttpClient().get("http://spam") args, kwargs = spy.last_call_to("get") expect(kwargs).to(have_keys(verify=True))