def test_get_meals_returns_a_meals_list(self): ''' A request to get a list of meals should return a list of meal objects with the appropriate data in them. Test steps: - send a request to get a list of meals for a user - iterate over the response data and check that each object has the appropriate properties ''' view = MealList.as_view() factory = APIRequestFactory() request = factory.get('meals', format='json') user = User.objects.get(username='******') force_authenticate(request, user=user) response = view(request) data = response.data for meal in data: with self.subTest(meal=meal): self.assertIn("pk", meal) self.assertIn("name", meal) self.assertIn("owner", meal) self.assertIn("collaborators", meal) self.assertIn("taste", meal) self.assertIn("difficulty", meal) self.assertIn("last_used", meal) self.assertIn("used_count", meal) self.assertIn("notes", meal) self.assertIn("ingredients", meal)
def test_add_pending_collaborator_on_meal_creation(self): ''' Create a new meal with collaborators. The collaborators should result in new pending collaborations being created. Test steps: - send a post request to create a meal with collaborators - check that new pending collaboration objects have been created for all specified collaborators ''' view = MealList.as_view() factory = APIRequestFactory() request = factory.post('meals', self.new_meal_data, format='json') user = User.objects.get(username='******') force_authenticate(request, user=user) view(request) collaborator1 = User.objects.get(pk=2) collaborator2 = User.objects.get(pk=3) meal = Meal.objects.get(name="turkey goop") self.assertIsNotNone( PendingCollaboration.objects.get(collaborator=collaborator1, meal=meal)) self.assertIsNotNone( PendingCollaboration.objects.get(collaborator=collaborator2, meal=meal))
def test_user_pending_collaborations(self): ''' Given a user, it should be possible to retrieve all the meals the user has shared with other people that are pending acceptance by accessing the pending_collaborations property. Test steps: - send a request to create a new meal with collaborators - check that the meal owner has new pending collaboration objects for those collaborator/meal combos on its pending_collaborations prop ''' view = MealList.as_view() factory = APIRequestFactory() request = factory.post('meals', self.new_meal_data, format='json') user = User.objects.get(username='******') force_authenticate(request, user=user) view(request) collaborator1 = User.objects.get(pk=2) collaborator2 = User.objects.get(pk=3) meal = Meal.objects.get(name="turkey goop") self.assertIsNotNone( user.pending_collaborations.get(collaborator=collaborator1, meal=meal)) self.assertIsNotNone( user.pending_collaborations.get(collaborator=collaborator2, meal=meal))
def test_get_meals_returns_401_status_if_not_logged_in(self): view = MealList.as_view() factory = APIRequestFactory() request = factory.get('meals', format='json') response = view(request) self.assertEqual(response.status_code, 401) self.assertEqual(response.status_text, 'Unauthorized')
def test_get_meals_returns_200_status(self): view = MealList.as_view() factory = APIRequestFactory() request = factory.get('meals', format='json') user = User.objects.get(username='******') force_authenticate(request, user=user) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(response.status_text, 'OK')
def test_post_meals_returns_201_status(self): view = MealList.as_view() factory = APIRequestFactory() request = factory.post('meals', self.new_meal_data, format='json') user = User.objects.get(username='******') force_authenticate(request, user=user) response = view(request) self.assertEqual(response.status_code, 201) self.assertEqual(response.status_text, 'Created')
def test_get_meals_returns_meals_for_specified_user(self): view = MealList.as_view() factory = APIRequestFactory() request = factory.get('meals', format='json') user = User.objects.get(username='******') force_authenticate(request, user=user) response = view(request) data = response.data for meal in data: with self.subTest(meal=meal): is_owner_or_collaborator = (meal.get("owner") == "test1") or user.shared_meals.filter(id=meal.get("pk")).exists() self.assertTrue(is_owner_or_collaborator) user = User.objects.get(username='******') force_authenticate(request, user=user) response = view(request) data = response.data for meal in data: with self.subTest(meal=meal): is_owner_or_collaborator = (meal.get("owner") == "admin") or user.shared_meals.filter(id=meal.get("pk")).exists() self.assertTrue(is_owner_or_collaborator)
from django.urls import path from meals.views import MealList, MealDetail app_name = "meals" urlpatterns = [ path('', MealList.as_view()), path('<int:pk>/', MealDetail.as_view()), ]