def test_user_update_authenticated_as_superuser(self): view = UserRetrieveUpdateDestroyView.as_view() user1 = UserFactory.create_superuser(username='******', email="*****@*****.**", password="******") user2 = UserFactory.create(email="*****@*****.**") self.client.force_authenticate(user=user1) response = self.client.put(reverse('user-retrieve-update-destroy', kwargs={"user_id": user2.id}), { 'username': user2.username, "id": user2.id, "name": "test2", "birth_date": user2.birth_date, "email": user2.email, "password": user2.password, }, format='json', secure=True) self.assertEqual(response.status_code, 200) user_new = User.objects.get(id=user2.id) self.assertEqual(user_new.name, "test2")
def test_create_superuser_is_not_superuser(self): with self.assertRaises(ValueError): user = UserFactory.create_superuser(username='******', email='*****@*****.**', password='******', is_superuser=False) self.assertEqual(len(User.objects.all()), 0)
def test_user_update_invalid_data(self): view = UserRetrieveUpdateDestroyView.as_view() user = UserFactory.create_superuser(username='******', name='test', email='*****@*****.**', password='******') self.client.force_authenticate(user=user) response = self.client.put(reverse('user-retrieve-update-destroy', kwargs={"user_id": user.id}), { 'username': user.username, "id": user.id, "name": None, "birth_date": user.birth_date, "email": user.email, "password": user.password, }, format='json', secure=True) self.assertEqual(response.status_code, 400) user_new = User.objects.get(id=user.id) self.assertEqual(user_new.name, "test")
def test_user_list_view_authenticated_as_superuser(self): user1 = UserFactory.create(email='*****@*****.**') user2 = UserFactory.create_superuser(username='******', email='*****@*****.**', password='******') view = UserListCreateView.as_view() self.client.force_authenticate(user=user2) response = self.client.get(reverse('user-list-create'), secure=True) self.assertEqual(len(response.data['results']), 2)
def test_user_view_authenticated_as_superuser(self): view = UserRetrieveUpdateDestroyView.as_view() user1 = UserFactory.create_superuser(username='******', email='*****@*****.**', password='******') user2 = UserFactory.create(email='*****@*****.**') self.client.force_authenticate(user=user1) response = self.client.get(reverse('user-retrieve-update-destroy', kwargs={"user_id": user2.id}), secure=True) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['id'], user2.id)
def test_user_destroy_authenticated_as_superuser(self): view = UserRetrieveUpdateDestroyView.as_view() user1 = UserFactory.create_superuser(username='******', email="*****@*****.**", password="******") user2 = UserFactory.create(email="*****@*****.**") self.client.force_authenticate(user=user1) response = self.client.delete(reverse('user-retrieve-update-destroy', kwargs={"user_id": user2.id}), secure=True) self.assertEqual(response.status_code, 204) with self.assertRaises(User.DoesNotExist): user2_new = User.objects.get(id=user2.id)
def test_create_superuser_email_non_existant(self): with self.assertRaises(ValueError): user = UserFactory.create_superuser(username='******', email=None, password='******') self.assertEqual(len(User.objects.all()), 0)
def test_create_superuser_success(self): user = UserFactory.create_superuser(username='******', email='*****@*****.**', password='******') self.assertNotEqual(len(User.objects.all()), 0)