Commit 7d791ab9 by Marko Jevtić Committed by GitHub

Merge pull request #13590 from edx/mjevtic/SOL-2082

[SOL-2082] Add a value to SiteConfiguration to enable ecommerce Receipt Page usage
parents 024e2e4b 363a0c8d
......@@ -15,6 +15,7 @@ class CommerceConfiguration(ConfigurationModel):
API_NAME = 'commerce'
CACHE_KEY = 'commerce.api.data'
DEFAULT_RECEIPT_PAGE_URL = '/commerce/checkout/receipt/?orderNum='
checkout_on_ecommerce_service = models.BooleanField(
default=False,
......@@ -35,7 +36,7 @@ class CommerceConfiguration(ConfigurationModel):
)
receipt_page = models.CharField(
max_length=255,
default='/commerce/checkout/receipt/?orderNum=',
default=DEFAULT_RECEIPT_PAGE_URL,
help_text=_('Path to order receipt page.')
)
......
"""Tests of commerce utilities."""
from django.conf import settings
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
......@@ -7,14 +8,20 @@ from mock import patch
from commerce.models import CommerceConfiguration
from commerce.utils import EcommerceService
from openedx.core.lib.log_utils import audit_log
from openedx.core.djangoapps.site_configuration.tests.test_util import with_site_configuration
from student.tests.factories import UserFactory
TEST_SITE_CONFIGURATION = {
'ECOMMERCE_RECEIPT_PAGE_URL': '/checkout/receipt/?order_number='
}
def update_commerce_config(enabled=False, checkout_page='/test_basket/'):
def update_commerce_config(enabled=False, checkout_page='/test_basket/', receipt_page='/checkout/receipt/'):
""" Enable / Disable CommerceConfiguration model """
CommerceConfiguration.objects.create(
checkout_on_ecommerce_service=enabled,
single_course_checkout_page=checkout_page
receipt_page=receipt_page,
single_course_checkout_page=checkout_page,
)
......@@ -61,14 +68,43 @@ class EcommerceServiceTests(TestCase):
self.assertTrue(is_enabled)
@override_settings(ECOMMERCE_PUBLIC_URL_ROOT='http://ecommerce_url')
def test_payment_page_url(self):
def test_ecommerce_url_root(self):
"""Verify that the proper root URL is returned."""
self.assertEqual(EcommerceService().ecommerce_url_root, 'http://ecommerce_url')
@override_settings(ECOMMERCE_PUBLIC_URL_ROOT='http://ecommerce_url')
def test_get_absolute_ecommerce_url(self):
"""Verify that the proper URL is returned."""
url = EcommerceService().payment_page_url()
url = EcommerceService().get_absolute_ecommerce_url('/test_basket/')
self.assertEqual(url, 'http://ecommerce_url/test_basket/')
@override_settings(ECOMMERCE_PUBLIC_URL_ROOT='http://ecommerce_url')
def test_get_receipt_page_url(self):
"""Verify that the proper Receipt page URL is returned."""
order_number = 'ORDER1'
url = EcommerceService().get_receipt_page_url(order_number)
expected_url = '/checkout/receipt/{}'.format(order_number)
self.assertEqual(url, expected_url)
@override_settings(ECOMMERCE_PUBLIC_URL_ROOT='http://ecommerce_url')
def test_checkout_page_url(self):
""" Verify the checkout page URL is properly constructed and returned. """
url = EcommerceService().checkout_page_url(self.SKU)
expected_url = 'http://ecommerce_url/test_basket/?sku={}'.format(self.SKU)
self.assertEqual(url, expected_url)
@override_settings(ECOMMERCE_PUBLIC_URL_ROOT='http://ecommerce_url')
@with_site_configuration(configuration=TEST_SITE_CONFIGURATION)
def test_get_receipt_page_url_with_site_configuration(self):
order_number = 'ORDER1'
config = CommerceConfiguration.current()
config.use_ecommerce_receipt_page = True
config.save()
receipt_page_url = EcommerceService().get_receipt_page_url(order_number)
expected_url = '{ecommerce_root}{receipt_page_url}{order_number}'.format(
ecommerce_root=settings.ECOMMERCE_PUBLIC_URL_ROOT,
order_number=order_number,
receipt_page_url=TEST_SITE_CONFIGURATION['ECOMMERCE_RECEIPT_PAGE_URL']
)
self.assertEqual(receipt_page_url, expected_url)
......@@ -12,6 +12,39 @@ class EcommerceService(object):
def __init__(self):
self.config = CommerceConfiguration.current()
@property
def ecommerce_url_root(self):
""" Retrieve Ecommerce service public url root. """
return configuration_helpers.get_value('ECOMMERCE_PUBLIC_URL_ROOT', settings.ECOMMERCE_PUBLIC_URL_ROOT)
def get_absolute_ecommerce_url(self, ecommerce_page_url):
""" Return the absolute URL to the ecommerce page.
Args:
ecommerce_page_url (str): Relative path to the ecommerce page.
Returns:
Absolute path to the ecommerce page.
"""
return urljoin(self.ecommerce_url_root, ecommerce_page_url)
def get_receipt_page_url(self, order_number):
"""
Gets the URL for the Order Receipt page hosted by the ecommerce service.
Args:
order_number (str): Order number.
Returns:
Receipt page for the specified Order.
"""
ecommerce_receipt_page_url = configuration_helpers.get_value('ECOMMERCE_RECEIPT_PAGE_URL')
if ecommerce_receipt_page_url:
receipt_page_url = self.get_absolute_ecommerce_url(ecommerce_receipt_page_url)
else:
receipt_page_url = self.config.receipt_page
return receipt_page_url + order_number
def is_enabled(self, user):
"""
Determines the availability of the EcommerceService based on user activation and service configuration.
......@@ -29,11 +62,7 @@ class EcommerceService(object):
Example:
http://localhost:8002/basket/single_item/
"""
ecommerce_url_root = configuration_helpers.get_value(
'ECOMMERCE_PUBLIC_URL_ROOT',
settings.ECOMMERCE_PUBLIC_URL_ROOT,
)
return urljoin(ecommerce_url_root, self.config.single_course_checkout_page)
return self.get_absolute_ecommerce_url(self.config.single_course_checkout_page)
def checkout_page_url(self, sku):
""" Construct the URL to the ecommerce checkout page and include a product.
......@@ -41,4 +70,4 @@ class EcommerceService(object):
Example:
http://localhost:8002/basket/single_item/?sku=5H3HG5
"""
return "{}?sku={}".format(self.payment_page_url(), sku)
return "{}?sku={}".format(self.get_absolute_ecommerce_url(self.config.single_course_checkout_page), sku)
......@@ -22,6 +22,7 @@ from edxmako.shortcuts import render_to_response
import pytz
from commerce.models import CommerceConfiguration
from lms.djangoapps.commerce.utils import EcommerceService
from openedx.core.djangoapps.external_auth.login_and_register import (
login as external_auth_login,
register as external_auth_register
......@@ -316,7 +317,7 @@ def _external_auth_intercept(request, mode):
def get_user_orders(user):
"""Given a user, get the detail of all the orders from the Ecommerce service.
Arguments:
Args:
user (User): The user to authenticate as when requesting ecommerce.
Returns:
......@@ -351,7 +352,7 @@ def get_user_orders(user):
'order_date': strftime_localized(
date_placed.replace(tzinfo=pytz.UTC), 'SHORT_DATE'
),
'receipt_url': commerce_configuration.receipt_page + order['number']
'receipt_url': EcommerceService().get_receipt_page_url(order['number'])
}
user_orders.append(order_data)
except KeyError:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment