def generate_short_url(): """ .. :quickref: ShortUrl; Generate short url. Generate short url for original url given by user using custom slug if specified. :reqheader Accept: application/json :<json string original_url: url of website to be shorten :<json string slug: (optional) slug to use in shortened url :resheader Content-Type: application/json :>json string short_url: short url which redirects to original url :status 201: ShortUrl created :status 500: slug already exists in db """ url_data = RegisterUrlSchema().load(request.get_json()).data url_data['user_id'] = str(current_user.id) short_url_obj = ShortUrl(**url_data) if 'slug' in url_data: if ShortUrl.objects(slug=short_url_obj.slug): log.error('Slug already exists in database') raise SlugAlreadyExistsException() else: slug = generate_random_slug(length=6) while ShortUrl.objects(slug=slug): log.warning('Slug duplicate detected! - {}'.format(slug)) slug = generate_random_slug(length=6) short_url_obj.slug = slug short_url_obj.save() short_url = url_for('main.get_url', slug=short_url_obj.slug, _external=True) return make_response(jsonify(dict(short_url=short_url)), CREATED)
def get_list_of_user_urls(): """ .. :quickref: ShortUrl; Get info about user`s short urls. Return details about short urls generated by current user. :resheader Content-Type: application/json :>json list[url_info] URLs: list of dicts with details about short url :status 200: query to database succeeded """ short_links = ShortUrl.objects(user_id=str(current_user.id)) return jsonify({'URLs': ShortUrlSchema(many=True).dump(short_links).data})
def get_url(slug): """ .. :quickref: ShortUrl; Get original url. Redirect to original url for given slug. :param slug: short string which was created during generation short url :status 302: ShortUrl for given slug found and redirect is performed :status 404: ShortUrl for given slug not found :status 500: multiple ShortUrls for given slug found """ try: # FIXME: update can raise different exceptions ShortUrl.objects(slug=slug).update(inc__access_counter=1) short_link = ShortUrl.objects.get(slug=slug) return redirect(short_link.original_url) except DoesNotExist as e: log.error(e) raise NotFound() except MultipleObjectsReturned as e: log.error(e) raise InternalServerError()
def test_when_only_original_url_specified_and_generated_slug_already_exist( self, generate_random_slug_mock): login_user(self.client, self.user) generate_random_slug_mock.side_effect = ['test_slug', 'new_slug'] ShortUrl(original_url='http://test.pl', slug='test_slug').save() url_data = dict(original_url='http://destination.pl') short_links = ShortUrl.objects() self.assertEqual(len(short_links), 1) response = self.client.post(self.ENDPOINT, data=url_data) self.assertEqual(response.status_code, 201) self.assertDictEqual(response.json, dict(short_url='http://localhost:5001/new_slug')) short_links = ShortUrl.objects(slug__ne='test_slug') self.assertEqual(len(short_links), 1) short_link = short_links[0] self.assertEqual(short_link.original_url, 'http://destination.pl') self.assertEqual(short_link.slug, 'new_slug') self.assertEqual(short_link.user_id, str(self.user.id)) self.assertEqual(short_link.access_counter, 0) self.assertEqual(short_link.created, datetime.datetime(2017, 2, 1, 12, 0, tzinfo=tzutc()))
def test_when_unique_slug_specified(self): login_user(self.client, self.user) url_data = dict(original_url='http://destination.pl', slug='test_slug') response = self.client.post(self.ENDPOINT, data=url_data) self.assertEqual(response.status_code, 201) self.assertDictEqual(response.json, dict(short_url='http://localhost:5001/test_slug')) short_links = ShortUrl.objects() self.assertEqual(len(short_links), 1) short_link = short_links[0] self.assertEqual(short_link.original_url, 'http://destination.pl') self.assertEqual(short_link.slug, 'test_slug') self.assertEqual(short_link.user_id, str(self.user.id)) self.assertEqual(short_link.access_counter, 0) self.assertEqual(short_link.created, datetime.datetime(2017, 2, 1, 12, 0, tzinfo=tzutc()))
def test_when_non_unique_slug_specified(self): login_user(self.client, self.user) new_user = User() new_user.save() ShortUrl(original_url='http://test.pl', slug='test_slug', user_id=str(new_user.id)).save() url_data = dict(original_url='http://destination.pl', slug='test_slug') response = self.client.post(self.ENDPOINT, data=url_data) self.assertEqual(response.status_code, 500) short_links = ShortUrl.objects() self.assertEqual(len(short_links), 1) short_link = short_links[0] self.assertEqual(short_link.original_url, 'http://test.pl') self.assertEqual(short_link.user_id, str(new_user.id)) self.assertEqual(short_link.slug, 'test_slug')