def test_unicode_token(self): mw = SecretBallotIpUseragentMiddleware() r = HttpRequest() r.META['REMOTE_ADDR'] = '1.2.3.4' r.META['HTTP_USER_AGENT'] = u"Orange España" mw.process_request(r) token = r.secretballot_token assert token == 'fdb9f3e35ac8355e1e97f338f0ede097'
def sub_vote(request, term_uid, def_uid): rc = RequestContext(request) definition = Definitions.objects.filter(show=True).get(uid=def_uid) sb = SecretBallotIpUseragentMiddleware() tok = sb.generate_token(request) definition.add_vote(tok, '-1') v = Vote.objects.filter(object_id=definition.id) ups = v.filter(vote=1).count() downs = v.filter(vote=-1).count() rank2 = confidence(ups, downs) definition.vote_rank2 = rank2 definition.save() t = Terms.objects.get(uid=term_uid) t.modified_time = datetime.now() t.save() target = "/" + str(term_uid) + '.html' return HttpResponseRedirect(target)
def test_ip_ua_middleware(self): mw = SecretBallotIpUseragentMiddleware() # basic token r = HttpRequest() r.META['REMOTE_ADDR'] = '1.2.3.4' r.META['HTTP_USER_AGENT'] = 'Firefox' mw.process_request(r) ff_token = r.secretballot_token # same one r = HttpRequest() r.META['REMOTE_ADDR'] = '1.2.3.4' r.META['HTTP_USER_AGENT'] = 'Firefox' mw.process_request(r) ff_token2 = r.secretballot_token assert ff_token == ff_token2 # different one r = HttpRequest() r.META['REMOTE_ADDR'] = '1.2.3.4' r.META['HTTP_USER_AGENT'] = 'Chrome' mw.process_request(r) chrome_token = r.secretballot_token assert ff_token != chrome_token # blank one r = HttpRequest() r.META['REMOTE_ADDR'] = '1.2.3.4' r.META['HTTP_USER_AGENT'] = '' mw.process_request(r) blank_token = r.secretballot_token assert ff_token != blank_token
def gen_token(request): temp = SecretBallotIpUseragentMiddleware() token = temp.generate_token(request) return token