Example #1
0
    def test_unavailable_items(self):
        """
        Assert that any references to items not for sale will cause a validation error.
        """
        self.course.live = False
        self.course.save()

        with self.assertRaises(ValidationError) as ex:
            validate_cart([{
                "uuids": [self.module.uuid],
                "seats": 10,
                "course_uuid": self.course.uuid
            }], self.user)
        assert ex.exception.detail[0] == "One or more courses are unavailable"

        self.course.live = True
        self.course.save()
        self.module.price_without_tax = None
        self.module.save()

        with self.assertRaises(ValidationError) as ex:
            validate_cart([{
                "uuids": [self.module.uuid],
                "seats": 10,
                "course_uuid": self.course.uuid
            }], self.user)
        assert ex.exception.detail[0] == "One or more courses are unavailable"
Example #2
0
    def validate_data(self):
        """
        Validates incoming request data.

        Returns:
            (string, dict): stripe token and cart information.
        """
        data = self.request.data
        try:
            token = str(data['token'])
            cart = data['cart']
            estimated_total = Decimal(float(data['total']))
        except KeyError as ex:
            raise ValidationError("Missing key {}".format(ex.args[0]))
        except (TypeError, ValueError):
            raise ValidationError("Invalid float")

        if not isinstance(cart, list):
            raise ValidationError("Cart must be a list of items")
        if len(cart) == 0:
            raise ValidationError("Cannot checkout an empty cart")
        validate_cart(cart, self.request.user)

        total = calculate_cart_subtotal(cart)
        if get_cents(total) != get_cents(estimated_total):
            log.error(
                "Cart total doesn't match expected value. "
                "Total from client: %f but actual total is: %f",
                estimated_total,
                total
            )
            raise ValidationError("Cart total doesn't match expected value")

        return token, cart
Example #3
0
 def test_validation(self):
     """
     Assert that a valid cart will pass validation.
     """
     validate_cart([{
         "uuids": [self.module.uuid],
         "seats": 10,
         "course_uuid": self.course.uuid
     }], self.user)
Example #4
0
 def test_not_a_list(self):
     """
     Raises a ValidationError if cart is not a list.
     """
     with self.assertRaises(ValidationError) as ex:
         validate_cart([{
             "uuids": None,
             "seats": 5,
             "course_uuid": self.course.uuid
         }], self.user)
     assert ex.exception.detail[0] == "uuids must be a list"
Example #5
0
 def test_duplicate_modules(self):
     """
     Assert that we don't allow duplicate modules in cart
     """
     with self.assertRaises(ValidationError) as ex:
         validate_cart([{
             "uuids": [self.module.uuid, self.module.uuid],
             "seats": 10,
             "course_uuid": self.course.uuid
         }], self.user)
     assert ex.exception.detail[0] == "Duplicate module in cart"
Example #6
0
 def test_missing_courses(self):
     """
     Assert that references to courses that are missing will cause a validation error.
     """
     with self.assertRaises(ValidationError) as ex:
         validate_cart([{
             "uuids": [self.module.uuid],
             "seats": 10,
             "course_uuid": "missing"
         }], self.user)
     assert ex.exception.detail[0] == "One or more courses are unavailable"
Example #7
0
 def test_no_seats(self):
     """
     Assert that a cart item cannot have zero seats.
     """
     with self.assertRaises(ValidationError) as ex:
         validate_cart([{
             "uuids": [self.module.uuid],
             "seats": 0,
             "course_uuid": self.course.uuid
         }], self.user)
     assert ex.exception.detail[0] == "Number of seats is zero"
Example #8
0
 def test_course_module_mismatch(self):
     """
     Assert that we don't allow duplicate items in cart
     """
     course2 = CourseFactory.create(live=True)
     ModuleFactory.create(price_without_tax=123, course=course2)
     with self.assertRaises(ValidationError) as ex:
         validate_cart([{
             "uuids": [self.module.uuid],
             "seats": 10,
             "course_uuid": course2.uuid
         }], self.user)
     assert ex.exception.detail[0] == "Course does not match up with module"
Example #9
0
 def test_int_seats(self):
     """
     Assert that non-int keys for number of seats are rejected.
     """
     for seats in ('6.5', 6.5, None, [], {}):
         item = {
             "uuids": [self.module.uuid],
             "seats": seats,
             "course_uuid": self.course.uuid
         }
         with self.assertRaises(ValidationError) as ex:
             validate_cart([item], self.user)
         assert ex.exception.detail[0] == "Seats must be an integer"
Example #10
0
    def test_buy_own_course(self):
        """
        Raises a ValidationError if a user has their own course in the cart
        """
        self.user.courses_owned.add(self.course)

        with self.assertRaises(ValidationError) as ex:
            validate_cart([{
                "uuids": [self.module.uuid],
                "seats": 5,
                "course_uuid": self.course.uuid
            }], self.user)
        message = "User cannot purchase this course"
        assert ex.exception.detail[0] == message
Example #11
0
    def test_dont_allow_empty_uuids(self):
        """
        Raise a ValidationError if uuids list is empty
        """
        with self.assertRaises(ValidationError) as ex:
            validate_cart([
                {
                    'uuids': [],
                    'seats': 5,
                    'course_uuid': self.course.uuid
                },
            ], self.user)

        assert ex.exception.detail[0] == 'uuids must not be empty'
Example #12
0
 def test_not_live_course(self):
     """
     Raises a ValidationError if a course is not live, even if we have
     access to it because we are its owner
     """
     self.user.groups.add(Group.objects.get(name="Instructor"))
     self.course.live = False
     self.course.save()
     with self.assertRaises(ValidationError) as ex:
         validate_cart([{
             "uuids": [self.module.uuid],
             "seats": 5,
             "course_uuid": self.course.uuid
         }], self.user)
     assert ex.exception.detail[0] == "One or more courses are unavailable"
Example #13
0
 def test_missing_keys(self):
     """
     Assert that missing keys cause a ValidationError.
     """
     item = {
         "uuids": [self.module.uuid],
         "seats": 10,
         "course_uuid": self.course.uuid
     }
     for key in ('uuids', 'seats', 'course_uuid'):
         with self.assertRaises(ValidationError) as ex:
             item_copy = dict(item)
             del item_copy[key]
             validate_cart([item_copy], self.user)
         assert ex.exception.detail[0] == "Missing key {}".format(key)
Example #14
0
 def test_duplicate_courses(self):
     """
     Assert that we don't allow duplicate courses in cart
     """
     module2 = ModuleFactory.create(course=self.course)
     with self.assertRaises(ValidationError) as ex:
         validate_cart([{
             "uuids": [self.module.uuid],
             "seats": 10,
             "course_uuid": self.course.uuid
         }, {
             "uuids": [module2.uuid],
             "seats": 15,
             "course_uuid": self.course.uuid
         }], self.user)
     assert ex.exception.detail[0] == "Duplicate course in cart"