Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
ecommerce
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
ecommerce
Commits
b1250ba5
Commit
b1250ba5
authored
Aug 23, 2016
by
Michael Frey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use catalog service for all course info.
parent
d7fc9529
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
80 additions
and
50 deletions
+80
-50
ecommerce/coupons/tests/mixins.py
+27
-0
ecommerce/courses/tests/test_utils.py
+11
-9
ecommerce/courses/utils.py
+10
-12
ecommerce/extensions/api/v2/tests/views/test_vouchers.py
+5
-5
ecommerce/extensions/api/v2/views/vouchers.py
+3
-5
ecommerce/extensions/basket/tests/test_views.py
+17
-13
ecommerce/extensions/basket/views.py
+7
-6
No files found.
ecommerce/coupons/tests/mixins.py
View file @
b1250ba5
...
@@ -21,6 +21,32 @@ class CourseCatalogMockMixin(object):
...
@@ -21,6 +21,32 @@ class CourseCatalogMockMixin(object):
super
(
CourseCatalogMockMixin
,
self
)
.
setUp
()
super
(
CourseCatalogMockMixin
,
self
)
.
setUp
()
cache
.
clear
()
cache
.
clear
()
def
mock_dynamic_catalog_single_course_runs_api
(
self
,
course_run
):
""" Helper function to register a dynamic course catalog API endpoint for the course run information. """
course_run_info
=
{
"course"
:
"edX+DemoX"
,
"key"
:
course_run
.
id
,
"title"
:
course_run
.
name
,
"short_description"
:
'Foo'
,
"start"
:
"2013-02-05T05:00:00Z"
,
"image"
:
{
"src"
:
"/path/to/image.jpg"
,
},
}
course_run_info_json
=
json
.
dumps
(
course_run_info
)
course_run_url
=
'{}course_runs/{}/?partner={}'
.
format
(
settings
.
COURSE_CATALOG_API_URL
,
course_run
.
id
,
self
.
site
.
siteconfiguration
.
partner
.
short_code
)
httpretty
.
register_uri
(
httpretty
.
GET
,
course_run_url
,
body
=
course_run_info_json
,
content_type
=
'application/json'
)
def
mock_dynamic_catalog_course_runs_api
(
self
,
course_run
=
None
,
query
=
None
,
course_run_info
=
None
):
def
mock_dynamic_catalog_course_runs_api
(
self
,
course_run
=
None
,
query
=
None
,
course_run_info
=
None
):
""" Helper function to register a dynamic course catalog API endpoint for the course run information. """
""" Helper function to register a dynamic course catalog API endpoint for the course run information. """
if
not
course_run_info
:
if
not
course_run_info
:
...
@@ -44,6 +70,7 @@ class CourseCatalogMockMixin(object):
...
@@ -44,6 +70,7 @@ class CourseCatalogMockMixin(object):
settings
.
COURSE_CATALOG_API_URL
,
settings
.
COURSE_CATALOG_API_URL
,
query
if
query
else
'id:course*'
query
if
query
else
'id:course*'
)
)
httpretty
.
register_uri
(
httpretty
.
register_uri
(
httpretty
.
GET
,
course_run_url
,
httpretty
.
GET
,
course_run_url
,
body
=
course_run_info_json
,
body
=
course_run_info_json
,
...
...
ecommerce/courses/tests/test_utils.py
View file @
b1250ba5
...
@@ -7,11 +7,12 @@ from django.core.cache import cache
...
@@ -7,11 +7,12 @@ from django.core.cache import cache
from
ecommerce.core.constants
import
ENROLLMENT_CODE_SWITCH
from
ecommerce.core.constants
import
ENROLLMENT_CODE_SWITCH
from
ecommerce.core.tests
import
toggle_switch
from
ecommerce.core.tests
import
toggle_switch
from
ecommerce.core.url_utils
import
get_lms_url
from
ecommerce.core.tests.decorators
import
mock_course_catalog_api_client
from
ecommerce.coupons.tests.mixins
import
CourseCatalogMockMixin
from
ecommerce.courses.models
import
Course
from
ecommerce.courses.models
import
Course
from
ecommerce.courses.tests.factories
import
CourseFactory
from
ecommerce.courses.tests.factories
import
CourseFactory
from
ecommerce.courses.utils
import
(
from
ecommerce.courses.utils
import
(
get_certificate_type_display_value
,
get_course_info_from_
lms
,
mode_for_seat
get_certificate_type_display_value
,
get_course_info_from_
catalog
,
mode_for_seat
)
)
from
ecommerce.extensions.catalogue.tests.mixins
import
CourseCatalogTestMixin
from
ecommerce.extensions.catalogue.tests.mixins
import
CourseCatalogTestMixin
from
ecommerce.tests.testcases
import
TestCase
from
ecommerce.tests.testcases
import
TestCase
...
@@ -19,7 +20,7 @@ from ecommerce.tests.testcases import TestCase
...
@@ -19,7 +20,7 @@ from ecommerce.tests.testcases import TestCase
@httpretty.activate
@httpretty.activate
@ddt.ddt
@ddt.ddt
class
UtilsTests
(
CourseCatalogTestMixin
,
TestCase
):
class
UtilsTests
(
CourseCatalogTestMixin
,
CourseCatalogMockMixin
,
TestCase
):
@ddt.unpack
@ddt.unpack
@ddt.data
(
@ddt.data
(
(
''
,
False
,
'audit'
),
(
''
,
False
,
'audit'
),
...
@@ -41,19 +42,20 @@ class UtilsTests(CourseCatalogTestMixin, TestCase):
...
@@ -41,19 +42,20 @@ class UtilsTests(CourseCatalogTestMixin, TestCase):
if
enrollment_code
:
# We should only have enrollment codes for allowed types
if
enrollment_code
:
# We should only have enrollment codes for allowed types
self
.
assertEqual
(
mode_for_seat
(
enrollment_code
),
mode
)
self
.
assertEqual
(
mode_for_seat
(
enrollment_code
),
mode
)
def
test_get_course_info_from_lms
(
self
):
@mock_course_catalog_api_client
def
test_get_course_info_from_catalog
(
self
):
""" Check to see if course info gets cached """
""" Check to see if course info gets cached """
course
=
CourseFactory
()
course
=
CourseFactory
()
course_url
=
get_lms_url
(
'api/courses/v1/courses/{}/'
.
format
(
course
.
id
))
self
.
mock_dynamic_catalog_single_course_runs_api
(
course
)
httpretty
.
register_uri
(
httpretty
.
GET
,
course_url
,
body
=
course
.
name
,
status
=
200
,
content_type
=
'application/json'
)
cache_key
=
'courses_api_detail_{}
'
.
format
(
course
.
id
)
cache_key
=
'courses_api_detail_{}
{}'
.
format
(
course
.
id
,
self
.
site
.
siteconfiguration
.
partner
.
short_code
)
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
cached_course
=
cache
.
get
(
cache_hash
)
cached_course
=
cache
.
get
(
cache_hash
)
self
.
assertIsNone
(
cached_course
)
self
.
assertIsNone
(
cached_course
)
response
=
get_course_info_from_lms
(
course
.
id
)
response
=
get_course_info_from_catalog
(
self
.
request
.
site
,
course
)
self
.
assertEqual
(
response
,
course
.
name
)
self
.
assertEqual
(
response
[
'title'
],
course
.
name
)
cached_course
=
cache
.
get
(
cache_hash
)
cached_course
=
cache
.
get
(
cache_hash
)
self
.
assertEqual
(
cached_course
,
response
)
self
.
assertEqual
(
cached_course
,
response
)
...
...
ecommerce/courses/utils.py
View file @
b1250ba5
...
@@ -3,9 +3,6 @@ import hashlib
...
@@ -3,9 +3,6 @@ import hashlib
from
django.conf
import
settings
from
django.conf
import
settings
from
django.core.cache
import
cache
from
django.core.cache
import
cache
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.translation
import
ugettext_lazy
as
_
from
edx_rest_api_client.client
import
EdxRestApiClient
from
ecommerce.core.url_utils
import
get_lms_url
def
mode_for_seat
(
product
):
def
mode_for_seat
(
product
):
...
@@ -23,16 +20,17 @@ def mode_for_seat(product):
...
@@ -23,16 +20,17 @@ def mode_for_seat(product):
return
mode
return
mode
def
get_course_info_from_lms
(
course_key
):
def
get_course_info_from_catalog
(
site
,
course_key
):
""" Get course information from LMS via the course api and cache """
""" Get course information from catalog service and cache """
api
=
EdxRestApiClient
(
get_lms_url
(
'api/courses/v1/'
))
api
=
site
.
siteconfiguration
.
course_catalog_api_client
cache_key
=
'courses_api_detail_{}'
.
format
(
course_key
)
partner_short_code
=
site
.
siteconfiguration
.
partner
.
short_code
cache_key
=
'courses_api_detail_{}{}'
.
format
(
course_key
,
partner_short_code
)
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
course
=
cache
.
get
(
cache_hash
)
course
_run
=
cache
.
get
(
cache_hash
)
if
not
course
:
# pragma: no cover
if
not
course
_run
:
# pragma: no cover
course
=
api
.
courses
(
course_key
)
.
get
(
)
course
_run
=
api
.
course_runs
(
course_key
)
.
get
(
partner
=
partner_short_code
)
cache
.
set
(
cache_hash
,
course
,
settings
.
COURSES_API_CACHE_TIMEOUT
)
cache
.
set
(
cache_hash
,
course
_run
,
settings
.
COURSES_API_CACHE_TIMEOUT
)
return
course
return
course
_run
def
get_certificate_type_display_value
(
certificate_type
):
def
get_certificate_type_display_value
(
certificate_type
):
...
...
ecommerce/extensions/api/v2/tests/views/test_vouchers.py
View file @
b1250ba5
...
@@ -17,7 +17,6 @@ from rest_framework.test import APIRequestFactory
...
@@ -17,7 +17,6 @@ from rest_framework.test import APIRequestFactory
from
slumber.exceptions
import
SlumberBaseException
from
slumber.exceptions
import
SlumberBaseException
from
ecommerce.core.tests.decorators
import
mock_course_catalog_api_client
from
ecommerce.core.tests.decorators
import
mock_course_catalog_api_client
from
ecommerce.core.url_utils
import
get_lms_url
from
ecommerce.coupons.tests.mixins
import
CourseCatalogMockMixin
,
CouponMixin
from
ecommerce.coupons.tests.mixins
import
CourseCatalogMockMixin
,
CouponMixin
from
ecommerce.coupons.views
import
get_voucher_and_products_from_code
from
ecommerce.coupons.views
import
get_voucher_and_products_from_code
from
ecommerce.courses.models
import
Course
from
ecommerce.courses.models
import
Course
...
@@ -146,10 +145,11 @@ class VoucherViewOffersEndpointTests(
...
@@ -146,10 +145,11 @@ class VoucherViewOffersEndpointTests(
self
.
assertEqual
(
response
.
status_code
,
400
)
self
.
assertEqual
(
response
.
status_code
,
400
)
@mock_course_catalog_api_client
def
test_voucher_offers_listing_for_a_single_course_voucher
(
self
):
def
test_voucher_offers_listing_for_a_single_course_voucher
(
self
):
""" Verify the endpoint returns offers data when a single product is in voucher range. """
""" Verify the endpoint returns offers data when a single product is in voucher range. """
course
,
seat
=
self
.
create_course_and_seat
()
course
,
seat
=
self
.
create_course_and_seat
()
self
.
mock_
course_api_response
(
course
=
course
)
self
.
mock_
dynamic_catalog_single_course_runs_api
(
course
)
new_range
=
RangeFactory
(
products
=
[
seat
,
])
new_range
=
RangeFactory
(
products
=
[
seat
,
])
new_range
.
catalog
=
Catalog
.
objects
.
create
(
partner
=
self
.
partner
)
new_range
.
catalog
=
Catalog
.
objects
.
create
(
partner
=
self
.
partner
)
new_range
.
catalog
.
stock_records
.
add
(
StockRecord
.
objects
.
get
(
product
=
seat
))
new_range
.
catalog
.
stock_records
.
add
(
StockRecord
.
objects
.
get
(
product
=
seat
))
...
@@ -200,7 +200,7 @@ class VoucherViewOffersEndpointTests(
...
@@ -200,7 +200,7 @@ class VoucherViewOffersEndpointTests(
def
test_voucher_offers_listing_product_found
(
self
):
def
test_voucher_offers_listing_product_found
(
self
):
""" Verify the endpoint returns offers data for single product range. """
""" Verify the endpoint returns offers data for single product range. """
course
,
seat
=
self
.
create_course_and_seat
()
course
,
seat
=
self
.
create_course_and_seat
()
self
.
mock_
course_api_response
(
course
=
course
)
self
.
mock_
dynamic_catalog_single_course_runs_api
(
course
)
new_range
=
RangeFactory
(
products
=
[
seat
,
])
new_range
=
RangeFactory
(
products
=
[
seat
,
])
voucher
,
__
=
prepare_voucher
(
_range
=
new_range
,
benefit_value
=
10
)
voucher
,
__
=
prepare_voucher
(
_range
=
new_range
,
benefit_value
=
10
)
...
@@ -263,7 +263,7 @@ class VoucherViewOffersEndpointTests(
...
@@ -263,7 +263,7 @@ class VoucherViewOffersEndpointTests(
voucher
,
products
=
get_voucher_and_products_from_code
(
voucher
.
code
)
voucher
,
products
=
get_voucher_and_products_from_code
(
voucher
.
code
)
benefit
=
voucher
.
offers
.
first
()
.
benefit
benefit
=
voucher
.
offers
.
first
()
.
benefit
request
=
self
.
prepare_offers_listing_request
(
voucher
.
code
)
request
=
self
.
prepare_offers_listing_request
(
voucher
.
code
)
self
.
mock_
course_api_response
(
course
=
course
)
self
.
mock_
dynamic_catalog_single_course_runs_api
(
course
)
offers
=
VoucherViewSet
()
.
get_offers
(
products
=
products
,
request
=
request
,
voucher
=
voucher
)[
'results'
]
offers
=
VoucherViewSet
()
.
get_offers
(
products
=
products
,
request
=
request
,
voucher
=
voucher
)[
'results'
]
first_offer
=
offers
[
0
]
first_offer
=
offers
[
0
]
...
@@ -276,7 +276,7 @@ class VoucherViewOffersEndpointTests(
...
@@ -276,7 +276,7 @@ class VoucherViewOffersEndpointTests(
'contains_verified'
:
True
,
'contains_verified'
:
True
,
'course_start_date'
:
'2013-02-05T05:00:00Z'
,
'course_start_date'
:
'2013-02-05T05:00:00Z'
,
'id'
:
course
.
id
,
'id'
:
course
.
id
,
'image_url'
:
get_lms_url
(
'/asset-v1:test+test+test+type@asset+block@images_course_image.jpg'
)
,
'image_url'
:
'/path/to/image.jpg'
,
'organization'
:
CourseKey
.
from_string
(
course
.
id
)
.
org
,
'organization'
:
CourseKey
.
from_string
(
course
.
id
)
.
org
,
'seat_type'
:
course
.
type
,
'seat_type'
:
course
.
type
,
'stockrecords'
:
serializers
.
StockRecordSerializer
(
seat
.
stockrecords
.
first
())
.
data
,
'stockrecords'
:
serializers
.
StockRecordSerializer
(
seat
.
stockrecords
.
first
())
.
data
,
...
...
ecommerce/extensions/api/v2/views/vouchers.py
View file @
b1250ba5
...
@@ -14,9 +14,8 @@ from rest_framework_extensions.decorators import action
...
@@ -14,9 +14,8 @@ from rest_framework_extensions.decorators import action
from
slumber.exceptions
import
SlumberBaseException
from
slumber.exceptions
import
SlumberBaseException
from
ecommerce.core.constants
import
DEFAULT_CATALOG_PAGE_SIZE
from
ecommerce.core.constants
import
DEFAULT_CATALOG_PAGE_SIZE
from
ecommerce.core.url_utils
import
get_lms_url
from
ecommerce.courses.models
import
Course
from
ecommerce.courses.models
import
Course
from
ecommerce.courses.utils
import
get_course_info_from_
lms
from
ecommerce.courses.utils
import
get_course_info_from_
catalog
from
ecommerce.coupons.utils
import
get_range_catalog_query_results
from
ecommerce.coupons.utils
import
get_range_catalog_query_results
from
ecommerce.coupons.views
import
get_voucher_and_products_from_code
from
ecommerce.coupons.views
import
get_voucher_and_products_from_code
from
ecommerce.extensions.api
import
exceptions
,
serializers
from
ecommerce.extensions.api
import
exceptions
,
serializers
...
@@ -160,10 +159,9 @@ class VoucherViewSet(NonDestroyableModelViewSet):
...
@@ -160,10 +159,9 @@ class VoucherViewSet(NonDestroyableModelViewSet):
course_id
=
product
.
course_id
course_id
=
product
.
course_id
course
=
get_object_or_404
(
Course
,
id
=
course_id
)
course
=
get_object_or_404
(
Course
,
id
=
course_id
)
stock_record
=
get_object_or_404
(
StockRecord
,
product__id
=
product
.
id
)
stock_record
=
get_object_or_404
(
StockRecord
,
product__id
=
product
.
id
)
course_info
=
get_course_info_from_
lms
(
course_id
)
course_info
=
get_course_info_from_
catalog
(
request
.
site
,
course_id
)
if
course_info
:
if
course_info
:
course_info
[
'image'
]
=
{
'src'
:
get_lms_url
(
course_info
[
'media'
][
'course_image'
][
'uri'
])}
offers
.
append
(
self
.
get_course_offer_data
(
offers
.
append
(
self
.
get_course_offer_data
(
benefit
=
benefit
,
benefit
=
benefit
,
...
@@ -193,7 +191,7 @@ class VoucherViewSet(NonDestroyableModelViewSet):
...
@@ -193,7 +191,7 @@ class VoucherViewSet(NonDestroyableModelViewSet):
'contains_verified'
:
is_verified
,
'contains_verified'
:
is_verified
,
'course_start_date'
:
course_info
[
'start'
],
'course_start_date'
:
course_info
[
'start'
],
'id'
:
course
.
id
,
'id'
:
course
.
id
,
'image_url'
:
course_info
[
'image'
]
[
'src'
]
,
'image_url'
:
course_info
[
'image'
]
.
get
(
'src'
,
''
)
,
'organization'
:
CourseKey
.
from_string
(
course
.
id
)
.
org
,
'organization'
:
CourseKey
.
from_string
(
course
.
id
)
.
org
,
'seat_type'
:
course
.
type
,
'seat_type'
:
course
.
type
,
'stockrecords'
:
serializers
.
StockRecordSerializer
(
stock_record
)
.
data
,
'stockrecords'
:
serializers
.
StockRecordSerializer
(
stock_record
)
.
data
,
...
...
ecommerce/extensions/basket/tests/test_views.py
View file @
b1250ba5
...
@@ -19,9 +19,10 @@ from testfixtures import LogCapture
...
@@ -19,9 +19,10 @@ from testfixtures import LogCapture
from
ecommerce.core.constants
import
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
,
ENROLLMENT_CODE_SWITCH
from
ecommerce.core.constants
import
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
,
ENROLLMENT_CODE_SWITCH
from
ecommerce.core.models
import
SiteConfiguration
from
ecommerce.core.models
import
SiteConfiguration
from
ecommerce.core.tests
import
toggle_switch
from
ecommerce.core.tests
import
toggle_switch
from
ecommerce.core.tests.decorators
import
mock_course_catalog_api_client
from
ecommerce.core.url_utils
import
get_lms_enrollment_api_url
from
ecommerce.core.url_utils
import
get_lms_enrollment_api_url
from
ecommerce.core.url_utils
import
get_lms_url
from
ecommerce.core.url_utils
import
get_lms_url
from
ecommerce.coupons.tests.mixins
import
CouponMixin
from
ecommerce.coupons.tests.mixins
import
CouponMixin
,
CourseCatalogMockMixin
from
ecommerce.courses.tests.factories
import
CourseFactory
from
ecommerce.courses.tests.factories
import
CourseFactory
from
ecommerce.extensions.basket.utils
import
get_basket_switch_data
from
ecommerce.extensions.basket.utils
import
get_basket_switch_data
from
ecommerce.extensions.catalogue.tests.mixins
import
CourseCatalogTestMixin
from
ecommerce.extensions.catalogue.tests.mixins
import
CourseCatalogTestMixin
...
@@ -44,7 +45,7 @@ COUPON_CODE = 'COUPONTEST'
...
@@ -44,7 +45,7 @@ COUPON_CODE = 'COUPONTEST'
@ddt.ddt
@ddt.ddt
class
BasketSingleItemViewTests
(
CouponMixin
,
CourseCatalogTestMixin
,
LmsApiMockMixin
,
TestCase
):
class
BasketSingleItemViewTests
(
CouponMixin
,
CourseCatalogTestMixin
,
CourseCatalogMockMixin
,
LmsApiMockMixin
,
TestCase
):
""" BasketSingleItemView view tests. """
""" BasketSingleItemView view tests. """
path
=
reverse
(
'basket:single-item'
)
path
=
reverse
(
'basket:single-item'
)
...
@@ -132,7 +133,7 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, LmsApiMockM
...
@@ -132,7 +133,7 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, LmsApiMockM
self
.
mock_enrollment_api_success_enrolled
(
self
.
course
.
id
)
self
.
mock_enrollment_api_success_enrolled
(
self
.
course
.
id
)
self
.
create_coupon
(
catalog
=
self
.
catalog
,
code
=
COUPON_CODE
,
benefit_value
=
5
)
self
.
create_coupon
(
catalog
=
self
.
catalog
,
code
=
COUPON_CODE
,
benefit_value
=
5
)
self
.
mock_
course_api_response
(
course
=
self
.
course
)
self
.
mock_
dynamic_catalog_course_runs_api
(
course_run
=
self
.
course
)
url
=
'{path}?sku={sku}&code={code}'
.
format
(
path
=
self
.
path
,
sku
=
self
.
stock_record
.
partner_sku
,
url
=
'{path}?sku={sku}&code={code}'
.
format
(
path
=
self
.
path
,
sku
=
self
.
stock_record
.
partner_sku
,
code
=
COUPON_CODE
)
code
=
COUPON_CODE
)
response
=
self
.
client
.
get
(
url
)
response
=
self
.
client
.
get
(
url
)
...
@@ -205,7 +206,7 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, LmsApiMockM
...
@@ -205,7 +206,7 @@ class BasketSingleItemViewTests(CouponMixin, CourseCatalogTestMixin, LmsApiMockM
@httpretty.activate
@httpretty.activate
@ddt.ddt
@ddt.ddt
@override_settings
(
PAYMENT_PROCESSORS
=
[
'ecommerce.extensions.payment.tests.processors.DummyProcessor'
])
@override_settings
(
PAYMENT_PROCESSORS
=
[
'ecommerce.extensions.payment.tests.processors.DummyProcessor'
])
class
BasketSummaryViewTests
(
CourseCatalogTestMixin
,
LmsApiMockMixin
,
ApiMockMixin
,
TestCase
):
class
BasketSummaryViewTests
(
CourseCatalogTestMixin
,
CourseCatalogMockMixin
,
LmsApiMockMixin
,
ApiMockMixin
,
TestCase
):
""" BasketSummaryView basket view tests. """
""" BasketSummaryView basket view tests. """
path
=
reverse
(
'basket:summary'
)
path
=
reverse
(
'basket:summary'
)
...
@@ -262,7 +263,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -262,7 +263,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
l
.
check
(
l
.
check
(
(
(
logger_name
,
'ERROR'
,
logger_name
,
'ERROR'
,
u'Failed to retrieve data from C
ourse API
for course [{}].'
.
format
(
self
.
course
.
id
)
u'Failed to retrieve data from C
atalog Service
for course [{}].'
.
format
(
self
.
course
.
id
)
)
)
)
)
...
@@ -273,7 +274,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -273,7 +274,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
course
.
create_or_update_seat
(
'verified'
,
False
,
10
,
self
.
partner
,
create_enrollment_code
=
True
)
course
.
create_or_update_seat
(
'verified'
,
False
,
10
,
self
.
partner
,
create_enrollment_code
=
True
)
enrollment_code
=
Product
.
objects
.
get
(
product_class__name
=
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
)
enrollment_code
=
Product
.
objects
.
get
(
product_class__name
=
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
)
self
.
create_basket_and_add_product
(
enrollment_code
)
self
.
create_basket_and_add_product
(
enrollment_code
)
self
.
mock_
course_api_response
(
course
)
self
.
mock_
dynamic_catalog_course_runs_api
(
course_run
=
course
)
response
=
self
.
client
.
get
(
self
.
path
)
response
=
self
.
client
.
get
(
self
.
path
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
status_code
,
200
)
...
@@ -297,7 +298,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -297,7 +298,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
seat_without_ec
=
no_ec_course
.
create_or_update_seat
(
'verified'
,
False
,
10
,
self
.
partner
)
seat_without_ec
=
no_ec_course
.
create_or_update_seat
(
'verified'
,
False
,
10
,
self
.
partner
)
seat_with_ec
=
ec_course
.
create_or_update_seat
(
'verified'
,
False
,
10
,
self
.
partner
,
create_enrollment_code
=
True
)
seat_with_ec
=
ec_course
.
create_or_update_seat
(
'verified'
,
False
,
10
,
self
.
partner
,
create_enrollment_code
=
True
)
self
.
create_basket_and_add_product
(
seat_without_ec
)
self
.
create_basket_and_add_product
(
seat_without_ec
)
self
.
mock_
course_api_response
(
no_ec_course
)
self
.
mock_
dynamic_catalog_course_runs_api
(
course_run
=
no_ec_course
)
response
=
self
.
client
.
get
(
self
.
path
)
response
=
self
.
client
.
get
(
self
.
path
)
self
.
assertFalse
(
response
.
context
[
'switch_link_text'
])
self
.
assertFalse
(
response
.
context
[
'switch_link_text'
])
...
@@ -309,7 +310,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -309,7 +310,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
Basket
.
objects
.
all
()
.
delete
()
Basket
.
objects
.
all
()
.
delete
()
self
.
create_basket_and_add_product
(
seat_with_ec
)
self
.
create_basket_and_add_product
(
seat_with_ec
)
self
.
mock_
course_api_response
(
ec_course
)
self
.
mock_
dynamic_catalog_course_runs_api
(
course_run
=
ec_course
)
response
=
self
.
client
.
get
(
self
.
path
)
response
=
self
.
client
.
get
(
self
.
path
)
enrollment_code
=
Product
.
objects
.
get
(
product_class__name
=
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
)
enrollment_code
=
Product
.
objects
.
get
(
product_class__name
=
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
)
...
@@ -338,6 +339,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -338,6 +339,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
(
Benefit
.
FIXED
,
50
)
(
Benefit
.
FIXED
,
50
)
)
)
@ddt.unpack
@ddt.unpack
@mock_course_catalog_api_client
def
test_response_success
(
self
,
benefit_type
,
benefit_value
):
def
test_response_success
(
self
,
benefit_type
,
benefit_value
):
""" Verify a successful response is returned. """
""" Verify a successful response is returned. """
seat
=
self
.
create_seat
(
self
.
course
,
500
)
seat
=
self
.
create_seat
(
self
.
course
,
500
)
...
@@ -345,7 +347,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -345,7 +347,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
self
.
create_and_apply_benefit_to_basket
(
basket
,
seat
,
benefit_type
,
benefit_value
)
self
.
create_and_apply_benefit_to_basket
(
basket
,
seat
,
benefit_type
,
benefit_value
)
self
.
assertEqual
(
basket
.
lines
.
count
(),
1
)
self
.
assertEqual
(
basket
.
lines
.
count
(),
1
)
self
.
mock_
course_api_response
(
self
.
course
)
self
.
mock_
dynamic_catalog_single_course_runs_api
(
self
.
course
)
benefit
,
__
=
Benefit
.
objects
.
get_or_create
(
type
=
benefit_type
,
value
=
benefit_value
)
benefit
,
__
=
Benefit
.
objects
.
get_or_create
(
type
=
benefit_type
,
value
=
benefit_value
)
...
@@ -367,7 +369,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -367,7 +369,7 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
def
test_line_item_discount_data
(
self
):
def
test_line_item_discount_data
(
self
):
""" Verify that line item has correct discount data. """
""" Verify that line item has correct discount data. """
self
.
mock_
course_api_response
(
self
.
course
)
self
.
mock_
dynamic_catalog_course_runs_api
(
course_run
=
self
.
course
)
seat
=
self
.
create_seat
(
self
.
course
)
seat
=
self
.
create_seat
(
self
.
course
)
basket
=
self
.
create_basket_and_add_product
(
seat
)
basket
=
self
.
create_basket_and_add_product
(
seat
)
self
.
create_and_apply_benefit_to_basket
(
basket
,
seat
,
Benefit
.
PERCENTAGE
,
50
)
self
.
create_and_apply_benefit_to_basket
(
basket
,
seat
,
Benefit
.
PERCENTAGE
,
50
)
...
@@ -381,14 +383,15 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -381,14 +383,15 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
self
.
assertEqual
(
lines
[
0
][
1
][
'benefit_value'
],
'50
%
'
)
self
.
assertEqual
(
lines
[
0
][
1
][
'benefit_value'
],
'50
%
'
)
self
.
assertEqual
(
lines
[
1
][
1
][
'benefit_value'
],
None
)
self
.
assertEqual
(
lines
[
1
][
1
][
'benefit_value'
],
None
)
@mock_course_catalog_api_client
def
test_cached_course
(
self
):
def
test_cached_course
(
self
):
""" Verify that the course info is cached. """
""" Verify that the course info is cached. """
seat
=
self
.
create_seat
(
self
.
course
,
50
)
seat
=
self
.
create_seat
(
self
.
course
,
50
)
basket
=
self
.
create_basket_and_add_product
(
seat
)
basket
=
self
.
create_basket_and_add_product
(
seat
)
self
.
assertEqual
(
basket
.
lines
.
count
(),
1
)
self
.
assertEqual
(
basket
.
lines
.
count
(),
1
)
self
.
mock_
course_api_response
(
self
.
course
)
self
.
mock_
dynamic_catalog_single_course_runs_api
(
self
.
course
)
cache_key
=
'courses_api_detail_{}
'
.
format
(
self
.
course
.
id
)
cache_key
=
'courses_api_detail_{}
{}'
.
format
(
self
.
course
.
id
,
self
.
site
.
siteconfiguration
.
partner
.
short_code
)
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
cached_course_before
=
cache
.
get
(
cache_hash
)
cached_course_before
=
cache
.
get
(
cache_hash
)
self
.
assertIsNone
(
cached_course_before
)
self
.
assertIsNone
(
cached_course_before
)
...
@@ -396,4 +399,5 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
...
@@ -396,4 +399,5 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
response
=
self
.
client
.
get
(
self
.
path
)
response
=
self
.
client
.
get
(
self
.
path
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
status_code
,
200
)
cached_course_after
=
cache
.
get
(
cache_hash
)
cached_course_after
=
cache
.
get
(
cache_hash
)
self
.
assertEqual
(
cached_course_after
[
'name'
],
self
.
course
.
name
)
self
.
assertEqual
(
cached_course_after
[
'title'
],
self
.
course
.
name
)
ecommerce/extensions/basket/views.py
View file @
b1250ba5
...
@@ -13,7 +13,7 @@ from slumber.exceptions import SlumberBaseException
...
@@ -13,7 +13,7 @@ from slumber.exceptions import SlumberBaseException
from
ecommerce.core.constants
import
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
,
SEAT_PRODUCT_CLASS_NAME
from
ecommerce.core.constants
import
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
,
SEAT_PRODUCT_CLASS_NAME
from
ecommerce.core.url_utils
import
get_lms_url
from
ecommerce.core.url_utils
import
get_lms_url
from
ecommerce.coupons.views
import
get_voucher_and_products_from_code
from
ecommerce.coupons.views
import
get_voucher_and_products_from_code
from
ecommerce.courses.utils
import
get_certificate_type_display_value
,
get_course_info_from_
lms
,
mode_for_seat
from
ecommerce.courses.utils
import
get_certificate_type_display_value
,
get_course_info_from_
catalog
,
mode_for_seat
from
ecommerce.extensions.analytics.utils
import
prepare_analytics_data
from
ecommerce.extensions.analytics.utils
import
prepare_analytics_data
from
ecommerce.extensions.basket.utils
import
prepare_basket
,
get_basket_switch_data
from
ecommerce.extensions.basket.utils
import
prepare_basket
,
get_basket_switch_data
from
ecommerce.extensions.offer.utils
import
format_benefit_value
from
ecommerce.extensions.offer.utils
import
format_benefit_value
...
@@ -106,12 +106,13 @@ class BasketSummaryView(BasketView):
...
@@ -106,12 +106,13 @@ class BasketSummaryView(BasketView):
image_url
=
None
image_url
=
None
short_description
=
None
short_description
=
None
try
:
try
:
course
=
get_course_info_from_lms
(
course_key
)
course
=
get_course_info_from_catalog
(
self
.
request
.
site
,
course_key
)
image_url
=
get_lms_url
(
course
[
'media'
][
'course_image'
][
'uri'
])
description
=
course
[
'short_description'
]
short_description
=
course
[
'short_description'
]
image_url
=
course
[
'image'
]
.
get
(
'src'
,
''
),
course_name
=
course
[
'name'
]
short_description
=
description
if
description
else
''
course_name
=
course
[
'title'
]
except
(
ConnectionError
,
SlumberBaseException
,
Timeout
):
except
(
ConnectionError
,
SlumberBaseException
,
Timeout
):
logger
.
exception
(
'Failed to retrieve data from C
ourse API
for course [
%
s].'
,
course_key
)
logger
.
exception
(
'Failed to retrieve data from C
atalog Service
for course [
%
s].'
,
course_key
)
if
self
.
request
.
site
.
siteconfiguration
.
enable_enrollment_codes
:
if
self
.
request
.
site
.
siteconfiguration
.
enable_enrollment_codes
:
# Get variables for the switch link that toggles from enrollment codes and seat.
# Get variables for the switch link that toggles from enrollment codes and seat.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment