Exemplo n.º 1
0
    def test_validation_fails(self):
        """
        Test that the validation fails if order.status is NOT one of the allowed statuses.
        """
        order = OrderFactory(status=OrderStatus.COMPLETE)

        validator = OrderInStatusSubValidator(allowed_statuses=(
            OrderStatus.DRAFT,
            OrderStatus.CANCELLED,
        ), )

        with pytest.raises(APIConflictException):
            validator(order=order)
Exemplo n.º 2
0
    def create_from_order(self, order, attrs=None):
        """
        :param order: Order instance for this payment gateway session
        :param attrs: dict with any extra property to be set on the object

        :returns: Payment Gateway Session instance created
        :raises GOVUKPayAPIException: if there is a problem with GOV.UK Pay
        :raises Conflict: if the order is not in the allowed status
        """
        # refresh ongoing sessions for this order first of all
        for session in self.filter(order=order).ongoing().select_for_update():
            session.refresh_from_govuk_payment()

        # validate that the order is in `quote_accepted`
        order.refresh_from_db()
        validator = OrderInStatusSubValidator(
            allowed_statuses=(OrderStatus.QUOTE_ACCEPTED,),
        )
        validator(order=order)

        # lock order to avoid race conditions
        order.__class__.objects.select_for_update().get(pk=order.pk)

        # cancel other ongoing sessions
        for session in self.filter(order=order).ongoing():
            session.cancel()

        # create a new payment gateway session
        session_id = uuid.uuid4()

        pay = PayClient()
        govuk_payment = pay.create_payment(
            amount=order.total_cost,
            reference=f'{order.reference}-{str(session_id)[:8].upper()}',
            description=settings.GOVUK_PAY_PAYMENT_DESCRIPTION.format(
                reference=order.reference,
            ),
            return_url=settings.GOVUK_PAY_RETURN_URL.format(
                public_token=order.public_token,
                session_id=session_id,
            ),
        )

        session = self.create(
            id=session_id,
            order=order,
            govuk_payment_id=govuk_payment['payment_id'],
            **(attrs or {}),
        )

        return session
    def test_validation_fails(self):
        """
        Test that the validation fails if order.status is NOT one of the allowed statuses.
        """
        order = OrderFactory(status=OrderStatus.complete)

        validator = OrderInStatusSubValidator(
            allowed_statuses=(
                OrderStatus.draft,
                OrderStatus.cancelled,
            ),
        )

        with pytest.raises(APIConflictException):
            validator(order=order)
Exemplo n.º 4
0
    def test_validation_passes(self):
        """
        Test that the validation passes if order.status is one of the allowed statuses.
        """
        order = OrderFactory(status=OrderStatus.COMPLETE)

        validator = OrderInStatusSubValidator(allowed_statuses=(
            OrderStatus.DRAFT,
            OrderStatus.COMPLETE,
            OrderStatus.CANCELLED,
        ), )

        try:
            validator(order=order)
        except Exception:
            pytest.fail('Should not raise a validator error.')
    def test_order_not_required(self):
        """
        Test that if order_required == False and the order passed in is None,
        the validation passes.
        """
        validator = OrderInStatusSubValidator(
            allowed_statuses=(
                OrderStatus.draft,
                OrderStatus.complete,
                OrderStatus.cancelled,
            ),
            order_required=False,
        )

        try:
            validator()
        except Exception:
            pytest.fail('Should not raise a validator error.')
    def test_validation_passes(self):
        """
        Test that the validation passes if order.status is one of the allowed statuses.
        """
        order = OrderFactory(status=OrderStatus.complete)

        validator = OrderInStatusSubValidator(
            allowed_statuses=(
                OrderStatus.draft,
                OrderStatus.complete,
                OrderStatus.cancelled,
            ),
        )

        try:
            validator(order=order)
        except Exception:
            pytest.fail('Should not raise a validator error.')
Exemplo n.º 7
0
    def test_order_not_required(self):
        """
        Test that if order_required == False and the order passed in is None,
        the validation passes.
        """
        validator = OrderInStatusSubValidator(
            allowed_statuses=(
                OrderStatus.DRAFT,
                OrderStatus.COMPLETE,
                OrderStatus.CANCELLED,
            ),
            order_required=False,
        )

        try:
            validator()
        except Exception:
            pytest.fail('Should not raise a validator error.')