예제 #1
0
class TestBrevisUrlRedirectView(TestCase):

    def setUp(self):
        self.LOCAL_BACKEND_DOMAIN = brevisurl.settings.LOCAL_BACKEND_DOMAIN
        brevisurl.settings.LOCAL_BACKEND_DOMAIN = None
        self.site = Site.objects.get_current()
        self.connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
        self.short_url = ShortUrl()
        self.short_url.original_url = 'http://www.codescale.net/'
        self.short_url.shortened_url = '{0}://{1}/12345'.format(brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL,
                                                                self.site.domain)
        self.short_url.backend = self.connection.class_path
        self.short_url.save()
        self.client = Client()

    def tearDown(self):
        brevisurl.settings.LOCAL_BACKEND_DOMAIN = self.LOCAL_BACKEND_DOMAIN

    def test_redirect(self):
        response = self.client.get(reverse('brevisurl_redirect', kwargs={'token': 12345}))
        self.assertEqual(response.status_code, 301)

    def test_redirect_non_existing_token(self):
        response = self.client.get(reverse('brevisurl_redirect', kwargs={'token': 54321}))
        self.assertEqual(response.status_code, 404)
예제 #2
0
 def test_model_save_invalid_shortened_url(self):
     with self.assertRaises(ValidationError):
         connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
         short_url = ShortUrl()
         short_url.original_url = 'http://www.codescale.net/'
         short_url.shortened_url = 'www.codescale.'
         short_url.backend = connection.class_path
         short_url.save()
예제 #3
0
 def test_model_save_invalid_shortened_url(self):
     with self.assertRaises(ValidationError):
         connection = get_connection(
             'brevisurl.backends.local.BrevisUrlBackend')
         short_url = ShortUrl()
         short_url.original_url = 'http://www.codescale.net/'
         short_url.shortened_url = 'www.codescale.'
         short_url.backend = connection.class_path
         short_url.save()
예제 #4
0
 def test_model_save(self):
     site = Site.objects.get_current()
     connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
     short_url = ShortUrl()
     short_url.original_url = 'http://www.codescale.net/'
     short_url.shortened_url = '{0}://{1}/12345'.format(brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL,
                                                        site.domain)
     short_url.backend = connection.class_path
     short_url.save()
     self.assertIsNotNone(short_url.pk)
예제 #5
0
 def test_model_save(self):
     site = Site.objects.get_current()
     connection = get_connection(
         'brevisurl.backends.local.BrevisUrlBackend')
     short_url = ShortUrl()
     short_url.original_url = 'http://www.codescale.net/'
     short_url.shortened_url = '{0}://{1}/12345'.format(
         brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL, site.domain)
     short_url.backend = connection.class_path
     short_url.save()
     self.assertIsNotNone(short_url.pk)
예제 #6
0
class TestModels(TestCase):
    def test_model_save(self):
        site = Site.objects.get_current()
        connection = get_connection(
            'brevisurl.backends.local.BrevisUrlBackend')
        short_url = ShortUrl()
        short_url.original_url = 'http://www.codescale.net/'
        short_url.shortened_url = '{0}://{1}/12345'.format(
            brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL, site.domain)
        short_url.backend = connection.class_path
        short_url.save()
        self.assertIsNotNone(short_url.pk)

    def test_model_save_invalid_original_url(self):
        with self.assertRaises(ValidationError):
            self.site = Site.objects.get_current()
            self.connection = get_connection(
                'brevisurl.backends.local.BrevisUrlBackend')
            self.short_url = ShortUrl()
            self.short_url.original_url = 'www.codescale{0}.'
            self.short_url.shortened_url = '{0}://{1}/12345'.format(
                brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL,
                self.site.domain)
            self.short_url.backend = self.connection.class_path
            self.short_url.save()

    def test_model_save_too_long_original_url(self):
        with self.assertRaises(ValidationError):
            self.site = Site.objects.get_current()
            self.connection = get_connection(
                'brevisurl.backends.local.BrevisUrlBackend')
            self.short_url = ShortUrl()
            # len(self.short_url.original_url) == brevisurl.settings.LOCAL_BACKEND_ORIGINAL_URL_MAX_LENGTH + 8
            self.short_url.original_url = 'www.{0}.com'.format(
                _random_string(
                    brevisurl.settings.LOCAL_BACKEND_ORIGINAL_URL_MAX_LENGTH))
            self.short_url.shortened_url = '{0}://{1}/12345'.format(
                brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL,
                self.site.domain)
            self.short_url.backend = self.connection.class_path
            self.short_url.save()

    def test_model_save_invalid_shortened_url(self):
        with self.assertRaises(ValidationError):
            connection = get_connection(
                'brevisurl.backends.local.BrevisUrlBackend')
            short_url = ShortUrl()
            short_url.original_url = 'http://www.codescale.net/'
            short_url.shortened_url = 'www.codescale.'
            short_url.backend = connection.class_path
            short_url.save()
예제 #7
0
class TestModels(TestCase):

    def test_model_save(self):
        site = Site.objects.get_current()
        connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
        short_url = ShortUrl()
        short_url.original_url = 'http://www.codescale.net/'
        short_url.shortened_url = '{0}://{1}/12345'.format(brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL,
                                                           site.domain)
        short_url.backend = connection.class_path
        short_url.save()
        self.assertIsNotNone(short_url.pk)

    def test_model_save_invalid_original_url(self):
        with self.assertRaises(ValidationError):
            self.site = Site.objects.get_current()
            self.connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
            self.short_url = ShortUrl()
            self.short_url.original_url = 'www.codescale{0}.' 
            self.short_url.shortened_url = '{0}://{1}/12345'.format(brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL,
                                                                    self.site.domain)
            self.short_url.backend = self.connection.class_path
            self.short_url.save()

    def test_model_save_too_long_original_url(self):
        with self.assertRaises(ValidationError):
            self.site = Site.objects.get_current()
            self.connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
            self.short_url = ShortUrl()
            # len(self.short_url.original_url) == brevisurl.settings.LOCAL_BACKEND_ORIGINAL_URL_MAX_LENGTH + 8
            self.short_url.original_url = 'www.{0}.com'.format(
                _random_string(brevisurl.settings.LOCAL_BACKEND_ORIGINAL_URL_MAX_LENGTH))
            self.short_url.shortened_url = '{0}://{1}/12345'.format(brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL,
                                                                    self.site.domain)
            self.short_url.backend = self.connection.class_path
            self.short_url.save()


    def test_model_save_invalid_shortened_url(self):
        with self.assertRaises(ValidationError):
            connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
            short_url = ShortUrl()
            short_url.original_url = 'http://www.codescale.net/'
            short_url.shortened_url = 'www.codescale.'
            short_url.backend = connection.class_path
            short_url.save()
예제 #8
0
    def shorten_url(self, original_url):
        """
        :raises: ImproperlyConfigured, django.core.exceptions.ValidationError
        :raises: brevisurl.backends.local.TokensExhaustedError
        """
        try:
            short_url = ShortUrl.objects.get(backend=self.class_path, original_url=original_url)
            log.info('Url "%s" already shortened to "%s"', original_url, short_url.shortened_url)
            return short_url
        except ShortUrl.DoesNotExist:
            pass

        try:
            short_url = ShortUrl()
            if self.kwargs.get('domain') is not None:
                # Domain is present in keyword arguments supplied by constructor.
                domain = self.kwargs.get('domain')
            elif brevisurl.settings.LOCAL_BACKEND_DOMAIN is not None:
                # Domain is defined in settings.
                domain = brevisurl.settings.LOCAL_BACKEND_DOMAIN
            else:
                # Domain is taken from django site framework.
                domain = Site.objects.get_current().domain
            # Saving newly generated shortened url.
            short_url.original_url = original_url
            short_url.shortened_url = absurl(domain=domain, path=reverse('brevisurl_redirect',
                                                                         kwargs={'token': self.__generate_token()}))
            short_url.backend = self.class_path
            short_url.save()
            log.info('Url "%s" shortened to "%s"', original_url, short_url.shortened_url)
            return short_url
        except Exception:
            if self.fail_silently:
                return None
            else:
                log.exception('Unknown exception raised while shortening url "%s"', original_url)
                raise