mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Ensure token validation is working correctly
This commit is contained in:
parent
576226ad30
commit
baf096b3e7
@ -11,6 +11,13 @@ from django.contrib.auth import get_user_model
|
|||||||
class APITests(APITestCase):
|
class APITests(APITestCase):
|
||||||
""" Tests for the InvenTree API """
|
""" Tests for the InvenTree API """
|
||||||
|
|
||||||
|
fixtures = [
|
||||||
|
'location',
|
||||||
|
'stock',
|
||||||
|
'part',
|
||||||
|
'category',
|
||||||
|
]
|
||||||
|
|
||||||
username = 'test_user'
|
username = 'test_user'
|
||||||
password = 'test_pass'
|
password = 'test_pass'
|
||||||
|
|
||||||
@ -29,7 +36,7 @@ class APITests(APITestCase):
|
|||||||
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
self.assertFalse('token' in response.data)
|
self.assertFalse('token' in response.data)
|
||||||
|
|
||||||
def test_get_token_pass(self):
|
def test_get_token_pass(self):
|
||||||
""" Ensure that a valid user can request an API token """
|
""" Ensure that a valid user can request an API token """
|
||||||
|
|
||||||
@ -43,3 +50,18 @@ class APITests(APITestCase):
|
|||||||
self.assertTrue('pk' in response.data)
|
self.assertTrue('pk' in response.data)
|
||||||
self.assertTrue(len(response.data['token']) > 0)
|
self.assertTrue(len(response.data['token']) > 0)
|
||||||
|
|
||||||
|
# Now, use the token to access other data
|
||||||
|
token = response.data['token']
|
||||||
|
|
||||||
|
part_url = reverse('api-part-list')
|
||||||
|
|
||||||
|
# Try to access without a token
|
||||||
|
response = self.client.get(part_url, format='json')
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
|
# Now, with the token
|
||||||
|
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
|
||||||
|
response = self.client.get(part_url, format='json')
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
Loading…
Reference in New Issue
Block a user