def test_cheating_client( doggo_proxy, session: Session, 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: Session, method: str, url: str, response: Response, **kwargs ): response.headers[HTTPHeaders.PRICE] = str( int(response.headers[HTTPHeaders.PRICE]) + self.price_adjust ) return Session.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]) Session.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. self.channel.update_balance(self.channel.balance - price) return False session.on_invalid_amount = types.MethodType( patched_on_invalid_amount, session ) session.on_payment_requested = types.MethodType( patched_payment, session ) session.invalid_amount_received = 0 # correct amount session.price_adjust = 0 response = session.get(http_doggo_url) check_response(response) assert session.invalid_amount_received == 0 # underpay session.price_adjust = -1 response = session.get(http_doggo_url) assert response is None assert session.invalid_amount_received == 1 # overpay session.price_adjust = 1 response = session.get(http_doggo_url) assert response is None assert session.invalid_amount_received == 2
def test_cheating_client( doggo_proxy, session: Session, 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: Session, method: str, url: str, response: Response, **kwargs ): response.headers[HTTPHeaders.PRICE] = str( int(response.headers[HTTPHeaders.PRICE]) + self.price_adjust ) return Session.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]) Session.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. self.channel.update_balance(self.channel.balance - price) return False session.on_invalid_amount = types.MethodType( patched_on_invalid_amount, session ) session.on_payment_requested = types.MethodType( patched_payment, session ) session.invalid_amount_received = 0 # correct amount session.price_adjust = 0 response = session.get(http_doggo_url) check_response(response) assert session.invalid_amount_received == 0 # underpay session.price_adjust = -1 response = session.get(http_doggo_url) assert response.status_code == 402 assert session.invalid_amount_received == 1 # overpay session.price_adjust = 1 response = session.get(http_doggo_url) assert response.status_code == 402 assert session.invalid_amount_received == 2
def patched_payment( self: Session, method: str, url: str, response: Response, **kwargs ): response.headers[HTTPHeaders.PRICE] = str( int(response.headers[HTTPHeaders.PRICE]) + self.price_adjust ) return Session.on_payment_requested(self, method, url, response, **kwargs)
def patched_payment( self: Session, method: str, url: str, response: Response, **kwargs ): response.headers[HTTPHeaders.PRICE] = str( int(response.headers[HTTPHeaders.PRICE]) + self.price_adjust ) return Session.on_payment_requested(self, method, url, response, **kwargs)
def on_payment_requested(self, method: str, url: str, response: Response, **kwargs) -> bool: price = int(response.headers[HTTPHeaders.PRICE]) if price > self.max_rei_per_request: log.error( 'Requested price exceeds configured maximum price per request ({} > {}). ' 'Aborting'.format(price, self.max_rei_per_request)) return False return uSession.on_payment_requested(self, method, url, response, **kwargs)