def test_save_shortLink(self): counter = random.randrange(0, 100000) url = "xlarrakoetxea.org" creation_date = dateutils.unix_now_utc() clicks = 20 title = "This is a title" host = url disabled = True # Save the links sl = ShortLink(counter=counter, url=url, creation_date=creation_date, clicks=clicks, title=title, host=host) sl.disabled = disabled sl.save() r = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB) # Construct the Keys rtk = ShortLink.REDIS_TOKEN_KEY.format(utils.counter_to_token(counter)) ruk = ShortLink.REDIS_URL_KEY.format(url) # Check token = utils.counter_to_token(counter) self.assertTrue(token in r.smembers(ruk)) keys = ('url', 'creation_date', 'clicks', 'title', 'host') data = [url, creation_date, clicks, title, host] aux = r.hmget(rtk, keys) data_result = [aux[0], int(aux[1]), int(aux[2]), aux[3], aux[4]] self.assertEquals(data, data_result)
def test_click_store(self): token = utils.counter_to_token(random.randrange(0, 100000)) click_id = random.randrange(0, 100000) OS = "linux" browser = "firefox" ip = "111.222.333.444" click_date = dateutils.unix_now_utc() language = "EN_us" location = "US" c = Click(click_id, token, ip, OS, browser, click_date, language, location) c.save() # Check the stored object key = Click.REDIS_CLICK_KEY.format(token, click_id) r = get_redis_connection() values = r.hgetall(key) self.assertEquals(OS, values["os"]) self.assertEquals(browser, values["browser"]) self.assertEquals(ip, values["ip"]) self.assertEquals(click_date, int(values["click_date"])) self.assertEquals(language, values["language"]) self.assertEquals(location, values["location"])
def test_click_store_autofields(self): token = utils.counter_to_token(random.randrange(0, 100000)) url = "http://xlarrakoetxea.org" OS = "linux" ip = "111.222.333.444" incr_times = 4 # Store a link sl = ShortLink(token=token, url=url) sl.save() for i in range(incr_times): ShortLink.incr_clicks(token) c = Click(token=token, os=OS, ip=ip) c.save() # The save method has set the click_date click_date = c.click_date self.assertIsNotNone(click_date) # Check the stored object key = Click.REDIS_CLICK_KEY.format(token, c.click_id) r = get_redis_connection() values = r.hgetall(key) # Check the key is correctly set (this means that the counter has # increased correctly) correct_key = Click.REDIS_CLICK_KEY.format(token, incr_times + 1) self.assertEquals(correct_key, key) self.assertEquals(OS, values["os"]) self.assertEquals(ip, values["ip"])
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 test_get_shortLink_by_token(self): counter = random.randrange(0, 100000) url = "xlarrakoetxea.org" title = "My webpage" host = "xlarrakoetxea.org" sl = ShortLink(token=utils.counter_to_token(counter), url=url, title=title, host=host) sl.save() sl2 = ShortLink.find(token=sl.token) self.assertEquals(sl, sl2)
def test_click_basic_object_str(self): token = utils.counter_to_token(random.randrange(0, 100000)) click_id = random.randrange(0, 100000) OS = "linux" browser = "firefox" ip = "111.222.333.444" click_date = dateutils.unix_now_utc() language = "EN_us" location = "US" c = Click(click_id, token, ip, OS, browser, click_date, language, location) format = Click.OBJECT_STR_FORMAT.format(click_id, token, ip, OS, browser, click_date, language, location) self.assertEquals(format, str(c))
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_click_find(self): token = utils.counter_to_token(random.randrange(0, 100000)) OS = "linux" ip = "111.222.333.444" browser = "firefox" click_date = dateutils.unix_now_utc() language = "EN_us" location = "US" url = "http://xlarrakoetxea.org" sl = ShortLink(token=token, url=url) sl.save() c = Click( token=token, os=OS, ip=ip, browser=browser, click_date=click_date, language=language, location=location ) c.save() c2 = Click.find(token, c.click_id) self.assertEquals(c, c2)
def test_click_basic_object(self): token = utils.counter_to_token(random.randrange(0, 100000)) click_id = random.randrange(0, 100000) OS = "linux" browser = "firefox" ip = "111.222.333.444" click_date = dateutils.unix_now_utc() language = "EN_us" location = "US" c = Click(click_id, token, ip, OS, browser, click_date, language, location) # Check getters self.assertEquals(token, c.token) self.assertEquals(click_id, c.click_id) self.assertEquals(OS, c.os) self.assertEquals(browser, c.browser) self.assertEquals(ip, c.ip) self.assertEquals(click_date, c.click_date) self.assertEquals(language, c.language) self.assertEquals(location, c.location) # Check setters c2 = Click() c2.token = token c2.click_id = click_id c2.os = OS c2.browser = browser c2.ip = ip c2.click_date = click_date c2.language = language c2.location = location self.assertEquals(token, c2.token) self.assertEquals(click_id, c2.click_id) self.assertEquals(OS, c2.os) self.assertEquals(browser, c2.browser) self.assertEquals(ip, c2.ip) self.assertEquals(click_date, c2.click_date) self.assertEquals(language, c2.language) self.assertEquals(location, c2.location)
def test_shortlink_basic_object_str(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 format = ShortLink.OBJECT_STR_FORMAT.format(counter, token, url, creation_date, clicks, title, host, disabled) sl = ShortLink(counter=counter, url=url, title=title, host=host) sl.disabled = disabled self.assertEquals(format, str(sl))
def find(cls, counter=None, token=None, url=None): """Finds a shortlink or various shortlinks based on the counter, token or url :param counter: The counter of the link (numeric id) :param token: The token calculated based on the counter id :param url: The url of the stored ShortLink """ aux_self = ShortLink() if counter is not None: aux_self.counter = counter aux_self.token = counter_to_token(counter) data = aux_self._find_by_token() #Not found... if not data: raise ShortLinkNotFoundError("Short link not found") aux_self.url = data.get('url') aux_self.creation_date = int(data.get('creation_date', 0)) aux_self.clicks = int(data.get('clicks', 0)) aux_self.title = data.get('title') aux_self.host = data.get('host') aux_self.disabled = True if int(data.get('disabled')) else False return aux_self elif token is not None: aux_self.token = token aux_self.counter = token_to_counter(token) data = aux_self._find_by_token() #Not found... if not data: raise ShortLinkNotFoundError("Short link not found") aux_self.url = data.get('url') aux_self.creation_date = int(data.get('creation_date', 0)) aux_self.clicks = int(data.get('clicks', 0)) aux_self.title = data.get('title') aux_self.host = data.get('host') aux_self.disabled = True if int(data.get('disabled')) else False return aux_self elif url: aux_self.url = url tokens = aux_self._find_by_url() #Not found... if not tokens: ShortLinkNotFoundError("Short links not found") #Check again (maybe we have a list with all Nones) nothing = True for i in tokens: if i: nothing = False break if nothing: raise ShortLinkNotFoundError("Short link not found") short_links = [] for i in tokens: aux_self.token = i data = aux_self._find_by_token() sl = ShortLink() sl.token = i sl.url = data.get('url') sl.creation_date = int(data.get('creation_date', 0)) sl.clicks = int(data.get('clicks', 0)) sl.title = data.get('title') sl.host = data.get('host') #aux_self.title = data.get('title') #aux_self.host = data.get('host') sl.disabled = True if int(data.get('disabled')) else False short_links.append(sl) return short_links raise ShortLinkError("No enought data to search")
def counter(self, value): self._counter = value self._token = counter_to_token(value)
def test_counter_translation(self): for i in UtilTest.test_data: self.assertEquals(i[0], utils.counter_to_token(i[1]))
def test_start_counter_translation(self): counter = 0 self.assertEquals("0000", utils.counter_to_token(counter))