def test_basic(self): templtxt = """\ {% load tracked_links %} {% trackedurl linkid "linkanalytics/r/path/to/file.ext" %} """ templtxt = textwrap.dedent(templtxt) t = Template(templtxt) uuid = '0'*32 urlbase = 'http://example.com' c = Context({'linkid':uuid, 'urlbase':urlbase}) s = t.render(c) url = urlex.hashedurl_redirect_local(uuid, 'path/to/file.ext') self.assertEquals(s, '\n{0}{1}\n'.format(urlbase,url))
def test_baduuid(self): # first ensure no visitors or trackers exist self.assertEquals(Visitor.objects.count(), 0) self.assertEquals(Tracker.objects.count(), 0) # try to follow a non-existent uuid uuid = '0'*32 url = urlex.hashedurl_redirect_local(uuid, 'linkanalytics/testurl/') response = self.client.get(url, follow=True) # it should generate a 404 error self.assertEquals(response.status_code, 404) self.assertEquals(Visitor.objects.count(), 1) self.assertEquals(Visitor.objects.all()[0].username, '_UNKNOWN') self.assertEquals(Tracker.objects.count(), 1) self.assertEquals(Tracker.objects.all()[0].name, '_UNKNOWN')
def test_trail(self): htmlsrc = "<html><head></head><body>" htmlsrc += "{% trackedurl linkid 'linkanalytics/r/path/to/file.ext' %}" htmlsrc += "</body></html>" textsrc = "{% trackedurl linkid 'linkanalytics/r/path/to/file.ext' %}" urlbase = 'http://example.com' uuid = '0'*32 inst = _email.email_instantiator(textsrc, htmlsrc, urlbase) text, html = inst(uuid) url = urlex.hashedurl_redirect_local(uuid, 'path/to/file.ext') self.assertEquals(text, '{0}{1}'.format(urlbase, url)) expecthtml = "<html><head></head><body>{0}{1}</body></html>" expecthtml = expecthtml.format(urlbase, url) self.assertEquals(html, expecthtml) #==============================================================================#
def test_hashValidation(self): t = self.new_tracker(name='Name1') v = self.new_visitor(username='******') i = t.add_visitor(v) # This first try will contain a hash calculated for a different url. hash = urlex.generate_urlhash(i.uuid, '/linkanalytics/nonexistent_url/') urltail = urlex.urltail_redirect_local('linkanalytics/testurl/') url = urlex.assemble_hashedurl(hash, i.uuid, urltail) # This should not pass the hash checker response = self.client.get(url, follow=True) self.assertEquals(response.status_code, 404) url = urlex.hashedurl_redirect_local(i.uuid, 'linkanalytics/testurl/') # This now should pass the hash checker response = self.client.get(url, follow=True) self.assertEquals(response.status_code, 200) self.assertEquals(len(response.redirect_chain),1)
def test_local_redirect(self): t = self.new_tracker('url1') v = self.new_visitor('trackee1') i = t.add_visitor(v) url = urlex.hashedurl_redirect_local(i.uuid, 'linkanalytics/testurl/') response = self.client.get(url, follow=True) chain = response.redirect_chain self.assertEquals(len(chain), 1) self.assertEquals(chain[0], (u'http://testserver/linkanalytics/testurl/',302)) # reload the instance so its fields represent its current state i = TrackedInstance.objects.filter(uuid=i.uuid)[0] self.assertEquals(i.first_access.date(), datetime.date.today()) self.assertEquals(i.recent_access.date(), datetime.date.today()) self.assertEquals(i.access_count, 1)