Unittest Django Views: I can’t log into the system by unittest

Asked

Viewed 160 times

-1

I need to give a post in a "request creation" view of mine, but when I try to give the post, I am redirected to my login screen, I am trying to log into the system to then give the post in this view, but I was not successful. Someone could help me?

tests py.

class OrderRegisterView(TestCase):
    def test_order_register_view(self):
        c = Client()
        User.objects.create_user('teste', 'teste', 'teste')

        c.login(username='teste', password='teste', follow=True)
        response = c.post('/pedidos/cadastro/', {'username':'teste', 'password': 'teste','ship_date':'12/04/14', 'ship_time': 'morning', 'truck': 7, 'city': 4304606, 'value': 12, 'action': 'selected', 'cheapest': True}, follow=True)
        # self.assertRedirects(response, '/login/')

Don’t move, don’t give me any mistakes, just pass OK.

  • Are you trying to test the View or Form? If you are testing the form, the ideal is to instantiate it within the test with the values you want to use.

  • The order registration that I am testing, goes through the form and if it is valid, the view itself, creates the Order() object, wanted to test this process, so I just needed to generate the POST in this view, so that it creates the Order itself.

  • Yes, but what exactly do you want to test? The validation of form data or the creation of Order by view? If it’s just the validation, you should do what I guided in the previous comment; if it’s the creation of the Order by the view: try passing the data through the kwarg data=, p ex.. c.post('/pedidos/cadastro/', data={'username': 'teste'....}.

  • I’m trying to test the creation of Order by view, I’m doing exactly that, passing the parameters in the post, but when I give the post in the order/registration, I’m redirected to the login screen, and nothing happens, even passing the username and pass in the post, I tried several ways to go through the login screen and nothing happened, so I can’t give the post on this screen of /orders/registration/

  • This question seems to be decontextualized because it is about a problem unrelated to what was asked, already solved by the author of the question.

2 answers

2

I believe that any of these three options will solve the problem:

1) Change the test client:

Instead of instantiating the client with c = Client(), use the client that already comes with Testcase. Like this:

self.client.login(username='teste', password='teste', follow=True)
self.client.post('/pedidos/cadastro/', {'username':'teste', 'password': 'teste','ship_date':'12/04/14', 'ship_time': 'morning', 'truck': 7, 'city': 4304606, 'value': 12, 'action': 'selected', 'cheapest': True}, follow=True)

2) Remove "follow=True" from login:

I never used follow=True in the login call. Maybe that’s it.

self.client.login(username='teste', password='teste')

3) @login_required

If you’re not already using, authenticate the view with the developer @login_required.

1

It was actually a Dispatch check of my view.

It was nothing to do with code syntax.

But anyway, thank you very much.

Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.