Beispiel #1
0
    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_list_coupons(self):
     """
     Test that we use pick_coupon to get the list of coupons
     """
     # Despite enabled=False, the API returns this coupon because we patched pick_coupons
     coupon = CouponFactory.create(enabled=False)
     with patch('ecommerce.views.pick_coupons',
                autospec=True) as _pick_coupons:
         _pick_coupons.return_value = [coupon]
         resp = self.client.get(reverse('coupon-list'))
     assert resp.status_code == status.HTTP_200_OK
     assert resp.json() == [CouponSerializer(coupon).data]
     assert _pick_coupons.call_count == 1
     _pick_coupons.assert_called_with(self.user)
Beispiel #3
0
 def test_coupon_course(self):
     """
     Test coupon serializer
     """
     course_run = CourseRunFactory.create(course__program__financial_aid_availability=True)
     coupon = CouponFactory.create(content_object=course_run.course)
     assert CouponSerializer(coupon).data == {
         'amount': str(coupon.amount),
         'amount_type': coupon.amount_type,
         'content_type': 'course',
         'coupon_type': Coupon.STANDARD,
         'coupon_code': coupon.coupon_code,
         'program_id': coupon.program.id,
         'object_id': coupon.object_id,
     }
Beispiel #4
0
def test_serialize_global_coupon():
    """Test that CouponSerializer produces the correct serialized data for a global coupon"""
    name = "FAKE"
    code = "1111"
    coupon = CouponFactory.build(
        payment__name=name, coupon_code=code, is_global=True, enabled=True
    )
    serialized_data = CouponSerializer(instance=coupon).data
    assert serialized_data == {
        "id": None,
        "name": name,
        "coupon_code": code,
        "enabled": True,
        "include_future_runs": False,
        "is_global": True,
    }
Beispiel #5
0
def test_serialize_coupon():
    """Test that CouponSerializer produces the correct serialized data"""
    name = "Some Coupon"
    code = "1234"
    coupon = CouponFactory.build(payment__name=name, coupon_code=code, enabled=True)
    assert str(coupon) == "Coupon {} for {}".format(
        coupon.coupon_code, str(coupon.payment)
    )
    serialized_data = CouponSerializer(instance=coupon).data
    assert serialized_data == {
        "id": None,
        "name": name,
        "coupon_code": code,
        "enabled": True,
        "include_future_runs": False,
        "is_global": False,
    }