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)
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 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()
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()
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()
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_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()
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_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()
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 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
def shorten_url(self, original_url): """ :raises: ImproperlyConfigured, django.core.exceptions.ValidationError :raises: brevisurl.backends.local.TokensExhaustedError """ 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 try: shortened_url = self.__generate_shortened_url(domain) try: short_url, created = ShortUrl.objects.get_or_create( backend=self.class_path, original_url=original_url, original_url_hash=ShortUrl.url_hash(original_url), defaults={'shortened_url': shortened_url}) if created: log.info('Url "%s" shortened to "%s"', original_url, shortened_url) else: log.info('Url "%s" already shortened to "%s"', original_url, short_url.shortened_url) return short_url except (IntegrityError, ValidationError) as e: # Check if the error is an URL validation error. if isinstance(e, ValidationError) and e.message_dict.has_key( 'original_url'): raise # Generate another token. self.__check_tokens_exhausted() while True: shortened_url = self.__generate_shortened_url(domain) sid = transaction.savepoint() try: short_url = ShortUrl.objects.create( backend=self.class_path, original_url=original_url, shortened_url=shortened_url) log.info('Url "%s" shortened to "%s"', original_url, shortened_url) transaction.savepoint_commit(sid) return short_url except (IntegrityError, ValidationError) as e: transaction.savepoint_rollback(sid) self.__check_tokens_exhausted() except Exception: if self.fail_silently: return None else: log.exception( 'Unknown exception raised while shortening url "%s"', original_url) raise