def patched_payment(self: DefaultHTTPClient, receiver: str, price: int, balance: int, balance_sig: bytes, channel_manager_address: str): self.invalid_amount_received = 0 return DefaultHTTPClient.on_payment_requested( self, receiver, price + self.price_adjust, balance, balance_sig, channel_manager_address)
def test_cheating_client( doggo_proxy, default_http_client: DefaultHTTPClient, http_doggo_url: str ): """this test scenario where client sends less funds than what is requested by the server. In such case, a "RDN-Invalid-Amount=1" header should be sent in a server's reply """ def patched_payment( self: DefaultHTTPClient, method: str, url: str, response: Response, **kwargs ): response.headers[HTTPHeaders.PRICE] = str( int(response.headers[HTTPHeaders.PRICE]) + self.price_adjust ) return DefaultHTTPClient.on_payment_requested(self, method, url, response, **kwargs) def patched_on_invalid_amount(self, method: str, url: str, response: Response, **kwargs): self.invalid_amount_received += 1 price = int(response.headers[HTTPHeaders.PRICE]) DefaultHTTPClient.on_invalid_amount(self, method, url, response, **kwargs) # on_invalid_amount will already prepare the next payment which we don't execute anymore, # so revert that. channel = self.get_channel(url) channel.update_balance(channel.balance - price) return False default_http_client.on_invalid_amount = types.MethodType( patched_on_invalid_amount, default_http_client ) default_http_client.on_payment_requested = types.MethodType( patched_payment, default_http_client ) default_http_client.invalid_amount_received = 0 # correct amount default_http_client.price_adjust = 0 response = default_http_client.get(http_doggo_url) check_response(response) assert default_http_client.invalid_amount_received == 0 # underpay default_http_client.price_adjust = -1 response = default_http_client.get(http_doggo_url) assert response is None assert default_http_client.invalid_amount_received == 1 # overpay default_http_client.price_adjust = 1 response = default_http_client.get(http_doggo_url) assert response is None assert default_http_client.invalid_amount_received == 2
def patched_payment( self: DefaultHTTPClient, method: str, url: str, response: Response, **kwargs ): response.headers[HTTPHeaders.PRICE] = str( int(response.headers[HTTPHeaders.PRICE]) + self.price_adjust ) return DefaultHTTPClient.on_payment_requested(self, method, url, response, **kwargs)
def test_cheating_client(doggo_proxy, default_http_client: DefaultHTTPClient): """this test scenario where client sends less funds than what is requested by the server. In such case, a "RDN-Invalid-Amount=1" header should be sent in a server's reply """ # patch default http client to use price lower than the server suggests def patched_payment(self: DefaultHTTPClient, receiver: str, price: int, balance: int, balance_sig: bytes, channel_manager_address: str): self.invalid_amount_received = 0 return DefaultHTTPClient.on_payment_requested( self, receiver, price + self.price_adjust, balance, balance_sig, channel_manager_address) def patched_on_invalid_amount(self: DefaultHTTPClient): DefaultHTTPClient.on_invalid_amount(self) self.invalid_amount_received = 1 default_http_client.on_invalid_amount = types.MethodType( patched_on_invalid_amount, default_http_client) default_http_client.on_payment_requested = types.MethodType( patched_payment, default_http_client) # correct amount default_http_client.price_adjust = 0 response = default_http_client.run('doggo.jpg') check_response(response) assert default_http_client.invalid_amount_received == 0 # underpay default_http_client.price_adjust = -1 response = default_http_client.run('doggo.jpg') assert response is None assert default_http_client.invalid_amount_received == 1 # overpay default_http_client.price_adjust = 1 response = default_http_client.run('doggo.jpg') assert response is None assert default_http_client.invalid_amount_received == 1