UnitTest Django Views: I can not login to the system by unittest [closed]

-1

I need to give post in my view of "creation of requests", but when I try to give post , I'm redirected to my login screen, I'm trying to login to the system and then give the post in that view , but I did not succeed. Could someone 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/')

I do not go from there, it does not give me any errors, it just happens OK.

    
asked by anonymous 26.02.2014 / 15:44

2 answers

2

I believe that any of these 3 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. So:

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) Get the "follow = True" login:

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

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

3) @login_required

If you are not already using it, authenticate the view with the decorator @login_required .

    
11.03.2014 / 18:21
1

Actually it was a dispatch check of my view.

It was nothing related to code syntax.

But anyway, thank you.

Hugs!

    
20.03.2014 / 20:54