Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django test - how to get response data for future use

I'm running a login test like so:

def test_login_user(self):
    client = APIClient()
    url = reverse('rest_login')
    data = {
        'username': 'test',
        'password': 'Welcome2'
    }
    response = self.client.post(url, data)
    self.assertEqual(response.status_code, status.HTTP_200_OK)
    client.logout()

If I login to the app normally I see a json return like this:

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyLCJ1c2VybmFtZSI6ImV2YW4iLCJleHAiOjE1MTQ2NzYzNTYsImVtYWlsIjoiZXZhbkAyOGJlYXR0eS5jb20iLCJvcmlnX2lhdCI6MTUxNDY3Mjc1Nn0.8CfhfgtMLkNjEaWBfNXbUWXQMZG4_LIru_y4pdLlmeI",
    "user": {
        "pk": 2,
        "username": "test",
        "email": "[email protected]",
        "first_name": "",
        "last_name": ""
    }
}

I want to be able to grab that token value for future use however the response does not seem to have a data value to grab.

like image 745
whoisearth Avatar asked Dec 31 '17 22:12

whoisearth


People also ask

What is RequestFactory in Django?

class RequestFactory. The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.

How do I skip a Django test?

Just trick it to always skip with the argument True : @skipIf(True, "I don't want to run this test yet") def test_something(): ... If you are looking to simply not run certain test files, the best way is probably to use fab or other tool and run particular tests.


1 Answers

What I'm looking for is response.content per the official documentation

https://docs.djangoproject.com/en/2.0/topics/testing/tools/#testing-responses

like image 139
whoisearth Avatar answered Nov 07 '22 04:11

whoisearth