Example #1
0
class AbsoluteURLFieldTests(TestCase):
    """ Tests for the AbsoluteURLField. """
    def setUp(self):
        super(AbsoluteURLFieldTests, self).setUp()
        self.field = AbsoluteURLField()
        self.field._context = {'request': MockRequest()}  # pylint:disable=protected-access

    def test_to_representation_without_request(self):
        """ Verify an AssertionError is raised if no request is passed as context to the field. """
        self.field._context = {}  # pylint:disable=protected-access
        self.assertRaises(AssertionError, self.field.to_representation,
                          '/image.jpg')

    @ddt.data('http://example.com', 'https://example.org')
    def test_to_representation_with_absolute_url(self, value):
        """ Verify the method returns the passed value, if the value is an absolute URL. """
        self.assertEqual(self.field.to_representation(value), value)

    def test_to_representation(self):
        """ Verify the method returns an absolute URL. """
        self.assertEqual(self.field.to_representation('/image.jpg'),
                         MockRequest.ROOT + '/image.jpg')
class AbsoluteURLFieldTests(TestCase):
    """ Tests for the AbsoluteURLField. """

    def setUp(self):
        super(AbsoluteURLFieldTests, self).setUp()
        self.field = AbsoluteURLField()
        self.field._context = {'request': MockRequest()}  # pylint:disable=protected-access

    def test_to_representation_without_request(self):
        """ Verify an AssertionError is raised if no request is passed as context to the field. """
        self.field._context = {}  # pylint:disable=protected-access
        self.assertRaises(AssertionError, self.field.to_representation, '/image.jpg')

    @ddt.data(
        'http://example.com',
        'https://example.org'
    )
    def test_to_representation_with_absolute_url(self, value):
        """ Verify the method returns the passed value, if the value is an absolute URL. """
        self.assertEqual(self.field.to_representation(value), value)

    def test_to_representation(self):
        """ Verify the method returns an absolute URL. """
        self.assertEqual(self.field.to_representation('/image.jpg'), MockRequest.ROOT + '/image.jpg')
Example #3
0
    def get_uri_absolute(self, course_overview):
        """
        Convert the media resource's URI to an absolute URI.
        """
        uri = getattr(course_overview, self.uri_attribute)

        if not uri:
            # Return empty string here, to keep the same
            # response type in case uri is empty as well.
            return ""

        cdn_applied_uri = course_overview.apply_cdn_to_url(uri)
        field = AbsoluteURLField()

        # In order to use the AbsoluteURLField to have the same
        # behaviour what ImageSerializer provides, we need to set
        # the request for the field
        field._context = {"request": self.context.get("request")}  # lint-amnesty, pylint: disable=protected-access

        return field.to_representation(cdn_applied_uri)