Пример #1
0
 def test_get_custom_connection(self):
     base_connection = get_connection(
         backend='brevisurl.backends.base.BaseBrevisUrlBackend')
     local_connection = get_connection(
         backend='brevisurl.backends.local.BrevisUrlBackend')
     self.assertIsInstance(base_connection, BaseBrevisUrlBackend)
     self.assertIsInstance(local_connection, BrevisUrlBackend)
Пример #2
0
 def get_connection(self, fail_silently=False):
     if not hasattr(self, 'brevis_connection'):
         if self.pk is not None:
             self.brevis_connection = get_connection(backend=self.backend,
                                                     fail_silently=fail_silently)
         else:
             self.brevis_connection = get_connection(fail_silently=fail_silently)
     return self.brevis_connection
Пример #3
0
 def get_connection(self, fail_silently=False):
     if not hasattr(self, 'brevis_connection'):
         if self.pk is not None:
             self.brevis_connection = get_connection(backend=self.backend,
                                                     fail_silently=fail_silently)
         else:
             self.brevis_connection = get_connection(fail_silently=fail_silently)
     return self.brevis_connection
Пример #4
0
 def test_custom_domain(self):
     original_url = 'http://www.codescale.net/'
     connection = get_connection('brevisurl.backends.local.BrevisUrlBackend', domain='http://test.com/')
     self.assertEqual(ShortUrl.objects.all().count(), 0)
     short_url = connection.shorten_url(original_url)
     self.assertEqual(ShortUrl.objects.all().count(), 1)
     self.assertRegexpMatches(short_url.shortened_url, r'^http://test\.com/.{5}')
Пример #5
0
 def test_url_path_slash_no_stripping(self):
     original_url = 'http://www.codescale.net/'
     connection = get_connection(
         'brevisurl.backends.local.BrevisUrlBackend',
         domain='http://test.com/d/')
     short_url = connection.shorten_url(original_url)
     self.assertRegexpMatches(short_url.shortened_url,
                              r'^http://test\.com/d/[^/]{5}$')
Пример #6
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()
Пример #7
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()
Пример #8
0
 def test_custom_domain(self):
     original_url = 'http://www.codescale.net/'
     connection = get_connection(
         'brevisurl.backends.local.BrevisUrlBackend',
         domain='http://test.com/')
     self.assertEqual(ShortUrl.objects.all().count(), 0)
     short_url = connection.shorten_url(original_url)
     self.assertEqual(ShortUrl.objects.all().count(), 1)
     self.assertRegexpMatches(short_url.shortened_url,
                              r'^http://test\.com/.{5}')
Пример #9
0
 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()
Пример #10
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)
Пример #11
0
    def get_redirect_url(self, **kwargs):
        # No token present
        try:
            token = kwargs.pop('token')
        except KeyError:
            log.exception('Token not found in keyword arguments')
            raise Http404

        connection = get_connection(
            'brevisurl.backends.local.BrevisUrlBackend')
        short_url_obj = None

        # Seeking domain in settings.
        if brevisurl.settings.LOCAL_BACKEND_DOMAIN is not None:
            domain = brevisurl.settings.LOCAL_BACKEND_DOMAIN.rstrip('/')
            short_url = absurl(domain=domain,
                               path=reverse('brevisurl_redirect',
                                            kwargs={'token': token}))
            try:
                short_url_obj = ShortUrl.objects.get(
                    backend=connection.class_path, shortened_url=short_url)
            except ShortUrl.DoesNotExist:
                pass

        # Seeking domain in django site framework and current site.
        if short_url_obj is None:
            try:
                site = Site.objects.get_current()
                short_url = absurl(site=site,
                                   path=reverse('brevisurl_redirect',
                                                kwargs={'token': token}))
                short_url_obj = ShortUrl.objects.get(
                    backend=connection.class_path, shortened_url=short_url)
            except ImproperlyConfigured:
                log.exception(
                    'No site object configured for this django project')
            except ShortUrl.DoesNotExist:
                pass

        # Seeking domain from current request.
        if short_url_obj is None:
            try:
                short_url = absurl(domain=self.request.get_host(),
                                   path=reverse('brevisurl_redirect',
                                                kwargs={'token': token}))
                short_url_obj = ShortUrl.objects.get(
                    backend=connection.class_path, shortened_url=short_url)
            except ShortUrl.DoesNotExist:
                log.exception(
                    'No shortened url found for backend: "%s" and token: "%s"',
                    connection.class_path, token)
                raise Http404

        return short_url_obj.original_url
Пример #12
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)
Пример #13
0
 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()
Пример #14
0
 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()
Пример #15
0
 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()
Пример #16
0
 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()
Пример #17
0
    def get_redirect_url(self, **kwargs):
        # No token present
        try:
            token = kwargs.pop('token')
        except KeyError:
            log.exception('Token not found in keyword arguments')
            raise Http404

        connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
        short_url_obj = None

        # Seeking domain in settings.
        if brevisurl.settings.LOCAL_BACKEND_DOMAIN is not None:
            domain = brevisurl.settings.LOCAL_BACKEND_DOMAIN.rstrip('/')
            short_url = absurl(domain=domain, path=reverse('brevisurl_redirect', kwargs={'token': token}))
            try:
                short_url_obj = ShortUrl.objects.get(backend=connection.class_path, shortened_url=short_url)
            except ShortUrl.DoesNotExist:
                pass

        # Seeking domain in django site framework and current site.
        if short_url_obj is None:
            try:
                site = Site.objects.get_current()
                short_url = absurl(site=site, path=reverse('brevisurl_redirect', kwargs={'token': token}))
                short_url_obj = ShortUrl.objects.get(backend=connection.class_path, shortened_url=short_url)
            except ImproperlyConfigured:
                log.exception('No site object configured for this django project')
            except ShortUrl.DoesNotExist:
                pass

        # Seeking domain from current request.
        if short_url_obj is None:
            try:
                short_url = absurl(domain=self.request.get_host(), path=reverse('brevisurl_redirect', kwargs={'token': token}))
                short_url_obj = ShortUrl.objects.get(backend=connection.class_path, shortened_url=short_url)
            except ShortUrl.DoesNotExist:
                log.exception('No shortened url found for backend: "%s" and token: "%s"', connection.class_path, token)
                raise Http404

        return short_url_obj.original_url
Пример #18
0
 def setUp(self):
     self.connection = get_connection(
         'brevisurl.backends.local.BrevisUrlBackend')
Пример #19
0
 def test_url_path_slash_no_stripping(self):
     original_url = 'http://www.codescale.net/'
     connection = get_connection('brevisurl.backends.local.BrevisUrlBackend', domain='http://test.com/d/')
     short_url = connection.shorten_url(original_url)
     self.assertRegexpMatches(short_url.shortened_url, r'^http://test\.com/d/[^/]{5}$')
Пример #20
0
 def test_get_default_connection(self):
     connection = get_connection()
     self.assertIsInstance(connection, BrevisUrlBackend)
Пример #21
0
 def test_get_custom_connection(self):
     base_connection = get_connection(backend='brevisurl.backends.base.BaseBrevisUrlBackend')
     local_connection = get_connection(backend='brevisurl.backends.local.BrevisUrlBackend')
     self.assertIsInstance(base_connection, BaseBrevisUrlBackend)
     self.assertIsInstance(local_connection, BrevisUrlBackend)
Пример #22
0
 def test_get_connection_non_existing_backend(self):
     with self.assertRaises(AttributeError):
         get_connection(backend='brevisurl.backends.local.NonExistingBackend')
Пример #23
0
 def test_get_default_connection(self):
     connection = get_connection()
     self.assertIsInstance(connection, BrevisUrlBackend)
Пример #24
0
 def setUp(self):
     self.connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
Пример #25
0
def shorten_url(original_url):
    short_url = shorten_url_util(original_url,
                                 connection=get_connection(fail_silently=True))
    return short_url.shortened_url if short_url is not None else original_url
Пример #26
0
 def test_get_connection_non_existing_backend(self):
     with self.assertRaises(AttributeError):
         get_connection(
             backend='brevisurl.backends.local.NonExistingBackend')