def test_create_new_token(self): counter = random.randrange(100000) url = "http://xlarrakoetxea{0}.org".format(random.randrange(100)) # Set the counter ShortLink.set_counter(counter) # Call the async task with celery result = tasks.create_token.delay(url) new_token = result.get() #Check if the returned token is ok self.assertEquals(utils.counter_to_token(counter + 1), new_token) self.assertTrue(result.successful()) # Check if the link is stored in the database correctly sl = ShortLink.find(url=url)[0] # creation_date is trap!! :P sl2 = ShortLink(counter=counter + 1, url=url, creation_date=sl.creation_date) # The host and title are set after the instance was created so we add sl2.host = sl.host sl2.title = sl.title self.assertEquals(sl2, sl)
def create_token(url, user_id=None, notification=True): # Get the next counter to create the token counter = ShortLink.incr_counter() # Sanitize the url url = sanitize_url(url) # Get the title # Fix this!! test if the url exists or not!! try: title = extract_url_title(url) except: title = "No title" # Get the host host = extract_url_host(url) # Create the instance with the data sl = ShortLink() sl.counter = counter sl.url = url sl.title = title sl.host = host # Save sl.save() # If is a user link save it also if user_id: user_link = UserLink() user_link.user = User.objects.get(id=user_id) user_link.token = sl.token user_link.save() # Only need notification if we have a user if notification: # Send notifications notif = ShortLinkNotification(sl, user_id=user_id) #notif.send_push() # Push realtime notification notif.save() # save the notification for the dashboard # Fill the metrics SharedLinkMetrics().increment() logger.debug("{0} shorted url '{1}' to token '{2}'".format(user_id, url, sl.token)) # Return the new token return sl.token
def test_shortlink_basic_object(self): url = "xlarrakoetxea.org" counter = random.randrange(0, 100000) token = utils.counter_to_token(counter) creation_date = None clicks = 0 title = "This is a title" host = url disabled = True # Setters from counter sl = ShortLink(counter=counter, url=url, title=title, host=host) sl.disabled = disabled # Getters self.assertEquals(url, sl.url) self.assertEquals(counter, sl.counter) self.assertEquals(token, sl.token) self.assertEquals(creation_date, sl.creation_date) self.assertEquals(clicks, sl.clicks) self.assertEquals(title, sl.title) self.assertEquals(host, sl.host) self.assertEquals(disabled, sl.disabled) # Setters from token sl2 = ShortLink(token=token, url=url) creation_date = dateutils.unix_now_utc() sl2.creation_date = creation_date clicks = 5 sl2.clicks = clicks sl2.title = title sl2.host = host sl2.disabled = disabled # Getters self.assertEquals(url, sl2.url) self.assertEquals(counter, sl2.counter) self.assertEquals(token, sl2.token) self.assertEquals(creation_date, sl2.creation_date) self.assertEquals(clicks, sl2.clicks) self.assertEquals(title, sl2.title) self.assertEquals(host, sl2.host) self.assertEquals(disabled, sl2.disabled)
def test_save_shortLink_autofield(self): times = random.randrange(1, 100) url = "xlarrakoetxea.org" title = "My webpage" host = "xlarrakoetxea.org" # Set the shor link counter for i in range(times): ShortLink.incr_counter() # Save sl = ShortLink() sl.url = url sl.title = title sl.host = host sl.save() # Check the correct counter sl2 = ShortLink.find(counter=times + 1) self.assertEquals(sl, sl2)