def test_tag_str(self): """Test the string representation""" email = '*****@*****.**' password = '******' user = create_user(email, password) tag = models.Tag.objects.create(user=user, name='Vegan') self.assertEqual(str(tag), tag.name)
def setUp(self): self.client = APIClient() self.email = '*****@*****.**' self.password = '******' self.user = create_user(email=self.email, password=self.password) self.client.force_authenticate(self.user) self.recipe = sample_recipe(user=self.user)
def test_create_user_with_email_successful(self): """Test creating a new user with an email is successful""" email = '*****@*****.**' password = '******' user = create_user(email, password) self.assertEqual(user.email, email) self.assertTrue(user.check_password(password))
def test_ingredient_str(self): """Test the ingredient string representation""" email = '*****@*****.**' password = '******' user = create_user(email, password) ingredient = models.Ingredient.objects.create(name='Cucumber', user=user) self.assertEqual(str(ingredient), ingredient.name)
def test_recipe_str(self): """Test the recipe string representation""" email = '*****@*****.**' password = '******' user = create_user(email, password) recipe = models.Recipe.objects.create( title='Meat on skewer', user=user, time_minutes=5, price=5.00 ) self.assertEqual(str(recipe), recipe.title)
def test_tags_limited_to_user(self): """Test that tags returned are for authenticated user""" user2 = create_user('*****@*****.**', 'OtherPassword') Tag.objects.create(user=user2, name='Fruity') tag = Tag.objects.create(user=self.user, name='Home Food') res = self.client.get(TAGS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data[0]['name'], tag.name)
def test_ingredients_limited_to_user(self): """Test that ingredients returned for authenticated user""" user2 = create_user('*****@*****.**', 'OtherPassword') ingredient_user = Ingredient.objects.create(name='Cucumber', user=self.user) ingredient_user2 = Ingredient.objects.create(name='Kale', user=user2) res = self.client.get(INGREDIENTS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data[0]['name'], ingredient_user.name)
def test_create_recipe_with_other_user_ingredients(self): """Test creating recipe with other's user ingredients""" user2 = create_user('*****@*****.**', 'OtherPassword') ingredient1 = sample_ingredient(user=self.user, name='Tomato') ingredient2 = sample_ingredient(user=user2, name='Potato') payload = { 'title': 'Vegetable stew', 'ingredients': [ingredient1.id, ingredient2.id], 'time_minutes': 60, 'price': 100, } res = self.client.post(RECIPES_URL, payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn(str(ingredient2.id), res.content.decode())
def test_create_recipe_with_other_users_tags(self): """Test creating recipe with other's user tags""" user2 = create_user('*****@*****.**', 'OtherPassword') tag1 = sample_tag(user=self.user, name='Beef') tag2 = sample_tag(user=user2, name='Mutton') payload = { 'title': 'Meat stew', 'tags': [tag1.id, tag2.id], 'time_minutes': 60, 'price': 100, } res = self.client.post(RECIPES_URL, payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn(str(tag2.id), res.content.decode())
def test_recipes_are_limited_to_user(self): recipe_by_user = sample_recipe(self.user, title='Meat on skewer.') user2 = create_user('*****@*****.**', 'OtherPassword') recipe_by_user_2 = sample_recipe(user2) recipes = Recipe.objects.filter(user=self.user) res = self.client.get(RECIPES_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) serializer = RecipeSerializer(recipes, many=True) self.assertEqual(len(res.data), 1) self.assertEqual(res.data[0]['title'], recipe_by_user.title) self.assertEqual(res.data, serializer.data)
def test_new_user_invalid_email(self): """Test creating user with no email raises error""" with self.assertRaises(ValueError): password = '******' create_user(None, password)
def test_new_user_email_normalized(self): """Test the email for a new user is normalized""" email = '*****@*****.**' password = '******' user = create_user(email, password) self.assertEqual(user.email, email.lower())