text: from django.contrib.auth import get_user_model
  from django.urls import reverse
  from django.test import TestCase
  
  from rest_framework import status
  from rest_framework.test import APIClient
  
  from core.models import Ingredient
  
  from recipe.serializers import IngredientSerializer
  
  
  INGREDIENT_URL = reverse('recipe:ingredient-list')
  
  
  class PublicIngredientAPITests(TestCase):
      """Test the publicly available ingredient api"""
  
      def setUp(self):
          self.client = APIClient()
  
      def test_login_required(self):
          """Test that login is required for retrieving ingredients"""
          res = self.client.get(INGREDIENT_URL)
  
          self.assertEquals(res.status_code, status.HTTP_401_UNAUTHORIZED)
  
  
  class PrivateIngredientsAPITests(TestCase):
      """Test the privately available ingredient API"""
  
      def setUp(self):
          self.user = get_user_model().objects.create_user(
              'test@gmail.com',
              'password123'
          )
          self.client = APIClient()
          self.client.force_authenticate(self.user)
  
      def test_retrieve_ingredients(self):
          """Test retrieving ingredients"""
          Ingredient.objects.create(user=self.user, name='Cheese')
          Ingredient.objects.create(user=self.user, name='Lettuce')
  
          res = self.client.get(INGREDIENT_URL)
  
          ingredients = Ingredient.objects.all().order_by('-name')
          serializer = IngredientSerializer(ingredients, many=True)
  
          self.assertEqual(res.status_code, status.HTTP_200_OK)
          self.assertEquals(res.data, serializer.data)
  
      def test_ingredients_limited_to_user(self):
          user2 = get_user_model().objects.create_user(
              'test2@gmail.com',
              'password123'
          )
          Ingredient.objects.create(user=user2, name='Apple')
          ingredient = Ingredient.objects.create(user=self.user, name='Grape')
  
          res = self.client.get(INGREDIENT_URL)
  
          self.assertEquals(res.status_code, status.HTTP_200_OK)
          self.assertEquals(len(res.data), 1)
          self.assertEquals(res.data[0]['name'], ingredient.name)
  
      def test_create_ingredients_succesful(self):
          """Test that ingredient is created successfully"""
          payload = {
              'name': 'Cheese'
          }
  
          res = self.client.post(INGREDIENT_URL, payload)
  
          exists = Ingredient.objects.filter(
              user=self.user,
              name=payload['name']
          ).exists()
  
          self.assertEquals(res.status_code, status.HTTP_201_CREATED)
          self.assertTrue(exists)
  
      def test_create_ingredient_invalid(self):
          """test creating ingredient with invalid payload"""
          payload = {'name': ''}
  
          res = self.client.post(INGREDIENT_URL, payload)
  
          self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
  