def test_send_payment_success(self): outputs = [paymentrequest.Output(P2PKH_SCRIPT_HEX)] payment = paymentrequest.Payment("merchant_data", "transaction_hex", outputs) ack_memo = "ack_memo" payment_ack = paymentrequest.PaymentACK(payment, ack_memo) ack_json = payment_ack.to_json() class PaymentRequestTestable(paymentrequest.PaymentRequest): def _make_request(self, url, message): return TestPaymentRequest._FakeRequestResponse( 200, NotImplemented, ack_json) outputs = [paymentrequest.Output(P2PKH_SCRIPT_HEX)] creation_timestamp = int(time.time() + 100) expiration_timestamp = creation_timestamp + 100 payment_request = PaymentRequestTestable(outputs, creation_timestamp, expiration_timestamp, "memo", "pay_url", "merchant_data") refund_address = _generate_address() response_status, response_message = payment_request.send_payment( TRANSACTION_HEX, refund_address) # The response was successful. self.assertTrue(response_status) # The ack memo is the response message. self.assertEqual(ack_memo, response_message)
def test_dict_optional_fields_used(self): outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] payment = paymentrequest.Payment("merchant_data", "transaction_hex", outputs, "memo") data = payment.to_dict() self.assertTrue('merchantData' in data) self.assertTrue('refundTo' in data) self.assertTrue('memo' in data)
def test_json_restoration_required(self): original_output = paymentrequest.Output(P2PKH_SCRIPT) output_json = original_output.to_json() restored_output = paymentrequest.Output.from_json(output_json) self.assertEqual(original_output.script, restored_output.script) self.assertEqual(original_output.amount, restored_output.amount) self.assertEqual(original_output.description, restored_output.description)
def test_dict_optional_fields_used(self): output = paymentrequest.Output(P2PKH_SCRIPT, 1, "description") data = output.to_dict() self.assertTrue('script' in data) self.assertEqual(P2PKH_SCRIPT.to_hex(), data['script']) self.assertTrue('amount' in data) self.assertTrue('description' in data)
def test_json_restoration_required(self): outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] payment = paymentrequest.Payment("merchant_data", "transaction_hex", outputs) original = paymentrequest.PaymentACK(payment) json_value = original.to_json() restored = paymentrequest.PaymentACK.from_json(json_value) self.assertEqual(original.memo, restored.memo)
def test_dict_optional_fields_used(self): outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] payment = paymentrequest.Payment("merchant_data", "transaction_hex", outputs, "memo") payment_ack = paymentrequest.PaymentACK(payment, 'memo') data = payment_ack.to_dict() self.assertTrue('payment' in data) self.assertTrue('memo' in data)
def test_json_restoration_required(self): outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] original = paymentrequest.Payment("merchant_data", "transaction_hex", outputs) json_value = original.to_json() restored = paymentrequest.Payment.from_json(json_value) self.assertEqual(original.merchant_data, restored.merchant_data) self.assertEqual(original.transaction_hex, restored.transaction_hex) self.assertEqual(len(original.refund_outputs), len(restored.refund_outputs)) self.assertEqual(original.refund_outputs[0].script, restored.refund_outputs[0].script) self.assertEqual(original.memo, restored.memo)
def test_json_restoration_required(self): outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] original = paymentrequest.PaymentRequest(outputs) json_value = original.to_json() restored = paymentrequest.PaymentRequest.from_json(json_value) self.assertEqual(len(original.outputs), len(restored.outputs)) self.assertEqual(original.outputs[0].script, restored.outputs[0].script) self.assertEqual(original.creation_timestamp, restored.creation_timestamp) self.assertEqual(original.expiration_timestamp, restored.expiration_timestamp) self.assertEqual(original.memo, restored.memo) self.assertEqual(original.payment_url, restored.payment_url) self.assertEqual(original.merchant_data, restored.merchant_data)
def test_dict_optional_fields_used(self): outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] creation_timestamp = int(time.time() + 100) expiration_timestamp = creation_timestamp + 100 request = paymentrequest.PaymentRequest( outputs, creation_timestamp, expiration_timestamp, "memo", "pay_url", "merchant_data") json_value = request.to_json() data = json.loads(json_value) self.assertTrue('creationTimestamp' in data) self.assertTrue('expirationTimestamp' in data) self.assertTrue('memo' in data) self.assertTrue('paymentUrl' in data) self.assertTrue('merchantData' in data)
def test_send_payment_ssl_exception(self): class PaymentRequestTestable(paymentrequest.PaymentRequest): def _make_request(self, url, message): return None outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] creation_timestamp = int(time.time() + 100) expiration_timestamp = creation_timestamp + 100 payment_request = PaymentRequestTestable( outputs, creation_timestamp, expiration_timestamp, "memo", "pay_url", "merchant_data") response_status, response_message = payment_request.send_payment( TRANSACTION_HEX) self.assertFalse(response_status)
def test_send_payment_400_error(self): class PaymentRequestTestable(paymentrequest.PaymentRequest): def _make_request(self, url, message): return TestPaymentRequest._FakeRequestResponse(400, "reason", b"content") outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] creation_timestamp = int(time.time() + 100) expiration_timestamp = creation_timestamp + 100 payment_request = PaymentRequestTestable( outputs, creation_timestamp, expiration_timestamp, "memo", "pay_url", "merchant_data") response_status, response_message = payment_request.send_payment( TRANSACTION_HEX) self.assertFalse(response_status) self.assertEqual("reason: content", response_message)
def test_json_restoration_all(self): outputs = [ paymentrequest.Output(P2PKH_SCRIPT) ] creation_timestamp = int(time.time() + 100) expiration_timestamp = creation_timestamp + 100 original = paymentrequest.PaymentRequest( outputs, creation_timestamp, expiration_timestamp, "memo", "pay_url", "merchant_data") json_value = original.to_json() restored = paymentrequest.PaymentRequest.from_json(json_value) self.assertEqual(len(original.outputs), len(restored.outputs)) self.assertEqual(original.outputs[0].script, restored.outputs[0].script) self.assertEqual(original.creation_timestamp, restored.creation_timestamp) self.assertEqual(original.expiration_timestamp, restored.expiration_timestamp) self.assertEqual(original.memo, restored.memo) self.assertEqual(original.payment_url, restored.payment_url) self.assertEqual(original.merchant_data, restored.merchant_data)
def test_json_restoration_all(self): outputs = [paymentrequest.Output(P2PKH_SCRIPT_HEX)] payment = paymentrequest.Payment("merchant_data", "transaction_hex", outputs, "memo") original = paymentrequest.PaymentACK(payment) json_value = original.to_json() restored = paymentrequest.PaymentACK.from_json(json_value) self.assertEqual(original.payment.merchant_data, restored.payment.merchant_data) self.assertEqual(original.payment.transaction_hex, restored.payment.transaction_hex) self.assertEqual(len(original.payment.refund_outputs), len(restored.payment.refund_outputs)) self.assertEqual(original.payment.refund_outputs[0].script_hex, restored.payment.refund_outputs[0].script_hex) self.assertEqual(original.payment.memo, restored.payment.memo) self.assertEqual(original.memo, restored.memo)
def test_dict_optional_fields_unused(self): output = paymentrequest.Output(P2PKH_SCRIPT) data = output.to_dict() self.assertTrue('amount' not in data) self.assertTrue('description' not in data)
def test_get_address_string(self): output = paymentrequest.Output(P2PKH_SCRIPT) self.assertEqual(PKH_ADDRESS.to_string(), output.get_address_string())