def test_prev_course_user_not_verified(self): """If a user is not verified, they should not get a coupon for the course""" coupon = CouponFactory.create( coupon_type=Coupon.DISCOUNTED_PREVIOUS_COURSE, content_object=self.run1.course, ) assert is_coupon_redeemable(coupon, self.user) is False
def post(self, request, code, *args, **kwargs): """Attach a coupon to a user""" with transaction.atomic(): coupon = get_object_or_404(Coupon, coupon_code=code) if not is_coupon_redeemable(coupon, self.request.user): # Coupon is not redeemable. Return a 404 to prevent the user from raise Http404 try: user_coupon = UserCoupon.objects.get( coupon=coupon, user=self.request.user, ) except UserCoupon.DoesNotExist: user_coupon = UserCoupon( coupon=coupon, user=self.request.user, ) # Note: we always want to save so that the modification date is updated user_coupon.save_and_log(request.user) return Response(status=HTTP_200_OK, data={ 'message': 'Attached user to coupon successfully.', 'coupon': CouponSerializer(coupon).data, })
def post(self, request, code, *args, **kwargs): """Attach a coupon to a user""" with transaction.atomic(): coupon = get_object_or_404(Coupon, coupon_code=code) if not is_coupon_redeemable(coupon, self.request.user): # Coupon is not redeemable. Return a 404 to prevent the user from raise Http404 try: user_coupon = UserCoupon.objects.get( coupon=coupon, user=self.request.user, ) except UserCoupon.DoesNotExist: user_coupon = UserCoupon( coupon=coupon, user=self.request.user, ) # Note: we always want to save so that the modification date is updated user_coupon.save_and_log(request.user) return Response( status=HTTP_200_OK, data={ 'message': 'Attached user to coupon successfully.', 'coupon': CouponSerializer(coupon).data, } )
def test_is_not_valid(self): """If a Coupon is not valid it should not be redeemable""" coupon = CouponFactory.create(content_object=self.program) with patch('ecommerce.api.Coupon.is_valid', new_callable=PropertyMock) as is_valid: is_valid.return_value = False assert is_coupon_redeemable(coupon, self.user) is False
def test_no_more_coupons(self): """If user has no redemptions left the coupon should not be redeemable""" coupon = CouponFactory.create(content_object=self.program) with patch('ecommerce.api.Coupon.user_has_redemptions_left', autospec=True) as _user_has_redemptions: _user_has_redemptions.return_value = False assert is_coupon_redeemable(coupon, self.user) is False assert _user_has_redemptions.call_count == 1 _user_has_redemptions.assert_called_with(coupon, self.user)
def test_prev_course(self): """ A coupon for a previously purchased course should be redeemable if it applies to the course which is being purchased """ coupon = CouponFactory.create( coupon_type=Coupon.DISCOUNTED_PREVIOUS_COURSE, content_object=self.run1.course, ) CachedEnrollmentFactory.create(user=self.user, course_run=self.run1) assert is_coupon_redeemable(coupon, self.user) is True
def test_user_not_enrolled_in_live_program(self): """A coupon is not redeemable if the coupon's program is not live""" coupon = CouponFactory.create(content_object=self.program) self.program.live = False self.program.save() assert is_coupon_redeemable(coupon, self.user) is False
def test_user_not_enrolled_in_program(self): """A coupon is not redeemable if the user is not enrolled in the same program as any coupon""" coupon = CouponFactory.create(content_object=self.program) user = UserFactory.create() assert is_coupon_redeemable(coupon, user) is False
def test_standard(self): """ A standard coupon should be redeemable if various conditions are met """ coupon = CouponFactory.create(content_object=self.program) assert is_coupon_redeemable(coupon, self.user) is True