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_findall(self): url = "http://xlarrakoetxea.org" clicks = set() # Save sl = ShortLink(url=url) sl.save() # Clicks for i in range(random.randrange(0, 5)): c = Click(token=sl.token, os="linux") c.save() clicks.add(c) clicks2 = Click.findall(sl.token) self.assertEquals(len(clicks), len(clicks2)) for i in clicks: clicks2_aux = set(clicks2) for j in clicks2_aux: if i == j: clicks2.remove(j) self.assertEquals(0, len(clicks2))
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_delete_link(self): counter = random.randrange(0, 100000) url = "xlarrakoetxea.org" sl = ShortLink(counter=counter, url=url) sl.save() r = get_redis_connection() times = random.randrange(10, 100) for i in range(times): c = Click(token=sl.token) c.save() self.assertTrue(r.exists(ShortLink.REDIS_TOKEN_KEY.format(sl.token))) self.assertTrue(r.sismember(ShortLink.REDIS_URL_KEY.format(sl.url), sl.token)) self.assertEquals(times, len(r.keys( Click.REDIS_CLICK_KEY.format(sl.token, "*")))) sl.delete() self.assertFalse(r.exists(ShortLink.REDIS_TOKEN_KEY.format(sl.token))) self.assertFalse(r.sismember(ShortLink.REDIS_URL_KEY.format(sl.url), sl.token)) self.assertEquals(0, len(r.keys( Click.REDIS_CLICK_KEY.format(sl.token, "*"))))
def click_link(token, request_meta_dict): #Extract data user_agent = request_meta_dict.get('HTTP_USER_AGENT') data = detect_browser_and_OS(user_agent) ip = request_meta_dict.get('REMOTE_ADDR') language = request_meta_dict.get('HTTP_ACCEPT_LANGUAGE', "None").split(",")[0] location = detect_country_location_with_geoip(ip).get('country_code') #TODO: remote host c = Click(token=token, browser=data[1], os=data[2], ip=ip, language=language, location=location) # Metrics ClickMetrics().increment() TotalClickMetrics().increment() c.save()
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)