def recipe_update(request): if request.method == 'PUT': recipe = Recipe.objects.get(pk=request.DATA.get('id')) data = { 'id' : request.DATA.get('id'), 'name': request.DATA.get('name'), 'prep_time': request.DATA.get('name'), 'code' : recipe.code, 'cook_time': request.DATA.get('cook_time'), 'serving_size': request.DATA.get('serving_size'), 'instructions': request.DATA.get('instructions')} serializer = RecipeSerializer(recipe, data=data, partial=True) if serializer.is_valid(): serializer.save() for item in request.DATA.get('ingredients'): cursor = connection.cursor() print item.has_key('ri_id') if item.has_key('ri_id'): cursor.execute("UPDATE recipe_ingredients SET unit = %s, ingredient_id = %s, recipe_id = %s WHERE id = %s", [item['unit'], item['id'], request.DATA.get('id'), item['ri_id']]) else : cursor.execute("INSERT INTO recipe_ingredients (unit, ingredient_id, recipe_id) VALUES(%s, %s, %s)", [item['unit'], item['id'], request.DATA.get('id')]) return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def recipe_create(request): if request.method == 'POST': data = {'name': request.DATA.get('name'), 'prep_time': request.DATA.get('name'), 'cook_time': request.DATA.get('cook_time'), 'serving_size': request.DATA.get('serving_size'), 'code': get_random_string(length=8), 'instructions': request.DATA.get('instructions')} serializer = RecipeSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_recipes_limited_to_user(self): """Test retrieving recipes for user""" user2 = get_user_model().objects.create_user( '*****@*****.**', 'testpass', ) sample_recipe(user=user2) sample_recipe(user=self.user) res = self.client.get(RECIPE_URL) recipes = Recipe.objects.filter(user=self.user) serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): """test recipe returned are for authenticated user only""" user2 = get_user_model().objects.create_user('*****@*****.**', 'test123$') sample_recipe(user=user2) recipe = sample_recipe(user=self.user, title='Pumpkin Soup') 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(len(res.data), 1) self.assertEqual(res.data[0]['title'], recipe.title) self.assertEqual(res.data, serializer.data)
def test_for_list_available(self): """Test that recipes are available only for authenticated users""" user2 = get_user_model().objects.create_user( '*****@*****.**', 'params1') sample_recipe(user=user2) sample_recipe(user=self.user) url = reverse('recipe:recipe-list') res = self.client.get(url) recipes = recipe.objects.filter(user=self.user) serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): """Test that recipes retrieved are for the authenticated user""" user2 = get_user_model().objects.create_user("*****@*****.**", "other@123") sample_recipe(user=user2) sample_recipe(user=self.user) res = self.client.get(RECIPES_URL) recipes = Recipe.objects.filter(user=self.user).order_by("-id") serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_create_recipe_successful(self): payload = { "name": "Mysterious Chicken nuggets", "description": "N", "ingredients": [ {"name": "Natural additives"}, {"name": "Maybe soylent green"}, ], } res = self.client.post(RECIPES_URL, payload, format="json") new_recipe = Recipe.objects.all().filter(name=payload["name"])[0] new_recipe_serialized = RecipeSerializer(new_recipe) self.assertEqual(len(new_recipe_serialized.data["ingredients"]), 2) self.assertEqual(res.status_code, status.HTTP_201_CREATED)
def test_recipe_limited_to_user(self): user2 = get_user_model().objects.create_user( '*****@*****.**', 'ammu', 'ammupass' ) object = sample_recipe(user = self.user) sample_recipe(user = user2) res = self.client.get(RECIPE_URL) query_set = Recipe.objects.all().order_by('-title').filter(user=self.user) serializer = RecipeSerializer(query_set, many = True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data) self.assertEqual(len(res.data),1) self.assertEqual(res.data[0]['title'], object.title)
def test_recipes_limited_to_user(self): """Test that only Recipes meant for the authenticated user are returned""" self.other_user = get_user_model().objects.create_user( email='*****@*****.**', password='******') sample_recipe(user=self.user) sample_recipe(user=self.other_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( len(res.data), 1) # should only return a single recipe of authorized user self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_authenticated_user(self): """Test retrieving the recipes for a user""" user2 = get_user_model().objects.create_user('*****@*****.**', 'passntgai') sample_recipe(user=user2) sample_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(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipe_limited_to_user(self): """Tests retrieving recipes of the auth user""" user2 = get_user_model().objects.create( email='*****@*****.**', password='******', name='Test2') create_sample_recipe(user2) create_sample_recipe(self.user) res = self.client.get(RECIPES_URL) recipes = Recipe.objects.all().filter(user=self.user) serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, 200) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): """Assert only user's recipes are returned""" user2 = get_user_model().objects.create_user( '*****@*****.**', 'OtherPasswd321!' ) sample_recipe(user=user2) sample_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(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_retrieve_recipes(self): """Test retrieving a list of recipes""" # create dummy recipes sample_recipe(self.user) # create with defaults sample_recipe(self.user) sample_recipe(self.user) res = self.client.get(RECIPES_URL) # retrieve the list of recipes recipes = Recipe.objects.all().order_by('-id') # pass in recipes into serializer serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) # check data matches serializer that we created self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): """test retrieving data of specific user """ user2 = get_user_model().objects.create_user('*****@*****.**','123456789') payload= {'title':'carbonara','price':20.00} recipe1 = sample_recipe(user = self.user) recipe2 = sample_recipe(user = user2,**payload) res = self.client.get(RECIPE_URL) recipes = Recipe.objects.filter(user=self.user) serializer = RecipeSerializer(recipes,many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data),1) self.assertEqual(res.data,serializer.data)
def test_retrieve_recipes( self, authenticated_client, recipes_url, recipe_with_tag_ingredient1, recipe_with_tag_ingredient2, ): res = authenticated_client.get(recipes_url) recipes = Recipe.objects.all().order_by("-id") serializer = RecipeSerializer(recipes, many=True) assert res.status_code == status.HTTP_200_OK assert res.data == serializer.data
def test_recipes_limited_to_user(self): '''Test retrieves recipes for user''' user = get_user_model().objects.create_user('*****@*****.**', '123123123123') sample_recipe(user=user) sample_recipe(user=user) sample_recipe(user=self.user) res = self.client.get(RECIPES_URL) recipes = Recipe.objects.filter(user=self.user).order_by('-id') serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): '''Test retrieving recipes for user''' user2 = get_user_model().objects.create_user('*****@*****.**', 'testpass') self.client.force_authenticate(self.user) sample_recipe(user=user2) sample_recipe(user=self.user) resp = self.client.get(RECIPES_URL) recipes = Recipe.objects.filter(user=self.user) serializer = RecipeSerializer(recipes, many=True) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(serializer.data), 1) self.assertEqual(resp.data, serializer.data)
def test_retrieve_recipes(self): # Test retrieving a list of recipes sample_recipe(user=self.user) sample_recipe(user=self.user) # We don't need to assign these to variables because we don't need # to access them in our test. We're going to access them by retrieving # all of the recipes from our database res = self.client.get(RECIPES_URL) recipes = Recipe.objects.all().order_by('-id') serializer = RecipeSerializer(recipes, many=True) # many=True, because we want to return the data as a list self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_receipes_limited_to_user(self): """Test get only own recipes""" user2 = get_user_model().objects.create_user('*****@*****.**', 'password') sample_recipe(user=user2) sample_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(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): ''' Test retrieving recipes for user ''' user2 = get_user_model().objects.create_user('*****@*****.**', 'password123') sample_recipe(user=user2) sample_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(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): """Test that recipes is limited only to user""" user2 = get_user_model().objects.create( email='*****@*****.**', password='******' ) sample_recipe(user=user2) sample_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(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_list_recipe(self): """Test listing recipe for authenticated user""" models.Recipe.objects.create(user=self.user, title='recipe-1', time_minutes=5, price=2.0) res = self.client.get(RECIPE_URL) recipes = models.Recipe.objects.all().order_by('-title') serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): user2 = get_user_model().objects.create_user( '*****@*****.**', 'testpass' ) sample_recipe(user=user2) sample_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(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipe_limited_to_user(self): """Test that recipes for authenticated user are returned""" user2 = get_user_model().objects.create_user( '*****@*****.**', 'testpass' ) sample_recipe(user=user2) sample_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(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): """test that recipes are specific to user""" other_usr = get_user_model().objects.create_user( '*****@*****.**', 'securityconcious') sample_recipe(user=other_usr) sample_recipe(user=self.user) res = self.client.get(RECIPE_URL) recipes = Recipe.objects.filter(user=self.user) serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): """Test retrieving a list of recipes""" user2 = get_user_model().objects.create_user('*****@*****.**', 'experiment') sample_recipe(user=user2) sample_recipe(user=self.user) res = self.client.get(RECIPES_URL) recipes = Recipe.objects.filter(user=self.user) # many true means, we retreive list so data type remains consistent serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data) self.assertEqual(len(res.data), 1)
def test_recipe_limited_to_user(self): """Test retrieving recipes for user""" user2 = get_user_model().objects.create_user('*****@*****.**', 'test123') sampleRecipe(user=user2) sampleRecipe(user=self.user) res = self.client.get(RECIPES_URL) recipes = Recipe.objects.all().filter(user=self.user) # set many=True even tho only 1 object returned - for API consistancy serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_retrieve_recipes(self): """Test retrieving a list of recipes""" # create a bunch of sample recipes sample_recipe(user=self.user) sample_recipe(user=self.user, title='Pizza') # make the API call response = self.client.get(RECIPE_URL) # retrieve manually from DB recipes = Recipe.objects.all().order_by('-id') serializer = RecipeSerializer(recipes, many=True) # run the assertions self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, serializer.data)
def test_recipe_limited_to_user(self): """Test that recipes list is shown only to the owner""" user2 = get_user_model().objects.create_user( "*****@*****.**", "passsssss" ) sample_recipe(user2) sample_recipe(self.user, title="BestRecipe") res = self.client.get(RECIPE_URL) recipes = Recipe.objects.filter(user=self.user) serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_limited_to_user(self): other_user = get_user_model().objects.create_user( '*****@*****.**', 'otherpersonpassword' ) sample_recipe(user=other_user) sample_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(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipes_restricted_to_user(self): """Test that get recipe list works for authenticated user""" user2 = get_user_model().objects.create_user(email='*****@*****.**', password='******') sample_recipe(user2) sample_recipe(self.user) res = self.client.get(RECIPE_URL) recipes = Recipe.objects.filter(user=self.user) serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_recipe_limited_to_user(self): """Test retiveing recipes for user""" user2 = get_user_model().objects.create_user( '*****@*****.**', 'passs12345' ) sample_recipe(user2) sample_recipe(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(len(res.data), 1) self.assertEqual(res.data, serializer.data)