Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
fix: add logging and test
Browse files Browse the repository at this point in the history
  • Loading branch information
aht007 committed Apr 18, 2023
1 parent 5331bd5 commit c756ed3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 7 additions & 3 deletions ecommerce/bff/subscriptions/tests/test_subscription_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import uuid
from unittest import mock

from django.urls import reverse
from oscar.core.loading import get_model
Expand All @@ -8,9 +9,8 @@

from ecommerce.core.constants import COURSE_ENTITLEMENT_PRODUCT_CLASS_NAME
from ecommerce.coupons.tests.mixins import DiscoveryMockMixin
from ecommerce.courses.tests.factories import CourseFactory
from ecommerce.extensions.catalogue.tests.mixins import DiscoveryTestMixin
from ecommerce.tests.factories import PartnerFactory, ProductFactory, StockRecordFactory
from ecommerce.tests.factories import ProductFactory
from ecommerce.tests.testcases import TestCase

Catalog = get_model('catalogue', 'Catalog')
Expand Down Expand Up @@ -56,7 +56,8 @@ def test_with_skus(self):
]
self.assertCountEqual(json.loads(response.content.decode('utf-8')), expected_data)

def test_with_valid_and_invalid_products(self):
@mock.patch('ecommerce.bff.subscriptions.views.logger.error')
def test_with_valid_and_invalid_products(self, mock_log):
product_class, _ = ProductClass.objects.get_or_create(name=COURSE_ENTITLEMENT_PRODUCT_CLASS_NAME)

product1 = ProductFactory(title="test product 1", product_class=product_class, stockrecords__partner=self.partner)
Expand All @@ -75,6 +76,9 @@ def test_with_valid_and_invalid_products(self):
response = self.client.get(url, data=[('sku', product1.stockrecords.first().partner_sku),
('sku', product2.stockrecords.first().partner_sku)
])

mock_log.assert_called_once_with(f"B2C_SUBSCRIPTIONS: Product {product2}"
f"does not have a UUID attribute or mode is None")
self.assertEqual(response.status_code, status.HTTP_200_OK)
expected_data = [
{'course_uuid': product1.attr.UUID, 'mode': product1.attr.certificate_type}
Expand Down
12 changes: 7 additions & 5 deletions ecommerce/bff/subscriptions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ def get(self, request, *args, **kwargs):
skus = self._get_skus(self.request)
products = self._get_products_by_skus(skus)
available_products = self._get_available_products(products)
data = [
{'course_uuid': product.attr.UUID, 'mode': self._mode_for_product(product)}
for product in available_products
if hasattr(product.attr, 'UUID') and self._mode_for_product(product) is not None
]
data = []
for product in available_products:
if hasattr(product.attr, 'UUID') and self._mode_for_product(product) is not None:
data.append({'course_uuid': product.attr.UUID, 'mode': self._mode_for_product(product)})
else:
logger.error(f"B2C_SUBSCRIPTIONS: Product {product}"
"does not have a UUID attribute or mode is None")
return Response(data)
except BadRequestException as e:
return HttpResponseBadRequest(str(e))
Expand Down

0 comments on commit c756ed3

Please sign in to comment.