def test_rerun_declined_transactions_for_pair(self):
        from silver.transaction_retries import TransactionRetryAttempter
        attempts = TransactionRetryAttempter()

        initial_try = dt.datetime(2019, 1, 1, 0, 0, 0, 0, tzinfo=pytz.UTC)
        retry_begins = dt.datetime(2019, 1, 3, 0, 0, 0, 0, tzinfo=pytz.UTC)
        retry_ends = dt.datetime(2019, 1, 5, 0, 0, 0, 0, tzinfo=pytz.UTC)

        customer, payment_method = self._create_default_payment_method()

        customer.save()
        payment_method.save()

        trx = TransactionFactory(state=Transaction.States.Failed,
                                 created_at=initial_try,
                                 updated_at=initial_try,
                                 payment_method=payment_method)
        trx.save()

        assert trx.invoice.transactions.count() == 1

        # payment method is not configured to allow retry attempts.
        attempts.check(billing_date=initial_try)
        assert trx.invoice.transactions.count() == 1

        attempts.check(billing_date=retry_begins)
        assert trx.invoice.transactions.count() == 2
    def test_management_command(self):
        initial_try = dt.datetime(2019, 1, 1, 0, 0, 0, 0, tzinfo=pytz.UTC)
        retry_begins = dt.datetime(2019, 1, 3, 0, 0, 0, 0, tzinfo=pytz.UTC)
        retry_ends = dt.datetime(2019, 1, 5, 0, 0, 0, 0, tzinfo=pytz.UTC)

        customer, payment_method = self._create_default_payment_method()

        customer.save()
        payment_method.save()

        trx = TransactionFactory(state=Transaction.States.Failed,
                                 created_at=initial_try,
                                 updated_at=initial_try,
                                 proforma=None,
                                 payment_method=payment_method)
        trx.save()

        assert trx.invoice.transactions.count() == 1

        call_command('retry_failed_transactions',
                     billing_date=initial_try,
                     stdout=self.output)

        assert trx.invoice.transactions.count() == 1

        call_command('retry_failed_transactions',
                     billing_date=retry_begins,
                     stdout=self.output)

        assert trx.invoice.transactions.count() == 2
Пример #3
0
    def test_get_transaction_from_token(self):
        transaction = TransactionFactory()

        mocked_view = MagicMock()
        token = _get_jwt_token(transaction)

        self.assertEqual(
            get_transaction_from_token(mocked_view)(None, token),
            mocked_view())
        mocked_view.has_calls([call(None, transaction, False), call()])
Пример #4
0
    def test_get_payment_url(self):
        transaction = TransactionFactory()

        expected_url = '/pay/token/'
        with patch('silver.utils.payments._get_jwt_token') as mocked_token:
            mocked_token.return_value = 'token'

            self.assertEqual(get_payment_url(transaction, None), expected_url)

            mocked_token.assert_called_once_with(transaction)
Пример #5
0
    def test_get_jwt_token(self):
        uuid = UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e', version=4)
        transaction = TransactionFactory(uuid=uuid)

        expected_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0cmFuc2FjdG' \
                         'lvbiI6IjZmYTQ1OWVhLWVlOGEtNGNhNC04OTRlLWRiNzdlMTYwM' \
                         'zU1ZSIsImV4cCI6MTQ5Nzk2NTY0MH0.-bpx5A3DfSe3-HO6aH_g' \
                         'lS8adcCxUn8lSK1-RPxohhI'
        with patch('silver.utils.payments.datetime') as mocked_datetime:
            mocked_datetime.utcnow.return_value = datetime.strptime(
                'Jun 20 2017 1:33PM', '%b %d %Y %I:%M%p')
            self.assertEqual(_get_jwt_token(transaction), expected_token)
    def test_failed_docs_query(self):
        from silver.transaction_retries import TransactionRetryAttempter
        attempts = TransactionRetryAttempter()

        c = list(attempts._query_payment_failures())
        assert len(c) == 0

        b = TransactionFactory(state=Transaction.States.Failed)
        b.save()

        c = list(attempts._query_payment_failures())
        assert len(c) == 1
Пример #7
0
    def test_get_transaction_from_expired_token(self):
        transaction = TransactionFactory()

        mocked_view = MagicMock()
        with patch('silver.utils.payments.datetime') as mocked_datetime:
            mocked_datetime.utcnow.return_value = datetime.utcnow(
            ) - timedelta(days=2 * 365)
            token = _get_jwt_token(transaction)

        self.assertEqual(
            get_transaction_from_token(mocked_view)(None, token),
            mocked_view())
        mocked_view.has_calls([call(None, transaction, True), call()])
Пример #8
0
    def test_get_payment_complete_url(self):
        transaction = TransactionFactory()

        expected_url = '/pay/token/complete?return_url=http://google.com'
        mocked_request = MagicMock(GET={'return_url': 'http://google.com'},
                                   versioning_scheme=None)
        mocked_request.build_absolute_uri.return_value = '/pay/token/complete'

        with patch('silver.utils.payments._get_jwt_token') as mocked_token:
            mocked_token.return_value = 'token'

            self.assertEqual(
                get_payment_complete_url(transaction, mocked_request),
                expected_url)

            mocked_token.assert_called_once_with(transaction)
    def test_cannot_issue_new_transaction_while_pending(self):
        """ The TransactionRetryAttempter should only be able to issue
        transactions while there are no pending re-attempted Transactions
        with state Issued. """

        from silver.transaction_retries import TransactionRetryAttempter
        attempts = TransactionRetryAttempter()

        initial_try = dt.datetime(2019, 1, 1, 0, 0, 0, 0, tzinfo=pytz.UTC)
        retry_begins = dt.datetime(2019, 1, 3, 0, 0, 0, 0, tzinfo=pytz.UTC)
        retry_ends = dt.datetime(2019, 1, 5, 0, 0, 0, 0, tzinfo=pytz.UTC)

        customer, payment_method = self._create_default_payment_method()

        customer.save()
        payment_method.save()

        trx = TransactionFactory(state=Transaction.States.Failed,
                                 created_at=initial_try,
                                 updated_at=initial_try,
                                 proforma=None,
                                 payment_method=payment_method)
        trx.save()

        assert trx.invoice.transactions.count() == 1

        # Spam some attempts
        attempts.check(billing_date=retry_begins)
        attempts.check(billing_date=retry_begins)
        attempts.check(billing_date=retry_begins)
        attempts.check(billing_date=retry_begins)

        assert trx.invoice.transactions.count() == 2

        # Spam some attempts with force.
        attempts.check(billing_date=retry_begins, force=True)
        attempts.check(billing_date=retry_begins, force=True)
        attempts.check(billing_date=retry_begins, force=True)
        attempts.check(billing_date=retry_begins, force=True)

        assert trx.invoice.transactions.count() == 2
    def test_no_new_attempts_for_existing_functionality(self):
        from silver.transaction_retries import TransactionRetryAttempter
        attempts = TransactionRetryAttempter()

        c = list(attempts._query_payment_failures())
        assert len(c) == 0

        b = TransactionFactory(state=Transaction.States.Failed)
        b.save()

        invoice = b.invoice
        proforma = b.proforma

        c = list(attempts._query_payment_failures())
        assert len(c) == 1

        # payment method is not configured to allow retry attempts.
        attempts.check(billing_date=timezone.now())

        assert proforma.transactions.count() == 1
        assert invoice.transactions.count() == 1