mercredi 26 juillet 2017

Django test - Get messages in HttpReponseRedirect with view call (so no follow = True)

I'm trying to test a login view, here is the test code:

#test login view
class LoginViewTest(TestCase):

    def setUp(self):
        self.client = Client()
        self.factory = RequestFactory()
        self.user = User.objects.create_user('user_test', 'user_test@test.com', 'pws_test')
        #self.browser = webdriver.Firefox()

    def test_successful_login(self):
        activate('en')
        request = self.client.post('/en/account/login/', {'username': 'user_test', 'password': 'pws_test'}, follow=True)
        request.user = self.user

        response = my_login(request)
        m = list(response.context['messages'])

        self.assertEqual(len(m), 1)
        self.assertEqual(str(m[0]), 'Login successfull')
        self.assertTrue(response.context['user'].is_active)
        self.assertRedirects(response, '/en/')

And the error message: 'HttpResponseRedirect' object has no attribute 'context'

I want to check if the message is correctly sent in case of successful login, but I am unable to access messages as my_login is a HttpRedirectResponse (as the login is successful), and I cannot simply set follow = True as I am directly calling a view and not a self.client.post.

Does anyone have a solution ? Thanks

Aucun commentaire:

Enregistrer un commentaire