def test_delete_recipe_not_owned_by_user(self):
     [id, token1] = self.register_login_and_post_recipe()
     token2 = register_and_login('username2', '*****@*****.**')
     # attemt to delete recipe with wrong credentials
     [code, data] = decode_response(self.delete_recipe(token2, id))
     self.assertEqual(code, 403)
     self.assertEqual(data['message'], 'action forbidden')
 def register_login_and_post_recipe(self,
                                    username='******',
                                    email='*****@*****.**',
                                    recipe=recipe_one_with_tags):
     token = register_and_login(username, email)
     response = self.post_recipe(token, recipe)
     recipe_id = json.loads(response.data.decode())['id']
     return recipe_id, token
 def test_get_single_recipe(self):
     # register and log in
     token = register_and_login()
     # post a recipe and then retrieve it
     response = self.post_recipe(token, recipe_one_no_tags)
     response = self.get_recipe(token, 1)
     self.assertEqual(response.status_code, 200)
     recipe = json.loads(response.data.decode())['data']
     for key in recipe_one_no_tags.keys():
         self.assertEqual(recipe[key], recipe_one_no_tags[key])
 def test_attempt_delete_non_existing_recipe(self):
     token = register_and_login()
     # make sure a given recipe does not exist
     [code, data] = decode_response(self.get_recipe(token, 9999))
     self.assertEqual(code, 404)
     self.assertEqual(data['message'], 'recipe does not exist')
     # attempt to delete this recipe
     [code, data] = decode_response(self.delete_recipe(token, 9999))
     self.assertEqual(code, 404)
     self.assertEqual(data['message'], 'recipe does not exist')
 def test_get_single_recipe_no_tags_after_created(self):
     # register and log in
     token = register_and_login()
     # post a recipe and then retrieve all recipes
     response = self.post_recipe(token, recipe_one_no_tags)
     self.assertEqual(response.status_code, 201)
     [code, data] = decode_response(self.get_recipes(token))
     self.assertEqual(code, 200)
     # # check if data returned matches what we saved
     recipe = data['data'][0]
     for key in recipe_one_no_tags.keys():
         self.assertEqual(recipe[key], recipe_one_no_tags[key])
 def test_get_multiple_recipes_no_tags(self):
     # register and log in
     token = register_and_login()
     # post two different recipes and then retrieve all recipes
     self.post_recipe(token, recipe_one_no_tags)
     self.post_recipe(token, recipe_two_no_tags)
     response = self.get_recipes(token)
     self.assertEqual(response.status_code, 200)
     # check if data returned matches what we saved
     data = json.loads(response.data.decode())
     recipe_one_data = data['data'][0]
     recipe_two_data = data['data'][1]
     for key in recipe_one_no_tags.keys():
         self.assertEqual(recipe_one_data[key], recipe_one_no_tags[key])
         self.assertEqual(recipe_two_data[key], recipe_two_no_tags[key])
 def test_tags_api_add_already_existing_tag(self):
     # register and log in
     token = register_and_login()
     # post a new recipe
     response = self.post_recipe(token, recipe_one_with_tags)
     self.assertEqual(response.status_code, 201)
     recipe_id = json.loads(response.data.decode())['id']
     # attempt to add a new tag
     response = self.post_tag(token, recipe_id, 'new_tag')
     self.assertEqual(response.status_code, 201)
     # check that tag was added
     response = self.get_tags(token, recipe_id)
     tags = json.loads(response.data.decode())['data']
     self.assertIn('new_tag', tags)
     self.assertEqual(3, len(tags))
     # try to re-add tag
     response = self.post_tag(token, recipe_id, 'new_tag')
     self.assertEqual(response.status_code, 409)
     # check we still only have three tags
     response = self.get_tags(token, recipe_id)
     tags = json.loads(response.data.decode())['data']
     self.assertIn('new_tag', tags)
     self.assertEqual(3, len(tags))
 def test_post_recipes(self):
     token = register_and_login()
     response = self.post_recipe(token, recipe_one_no_tags)
     data = json.loads(response.data.decode())
     self.assertEqual(response.status_code, 201)
     self.assertEqual(data['status'], 'success')