def test_returns_a_browser_with_the_remote_driver(self): get_spy = FunctionSpy(return_value=EndlessFake()) remote_stub = EndlessFake() remote_stub.get = get_spy self.webdriver_stub.Remote = lambda *args, **kwargs: remote_stub self.context.set_env(BROWSER_LOCATION="selenium_grid", SELENIUM_GRID_HUB_URL="http://fang") Browser().get("http://stuffed")
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_logging=True) self.get_spy = FunctionSpy(return_value=EndlessFake()) self.requests_stub = EndlessFake(pattern_obj=requests) self.requests_stub.get = self.get_spy self.context.inject(requests, self.requests_stub)
def test_rejects_parent_class_of_existing_key(self): client = HttpClient() client.set_exceptional_response_callback(exception_class=SonOfSpam, callback=EndlessFake()) expect( partial(client.set_exceptional_response_callback, exception_class=Spam, callback=EndlessFake())).to(raise_ex(TypeError))
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.inject(webdriver, EndlessFake()) self.fake_tunnel_class = MasterSpy(EndlessFake()) self.context.inject(BrowserStackTunnel, self.fake_tunnel_class) self.context.set_env(browserstack_access_key="whatever", browserstack_url="gopher:hahah", browserstack_username="******")
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_sends_configured_timeout_for_session_request(self): planted = 37 self.context.set_env(HTTP_CLIENT_SOCKET_TIMEOUT=planted) session_stub = EndlessFake() self.requests_stub.Session = lambda: session_stub send_spy = FunctionSpy(return_value=EndlessFake()) session_stub.send = send_spy client = HttpClient() client.enable_cookies() client.get("http://truffles") expect(send_spy["timeout"]).to(equal(planted))
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))
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.set_env( BROWSERSTACK_ACCESS_KEY="lomein", BROWSERSTACK_URL="spam", BROWSERSTACK_USERNAME="******", BROWSER_LOCATION="BrowserStack", BROWSERSTACK_SET_LOCAL="True", ) self.fake_tunnel = EndlessFake() self.context.inject_as_class(BrowserStackTunnel, self.fake_tunnel) self.webdriver_spy = MasterSpy(EndlessFake()) self.context.inject(webdriver, self.webdriver_spy)
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.inject(HttpClient, EndlessFake()) self.context.inject_as_class(BrowserStackTunnel, EndlessFake()) self.fake_webdriver = FakeWebdriver() self.context.inject(webdriver, self.fake_webdriver) self.context.set_env( BROWSER_LOCATION="BrowserStack", BROWSERSTACK_ACCESS_KEY="whatever", BROWSERSTACK_SET_LOCAL="true", BROWSERSTACK_URL="gopher:veronica", BROWSERSTACK_USERNAME="******", )
def test_publishes_response_object(self): planted_response = EndlessFake() published_response = None def subscriber(response, **kwargs): nonlocal published_response published_response = response requests_stub = EndlessFake() requests_stub.post = lambda *a, **k: planted_response self.context.inject(requests, requests_stub) EventBroker.subscribe(event=TestEvent.http_response_received, func=subscriber) HttpClient().post("http://something") expect(published_response).to(be(planted_response))
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_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_returns_response_returned_by_callback(self): callback_response = EndlessFake() self.context.inject(inspect_response, func_that_raises(HttpNotFound())) client = HttpClient() client.set_exceptional_response_callback( exception_class=HttpNotFound, callback=lambda *a, **k: callback_response) expect(client.get("http://stuffed")).to(be(callback_response))
def log_record(level, msg): return logging.LogRecord(name="spam", level=level, pathname="spam", lineno=1, msg=msg, args={}, exc_info=EndlessFake())
def setUp(self): self.context = open_dependency_context() self.spies = MutableObject() requests_stub = EndlessFake(pattern_obj=requests) for method in ("get", "post", "put", "delete", "head"): fake = FakeRequestsMethod() setattr(self.spies, method, fake) setattr(requests_stub, method, fake) self.context.inject(requests, requests_stub)
def setUp(self): self.context = open_dependency_context() requests_stub = EndlessFake(pattern_obj=requests) session_stub = EndlessFake(pattern_obj=requests.sessions.Session()) self.send_spy = FunctionSpy(return_value=EndlessFake()) session_stub.send = self.send_spy requests_stub.Session = lambda *a, **k: session_stub self.context.inject(requests, requests_stub)
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_logging=True) self.fake_http_client = EndlessFake() self.fake_requests_response = requests.Response() self.fake_requests_response.json = lambda: {} self.post_spy = FunctionSpy(return_value=self.fake_requests_response) self.fake_http_client.post = self.post_spy self.graph_client = GraphqlClient(http_client=self.fake_http_client, url="ignore me")
def test_passes_exception_as_kwarg(self): exception_class = HttpImATeapot spy = FunctionSpy(return_value=EndlessFake()) client = HttpClient() client.set_exceptional_response_callback( exception_class=exception_class, callback=spy) self.context.inject(inspect_response, func_that_raises(exception_class())) client.get("http://foo.bar") expect(spy["exception"]).to(be_a(exception_class))
def setUp(self): self.context = open_dependency_context() self.spies = MutableObject() requests_stub = EndlessFake(pattern_obj=requests) response = requests.Response() response.status_code = 200 for method in ("get", "post", "put", "delete", "head"): spy = FunctionSpy(return_value=response) setattr(self.spies, method, spy) setattr(requests_stub, method, spy) self.context.inject(requests, requests_stub)
def setUp(self): self.context = open_dependency_context(supply_env=True) self.timeout = 1 self.context.set_env( BROWSERSTACK_ACCESS_KEY="whatever", BROWSERSTACK_URL="http://whatever", BROWSERSTACK_USERNAME="******", BROWSER_LOCATION="browserstack", BROWSER_AVAILABILITY_TIMEOUT=self.timeout, BROWSER_AVAILABILITY_THROTTLE=0, ) self.webdriver = EndlessFake() self.context.inject(webdriver, self.webdriver)
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.fake_http = FakeHttpClient() self.context.inject_as_class(HttpClient, self.fake_http) self.fake_popen = FakePopen() self.popen_spy = MasterSpy(self.fake_popen) self.context.inject(Popen, self.popen_spy) self.context.inject(logging, EndlessFake()) # Thwart actual sleeping self.context.inject(time.sleep, lambda n: None) self.binary_zip_url = "https://where-we-expect.the/binary.zip" local_binary = "/where/we/put/the/binary" self.local_binary_path, self.local_binary_filename = os.path.split( local_binary) self.context.set_env( BROWSERSTACK_ACCESS_KEY="whatever", BROWSERSTACK_LOCAL_BINARY_ZIP_URL=self.binary_zip_url, BROWSERSTACK_LOCAL_BINARY=local_binary, )
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 test_operation_failed_passes_args_to_super_correctly( self, expected_args): fake_requests_response = requests.Response() fake_requests_response.json = lambda: {"errors": "who cares"} fake_init = EndlessFake() class FakeSupered: def __init__(*args, **kwargs): fake_init.inited_args = args fake_init.inited_kwargs = kwargs with dependency_context(supply_env=True, supply_logging=True) as temporary_context: temporary_context.inject(super, lambda *args: FakeSupered) OperationFailed(*expected_args, requests_response=fake_requests_response) for index, arg in enumerate(expected_args): expect(fake_init.inited_args[index]).to(equal(arg))
def __init__(self, session=EndlessFake()): self.status_code = 200 self._session = session
def __init__(self, session_class=EndlessFake()): self.Session = session_class super().__init__()
def setUp(self): self.context = open_dependency_context() self.context.inject(requests, EndlessFake())
def __call__(self, *args, **kwargs): response = EndlessFake() response.status_code = 200 return response
def stub_post(*args, **kwargs): nonlocal request_sent request_sent = True return EndlessFake()
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_logging=True) self.requests_stub = EndlessFake() self.context.inject(requests, self.requests_stub)
def client(self, service_name): client = EndlessFake() if "s3" == service_name: client.put_object = self.s3_put_object_spy return client