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
788809af
Commit
788809af
authored
Apr 09, 2018
by
christopher lee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ugly refactor
parent
0620976e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
50 deletions
+39
-50
ecommerce/extensions/api/v2/tests/views/test_baskets.py
+39
-50
No files found.
ecommerce/extensions/api/v2/tests/views/test_baskets.py
View file @
788809af
...
...
@@ -319,9 +319,8 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
def
setUp
(
self
):
super
(
BasketCalculateViewTests
,
self
)
.
setUp
()
self
.
products
=
ProductFactory
.
create_batch
(
3
,
stockrecords__partner
=
self
.
partner
,
categories
=
[])
qs
=
urllib
.
urlencode
({
'sku'
:
[
product
.
stockrecords
.
first
()
.
partner_sku
for
product
in
self
.
products
]},
True
)
self
.
path
=
reverse
(
'api:v2:baskets:calculate'
)
self
.
url
=
'{root}?{qs}'
.
format
(
root
=
self
.
path
,
qs
=
q
s
)
self
.
url
=
self
.
_generate_sku_url
(
self
.
product
s
)
self
.
range
=
factories
.
RangeFactory
(
includes_all_products
=
True
)
self
.
product_total
=
sum
(
product
.
stockrecords
.
first
()
.
price_excl_tax
for
product
in
self
.
products
)
...
...
@@ -472,13 +471,13 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
differentuser
=
self
.
create_user
(
username
=
'differentuser'
,
is_staff
=
False
)
products
=
self
.
_get_program_verified_seats
(
program
)
url
=
self
.
_generate_sku_u
sername_url
(
products
,
differentuser
.
username
)
url
=
self
.
_generate_sku_u
rl
(
products
,
username
=
differentuser
.
username
)
enrollment
=
[{
'mode'
:
'verified'
,
'course_details'
:
{
'course_id'
:
program
[
'courses'
][
0
][
'key'
]}}]
self
.
mock_user_data
(
differentuser
.
username
,
owned_products
=
enrollment
)
expected
=
{
'total_incl_tax_excl_discounts'
:
sum
(
product
.
stockrecords
.
first
()
.
price_excl_tax
for
product
in
products
[
1
:
]),
for
product
in
products
[
:
1
]),
'total_incl_tax'
:
Decimal
(
'0.00'
),
'currency'
:
'USD'
}
...
...
@@ -492,27 +491,12 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
@override_flag
(
"use_cached_basket_calculate_for_marketing_user"
,
active
=
True
)
def
test_basket_calculate_by_staff_user_anon_username
(
self
,
mock_calculate_basket
):
"""Verify a request made by the Marketing Staff user is cached"""
marketing_user
=
self
.
create_user
(
username
=
BasketCalculateView
.
MARKETING_USER
,
is_staff
=
True
)
self
.
client
.
logout
()
self
.
client
.
login
(
username
=
marketing_user
.
username
,
password
=
self
.
password
)
# Generate the Offer and Product data
products
=
self
.
products
qs
=
urllib
.
urlencode
(
{
'sku'
:
[
product
.
stockrecords
.
first
()
.
partner_sku
for
product
in
products
]},
True
)
self
.
_login_as_marketing_user
()
# Generate the URL containing the SKUs
path
=
reverse
(
'api:v2:baskets:calculate'
)
url
=
'{root}?{qs}'
.
format
(
root
=
path
,
qs
=
qs
)
expected
=
{
'Test Succeeded'
:
True
}
mock_calculate_basket
.
return_value
=
{
'Test Succeeded'
:
True
}
response
=
self
.
client
.
get
(
url
)
response
=
self
.
client
.
get
(
self
.
url
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertTrue
(
mock_calculate_basket
.
called
)
self
.
assertEqual
(
response
.
data
,
expected
)
...
...
@@ -520,14 +504,14 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
mock_calculate_basket
.
reset_mock
()
# Call BasketCalculate again to test that we get the Cached response
response
=
self
.
client
.
get
(
url
)
response
=
self
.
client
.
get
(
self
.
url
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertFalse
(
mock_calculate_basket
.
called
)
self
.
assertEqual
(
response
.
data
,
expected
)
different_user
=
self
.
create_user
(
username
=
'different_user'
,
is_staff
=
False
)
url
=
self
.
_generate_sku_u
sername_url
(
products
,
different_user
.
username
)
url
=
self
.
_generate_sku_u
rl
(
self
.
products
,
username
=
different_user
.
username
)
mock_calculate_basket
.
reset_mock
()
...
...
@@ -541,28 +525,12 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
@override_flag
(
"use_cached_basket_calculate_for_marketing_user"
,
active
=
False
)
def
test_basket_calculate_by_staff_user_anon_username_without_cache
(
self
,
mock_calculate_basket
):
"""Verify a request made by the Marketing Staff user is cached"""
marketing_user
=
self
.
create_user
(
username
=
BasketCalculateView
.
MARKETING_USER
,
is_staff
=
True
)
self
.
client
.
logout
()
self
.
client
.
login
(
username
=
marketing_user
.
username
,
password
=
self
.
password
)
self
.
_login_as_marketing_user
()
# Generate the Offer and Product data
products
=
self
.
products
qs
=
urllib
.
urlencode
(
{
'sku'
:
[
product
.
stockrecords
.
first
()
.
partner_sku
for
product
in
products
]},
True
)
# Generate the URL containing the SKUs
path
=
reverse
(
'api:v2:baskets:calculate'
)
url
=
'{root}?{qs}'
.
format
(
root
=
path
,
qs
=
qs
)
self
.
mock_user_data
(
marketing_user
,
owned_products
=
[])
expected
=
{
'Test Succeeded'
:
True
}
mock_calculate_basket
.
return_value
=
{
'Test Succeeded'
:
True
}
response
=
self
.
client
.
get
(
url
)
response
=
self
.
client
.
get
(
self
.
url
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertTrue
(
mock_calculate_basket
.
called
)
self
.
assertEqual
(
response
.
data
,
expected
)
...
...
@@ -570,12 +538,11 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
mock_calculate_basket
.
reset_mock
()
# Call BasketCalculate again to test that we get the Cached response
response
=
self
.
client
.
get
(
url
)
response
=
self
.
client
.
get
(
self
.
url
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertTrue
(
mock_calculate_basket
.
called
)
self
.
assertEqual
(
response
.
data
,
expected
)
@httpretty.activate
@mock.patch
(
'ecommerce.extensions.api.v2.views.baskets.logger.exception'
)
def
test_basket_calculate_by_staff_user_invalid_username
(
self
,
mocked_logger
):
...
...
@@ -593,7 +560,7 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
differentuser
=
self
.
create_user
(
username
=
'differentuser'
,
is_staff
=
False
)
products
=
self
.
_get_program_verified_seats
(
program
)
url
=
self
.
_generate_sku_u
sername_url
(
products
,
'invalidusername'
)
url
=
self
.
_generate_sku_u
rl
(
products
,
username
=
'invalidusername'
)
enrollment
=
[{
'mode'
:
'verified'
,
'course_details'
:
{
'course_id'
:
program
[
'courses'
][
0
][
'key'
]}}]
self
.
mock_user_data
(
differentuser
.
username
,
enrollment
)
...
...
@@ -663,8 +630,7 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
# If it's only one product, the entitlement voucher is applied
product
=
self
.
products
[
0
]
qs
=
urllib
.
urlencode
({
'sku'
:
[
product
.
stockrecords
.
first
()
.
partner_sku
]},
True
)
url
=
'{root}?{qs}'
.
format
(
root
=
self
.
path
,
qs
=
qs
)
url
=
self
.
_generate_sku_url
(
self
.
products
,
1
)
product_total
=
product
.
stockrecords
.
first
()
.
price_excl_tax
response
=
self
.
client
.
get
(
url
)
...
...
@@ -678,13 +644,27 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
data
,
expected
)
def
_generate_sku_username_url
(
self
,
products
,
username
,
skippedskus
=
1
):
def
_generate_sku_url
(
self
,
products
,
number_of_products
=
None
,
username
=
None
):
"""
Generates the calculate basket view's url with for the given products
Args:
products (Products): TODO of products
number_of_products (int): Number of given products to add in the url
username (string, optional): Username to add in the url
Returns:
(string): Url with product skus and username appended as parameters
"""
if
not
number_of_products
:
number_of_products
=
len
(
products
)
qs
=
urllib
.
urlencode
(
{
'sku'
:
[
product
.
stockrecords
.
first
()
.
partner_sku
for
product
in
products
[
skippedskus
:
]]},
{
'sku'
:
[
product
.
stockrecords
.
first
()
.
partner_sku
for
product
in
products
[
:
number_of_products
]]},
True
)
path
=
reverse
(
'api:v2:baskets:calculate'
)
url
=
'{root}?{qs}'
.
format
(
root
=
path
,
qs
=
qs
)
+
'&username={username}'
.
format
(
username
=
username
)
url
=
'{root}?{qs}'
.
format
(
root
=
self
.
path
,
qs
=
qs
)
if
username
:
url
+=
'&username={username}'
.
format
(
username
=
username
)
return
url
def
_get_program_verified_seats
(
self
,
program
):
...
...
@@ -695,3 +675,12 @@ class BasketCalculateViewTests(ProgramTestMixin, TestCase):
if
seat
.
attr
.
certificate_type
==
'verified'
:
products
.
append
(
seat
)
return
products
def
_login_as_marketing_user
(
self
):
marketing_user
=
self
.
create_user
(
username
=
BasketCalculateView
.
MARKETING_USER
,
is_staff
=
True
)
self
.
client
.
logout
()
self
.
client
.
login
(
username
=
marketing_user
.
username
,
password
=
self
.
password
)
return
marketing_user
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