Add force_authenticate in APITestCase example

This commit is contained in:
Jezeniel Zapanta 2017-10-30 06:41:38 +08:00 committed by GitHub
parent 1f693c331e
commit 9ce41698f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -317,6 +317,11 @@ You can use any of REST framework's test case classes as you would for the regul
""" """
url = reverse('account-list') url = reverse('account-list')
data = {'name': 'DabApps'} data = {'name': 'DabApps'}
# Make an authenticated request
user = User.objects.get(username='arlene')
self.client.force_authenticate(user=user)
response = self.client.post(url, data, format='json') response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Account.objects.count(), 1) self.assertEqual(Account.objects.count(), 1)