예제 #1
0
    def test_fulfillment_failure(self):
        """Verify that the task raises an exception when fulfillment fails."""
        httpretty.register_uri(httpretty.PUT,
                               self.API_URL,
                               status=500,
                               body={})

        with self.assertRaises(exceptions.HttpServerError):
            fulfill_order.delay(self.ORDER_NUMBER).get()
예제 #2
0
    def test_fulfillment_timeout(self):
        """Verify that the task raises an exception when fulfillment times out."""
        httpretty.register_uri(httpretty.PUT,
                               self.API_URL,
                               status=404,
                               body=self._timeout_body)

        with self.assertRaises(exceptions.Timeout):
            fulfill_order.delay(self.ORDER_NUMBER).get()
예제 #3
0
    def test_fulfillment_timeout(self):
        """Verify that the task raises an exception when fulfillment times out."""
        responses.add(responses.PUT,
                      self.API_URL,
                      status=404,
                      body=exceptions.Timeout())

        with self.assertRaises(exceptions.Timeout):
            fulfill_order.delay(self.ORDER_NUMBER).get()
예제 #4
0
    def test_fulfillment_unknown_client_error_retry_success(self):
        """Verify that the task is capable of successfully retrying after client error."""
        httpretty.register_uri(httpretty.PUT, self.API_URL, responses=[
            httpretty.Response(status=404, body={}),
            httpretty.Response(status=200, body={}),
        ])

        result = fulfill_order.delay(self.ORDER_NUMBER).get()
        self.assertIsNone(result)
예제 #5
0
파일: mixins.py 프로젝트: wkoha/ecommerce
    def handle_successful_order(self, order):
        """Send a signal so that receivers can perform relevant tasks (e.g., fulfill the order)."""
        audit_log('order_placed',
                  amount=order.total_excl_tax,
                  basket_id=order.basket.id,
                  currency=order.currency,
                  order_number=order.number,
                  user_id=order.user.id)

        if waffle.switch_is_active('async_order_fulfillment'):
            # Always commit transactions before sending tasks depending on state from the current transaction!
            # There's potential for a race condition here if the task starts executing before the active
            # transaction has been committed; the necessary order doesn't exist in the database yet.
            # See http://celery.readthedocs.org/en/latest/userguide/tasks.html#database-transactions.
            fulfill_order.delay(order.number)
        else:
            post_checkout.send(sender=self, order=order)

        return order
예제 #6
0
    def handle_successful_order(self, order, request=None):  # pylint: disable=arguments-differ
        """Send a signal so that receivers can perform relevant tasks (e.g., fulfill the order)."""
        audit_log('order_placed',
                  amount=order.total_excl_tax,
                  basket_id=order.basket.id,
                  currency=order.currency,
                  order_number=order.number,
                  user_id=order.user.id,
                  contains_coupon=order.contains_coupon)

        # Check for the user's email opt in preference, defaulting to false if it hasn't been set
        try:
            email_opt_in = BasketAttribute.objects.get(
                basket=order.basket,
                attribute_type=BasketAttributeType.objects.get(
                    name=EMAIL_OPT_IN_ATTRIBUTE),
            ).value_text == 'True'
        except BasketAttribute.DoesNotExist:
            email_opt_in = False

        # create offer assignment for MULTI_USE_PER_CUSTOMER
        self.create_assignments_for_multi_use_per_customer(order)

        # update offer assignment with voucher application
        self.update_assigned_voucher_offer_assignment(order)

        if waffle.sample_is_active('async_order_fulfillment'):
            # Always commit transactions before sending tasks depending on state from the current transaction!
            # There's potential for a race condition here if the task starts executing before the active
            # transaction has been committed; the necessary order doesn't exist in the database yet.
            # See http://celery.readthedocs.org/en/latest/userguide/tasks.html#database-transactions.
            fulfill_order.delay(
                order.number,
                site_code=order.site.siteconfiguration.partner.short_code,
                email_opt_in=email_opt_in)
        else:
            post_checkout.send(sender=self,
                               order=order,
                               request=request,
                               email_opt_in=email_opt_in)

        return order
예제 #7
0
    def test_fulfillment_retry_success(self):
        """Verify that the task is capable of successfully retrying after fulfillment failure."""
        httpretty.register_uri(httpretty.PUT,
                               self.API_URL,
                               responses=[
                                   httpretty.Response(status=500, body={}),
                                   httpretty.Response(status=200, body={}),
                               ])

        result = fulfill_order.delay(self.ORDER_NUMBER).get()
        self.assertIsNone(result)
예제 #8
0
    def test_fulfillment_success(self):
        """Verify that the task exits without an error when fulfillment succeeds."""
        httpretty.register_uri(httpretty.PUT, self.API_URL, status=200, body={})

        result = fulfill_order.delay(self.ORDER_NUMBER).get()
        self.assertIsNone(result)

        # Validate the value of the HTTP Authorization header.
        last_request = httpretty.last_request()
        token = last_request.headers.get('authorization').split()[1]
        payload = jwt.decode(token, get_configuration('JWT_SECRET_KEY'))
        self.assertEqual(payload['username'], get_configuration('ECOMMERCE_SERVICE_USERNAME'))
예제 #9
0
    def handle_successful_order(self, order):
        """Send a signal so that receivers can perform relevant tasks (e.g., fulfill the order)."""
        audit_log(
            'order_placed',
            amount=order.total_excl_tax,
            basket_id=order.basket.id,
            currency=order.currency,
            order_number=order.number,
            user_id=order.user.id
        )

        if waffle.sample_is_active('async_order_fulfillment'):
            # Always commit transactions before sending tasks depending on state from the current transaction!
            # There's potential for a race condition here if the task starts executing before the active
            # transaction has been committed; the necessary order doesn't exist in the database yet.
            # See http://celery.readthedocs.org/en/latest/userguide/tasks.html#database-transactions.
            fulfill_order.delay(order.number)
        else:
            post_checkout.send(sender=self, order=order)

        return order
예제 #10
0
    def test_fulfillment_success(self):
        """Verify that the task exits without an error when fulfillment succeeds."""
        responses.add(responses.PUT, self.API_URL, status=200, body="{}")

        result = fulfill_order.delay(self.ORDER_NUMBER).get()
        self.assertIsNone(result)

        # Validate the value of the HTTP Authorization header.
        last_request = responses.calls[-1].request
        token = last_request.headers.get('authorization').split()[1]
        payload = jwt.decode(token, get_configuration('JWT_SECRET_KEY'))
        self.assertEqual(payload['username'],
                         get_configuration('ECOMMERCE_SERVICE_USERNAME'))
예제 #11
0
    def test_fulfillment_unknown_client_error_retry_success(self):
        """Verify that the task is capable of successfully retrying after client error."""
        responses.add(
            responses.Response(responses.PUT,
                               self.API_URL,
                               status=404,
                               body="{}"), )
        responses.add(
            responses.Response(responses.PUT,
                               self.API_URL,
                               status=200,
                               body="{}"), )

        result = fulfill_order.delay(self.ORDER_NUMBER).get()
        self.assertIsNone(result)
예제 #12
0
    def test_email_opt_in_parameter_sent(self, email_opt_in_bool,
                                         email_opt_in_str):
        """Verify that the task correctly adds the email_opt_in parameter to the request."""
        email_opt_in_api_url = self.API_URL + '?email_opt_in=' + email_opt_in_str
        responses.add(responses.PUT,
                      email_opt_in_api_url,
                      status=200,
                      body="{}")

        result = fulfill_order.delay(self.ORDER_NUMBER,
                                     email_opt_in=email_opt_in_bool).get()
        self.assertIsNone(result)

        last_request = responses.calls[-1].request
        self.assertIn('?email_opt_in=' + email_opt_in_str,
                      last_request.path_url)
예제 #13
0
    def test_email_opt_in_parameter_sent(self, email_opt_in_bool,
                                         email_opt_in_str):
        """Verify that the task correctly adds the email_opt_in parameter to the request."""
        email_opt_in_api_url = self.API_URL + '?email_opt_in=' + email_opt_in_str
        httpretty.register_uri(httpretty.PUT,
                               email_opt_in_api_url,
                               status=200,
                               body={})

        result = fulfill_order.delay(self.ORDER_NUMBER,
                                     email_opt_in=email_opt_in_bool).get()
        self.assertIsNone(result)

        last_request = httpretty.last_request()
        self.assertIn('?email_opt_in=' + email_opt_in_str, last_request.path)
        # QueryDicts store their values as lists in case multiple values are passed in.
        # last_request.querystring is returned as a dict instead of a QueryDict
        # so we have the grab the first element in the list to actually get the value.
        self.assertEqual(last_request.querystring['email_opt_in'][0],
                         email_opt_in_str)
예제 #14
0
    def test_fulfillment_failure(self):
        """Verify that the task raises an exception when fulfillment fails."""
        responses.add(responses.PUT, self.API_URL, status=500, body="{}")

        with self.assertRaises(exceptions.HttpServerError):
            fulfill_order.delay(self.ORDER_NUMBER).get()
예제 #15
0
    def test_fulfillment_timeout(self):
        """Verify that the task raises an exception when fulfillment times out."""
        httpretty.register_uri(httpretty.PUT, self.API_URL, status=404, body=self._timeout_body)

        with self.assertRaises(exceptions.Timeout):
            fulfill_order.delay(self.ORDER_NUMBER).get()
예제 #16
0
    def test_fulfillment_failure(self):
        """Verify that the task raises an exception when fulfillment fails."""
        httpretty.register_uri(httpretty.PUT, self.API_URL, status=500, body={})

        with self.assertRaises(exceptions.HttpServerError):
            fulfill_order.delay(self.ORDER_NUMBER).get()