def test_constructor_with_sandbox(self): print("testing constructor with sandbox.") channel_id = "hoge" channel_secret = "fuga" api = linepay.LinePayApi(channel_id, channel_secret, is_sandbox=True) # assert self.assertEqual(api.headers.get("Content-Type"), "application/json") self.assertEqual(api.headers.get("X-LINE-ChannelId"), channel_id) self.assertEqual(api.channel_id, channel_id) self.assertEqual(api.channel_secret, channel_secret) self.assertEqual(api.api_endpoint, linepay.LinePayApi.SANDBOX_API_ENDPOINT)
def test_constructor_with_default_endpoint(self): print("testing constructor with production.") channel_id = "hoge" channel_secret = "fuga" api = linepay.LinePayApi(channel_id, channel_secret) # assert self.assertEqual(api.headers.get("Content-Type"), "application/json") self.assertEqual(api.headers.get("X-LINE-ChannelId"), channel_id) self.assertEqual(api.channel_id, channel_id) self.assertEqual(api.channel_secret, channel_secret) self.assertEqual(api.api_endpoint, linepay.LinePayApi.DEFAULT_API_ENDPOINT)
def test_payment_details(self): with patch('linepay.api.requests.get') as get: mock_api_result = MagicMock(return_value={"returnCode": "0000"}) get.return_value.json = mock_api_result mock_sign = MagicMock(return_value={"X-LINE-Authorization": "dummy"}) api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) api.sign = mock_sign result = api.payment_details() self.assertEqual(result, mock_api_result.return_value) path = "/v3/payments" mock_sign.assert_called_once_with(api.headers, path, "")
def test_payment_status_with_safe_return_code_0122(self): with patch('linepay.api.requests.get') as get: mock_api_result = MagicMock(return_value={"returnCode": "0122"}) get.return_value.json = mock_api_result mock_sign = MagicMock(return_value={"X-LINE-Authorization": "dummy"}) api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) api.sign = mock_sign transaction_id = 1234567890 result = api.check_payment_status(transaction_id) self.assertEqual(result, mock_api_result.return_value) path = "/v3/payments/requests/{}/check".format( transaction_id ) mock_sign.assert_called_once_with(api.headers, path, "")
def test_check_reg_key_with_safe_return_code_1193(self): with patch('linepay.api.requests.get') as get: mock_api_result = MagicMock(return_value={"returnCode": "1193"}) get.return_value.json = mock_api_result mock_sign = MagicMock(return_value={"X-LINE-Authorization": "dummy"}) api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) api.sign = mock_sign reg_key = "regkey-1234567890" result = api.check_regkey(reg_key) self.assertEqual(result, mock_api_result.return_value) path = "/v3/payments/preapprovedPay/{}/check".format( reg_key ) mock_sign.assert_called_once_with(api.headers, path, "")
def test_sign(self): print("testing sign.") channel_id = "hoge" channel_secret = "fuga" api = linepay.LinePayApi(channel_id, channel_secret, is_sandbox=True) body_str = '{"amount": 1, "currency": "JPY", "orderId": "5383b36e-fe10-4767-b11b-81eefd1752fa", "packages": [{"id": "package-999", "amount": 1, "name": "Sample package", "products": [{"id": "product-001", "name": "Sample product", "quantity": 1, "price": 1}]}], "redirectUrls": {"confirmUrl": "https://example.com/pay/confirm", "cancelUrl": "https://example.com/pay/cancel"}}' nonce = "021a6bb9-ed18-4562-b9bd-ad07a27532f6" api._create_nonce = MagicMock(return_value=nonce) result = api.sign(api.headers, "/v3/payments/request", body_str) print(result) self.assertEqual(result["X-LINE-ChannelId"], channel_id) self.assertEqual(result["Content-Type"], "application/json") self.assertEqual(result["X-LINE-Authorization-Nonce"], nonce) self.assertEqual(result["X-LINE-Authorization"], "Rz5VEwPHChlQgN+dEmYWWbtWKw0XS41MblRB/dRdygE=")
def test_expire_regkey(self): with patch('linepay.api.requests.post') as post: mock_api_result = MagicMock(return_value={"returnCode": "0000"}) post.return_value.json = mock_api_result mock_sign = MagicMock(return_value={"X-LINE-Authorization": "dummy"}) api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) api.sign = mock_sign reg_key = "regkey-1234567890" result = api.expire_regkey(reg_key) self.assertEqual(result, mock_api_result.return_value) path = "/v3/payments/preapprovedPay/{}/expire".format( reg_key ) request_options = {} mock_sign.assert_called_once_with(api.headers, path, json.dumps(request_options))
def test_payment_details_with_transaction_id(self): with patch('linepay.api.requests.get') as get: mock_api_result = MagicMock(return_value={"returnCode": "0000"}) get.return_value.json = mock_api_result mock_sign = MagicMock(return_value={"X-LINE-Authorization": "dummy"}) api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) api.sign = mock_sign transaction_id = 1234567890 result = api.payment_details(transaction_id=transaction_id) self.assertEqual(result, mock_api_result.return_value) path = "/v3/payments" query = "transactionId={}".format( transaction_id ) mock_sign.assert_called_once_with(api.headers, path, query)
def test_pay_preapproved_with_failed_return_code(self): with patch('linepay.api.requests.post') as post: # setup mocks api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) signed_header = deepcopy(api.headers) signed_header["X-LINE-Authorization"] = "dummy" mock_sign = MagicMock(return_value=signed_header) api.sign = mock_sign mock_api_result = MagicMock(return_value={"returnCode": "1101"}) post.return_value.json = mock_api_result reg_key = "regkey-1234567890" product_name = "product-1234567890" amount = 10.0 currency = "JPY" order_id = "order-1234567890" expected_path = "/v3/payments/preapprovedPay/{}/payment".format( reg_key ) expected_url = "{api_endpoint}{path}".format( api_endpoint=api.SANDBOX_API_ENDPOINT, path=expected_path ) request_options = { "productName": product_name, "amount": int(amount), "currency": currency, "orderId": order_id, "capture": True } # execute with self.assertRaises(LinePayApiError): result = api.pay_preapproved(reg_key, product_name, amount, currency, order_id) # assert mock_sign.assert_called_once_with( api.headers, expected_path, json.dumps(request_options) ) post.assert_called_once_with( expected_url, json.dumps(request_options), headers=signed_header )
def test_capture_with_failed_return_code(self): with patch('linepay.api.requests.post') as post: # setup mocks api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) signed_header = deepcopy(api.headers) signed_header["X-LINE-Authorization"] = "dummy" mock_sign = MagicMock(return_value=signed_header) api.sign = mock_sign mock_api_result = MagicMock(return_value={"returnCode": "1104"}) post.return_value.json = mock_api_result transaction_id = 1234567890 amount = 10.0 currency = "JPY" expected_path = "/v3/payments/authorizations/{}/capture".format( transaction_id ) expected_url = "{api_endpoint}{path}".format( api_endpoint=api.SANDBOX_API_ENDPOINT, path=expected_path ) request_options = { "amount": int(amount), "currency": currency } # execute with self.assertRaises(LinePayApiError): result = api.capture(transaction_id, amount, currency) # assert mock_sign.assert_called_once_with( api.headers, expected_path, json.dumps(request_options) ) post.assert_called_once_with( expected_url, json.dumps(request_options), headers=signed_header )
def test_constructor(self): print("testing constructor.") with self.assertRaises(ValueError): channel_id = "hoge" channel_secret = None api = linepay.LinePayApi(channel_id, channel_secret, is_sandbox=True)
def test_request_with_invalid_param(self): with patch('linepay.api.requests.post') as post: api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) with self.assertRaises(ValueError): api.request(None) post.assert_not_called()
def test_payment_details_with_failed_return_code(self): with patch('linepay.api.requests.get') as get: get.return_value.json = MagicMock(return_value={"returnCode": "1104"}) api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) with self.assertRaises(LinePayApiError): result = api.payment_details()
def test_payment_status_with_invalid_transaction_id(self): with patch('linepay.api.requests.get') as get: api = linepay.LinePayApi("channel_id", "channel_secret", is_sandbox=True) with self.assertRaises(ValueError): transaction_id = "invalid" result = api.check_payment_status(transaction_id)