2023-10-09 03:25:09 -03:00
|
|
|
"""
|
2023-10-09 15:34:58 -03:00
|
|
|
Test for recipe APIs.
|
2023-10-09 03:25:09 -03:00
|
|
|
"""
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2023-10-09 15:39:28 -03:00
|
|
|
from django.contrib.auth import get_user_model
|
2023-10-09 03:25:09 -03:00
|
|
|
from django.test import TestCase
|
|
|
|
from django. urls import reverse
|
|
|
|
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.test import APIClient
|
|
|
|
|
|
|
|
from core.models import Recipe
|
|
|
|
|
2023-10-09 15:34:58 -03:00
|
|
|
from recipe.serializers import (
|
|
|
|
RecipeSerializer,
|
|
|
|
RecipeDetailSerializer,
|
|
|
|
)
|
2023-10-09 03:25:09 -03:00
|
|
|
|
|
|
|
|
|
|
|
RECIPES_URL = reverse('recipe:recipe-list')
|
|
|
|
|
|
|
|
|
2023-10-09 15:34:58 -03:00
|
|
|
def detail_url(recipe_id):
|
|
|
|
"""Create and return a recipe detail URL."""
|
|
|
|
return reverse('recipe:recipe-detail', args=[recipe_id])
|
|
|
|
|
|
|
|
|
2023-10-09 03:25:09 -03:00
|
|
|
def create_recipe(user, **params):
|
|
|
|
"""Create and return a sample recipe."""
|
|
|
|
defaults = {
|
2023-10-09 15:34:58 -03:00
|
|
|
'title': 'Titulo receta de ejemplo',
|
2023-10-09 03:25:09 -03:00
|
|
|
'time_minutes': 31,
|
|
|
|
'price': Decimal('5.25'),
|
2023-10-09 15:34:58 -03:00
|
|
|
'description': 'Descripción de ejemplo',
|
2023-10-09 03:25:09 -03:00
|
|
|
'link': 'https://defzn.kickto.net/blog',
|
|
|
|
}
|
|
|
|
defaults.update(params)
|
|
|
|
|
|
|
|
recipe = Recipe.objects.create(user=user, **defaults)
|
|
|
|
return recipe
|
|
|
|
|
|
|
|
|
2023-10-09 15:34:58 -03:00
|
|
|
def create_user(**params):
|
|
|
|
"""Create and create a new user."""
|
|
|
|
return get_user_model().objects.create_user(**params)
|
|
|
|
|
|
|
|
|
2023-10-09 03:25:09 -03:00
|
|
|
class PublicRecipeApiTests(TestCase):
|
|
|
|
"""Test unauthenticated API requests."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
|
|
|
|
def test_auth_required(self):
|
|
|
|
"""Test auth is required to call API."""
|
|
|
|
res = self.client.get(RECIPES_URL)
|
|
|
|
|
|
|
|
self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED)
|
|
|
|
|
|
|
|
|
|
|
|
class PrivateRecipeApiTests(TestCase):
|
|
|
|
"""Test authenticated API requests."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client = APIClient()
|
2023-10-09 15:39:28 -03:00
|
|
|
self.user = create_user(
|
|
|
|
email='user@example.com',
|
|
|
|
password='testpass123'
|
|
|
|
)
|
2023-10-09 03:25:09 -03:00
|
|
|
self.client.force_authenticate(self.user)
|
|
|
|
|
|
|
|
def test_retrive_recipes(self):
|
|
|
|
"""Test retrieving a list of recipes."""
|
|
|
|
create_recipe(user=self.user)
|
|
|
|
create_recipe(user=self.user)
|
|
|
|
|
|
|
|
res = self.client.get(RECIPES_URL)
|
|
|
|
|
|
|
|
recipes = Recipe.objects.all().order_by('-id')
|
|
|
|
serializer = RecipeSerializer(recipes, many=True)
|
|
|
|
|
|
|
|
self.assertEqual(res.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(res.data, serializer.data)
|
|
|
|
|
|
|
|
def test_recipe_list_limited_to_user(self):
|
|
|
|
"""Test list of recipes is limited to authenticated user."""
|
2023-10-09 15:39:28 -03:00
|
|
|
other_user = create_user(
|
|
|
|
email='other@example.com',
|
|
|
|
password='password123'
|
|
|
|
)
|
2023-10-09 03:25:09 -03:00
|
|
|
create_recipe(user=other_user)
|
|
|
|
create_recipe(user=self.user)
|
|
|
|
|
|
|
|
res = self.client.get(RECIPES_URL)
|
|
|
|
|
|
|
|
recipes = Recipe.objects.filter(user=self.user)
|
|
|
|
serializer = RecipeSerializer(recipes, many=True)
|
|
|
|
self.assertEqual(res.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(res.data, serializer.data)
|
2023-10-09 15:34:58 -03:00
|
|
|
|
|
|
|
def test_get_recipe_detail(self):
|
|
|
|
"""Test get recipe detail."""
|
|
|
|
recipe = create_recipe(user=self.user)
|
|
|
|
|
|
|
|
url = detail_url(recipe.id)
|
|
|
|
res = self.client.get(url)
|
|
|
|
|
|
|
|
serializer = RecipeDetailSerializer(recipe)
|
|
|
|
self.assertEqual(res.data, serializer.data)
|
|
|
|
|
|
|
|
def test_create_recipe(self):
|
|
|
|
"""Test creating a recipe."""
|
|
|
|
payload = {
|
|
|
|
'title': 'Titulo receta de ejemplo',
|
|
|
|
'time_minutes': 16,
|
|
|
|
'price': Decimal('5.99'),
|
|
|
|
}
|
|
|
|
res = self.client.post(RECIPES_URL, payload)
|
|
|
|
|
|
|
|
self.assertEqual(res.status_code, status.HTTP_201_CREATED)
|
|
|
|
recipe = Recipe.objects.get(id=res.data['id'])
|
|
|
|
for k, v in payload.items():
|
|
|
|
self.assertEqual(getattr(recipe, k), v)
|
|
|
|
self.assertEqual(recipe.user, self.user)
|
|
|
|
|
|
|
|
def test_partial_update(self):
|
|
|
|
"""Test partial update of a recipe."""
|
|
|
|
original_link = 'https://devfzn.kickto.net/acerca'
|
|
|
|
recipe = create_recipe(
|
|
|
|
user=self.user,
|
|
|
|
title='Titulo de la Receta de ejemplo',
|
|
|
|
link=original_link,
|
|
|
|
)
|
|
|
|
|
|
|
|
payload = {'title': 'Nuevo titulo de la receta de ejemplo'}
|
|
|
|
url = detail_url(recipe.id)
|
|
|
|
res = self.client.patch(url, payload)
|
|
|
|
|
|
|
|
self.assertEqual(res.status_code, status.HTTP_200_OK)
|
|
|
|
recipe.refresh_from_db()
|
|
|
|
self.assertEqual(recipe.title, payload['title'])
|
|
|
|
self.assertEqual(recipe.link, original_link)
|
|
|
|
self.assertEqual(recipe.user, self.user)
|
|
|
|
|
|
|
|
def test_full_update(self):
|
|
|
|
"""Test full update of recipe."""
|
|
|
|
recipe = create_recipe(
|
|
|
|
user=self.user,
|
|
|
|
title='Titulo receta de ejemplo',
|
|
|
|
link='https://devfzn.kickto.net/blog',
|
|
|
|
description='Descripción receta de ejemplo',
|
|
|
|
)
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
'title': 'Titulo receta de ejemplo',
|
|
|
|
'link': 'https://defzn.kickto.net/blog',
|
|
|
|
'description': 'Descripción de ejemplo',
|
|
|
|
'time_minutes': 10,
|
|
|
|
'price': Decimal('3.65'),
|
|
|
|
}
|
|
|
|
url = detail_url(recipe.id)
|
|
|
|
res = self.client.put(url, payload)
|
|
|
|
|
|
|
|
self.assertEqual(res.status_code, status.HTTP_200_OK)
|
|
|
|
recipe.refresh_from_db()
|
|
|
|
for k, v in payload.items():
|
|
|
|
self.assertEqual(getattr(recipe, k), v)
|
|
|
|
self.assertEqual(recipe.user, self.user)
|
|
|
|
|
|
|
|
def test_update_user_returns_error(self):
|
|
|
|
"""Test changing the recipe user results in an error."""
|
2023-10-09 15:39:28 -03:00
|
|
|
new_user = create_user(
|
|
|
|
email='user2@example.com',
|
|
|
|
password='testpass123'
|
|
|
|
)
|
2023-10-09 15:34:58 -03:00
|
|
|
recipe = create_recipe(user=self.user)
|
|
|
|
|
|
|
|
payload = {'user': new_user.id}
|
|
|
|
url = detail_url(recipe.id)
|
|
|
|
self.client.patch(url, payload)
|
2023-10-09 15:39:28 -03:00
|
|
|
|
2023-10-09 15:34:58 -03:00
|
|
|
recipe.refresh_from_db()
|
|
|
|
self.assertEqual(recipe.user, self.user)
|
|
|
|
|
|
|
|
def test_delete_recipe(self):
|
|
|
|
"""Test deleting a recipe sucessful."""
|
|
|
|
recipe = create_recipe(user=self.user)
|
|
|
|
|
|
|
|
url = detail_url(recipe.id)
|
|
|
|
res = self.client.delete(url)
|
|
|
|
|
|
|
|
self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT)
|
|
|
|
self.assertFalse(Recipe.objects.filter(id=recipe.id).exists())
|
|
|
|
|
|
|
|
def test_recipe_other_users_recipe_error(self):
|
|
|
|
"""Test trying to delete another users recipe gives error."""
|
2023-10-09 15:39:28 -03:00
|
|
|
new_user = create_user(
|
|
|
|
email='user2@example.com',
|
|
|
|
password='testpass123'
|
|
|
|
)
|
2023-10-09 15:34:58 -03:00
|
|
|
recipe = create_recipe(user=new_user)
|
|
|
|
|
|
|
|
url = detail_url(recipe.id)
|
|
|
|
res = self.client.delete(url)
|
|
|
|
|
|
|
|
self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)
|
|
|
|
self.assertTrue(Recipe.objects.filter(id=recipe.id).exists())
|