This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Error if products in basket are already purchased (#3929)
* feat: Error if products in basket are already purchased * refactor: Add tests, Improve error message * refactor: Update docstring * test: Increase coverage
- Loading branch information
1 parent
3e4ec38
commit b727b45
Showing
4 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import mock | ||
|
||
from ecommerce.courses.tests.factories import CourseFactory | ||
from ecommerce.extensions.iap.api.v1.utils import products_in_basket_already_purchased | ||
from ecommerce.extensions.order.utils import UserAlreadyPlacedOrder | ||
from ecommerce.extensions.test.factories import create_basket, create_order | ||
from ecommerce.tests.testcases import TestCase | ||
|
||
|
||
class TestProductsInBasketPurchased(TestCase): | ||
""" Tests for products_in_basket_already_purchased method. """ | ||
|
||
def setUp(self): | ||
super(TestProductsInBasketPurchased, self).setUp() | ||
self.user = self.create_user() | ||
self.client.login(username=self.user.username, password=self.password) | ||
|
||
self.course = CourseFactory(partner=self.partner) | ||
product = self.course.create_or_update_seat('verified', False, 50) | ||
self.basket = create_basket( | ||
owner=self.user, site=self.site, price='50.0', product_class=product.product_class | ||
) | ||
create_order(site=self.site, user=self.user, basket=self.basket) | ||
|
||
def test_already_purchased(self): | ||
""" | ||
Test products in basket already purchased by user | ||
""" | ||
with mock.patch.object(UserAlreadyPlacedOrder, 'user_already_placed_order', return_value=True): | ||
return_value = products_in_basket_already_purchased(self.user, self.basket, self.site) | ||
self.assertTrue(return_value) | ||
|
||
def test_not_purchased_yet(self): | ||
""" | ||
Test products in basket not yet purchased by user | ||
""" | ||
with mock.patch.object(UserAlreadyPlacedOrder, 'user_already_placed_order', return_value=False): | ||
return_value = products_in_basket_already_purchased(self.user, self.basket, self.site) | ||
self.assertFalse(return_value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
|
||
from oscar.core.loading import get_model | ||
|
||
from ecommerce.extensions.order.utils import UserAlreadyPlacedOrder | ||
|
||
Product = get_model('catalogue', 'Product') | ||
|
||
|
||
def products_in_basket_already_purchased(user, basket, site): | ||
""" | ||
Check if products in a basket are already purchased by a user. | ||
""" | ||
products = Product.objects.filter(line__order__basket=basket) | ||
for product in products: | ||
if not product.is_enrollment_code_product and \ | ||
UserAlreadyPlacedOrder.user_already_placed_order(user=user, product=product, site=site): | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters