added user-specific endpoints

This commit is contained in:
Sam
2021-02-04 10:14:28 +00:00
parent ea8cd97baf
commit 77acc668b8
7 changed files with 145 additions and 9 deletions

View File

@@ -460,3 +460,38 @@ class LoadCoopManagerTestCase(APITestCase):
self.assertEqual(company_count, self.company_model.objects.count())
self.assertEqual(user_count, self.user_model.objects.count())
class MyUserViewTest(APITestCase):
"""my_user tests
"""
def setUp(self):
"""Tests setup
"""
self.endpoint = '/api/v1/my_user/'
self.factory = factories.CustomUserFactory
self.model = models.CustomUser
# create user
self.email = f"user@mail.com"
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
self.user = self.factory(email=self.email, is_active=True)
self.user.set_password(self.password)
self.user.save()
def test_auth_user_gets_data(self):
# Authenticate
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# Query endpoint
response = self.client.get(self.endpoint)
# Assert forbidden code
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_anon_user_cannot_access(self):
# send in request
response = self.client.get(self.endpoint)
# check response
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)