Esempio n. 1
0
    def test_post_journal_access(self):
        """ Test 'post_journal_access' """
        self.mock_access_token_response()
        data = {
            'order_number': 'ORDER-64242',
            'user': '******',
            'journal': '4786e7be-2390-4332-a20e-e24895c38109'
        }
        responses.add(responses.POST,
                      self.journal_access_url,
                      status=200,
                      body='{}',
                      content_type='application/json')

        post_journal_access(site_configuration=self.site_configuration,
                            order_number=data['order_number'],
                            username=data['user'],
                            journal_uuid=data['journal'])

        # The first call (response.calls[0]) is to get post the access token
        # The second call (response.calls[1]) is the 'post_journal_access' call
        self.assertEqual(len(responses.calls), 2,
                         "Incorrect number of API calls")
        request = responses.calls[1].request
        self.assertEqual(json.loads(request.body), data)
        self.assertEqual(request.method, responses.POST)
        response = responses.calls[1].response
        self.assertEqual(response.status_code, 200)
Esempio n. 2
0
    def fulfill_product(self, order, lines, email_opt_in=False):
        """
        Fulfills the purchase of a 'Journal'
        Args:
            order (Order): The Order associated with the lines to be fulfilled.  The user associated with the order is
                presumed to be the student to grant access to the journal
            lines (List of Lines): Order Lines, associated with purchased products in the Order.  These should only be
                'Journal' products.
            email_opt_in (bool): Whether the user should be opted in to emails
                as part of the fulfillment. Defaults to False.
        Returns:
            The original set of lines, with new statuses set based on the success or failure of fulfillment.
        """
        logger.info(
            'Attempting to fulfill "Journal" product types for order [%s]',
            order.number)

        for line in lines:
            try:
                journal_uuid = line.product.attr.UUID
            except AttributeError:
                logger.error(
                    'Journal fulfillment failed, journal does not have uuid. Order [%s]',
                    order.number)
                line.set_status(LINE.FULFILLMENT_CONFIGURATION_ERROR)
                continue

            try:
                # TODO: WL-1680: All calls from ecommerce to other services should be async
                post_journal_access(
                    site_configuration=order.site.siteconfiguration,
                    order_number=order.number,
                    username=order.user.username,
                    journal_uuid=journal_uuid)

                line.set_status(LINE.COMPLETE)

                audit_log(
                    'line_fulfilled',
                    order_line_id=line.id,
                    order_number=order.number,
                    product_class=line.product.get_product_class().name,
                    user_id=order.user.id,
                    journal_uuid=journal_uuid,
                )
                logger.info(
                    'Successfully fulfilled Journal purchase for line [%d] of order [%s]',
                    line.id, order.number)
            except (Timeout, ConnectionError) as neterr:
                logger.error(
                    'Error fulfilling Journal purchase, line [%d] of order [%s] due to a network problem, err=[%s]',
                    line.id, order.number, neterr)
                line.set_status(LINE.FULFILLMENT_NETWORK_ERROR)
            except (Exception, HttpClientError) as err:  # pylint: disable=broad-except
                logger.error(
                    'Error fulfilling Journal purchase, line [%d] of order [%s] due to an error, err=[%s]',
                    line.id, order.number, err)
                line.set_status(LINE.FULFILLMENT_SERVER_ERROR)

        return order, lines
Esempio n. 3
0
    def fulfill_product(self, order, lines):
        """
        Fulfills the purchase of a 'Journal'
        Args:
            order (Order): The Order associated with the lines to be fulfilled.  The user associated with the order is
                presumed to be the student to grant access to the journal
            lines (List of Lines): Order Lines, associated with purchased products in the Order.  These should only be
                'Journal' products.
        Returns:
            The original set of lines, with new statuses set based on the success or failure of fulfillment.
        """
        logger.info(
            'Attempting to fulfill "Journal" product types for order [%s]',
            order.number)

        for line in lines:
            try:
                journal_uuid = line.product.attr.UUID
            except AttributeError:
                logger.error(
                    'Journal Product does not have required attributes, [uuid]'
                )
                line.set_status(LINE.FULFILLMENT_CONFIGURATION_ERROR)
                continue

            try:
                # TODO: WL-1680: All calls from ecommerce to other services should be async
                post_journal_access(
                    site_configuration=order.site.siteconfiguration,
                    order_number=order.number,
                    username=order.user.username,
                    journal_uuid=journal_uuid)

                line.set_status(LINE.COMPLETE)

                audit_log(
                    'line_fulfilled',
                    order_line_id=line.id,
                    order_number=order.number,
                    product_class=line.product.get_product_class().name,
                    user_id=order.user.id,
                    journal_uuid=journal_uuid,
                )
            except (Timeout, ConnectionError):
                logger.exception(
                    'Unable to fulfill line [%d] of order [%s] due to a network problem',
                    line.id, order.number)
                line.set_status(LINE.FULFILLMENT_NETWORK_ERROR)
            except Exception:  # pylint: disable=broad-except
                logger.exception('Unable to fulfill line [%d] of order [%s]',
                                 line.id, order.number)
                line.set_status(LINE.FULFILLMENT_SERVER_ERROR)

        logger.info(
            'Finished fulfilling "Journal" product types for order [%s]',
            order.number)
        return order, lines