def test_save_without_custom(self): url = "www.a.com" Shortener.shorten(url=url) result = OriginalUrl.objects.get(original=url) result_custom = ShortUrl.objects.get(url_associated=result.id) self.assertEqual(result.original, url) self.assertEqual(result_custom.shortened, Shortener.encode(result_custom.hash_id))
def clean_custom(self): custom = self.cleaned_data['custom'] try: Shortener.validate_custom(custom) except AlreadyTakenError as e: raise ValidationError(e.message) return custom
def test_save_existent_with_custom(self): url = "www.c.com" custom = "kame" result_url1 = Shortener.shorten(url=url) result_url2 = Shortener.shorten(url=url, custom=custom) custom_result = ShortUrl.objects.get(shortened=custom) self.assertNotEqual(result_url1, result_url2) self.assertEqual(result_url1.url_associated_id, custom_result.url_associated_id)
def test_save_with_custom(self): url = "www.b.com" custom = "nope" Shortener.shorten(url=url, custom=custom) result_url = OriginalUrl.objects.get(original=url) result_custom = ShortUrl.objects.get(shortened=custom) self.assertEqual(result_url.original, url) self.assertEqual(result_custom.shortened, Shortener.encode(result_custom.hash_id)) self.assertEqual(result_custom.url_associated_id, result_url.id)
def get(self, _, encoded): decoded = Shortener.decode(encoded) try: url_shortened = ShortUrl.objects.select_for_update().get( hash_id=decoded) domain = Domain.objects.select_for_update().get( name=url_shortened.url_associated.domain.name) domain.count += 1 domain.save() url_shortened.count += 1 url_shortened.save() return redirect(url_shortened.url_associated.original) except ObjectDoesNotExist: raise Http404("URL not existent")
def post(self, request, *args, **kwargs): body_data = json.loads(request.body) form = ShortUrlForm(body_data) if form.is_valid(): to_shorten = form.cleaned_data['url'] custom = form.cleaned_data["custom"] user = request.user if not request.user.is_anonymous() else None result = Shortener.shorten(to_shorten, custom, user=user) shortened = custom if custom else result.shortened context = { 'shortened': shortened, 'url': "http://" + settings.HOST + "/" + shortened } return JsonResponse(context, status=http.HTTPStatus.OK) else: return JsonResponse( {'errors': [(f, e) for f, e in form.errors.items()]}, status=http.HTTPStatus.UNPROCESSABLE_ENTITY)
def test_encode(self): self.assertEqual(Shortener.AVAILABLE_CHARS[0], Shortener.encode(0)) self.assertEqual(Shortener.AVAILABLE_CHARS[26], Shortener.encode(26)) self.assertEqual('eyay', Shortener.encode(217752)) self.assertEqual('ooo', Shortener.encode(18662)) self.assertEqual('upiz', Shortener.encode(952873))
def test_save_existent_custom(self): custom = "right" Shortener.shorten(url="www.d.com", custom=custom) with self.assertRaises(AlreadyTakenError): Shortener.shorten(url="www.e.com", custom=custom)
def test_save_existent(self): url = "www.c.com" result_url1 = Shortener.shorten(url=url) result_url2 = Shortener.shorten(url=url) self.assertNotEqual(result_url1, result_url2)
def test_next_encoded(self): value, encoded = Shortener.get_next_encoded() self.assertEqual(value, Shortener.decode(encoded)) self.assertEqual(encoded, Shortener.encode(value))
def test_invalid_chars_custom(self): Shortener.shorten(url="www.b.com", custom="nope") with self.assertRaises(AlreadyTakenError): Shortener.validate_custom("-.google")
def test_already_taken_custom(self): Shortener.shorten(url="www.a.com", custom="upiz") with self.assertRaises(AlreadyTakenError): Shortener.validate_custom("upiz")
def test_decode(self): self.assertEqual(0, Shortener.decode(Shortener.AVAILABLE_CHARS[0])) self.assertEqual(26, Shortener.decode(Shortener.AVAILABLE_CHARS[26])) self.assertEqual(217752, Shortener.decode('eyay')) self.assertEqual(18662, Shortener.decode('ooo')) self.assertEqual(952873, Shortener.decode('upiz'))