mercredi 28 novembre 2018

Django testing in creating a user with email address which is from session input, assertRedirects is not working

The user creation is using an email address as USERNAME_FIELD and it is extracted from session and save in the form save(). It seems it is not going further down to the redirection. How can I test the redirection in this case?

tests.py:

class RegistraionViewTest(TestCase):

    valid_data = {
        'email': 'good@day.com',
        'password1': 'test1234', 
    }

    kwargs = {
        'email': 'good@day.com'
    }

    def test_registration(self):
        response = self.client.post(reverse('registration'), data=self.valid_data, follow=True)
        self.assertTrue(response.context['form'].is_valid())

        # mocking the session input
        response.context['form'].save(email=self.kwargs['email'])
        self.assertTrue(account.check_password(self.valid_data['password1']))

        # working so far, but it seems there is no redirect url in response
        self.assertRedirects(response, reverse('next_url'))

In views.py:

   if request.method == 'POST':
    form = RegistraionForm(request.POST)

    if form.is_valid():   
        email = request.session.get('email') 
        try: 
            account = form.save(email=email)
            return HttpResponseRedirect('next_url'))

In forms.py:

def save(self, **kwargs):
    user = super(RegistrationForm, self).save(commit=False)
    user.email = kwargs.pop('email')
    user.save()
    return user

It seems there is no url in the response in tests.py. What went wrong here?

Aucun commentaire:

Enregistrer un commentaire