Exemplo n.º 1
0
    def test_too_much_cheap_gateway(self):

        data = {}
        data["creditcardnumber"] = "4024007129931746"
        data["cardholder"] = "John Doe"
        data["expirationdate"] = datetime.date(2021,6,5)
        data["securitycode"] = "043"
        data["amount"] = 1000

        payment = PaymentForm(data)
        payment = payment.save()
        payment = model_to_dict(payment)

        gateway = CheapGateway()
        name, status = gateway.process(payment)

        self.assertEqual(status,HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
Exemplo n.º 2
0
    def test_unavailable_cheap_gateway(self):

        data = {}
        data["creditcardnumber"] = "4024007129931746"
        data["cardholder"] = "John Doe"
        data["expirationdate"] = datetime.date(2021,6,5)
        data["securitycode"] = "043"
        data["amount"] = 12

        payment = PaymentForm(data)
        payment = payment.save()
        payment = model_to_dict(payment)

        gateway = CheapGateway(False)
        name, status = gateway.process(payment)

        self.assertEqual(status,HTTPStatus.TOO_MANY_REQUESTS)
class Dispatcher():

    cheap_gateway = CheapGateway(True)
    expensive_gateway = ExpensiveGateway(True)
    premium_gateway = PremiumGateway(True)

    @staticmethod
    @app.task(bind=True, ignore_result=True)
    def dispatch(self, payment):

        try:
            self.update_state(state="STARTED",
                              meta={"resolver": "Not decided!"})
        except:
            pass

        resolver, status = None, HTTPStatus.UNAVAILABLE_FOR_LEGAL_REASONS

        if payment["amount"] <= 20:

            resolver, status = Dispatcher.cheap_gateway.process(payment)

        if payment["amount"] >= 21 and payment["amount"] <= 500:

            resolver, status = Dispatcher.expensive_gateway.process(payment)

            if status != HTTPStatus.OK:

                resolver, status = Dispatcher.cheap_gateway.process(payment)

        if payment["amount"] >= 501:

            for _ in range(3):

                resolver, status = Dispatcher.premium_gateway.process(payment)

                if status == HTTPStatus.OK:
                    break
        try:
            if status == HTTPStatus.OK:
                self.update_state(state="SUCCESS", meta={"resolver": resolver})
            else:
                self.update_state(state="FAILURE", meta={"resolver": resolver})
        except:
            pass

        return resolver, status
Exemplo n.º 4
0
    def test_functionality_case_cheap(self):

        data = {}
        data["creditcardnumber"] = "4024007129931746"
        data["cardholder"] = "John Doe"
        data["expirationdate"] = datetime.date(2021,6,5)
        data["securitycode"] = "043"
        data["amount"] = 10

        payment = PaymentForm(data)
        payment = payment.save()
        payment = model_to_dict(payment)

        Dispatcher.cheap_gateway = CheapGateway(True)

        name, status = Dispatcher.dispatch(payment)

        self.assertEqual(status, HTTPStatus.OK)
        self.assertEqual(name, Dispatcher.cheap_gateway.name)