Пример #1
0
 def test_initialization_without_client_secret_fails(self):
     with self.assertRaises(exceptions.InvalidArgumentError):
         token_service = authorization.TokenService(
             base_url=SAMPLE_BASE_URL,
             client_id=SAMPLE_CLIENT_ID,
             client_secret=None)
         self.assertIsInstance(token_service, authorization.TokenService)
Пример #2
0
class TransactionNotificationsTestCase(unittest.TestCase):
    transaction_notifications_url = ''
    # Establish environment
    validate = URLValidator()

    token_service = authorization.TokenService(SAMPLE_BASE_URL, SAMPLE_CLIENT_ID, SAMPLE_CLIENT_SECRET)
    access_token_request = token_service.request_access_token()
    ACCESS_TOKEN = token_service.get_access_token(access_token_request)

    transaction_notifications_obj = notifications.NotificationService(base_url=SAMPLE_BASE_URL)
    header = dict(transaction_notifications_obj._headers)
    header['Authorization'] = 'Bearer ' + ACCESS_TOKEN

    def test_init_method_with_base_url_argument_succeeds(self):
        transaction_notifications_service = notifications.NotificationService(base_url=SAMPLE_BASE_URL)
        self.assertIsInstance(transaction_notifications_service, notifications.NotificationService)

    def test_init_method_without_base_url_argument_fails(self):
        self.assertRaises(TypeError, lambda: notifications.NotificationService())

    # Transaction Notification transaction_notifications
    def test_sending_transaction_sms_notification_succeeds(self):
        test_payload = {
            "access_token": TransactionNotificationsTestCase.ACCESS_TOKEN,
            "callback_url": 'https://webhook.site/48d6113c-8967-4bf4-ab56-dcf470e0b005',
            "webhook_event_reference": "d81312b9-4c0e-4347-971f-c3d5b14bdbe4",
            "message": 'Alleluia',
        }
        self.assertIsNotNone(
            TransactionNotificationsTestCase.transaction_notifications_obj.send_transaction_sms_notification(test_payload))

    def test_sending_transaction_sms_notification_returns_resource_url(self):
        test_payload = {
            "access_token": TransactionNotificationsTestCase.ACCESS_TOKEN,
            "callback_url": 'https://webhook.site/48d6113c-8967-4bf4-ab56-dcf470e0b005',
            "webhook_event_reference": "d81312b9-4c0e-4347-971f-c3d5b14bdbe4",
            "message": 'Alleluia',
        }
        response = TransactionNotificationsTestCase.transaction_notifications_obj.send_transaction_sms_notification(test_payload)
        if self.assertIsNone(TransactionNotificationsTestCase.validate(response)) is None:
            TransactionNotificationsTestCase.transaction_notifications_url = response
        self.assertIsNone(TransactionNotificationsTestCase.validate(response))

    # Query Request
    def test_successfully_sent_transaction_sms_notification_status_succeeds(self):
        self.assertIsNotNone(
            TransactionNotificationsTestCase.transaction_notifications_obj.transaction_notification_status(
                TransactionNotificationsTestCase.ACCESS_TOKEN,
                TransactionNotificationsTestCase.transaction_notifications_url))

    def test_successfully_sent_transaction_sms_notification_status_request(self):
        response = requests.get(
            headers=TransactionNotificationsTestCase.header,
            url=TransactionNotificationsTestCase.transaction_notifications_url)
        self.assertEqual(response.status_code, 200)
Пример #3
0
class PollingTestCase(unittest.TestCase):
    polling_url = ''
    # Establish environment
    validate = URLValidator()

    token_service = authorization.TokenService(SAMPLE_BASE_URL,
                                               SAMPLE_CLIENT_ID,
                                               SAMPLE_CLIENT_SECRET)
    access_token_request = token_service.request_access_token()
    ACCESS_TOKEN = token_service.get_access_token(access_token_request)

    polling_obj = polling.PollingService(base_url=SAMPLE_BASE_URL)
    header = dict(polling_obj._headers)
    header['Authorization'] = 'Bearer ' + ACCESS_TOKEN

    def test_init_method_with_base_url_argument_succeeds(self):
        transaction_notifications_service = polling.PollingService(
            base_url=SAMPLE_BASE_URL)
        self.assertIsInstance(transaction_notifications_service,
                              polling.PollingService)

    def test_init_method_without_base_url_argument_fails(self):
        self.assertRaises(TypeError, lambda: polling.PollingService())

    # Transaction Notification transaction_notifications
    def test_create_polling_request_returns_resource_url(self):
        test_payload = {
            "access_token": PollingTestCase.ACCESS_TOKEN,
            "callback_url":
            'https://webhook.site/48d6113c-8967-4bf4-ab56-dcf470e0b005',
            "scope": "till",
            "scope_reference": "112233",
            "from_time": "2021-07-09T08:50:22+03:00",
            "to_time": "2021-07-10T18:00:22+03:00",
        }
        response = PollingTestCase.polling_obj.create_polling_request(
            test_payload)
        if self.assertIsNone(PollingTestCase.validate(response)) is None:
            PollingTestCase.polling_url = response
        self.assertIsNone(PollingTestCase.validate(response))

    # Query Request
    def test_successfully_sent_transaction_sms_notification_status_succeeds(
            self):
        self.assertIsNotNone(
            PollingTestCase.polling_obj.polling_request_status(
                PollingTestCase.ACCESS_TOKEN, PollingTestCase.polling_url))

    def test_successfully_sent_transaction_sms_notification_status_request(
            self):
        response = requests.get(headers=PollingTestCase.header,
                                url=PollingTestCase.polling_url)
        self.assertEqual(response.status_code, 200)
Пример #4
0
 def test_initialization_without_all_arguments_fails(self):
     with self.assertRaises(exceptions.InvalidArgumentError):
         token_service = authorization.TokenService(base_url=None,
                                                    client_id=None,
                                                    client_secret=None)
         self.assertIsInstance(token_service, authorization.TokenService)
Пример #5
0
 def test_initialization_with_all_arguments_present_succeeds(self):
     token_service = authorization.TokenService(
         base_url=SAMPLE_BASE_URL,
         client_id=SAMPLE_CLIENT_ID,
         client_secret=SAMPLE_CLIENT_SECRET)
     self.assertIsInstance(token_service, authorization.TokenService)
Пример #6
0
class TokenServiceTestCase(unittest.TestCase):
    token_service_obj = authorization.TokenService(
        base_url=SAMPLE_BASE_URL,
        client_id=SAMPLE_CLIENT_ID,
        client_secret=SAMPLE_CLIENT_SECRET)
    header = dict(token_service_obj._headers)

    def test_initialization_with_all_arguments_present_succeeds(self):
        token_service = authorization.TokenService(
            base_url=SAMPLE_BASE_URL,
            client_id=SAMPLE_CLIENT_ID,
            client_secret=SAMPLE_CLIENT_SECRET)
        self.assertIsInstance(token_service, authorization.TokenService)

    def test_initialization_without_base_url_fails(self):
        with self.assertRaises(exceptions.InvalidArgumentError):
            token_service = authorization.TokenService(
                base_url=None,
                client_id=SAMPLE_CLIENT_ID,
                client_secret=SAMPLE_CLIENT_SECRET)
            self.assertIsInstance(token_service, authorization.TokenService)

    def test_initialization_without_client_id_fails(self):
        with self.assertRaises(exceptions.InvalidArgumentError):
            token_service = authorization.TokenService(
                base_url=SAMPLE_BASE_URL,
                client_id=None,
                client_secret=SAMPLE_CLIENT_SECRET)
            self.assertIsInstance(token_service, authorization.TokenService)

    def test_initialization_without_client_secret_fails(self):
        with self.assertRaises(exceptions.InvalidArgumentError):
            token_service = authorization.TokenService(
                base_url=SAMPLE_BASE_URL,
                client_id=SAMPLE_CLIENT_ID,
                client_secret=None)
            self.assertIsInstance(token_service, authorization.TokenService)

    def test_initialization_without_all_arguments_fails(self):
        with self.assertRaises(exceptions.InvalidArgumentError):
            token_service = authorization.TokenService(base_url=None,
                                                       client_id=None,
                                                       client_secret=None)
            self.assertIsInstance(token_service, authorization.TokenService)

    def test_successful_create_incoming_payment_request(self):
        response = requests.post(
            headers=TokenServiceTestCase.header,
            params={
                'grant_type': 'client_credentials',
                'client_id': SAMPLE_CLIENT_ID,
                'client_secret': SAMPLE_CLIENT_SECRET,
            },
            data=None,
            url=TokenServiceTestCase.token_service_obj._build_url(
                authorization.AUTHORIZATION_PATH))
        self.assertEqual(response.status_code, 200)

    def test_request_access_token_returns_response(self):
        response = TokenServiceTestCase.token_service_obj.request_access_token(
        )
        self.assertIsNotNone(response)

    def test_request_access_token_returns_access_token(self):
        token_request = TokenServiceTestCase.token_service_obj.request_access_token(
        )
        access_token = TokenServiceTestCase.token_service_obj.get_access_token(
            token_request)
        self.assertIsNotNone(access_token)
Пример #7
0
class TransferTestCase(unittest.TestCase):
    verified_settlement_url = ''
    transfer_funds_url = ''
    # Establish environment
    validate = URLValidator()

    token_service = authorization.TokenService(SAMPLE_BASE_URL,
                                               SAMPLE_CLIENT_ID,
                                               SAMPLE_CLIENT_SECRET)
    access_token_request = token_service.request_access_token()
    ACCESS_TOKEN = token_service.get_access_token(access_token_request)

    settlement_transfer_obj = transfers.TransferService(
        base_url=SAMPLE_BASE_URL)
    header = dict(settlement_transfer_obj._headers)
    header['Authorization'] = 'Bearer ' + ACCESS_TOKEN

    def test_init_method_with_base_url_argument_succeeds(self):
        transfer_service = transfers.TransferService(base_url=SAMPLE_BASE_URL)
        self.assertIsInstance(transfer_service, transfers.TransferService)

    def test_init_method_without_base_url_argument_fails(self):
        self.assertRaises(TypeError, lambda: transfers.TransferService())

    # Add Settlement Accounts
    # Bank account
    def test_add_bank_settlement_account_for_RTS_transfer_succeeds(self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "settlement_method": 'RTS',
            "account_name": 'py_sdk_account_name',
            "account_number": 'py_sdk_account_number',
            "bank_branch_ref": '633aa26c-7b7c-4091-ae28-96c0687cf886'
        }
        self.assertIsNotNone(
            TransferTestCase.settlement_transfer_obj.
            add_bank_settlement_account(test_payload))

    def test_add_bank_settlement_account_for_EFT_transfer_succeeds(self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "settlement_method": 'EFT',
            "account_name": 'py_sdk_account_name',
            "account_number": 'py_sdk_account_number',
            "bank_branch_ref": '633aa26c-7b7c-4091-ae28-96c0687cf886'
        }
        self.assertIsNotNone(
            TransferTestCase.settlement_transfer_obj.
            add_bank_settlement_account(test_payload))

    def test_successful_add_bank_settlement_account_for_EFT_transfer_returns_resource_url(
            self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "settlement_method": 'EFT',
            "account_name": 'py_sdk_account_name',
            "account_number": 'py_sdk_account_number',
            "bank_branch_ref": '633aa26c-7b7c-4091-ae28-96c0687cf886'
        }
        response = TransferTestCase.settlement_transfer_obj.add_bank_settlement_account(
            test_payload)
        if self.assertIsNone(TransferTestCase.validate(response)) is None:
            TransferTestCase.verified_settlement_url = response
        self.assertIsNone(TransferTestCase.validate(response))

    def test_successful_add_bank_settlement_account_for_RTS_transfer_returns_resource_url(
            self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "settlement_method": 'RTS',
            "account_name": 'py_sdk_account_name',
            "account_number": 'py_sdk_account_number',
            "bank_branch_ref": '633aa26c-7b7c-4091-ae28-96c0687cf886'
        }
        response = TransferTestCase.settlement_transfer_obj.add_bank_settlement_account(
            test_payload)
        if self.assertIsNone(TransferTestCase.validate(response)) is None:
            TransferTestCase.verified_settlement_url = response
        self.assertIsNone(TransferTestCase.validate(response))

    def test_add_bank_settlement_account_for_EFT_transfer_request(self):
        response = requests.post(
            headers=TransferTestCase.header,
            json=json_builder.bank_settlement_account(
                "EFT", "py_sdk_account_name", "py_sdk_account_number",
                "633aa26c-7b7c-4091-ae28-96c0687cf886"),
            data=None,
            url=TransferTestCase.settlement_transfer_obj._build_url(
                transfers.SETTLEMENT_BANK_ACCOUNTS_PATH))
        self.assertEqual(response.status_code, 201)

    def test_add_bank_settlement_account_for_RTS_transfer_request(self):
        response = requests.post(
            headers=TransferTestCase.header,
            json=json_builder.bank_settlement_account(
                "RTS", "py_sdk_account_name", "py_sdk_account_number",
                "633aa26c-7b7c-4091-ae28-96c0687cf886"),
            data=None,
            url=TransferTestCase.settlement_transfer_obj._build_url(
                transfers.SETTLEMENT_BANK_ACCOUNTS_PATH))
        self.assertEqual(response.status_code, 201)

    # Failure scenarios
    def test_add_bank_settlement_account_with_invalid_params_fails(self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "settlement_method": 'EFT',
            "account_number": 'account_number',
            "bank_branch_ref": '633aa26c-7b7c-4091-ae28-96c0687cf886'
        }
        with self.assertRaisesRegex(
                InvalidArgumentError,
                'Invalid arguments for creating Bank Settlement Account.'):
            TransferTestCase.settlement_transfer_obj.add_bank_settlement_account(
                test_payload)

    # Mobile Wallet
    def test_add_mobile_wallet_settlement_account_succeeds(self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "first_name": 'py_sdk_first_name',
            "last_name": 'py_sdk_last_name',
            "phone_number": '+254911222538',
            "network": 'Safaricom'
        }
        self.assertIsNotNone(
            TransferTestCase.settlement_transfer_obj.
            add_mobile_wallet_settlement_account(test_payload))

    def test_successful_add_mobile_wallet_settlement_account_returns_resource_url(
            self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "first_name": 'py_sdk_first_name',
            "last_name": 'py_sdk_last_name',
            "phone_number": '+254911222538',
            "network": 'Safaricom'
        }
        response = TransferTestCase.settlement_transfer_obj.add_mobile_wallet_settlement_account(
            test_payload)
        if self.assertIsNone(TransferTestCase.validate(response)) is None:
            TransferTestCase.verified_settlement_url = response
        self.assertIsNone(TransferTestCase.validate(response))

    def test_add_mobile_wallet_settlement_account_request(self):
        response = requests.post(
            headers=TransferTestCase.header,
            json=json_builder.mobile_settlement_account(
                "py_sdk_first_name", "py_sdk_last_name", "254900112502",
                "safaricom"),
            data=None,
            url=TransferTestCase.settlement_transfer_obj._build_url(
                transfers.SETTLEMENT_MOBILE_ACCOUNTS_PATH))
        self.assertEqual(response.status_code, 201)

    # Failure scenarios
    def test_add_mobile_wallet_settlement_account_with_invalid_phone_fails(
            self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "first_name": 'py_sdk_first_name',
            "last_name": 'py_sdk_last_name',
            "phone_number": 'phone_number',
            "network": 'Safaricom'
        }
        with self.assertRaises(InvalidArgumentError):
            TransferTestCase.settlement_transfer_obj.add_mobile_wallet_settlement_account(
                test_payload)

    # Transfer/Settle funds
    # Blind Transfer
    def test_blind_transfer_succeeds(self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "value": '10',
        }
        self.assertIsNotNone(
            TransferTestCase.settlement_transfer_obj.settle_funds(
                test_payload))

    def test_successful_blind_transfer_transaction_returns_resource_url(self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "value": '10',
        }
        response = TransferTestCase.settlement_transfer_obj.settle_funds(
            test_payload)
        if self.assertIsNone(TransferTestCase.validate(response)) is None:
            TransferTestCase.transfer_funds_url = response
        self.assertIsNone(TransferTestCase.validate(response))

    def test_successful_blind_transfer_request(self):
        response = requests.post(
            headers=TransferTestCase.header,
            json=json_builder.transfers(
                json_builder.links(
                    'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d'
                ), json_builder.amount('KES', "3300")),
            data=None,
            url=TransferTestCase.settlement_transfer_obj._build_url(
                transfers.TRANSFER_PATH))
        self.assertEqual(response.status_code, 201)

    # Targeted Transfer
    # Merchant Bank Account
    def test_targeted_transfer_to_merchant_bank_account_succeeds(self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "destination_type": 'merchant_bank_account',
            "destination_reference": '87bbfdcf-fb59-4d8e-b039-b85b97015a7e',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "value": '10',
        }
        self.assertIsNotNone(
            TransferTestCase.settlement_transfer_obj.settle_funds(
                test_payload))

    def test_successful_targeted_transfer_to_merchant_bank_account_returns_resource_url(
            self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "destination_type": 'merchant_bank_account',
            "destination_reference": '87bbfdcf-fb59-4d8e-b039-b85b97015a7e',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "value": '10',
        }
        response = TransferTestCase.settlement_transfer_obj.settle_funds(
            test_payload)
        if self.assertIsNone(TransferTestCase.validate(response)) is None:
            TransferTestCase.transfer_funds_url = response
        self.assertIsNone(TransferTestCase.validate(response))

    def test_successful_targeted_transfer_to_merchant_bank_account_request(
            self):
        response = requests.post(
            headers=TransferTestCase.header,
            json=json_builder.transfers(
                json_builder.links(
                    'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d'
                ), json_builder.amount('KES', "3300"), **{
                    "destination_type": "merchant_bank_account",
                    "destination_reference":
                    "87bbfdcf-fb59-4d8e-b039-b85b97015a7e"
                }),
            data=None,
            url=TransferTestCase.settlement_transfer_obj._build_url(
                transfers.TRANSFER_PATH))
        self.assertEqual(response.status_code, 201)

    # Merchant Wallet
    def test_targeted_transfer_to_merchant_wallet_succeeds(self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "destination_type": 'merchant_wallet',
            "destination_reference": 'eba238ae-e03f-46f6-aed5-db357fb00f9c',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "value": '10',
        }
        self.assertIsNotNone(
            TransferTestCase.settlement_transfer_obj.settle_funds(
                test_payload))

    def test_successful_targeted_transfer_to_merchant_wallet_returns_resource_url(
            self):
        test_payload = {
            "access_token": TransferTestCase.ACCESS_TOKEN,
            "destination_type": 'merchant_wallet',
            "destination_reference": 'eba238ae-e03f-46f6-aed5-db357fb00f9c',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "value": '10',
        }
        response = TransferTestCase.settlement_transfer_obj.settle_funds(
            test_payload)
        if self.assertIsNone(TransferTestCase.validate(response)) is None:
            TransferTestCase.transfer_funds_url = response
        self.assertIsNone(TransferTestCase.validate(response))

    def test_successful_targeted_transfer_to_merchant_wallet_request(self):
        response = requests.post(
            headers=TransferTestCase.header,
            json=json_builder.transfers(
                json_builder.links(
                    'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d'
                ), json_builder.amount('KES', "3300"), **{
                    "destination_type": "merchant_wallet",
                    "destination_reference":
                    "eba238ae-e03f-46f6-aed5-db357fb00f9c"
                }),
            data=None,
            url=TransferTestCase.settlement_transfer_obj._build_url(
                transfers.TRANSFER_PATH))
        self.assertEqual(response.status_code, 201)

    # Query Transactions
    # Verified Settlement Account Status
    def test_successfully_added_settlement_account_status_succeeds(self):
        self.assertIsNotNone(
            TransferTestCase.settlement_transfer_obj.
            transfer_transaction_status(
                TransferTestCase.ACCESS_TOKEN,
                TransferTestCase.verified_settlement_url))

    def test_successfully_added_settlement_account_status_request(self):
        response = requests.get(headers=TransferTestCase.header,
                                url=TransferTestCase.verified_settlement_url)
        self.assertEqual(response.status_code, 200)

    # Transfer Transaction Status
    def test_successful_transferred_funds_transaction_status_succeeds(self):
        self.assertIsNotNone(
            TransferTestCase.settlement_transfer_obj.
            transfer_transaction_status(TransferTestCase.ACCESS_TOKEN,
                                        TransferTestCase.transfer_funds_url))

    def test_successful_transferred_funds_transaction_status_request(self):
        response = requests.get(headers=TransferTestCase.header,
                                url=TransferTestCase.transfer_funds_url)
        self.assertEqual(response.status_code, 200)
Пример #8
0
class ReceivePaymentTestCase(unittest.TestCase):
    query_url = ''
    # Establish environment
    validate = URLValidator()

    token_service = authorization.TokenService(SAMPLE_BASE_URL,
                                               SAMPLE_CLIENT_ID,
                                               SAMPLE_CLIENT_SECRET)
    access_token_request = token_service.request_access_token()
    ACCESS_TOKEN = token_service.get_access_token(access_token_request)

    incoming_payments_obj = receive_payments.ReceivePaymentsService(
        base_url=SAMPLE_BASE_URL)
    header = dict(incoming_payments_obj._headers)
    header['Authorization'] = 'Bearer ' + ACCESS_TOKEN

    def test_init_method_with_base_url_argument_succeeds(self):
        receive_payments_service = receive_payments.ReceivePaymentsService(
            base_url=SAMPLE_BASE_URL)
        self.assertIsInstance(receive_payments_service,
                              receive_payments.ReceivePaymentsService)

    def test_init_method_without_base_url_argument_fails(self):
        self.assertRaises(TypeError,
                          lambda: receive_payments.ReceivePaymentsService())

    def test_successful_create_incoming_payment_request(self):
        response = requests.post(
            headers=ReceivePaymentTestCase.header,
            json=json_builder.mpesa_payment(
                json_builder.links(
                    "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d"
                ), json_builder.amount('KES', 'python_sdk_value'),
                json_builder.subscriber('first_name', 'last_name',
                                        "+254712345678", 'Null'),
                'payment_channel', 'K112233'),
            data=None,
            url=ReceivePaymentTestCase.incoming_payments_obj._build_url(
                receive_payments.CREATE_RECEIVE_MPESA_PAYMENT_PATH))
        self.assertEqual(response.status_code, 201)

    def test_correct_incoming_payment_method_format(self):
        test_payload = {
            "access_token": ReceivePaymentTestCase.ACCESS_TOKEN,
            "callback_url":
            "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d",
            "first_name": "python_first_name",
            "last_name": "python_last_name",
            "email": "*****@*****.**",
            "payment_channel": "MPESA",
            "phone_number": "+254911222536",
            "till_number": "K112233",
            "amount": "10"
        }
        self.assertIsNotNone(
            ReceivePaymentTestCase.incoming_payments_obj.
            create_payment_request(test_payload))

    def test_create_incoming_payment_returns_resource_url(self):
        test_payload = {
            "access_token": ReceivePaymentTestCase.ACCESS_TOKEN,
            "callback_url":
            "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d",
            "first_name": "python_first_name",
            "last_name": "python_last_name",
            "email": "*****@*****.**",
            "payment_channel": "MPESA",
            "phone_number": "+254911222536",
            "till_number": "K112233",
            "amount": "10",
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        response = ReceivePaymentTestCase.incoming_payments_obj.create_payment_request(
            test_payload)
        if self.assertIsNone(
                ReceivePaymentTestCase.validate(response)) is None:
            ReceivePaymentTestCase.query_url = response
        self.assertIsNone(ReceivePaymentTestCase.validate(response))

    def test_create_payment_request_with_no_access_token_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            test_payload = {
                "callback_url":
                "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d",
                "first_name": "python_first_name",
                "last_name": "python_last_name",
                "email": "*****@*****.**",
                "payment_channel": "MPESA",
                "phone_number": "+254911222536",
                "till_number": "K112233",
                "amount": "10"
            }
            ReceivePaymentTestCase.incoming_payments_obj.create_payment_request(
                test_payload)

    def test_create_payment_request_with_invalid_params_fails(self):
        with self.assertRaisesRegex(
                InvalidArgumentError,
                'Invalid arguments for creating Incoming Payment Request.'):
            test_payload = {
                "access_token": ReceivePaymentTestCase.ACCESS_TOKEN,
                "callback_url":
                "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d",
                "last_name": "python_last_name",
                "email": "*****@*****.**",
                "payment_channel": "MPESA",
                "phone_number": "+254911222536",
                "till_number": "K112233",
                "amount": "10"
            }
            ReceivePaymentTestCase.incoming_payments_obj.create_payment_request(
                test_payload)

    def test_create_payment_request_with_invalid_phone_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    MSG["invalid_phone"]):
            ReceivePaymentTestCase.incoming_payments_obj.create_payment_request(
                {
                    "access_token": ReceivePaymentTestCase.ACCESS_TOKEN,
                    "callback_url":
                    "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d",
                    "first_name": "python_first_name",
                    "last_name": "python_last_name",
                    "email": "*****@*****.**",
                    "payment_channel": "MPESA",
                    "phone_number": "254911222536",
                    "till_number": "K112233",
                    "amount": "10"
                })

    def test_payment_request_status_succeeds(self):
        self.assertIsNotNone(
            ReceivePaymentTestCase.incoming_payments_obj.
            payment_request_status(ReceivePaymentTestCase.ACCESS_TOKEN,
                                   ReceivePaymentTestCase.query_url))

    def test_successful_query_incoming_payment_request(self):
        response = requests.get(headers=ReceivePaymentTestCase.header,
                                url=ReceivePaymentTestCase.query_url)
        self.assertEqual(response.status_code, 200)

    def test_payment_request_status_with_invalid_query_url_fails(self):
        with self.assertRaises(InvalidArgumentError):
            ReceivePaymentTestCase.incoming_payments_obj.payment_request_status(
                ReceivePaymentTestCase.ACCESS_TOKEN, 'payment/incoming')
Пример #9
0
class PayTestCase(unittest.TestCase):
    pay_transaction_query_url = ''
    pay_recipient_query_url = ''
    # Establish environment
    validate = URLValidator()

    token_service = authorization.TokenService(SAMPLE_BASE_URL,
                                               SAMPLE_CLIENT_ID,
                                               SAMPLE_CLIENT_SECRET)
    access_token_request = token_service.request_access_token()
    ACCESS_TOKEN = token_service.get_access_token(access_token_request)

    pay_obj = pay.PayService(base_url=SAMPLE_BASE_URL)
    header = dict(pay_obj._headers)
    header['Authorization'] = 'Bearer ' + ACCESS_TOKEN

    def test_init_method_with_base_url_argument_succeeds(self):
        pay_service = pay.PayService(base_url=SAMPLE_BASE_URL)
        self.assertIsInstance(pay_service, pay.PayService)

    def test_init_method_without_base_url_argument_fails(self):
        self.assertRaises(TypeError, lambda: pay.PayService())

    # Mobile Pay recipient
    def test_add_mobile_pay_recipient_succeeds(self):
        payload = PAY["mobile_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'mobile_wallet'
        })
        self.assertIsNotNone(PayTestCase.pay_obj.add_pay_recipient(payload))

    def test_successful_add_mobile_pay_receipient_request(self):
        response = requests.post(
            headers=PayTestCase.header,
            json=json_builder.pay_recipient(
                "mobile_wallet",
                json_builder.mobile_wallet(
                    "first_name", "last_name",
                    "9764ef5f-fcd6-42c1-bbff-de280becc64b", "safaricom")),
            data=None,
            url=PayTestCase.pay_obj._build_url(pay.ADD_PAY_PATH))
        self.assertEqual(response.status_code, 201)

    def test_add_mobile_pay_recipient_returns_resource_url(self):
        payload = PAY["mobile_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'mobile_wallet'
        })
        response = PayTestCase.pay_obj.add_pay_recipient(payload)
        if self.assertIsNone(PayTestCase.validate(response)) is None:
            PayTestCase.pay_recipient_query_url = response
        self.assertIsNone(PayTestCase.validate(response))

    # Mobile Pay Recipient Failure Scenarios
    def test_add_mobile_pay_recipient_nil_access_token_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            PayTestCase.pay_obj.add_pay_recipient(PAY["mobile_pay"])

    def test_add_mobile_pay_recipient_nil_recipient_type_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            PayTestCase.pay_obj.add_pay_recipient(PAY["mobile_pay"])

    def test_add_mobile_pay_recipient_without_first_name_fails(self):
        payload = PAY["invalid_first_name_mobile_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'mobile_wallet'
        })
        with self.assertRaises(InvalidArgumentError):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    def test_add_mobile_pay_recipient_with_invalid_email_fails(self):
        payload = PAY["invalid_email_mobile_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'mobile_wallet'
        })
        with self.assertRaisesRegex(InvalidArgumentError,
                                    MSG["invalid_email"]):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    def test_add_mobile_pay_recipient_with_invalid_phone_fails(self):
        payload = PAY["invalid_phone_mobile_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'mobile_wallet'
        })
        with self.assertRaisesRegex(InvalidArgumentError,
                                    MSG["invalid_phone"]):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    # Bank Pay recipient
    def test_add_bank_pay_recipient_succeeds(self):
        payload = PAY["bank_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'bank_account'
        })
        self.assertIsNotNone(PayTestCase.pay_obj.add_pay_recipient(payload))

    def test_successful_add_bank_receipient_request(self):
        response = requests.post(
            headers=PayTestCase.header,
            json=json_builder.pay_recipient(
                "bank_account",
                json_builder.bank_account(
                    account_name="David Kariuki Python",
                    account_number="566566",
                    settlement_method="EFT",
                    bank_branch_ref="633aa26c-7b7c-4091-ae28-96c0687cf886",
                )),
            data=None,
            url=PayTestCase.pay_obj._build_url(pay.ADD_PAY_PATH))
        self.assertEqual(response.status_code, 201)

    def test_add_bank_pay_recipient_returns_resource_url(self):
        payload = PAY["bank_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'bank_account'
        })
        response = PayTestCase.pay_obj.add_pay_recipient(payload)
        if self.assertIsNone(PayTestCase.validate(response)) is None:
            PayTestCase.pay_recipient_query_url = response
        self.assertIsNone(PayTestCase.validate(response))

    # Bank Pay Recipient Failure Scenarios
    def test_add_bank_pay_recipient_nil_access_token_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            PayTestCase.pay_obj.add_pay_recipient(PAY["bank_pay"])

    def test_add_bank_pay_recipient_nil_recipient_type_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            PayTestCase.pay_obj.add_pay_recipient(PAY["bank_pay"])

    def test_add_bank_pay_recipient_without_first_name_fails(self):
        payload = PAY["invalid_first_name_bank_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'bank_account'
        })
        with self.assertRaises(InvalidArgumentError):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    def test_add_bank_pay_recipient_with_invalid_email_fails(self):
        payload = PAY["invalid_email_bank_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'bank_account'
        })
        with self.assertRaisesRegex(InvalidArgumentError,
                                    MSG["invalid_email"]):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    def test_add_bank_pay_recipient_with_invalid_phone_fails(self):
        payload = PAY["invalid_phone_bank_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'bank_account'
        })
        with self.assertRaisesRegex(InvalidArgumentError,
                                    MSG["invalid_phone"]):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    # Till Pay recipient
    def test_add_till_pay_recipient_succeeds(self):
        payload = PAY["till_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'till'
        })
        self.assertIsNotNone(PayTestCase.pay_obj.add_pay_recipient(payload))

    def test_successful_add_till_receipient_request(self):
        response = requests.post(
            headers=PayTestCase.header,
            json=json_builder.pay_recipient(
                "till",
                json_builder.till_pay_recipient(till_name="Python Test Till",
                                                till_number="567567")),
            data=None,
            url=PayTestCase.pay_obj._build_url(pay.ADD_PAY_PATH))
        self.assertEqual(response.status_code, 201)

    def test_add_till_pay_recipient_returns_resource_url(self):
        payload = PAY["till_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'till'
        })
        response = PayTestCase.pay_obj.add_pay_recipient(payload)
        if self.assertIsNone(PayTestCase.validate(response)) is None:
            PayTestCase.pay_recipient_query_url = response
        self.assertIsNone(PayTestCase.validate(response))

    # Till Pay Recipient Failure Scenarios
    def test_add_till_pay_recipient_nil_access_token_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            PayTestCase.pay_obj.add_pay_recipient(PAY["till_pay"])

    def test_add_till_pay_recipient_nil_recipient_type_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            PayTestCase.pay_obj.add_pay_recipient(PAY["till_pay"])

    def test_add_till_pay_recipient_without_till_name_fails(self):
        payload = PAY["invalid_till_name_till_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'till'
        })
        with self.assertRaisesRegex(
                InvalidArgumentError,
                "Invalid arguments for till Pay recipient"):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    def test_add_till_pay_recipient_without_till_number_fails(self):
        payload = PAY["invalid_till_number_till_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'till'
        })
        with self.assertRaisesRegex(
                InvalidArgumentError,
                "Invalid arguments for till Pay recipient"):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    # Paybill Pay recipient
    def test_add_paybill_pay_recipient_succeeds(self):
        payload = PAY["paybill_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'paybill'
        })
        self.assertIsNotNone(PayTestCase.pay_obj.add_pay_recipient(payload))

    def test_successful_add_paybill_receipient_request(self):
        response = requests.post(headers=PayTestCase.header,
                                 json=json_builder.pay_recipient(
                                     "paybill",
                                     json_builder.paybill_pay_recipient(
                                         paybill_name="Python Paybill",
                                         paybill_number="561830",
                                         paybill_account_number="account_two",
                                     )),
                                 data=None,
                                 url=PayTestCase.pay_obj._build_url(
                                     pay.ADD_PAY_PATH))
        self.assertEqual(response.status_code, 201)

    def test_add_paybill_pay_recipient_returns_resource_url(self):
        payload = PAY["paybill_pay"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'paybill'
        })
        response = PayTestCase.pay_obj.add_pay_recipient(payload)
        if self.assertIsNone(PayTestCase.validate(response)) is None:
            PayTestCase.pay_recipient_query_url = response
        self.assertIsNone(PayTestCase.validate(response))

    # Paybill Pay Recipient Failure Scenarios
    def test_add_paybill_pay_recipient_nil_access_token_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            PayTestCase.pay_obj.add_pay_recipient(PAY["paybill_pay"])

    def test_add_paybill_pay_recipient_nil_recipient_type_fails(self):
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Access Token not given.'):
            PayTestCase.pay_obj.add_pay_recipient(PAY["paybill_pay"])

    def test_add_paybill_pay_recipient_without_paybill_name_fails(self):
        payload = PAY["invalid_paybill_name_paybill"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'paybill'
        })
        with self.assertRaisesRegex(
                InvalidArgumentError,
                "Invalid arguments for paybill Pay recipient"):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    def test_add_paybill_pay_recipient_without_paybill_number_fails(self):
        payload = PAY["invalid_paybill_number_paybill"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'paybill'
        })
        with self.assertRaisesRegex(
                InvalidArgumentError,
                "Invalid arguments for paybill Pay recipient"):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    def test_add_paybill_pay_recipient_without_paybill_account_number_fails(
            self):
        payload = PAY["invalid_paybill_account_number_paybill"]
        payload.update({
            "access_token": PayTestCase.ACCESS_TOKEN,
            "recipient_type": 'paybill'
        })
        with self.assertRaisesRegex(
                InvalidArgumentError,
                "Invalid arguments for paybill Pay recipient"):
            PayTestCase.pay_obj.add_pay_recipient(payload)

    # Send Pay Transaction
    def test_send_pay_to_mobile_wallet_succeeds(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_reference": '9764ef5f-fcd6-42c1-bbff-de280becc64b',
            "destination_type": 'mobile_wallet',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "description": "test",
            "amount": '10',
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        self.assertIsNotNone(PayTestCase.pay_obj.send_pay(test_payload))

    def test_create_pay_to_bank_account_succeeds(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_reference": 'c533cb60-8501-440d-8150-7eaaff84616a',
            "destination_type": 'bank_account',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "description": "test",
            "amount": '10',
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        self.assertIsNotNone(PayTestCase.pay_obj.send_pay(test_payload))

    def test_create_pay_to_mobile_wallet_returns_resource_url(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_reference": '9764ef5f-fcd6-42c1-bbff-de280becc64b',
            "destination_type": 'mobile_wallet',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "description": "test",
            "amount": '10',
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        response = PayTestCase.pay_obj.send_pay(test_payload)
        if self.assertIsNone(PayTestCase.validate(response)) is None:
            PayTestCase.pay_transaction_query_url = response
        self.assertIsNone(PayTestCase.validate(response))

    def test_create_pay_to_bank_account_returns_resource_url(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_reference": 'c533cb60-8501-440d-8150-7eaaff84616a',
            "destination_type": 'bank_account',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "description": "test",
            "amount": '10',
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        response = PayTestCase.pay_obj.send_pay(test_payload)
        if self.assertIsNone(PayTestCase.validate(response)) is None:
            PayTestCase.pay_transaction_query_url = response
        self.assertIsNone(PayTestCase.validate(response))

    def test_successful_create_pay_request_to_mobile_wallet(self):
        response = requests.post(
            headers=PayTestCase.header,
            json=json_builder.pay(
                "9764ef5f-fcd6-42c1-bbff-de280becc64b", "mobile_wallet",
                json_builder.amount('KES', 'python_sdk_value'), "test",
                json_builder.links(
                    "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d"
                ),
                json_builder.metadata({
                    "cId": '8_675_309',
                    "notes": 'Salary payment May 2018'
                })),
            data=None,
            url=PayTestCase.pay_obj._build_url(pay.SEND_PAY_PATH))
        self.assertEqual(response.status_code, 201)

    def test_successful_create_pay_request_to_bank_account(self):
        response = requests.post(
            headers=PayTestCase.header,
            json=json_builder.pay(
                "c533cb60-8501-440d-8150-7eaaff84616a", "bank_account",
                json_builder.amount('KES', 'python_sdk_value'), "test",
                json_builder.links(
                    "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d"
                ),
                json_builder.metadata({
                    "cId": '8_675_309',
                    "notes": 'Salary payment May 2018'
                })),
            data=None,
            url=PayTestCase.pay_obj._build_url(pay.SEND_PAY_PATH))
        self.assertEqual(response.status_code, 201)

    # Send Pay Transaction Failure Scenarios
    def test_send_pay_without_destination_reference_fails(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_type": 'bank_account',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "amount": '10',
            "description": "test",
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        with self.assertRaisesRegex(
                InvalidArgumentError,
                'Invalid arguments for creating Outgoing Pay.'):
            PayTestCase.pay_obj.send_pay(test_payload)

    def test_send_pay_without_destination_type_fails(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_reference": '3344-effefnkka-132',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "amount": '10',
            "description": "test",
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        with self.assertRaisesRegex(
                InvalidArgumentError,
                'Invalid arguments for creating Outgoing Pay.'):
            PayTestCase.pay_obj.send_pay(test_payload)

    def test_send_pay_without_amount_fails(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_reference": '9764ef5f-fcd6-42c1-bbff-de280becc64b',
            "destination_type": 'mobile_wallet',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "description": "test",
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        with self.assertRaisesRegex(
                InvalidArgumentError,
                'Invalid arguments for creating Outgoing Pay.'):
            PayTestCase.pay_obj.send_pay(test_payload)

    def test_send_pay_with_invalid_callback_fails(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_reference": '9764ef5f-fcd6-42c1-bbff-de280becc64b',
            "destination_type": 'mobile_wallet',
            "amount": '10',
            "description": "test",
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        with self.assertRaisesRegex(
                InvalidArgumentError,
                'Invalid arguments for creating Outgoing Pay.'):
            PayTestCase.pay_obj.send_pay(test_payload)

    def test_send_pay_without_description_fails(self):
        test_payload = {
            "access_token": PayTestCase.ACCESS_TOKEN,
            "destination_reference": 'c533cb60-8501-440d-8150-7eaaff84616a',
            "destination_type": 'bank_account',
            "callback_url":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "amount": '10',
            "currency": 'KES',
            "metadata": {
                "hey": 'there',
                "mister": 'dude'
            }
        }
        with self.assertRaisesRegex(
                InvalidArgumentError,
                'Invalid arguments for creating Outgoing Pay.'):
            PayTestCase.pay_obj.send_pay(test_payload)

    # Query Status
    # Query Pay Transaction
    def test_pay_transaction_status_succeeds(self):
        self.assertIsNotNone(
            PayTestCase.pay_obj.pay_transaction_status(
                PayTestCase.ACCESS_TOKEN,
                PayTestCase.pay_transaction_query_url))

    def test_pay_transaction_request_status_returns_object(self):
        self.assertIsNotNone(
            PayTestCase.pay_obj.pay_transaction_status(
                PayTestCase.ACCESS_TOKEN,
                PayTestCase.pay_transaction_query_url))

    def test_successful_query_pay_transaction_request(self):
        response = requests.get(headers=PayTestCase.header,
                                url=PayTestCase.pay_transaction_query_url)
        self.assertEqual(response.status_code, 200)

    # Query Pay Recipient
    def test_pay_recipient_request_status_returns_object(self):
        self.assertIsNotNone(
            PayTestCase.pay_obj.pay_transaction_status(
                PayTestCase.ACCESS_TOKEN, PayTestCase.pay_recipient_query_url))

    def test_successful_query_pay_recipient_request(self):
        response = requests.get(headers=PayTestCase.header,
                                url=PayTestCase.pay_recipient_query_url)
        self.assertEqual(response.status_code, 200)

    # Query Status Failure Scenarios
    def test_pay_transaction_status_with_invalid_query_url_fails(self):
        with self.assertRaises(InvalidArgumentError):
            PayTestCase.pay_obj.pay_transaction_status(
                PayTestCase.ACCESS_TOKEN, "destination")

    def test_pay_transaction_status_with_invalid_access_token_fails(self):
        with self.assertRaises(InvalidArgumentError):
            PayTestCase.pay_obj.pay_transaction_status('access_token',
                                                       "destination")
Пример #10
0
class WebhooksTestCase(unittest.TestCase):
    # Establish environment
    validate = URLValidator()

    token_service = authorization.TokenService(SAMPLE_BASE_URL,
                                               SAMPLE_CLIENT_ID,
                                               SAMPLE_CLIENT_SECRET)
    access_token_request = token_service.request_access_token()
    ACCESS_TOKEN = token_service.get_access_token(access_token_request)

    webhook_obj = webhooks.WebhookService(base_url=SAMPLE_BASE_URL)
    header = dict(webhook_obj._headers)
    header['Authorization'] = 'Bearer ' + ACCESS_TOKEN

    def test_init_method_with_base_url_argument_succeeds(self):
        webhook_service = webhooks.WebhookService(base_url=SAMPLE_BASE_URL)
        self.assertIsInstance(webhook_service, webhooks.WebhookService)

    # Test Request Status
    def test_create_buygoods_webhook_subscription_request_succeeds(self):
        response = requests.post(
            headers=WebhooksTestCase.header,
            json=json_builder.webhook_subscription(
                "buygoods_transaction_received",
                "https://webhook.site/dcbdce14-dd4f-4493-be2c-ad3526354fa8",
                'till', '112233'),
            data=None,
            url=WebhooksTestCase.webhook_obj._build_url(
                webhooks.WEBHOOK_SUBSCRIPTION_PATH),
        )
        self.assertEqual(response.status_code, 201)

    def test_create_b2b_webhook_subscription_request_succeeds(self):
        response = requests.post(
            headers=WebhooksTestCase.header,
            json=json_builder.webhook_subscription(
                "b2b_transaction_received",
                "https://webhook.site/dcbdce14-dd4f-4493-be2c-ad3526354fa8",
                'till', '112233'),
            data=None,
            url=WebhooksTestCase.webhook_obj._build_url(
                webhooks.WEBHOOK_SUBSCRIPTION_PATH),
        )
        self.assertEqual(response.status_code, 201)

    def test_create_buygoods_reversal_webhook_subscription_request_succeeds(
            self):
        response = requests.post(
            headers=WebhooksTestCase.header,
            json=json_builder.webhook_subscription(
                "buygoods_transaction_reversed",
                "https://webhook.site/dcbdce14-dd4f-4493-be2c-ad3526354fa8",
                'till', '112233'),
            data=None,
            url=WebhooksTestCase.webhook_obj._build_url(
                webhooks.WEBHOOK_SUBSCRIPTION_PATH),
        )
        self.assertEqual(response.status_code, 201)

    def test_create_customer_created_webhook_subscription_request_succeeds(
            self):
        response = requests.post(
            headers=WebhooksTestCase.header,
            json=json_builder.webhook_subscription(
                "customer_created",
                "https://webhook.site/dcbdce14-dd4f-4493-be2c-ad3526354fa8",
                'company'),
            data=None,
            url=WebhooksTestCase.webhook_obj._build_url(
                webhooks.WEBHOOK_SUBSCRIPTION_PATH),
        )
        self.assertEqual(response.status_code, 201)

    def test_create_settlement_transfer_webhook_subscription_request_succeeds(
            self):
        response = requests.post(
            headers=WebhooksTestCase.header,
            json=json_builder.webhook_subscription(
                "settlement_transfer_completed",
                "https://webhook.site/dcbdce14-dd4f-4493-be2c-ad3526354fa8",
                'company'),
            data=None,
            url=WebhooksTestCase.webhook_obj._build_url(
                webhooks.WEBHOOK_SUBSCRIPTION_PATH),
        )
        self.assertEqual(response.status_code, 201)

    def test_create_m2m_transaction_received_webhook_subscription_request_succeeds(
            self):
        response = requests.post(
            headers=WebhooksTestCase.header,
            json=json_builder.webhook_subscription(
                "m2m_transaction_received",
                "https://webhook.site/dcbdce14-dd4f-4493-be2c-ad3526354fa8",
                'company'),
            data=None,
            url=WebhooksTestCase.webhook_obj._build_url(
                webhooks.WEBHOOK_SUBSCRIPTION_PATH),
        )
        self.assertEqual(response.status_code, 201)

    # Test that module successfully creates and sends the request
    def test_create_buygoods_webhook_succeeds(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'buygoods_transaction_received',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till',
            "scope_reference": '112233'
        }
        self.assertIsNotNone(
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload))

    def test_create_b2b_webhook_succeeds(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'b2b_transaction_received',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till',
            "scope_reference": '112233'
        }
        self.assertIsNotNone(
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload))

    def test_create_buygoods_reversal_webhook_succeeds(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'buygoods_transaction_reversed',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till',
            "scope_reference": '112233'
        }
        self.assertIsNotNone(
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload))

    def test_create_customer_created_succeeds(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'customer_created',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'company'
        }
        self.assertIsNotNone(
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload))

    def test_create_settlement_transfer_webhook_succeeds(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'settlement_transfer_completed',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'company'
        }
        self.assertIsNotNone(
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload))

    def test_create_m2m_transaction_received_webhook_succeeds(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'm2m_transaction_received',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'company'
        }
        self.assertIsNotNone(
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload))

    # Test it returns the resource_url
    def test_buygoods_webhook_subscription_returns_resource_url(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'buygoods_transaction_received',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till',
            "scope_reference": '112233'
        }
        response = webhooks.WebhookService(
            base_url=SAMPLE_BASE_URL).create_subscription(test_payload)
        self.assertIsNone(WebhooksTestCase.validate(response))

    def test_b2b_webhook_subscription_returns_resource_url(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'b2b_transaction_received',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till',
            "scope_reference": '112233'
        }
        response = webhooks.WebhookService(
            base_url=SAMPLE_BASE_URL).create_subscription(test_payload)
        self.assertIsNone(WebhooksTestCase.validate(response))

    def test_buygoods_reversal_webhook_subscription_returns_resource_url(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'buygoods_transaction_reversed',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till',
            "scope_reference": '112233'
        }
        response = webhooks.WebhookService(
            base_url=SAMPLE_BASE_URL).create_subscription(test_payload)
        self.assertIsNone(WebhooksTestCase.validate(response))

    def test_customer_created_webhook_subscription_returns_resource_url(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'customer_created',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'company'
        }
        response = webhooks.WebhookService(
            base_url=SAMPLE_BASE_URL).create_subscription(test_payload)
        self.assertIsNone(WebhooksTestCase.validate(response))

    def test_settlement_transfer_webhook_subscription_returns_resource_url(
            self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'settlement_transfer_completed',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'company'
        }
        response = webhooks.WebhookService(
            base_url=SAMPLE_BASE_URL).create_subscription(test_payload)
        self.assertIsNone(WebhooksTestCase.validate(response))

    def test_m2m_webhook_subscription_returns_resource_url(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'm2m_transaction_received',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'company'
        }
        response = webhooks.WebhookService(
            base_url=SAMPLE_BASE_URL).create_subscription(test_payload)
        self.assertIsNone(WebhooksTestCase.validate(response))

    # Test Failure scenarios
    def test_create_invalid_webhook_fails(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'settlement',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "webhook_secret": SAMPLE_WEBHOOK_SECRET,
            "scope": 'Till',
            "scope_reference": '112233'
        }
        with self.assertRaises(InvalidArgumentError):
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload)

    def test_create_invalid_till_scope_webhook_fails(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'b2b_transaction_received',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'company'
        }
        with self.assertRaisesRegex(InvalidArgumentError,
                                    "Invalid scope for given event type."):
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload)

    def test_create_till_scope_webhook_with_no_scope_reference_fails(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'b2b_transaction_received',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till'
        }
        with self.assertRaisesRegex(InvalidArgumentError,
                                    'Scope reference not given.'):
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload)

    def test_create_invalid_company_scope_webhook_fails(self):
        test_payload = {
            "access_token": WebhooksTestCase.ACCESS_TOKEN,
            "event_type": 'settlement_transfer_completed',
            "webhook_endpoint":
            'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till',
            "scope_reference": '112233'
        }
        with self.assertRaisesRegex(InvalidArgumentError,
                                    "Invalid scope for given event type."):
            webhooks.WebhookService(
                base_url=SAMPLE_BASE_URL).create_subscription(test_payload)