jeudi 25 février 2016

Django testing, change request

I have a middleware from secretballot

class SecretBallotMiddleware(object):
    def process_request(self, request):
        request.secretballot_token = self.generate_token(request)

    def generate_token(self, request):
        raise NotImplementedError


class SecretBallotIpMiddleware(SecretBallotMiddleware):
    def generate_token(self, request):
        return request.META['REMOTE_ADDR']


class SecretBallotIpUseragentMiddleware(SecretBallotMiddleware):
    def generate_token(self, request):
        s = ''.join((request.META['REMOTE_ADDR'], request.META.get('HTTP_USER_AGENT', '')))
        return md5(s.encode('utf8')).hexdigest()

and I use this in my view (e.g. 'different_view'):

token = request.secretballot_token

How can I change this token form request in my tests?

class BasicTest(TestCase):
    def test_one(self):
        self.client.request['secretballot_token']='asd' #??
        response = self.client.post('/different_view/')

And I want to send post in this test to /different_view/ but with my own, changed token.

Aucun commentaire:

Enregistrer un commentaire