def test_search_user_recipes(self): """ Test for searching all user recipes """ response = self.register_user("Patrick", "Walukagga", "*****@*****.**", "telnetcmd123") # registered user login rep_login = self.login_user("*****@*****.**", "telnetcmd123") # valid token headers = dict(Authorization='Bearer ' + json.loads(rep_login.data.decode())['auth_token']) category = RecipeCategory(name="Breakfast", description="How to make breakfast", user_id=1) category.save() response = self.create_category("LunchBuffe", "How to make lunch buffe", headers) recipe = Recipe(name="Rolex for Lunch", cat_id=2, user_id=1, ingredients="oil, Onions, Tomatoes", description="How to make lunch rolex") recipe.save() recipe = Recipe(name="Rolex for Breakfast", cat_id=1, user_id=1, ingredients="oil, Onions, Tomatoes", description="How to make breakfast rolex") recipe.save() response = self.create_recipe_in_category(2, "Chicken Lunch Buffe", "oil, Onions,Tomatoes", "Fresh chicken", "Mix and boil", headers) response = self.client.get('/search_recipes?q=Rolex', headers=headers) self.assertEqual(response.status_code, 200) self.assertIn('Rolex for Breakfast', str(response.data)) self.assertIn('Rolex for Lunch', str(response.data)) self.assertNotIn('Mix and boil', str(response.data)) # get recipes in category with limit response = self.client.get('/search_recipes?limit=1&page=2', headers=headers) self.assertEqual(response.status_code, 200) self.assertIn('Rolex for Breakfast', str(response.data)) self.assertNotIn('Mix and boil', str(response.data)) # get recipes in category with limit=a&page=b response = self.client.get('/search_recipes?limit=a&page=b', headers=headers) self.assertEqual(response.status_code, 400) self.assertIn('limit and page query parameters should be integers', str(response.data)) self.assertNotIn('Mix and boil', str(response.data))
def test_delete_recipe_in_category_catid_recipeid_not_number(self): """ Test for deleting recipe in category """ response = self.register_user("Patrick", "Walukagga", "*****@*****.**", "telnetcmd123") # registered user login rep_login = self.login_user("*****@*****.**", "telnetcmd123") # valid token headers = dict(Authorization='Bearer ' + json.loads(rep_login.data.decode())['auth_token']) category = RecipeCategory(name="Breakfast", description="How to make breakfast", user_id=1) category.save() response = self.create_category("LunchBuffe", "How to make lunch buffe", headers) recipe = Recipe(name="Rolex for Lunch", cat_id=2, user_id=1, ingredients="oil, Onions, Tomatoes", description="How to make breakfast rolex") recipe.save() response = self.create_recipe_in_category(2, "Chicken Lunch Buffe", "oil, Onions,Tomatoes", "Mix and boil", headers) response = self.client.delete('/recipe_category/a/recipes/2', headers=headers) self.assertEqual(response.status_code, 400) self.assertIn('Category ID must be an integer', str(response.data)) # recipe id not number response = self.client.delete('/recipe_category/2/recipes/a', headers=headers) self.assertEqual(response.status_code, 400) self.assertIn('Recipe ID must be an integer', str(response.data))
def test_get_single_recipe_in_category(self): """ Test for getting single recipe in category """ response = self.register_user( "Patrick", "Walukagga", "*****@*****.**", "telnetcmd123" ) # registered user login rep_login = self.login_user("*****@*****.**", "telnetcmd123") # valid token headers=dict( Authorization='Bearer ' + json.loads( rep_login.data.decode() )['auth_token'] ) category = RecipeCategory( name="Breakfast", description="How to make breakfast", user_id=1 ) category.save() response = self.create_category("LunchBuffe", "How to make lunch buffe", headers) recipe = Recipe( name="Rolex for Lunch", cat_id=2, user_id=1, ingredients="oil, Onions, Tomatoes", description="How to make breakfast rolex" ) recipe.save() response = self.create_recipe_in_category(2, "Chicken Lunch Buffe", "oil, Onions,Tomatoes", "Mix and boil", headers ) response = self.client.get('/recipe_category/2/recipes/1', headers=headers) self.assertEqual(response.status_code, 200) self.assertIn('Rolex for Lunch', str(response.data)) self.assertNotIn('Mix and boil', str(response.data)) # get recipe not yet in database response = self.client.get('/recipe_category/2/recipes/4', headers=headers) self.assertEqual(response.status_code, 404) self.assertIn('Recipe not found', str(response.data)) # get recipe in category not yet in database response = self.client.get('/recipe_category/3/recipes/1', headers=headers) self.assertEqual(response.status_code, 404) self.assertIn('Category not found in database', str(response.data))
def test_update_recipe_in_category_with_one_field(self): """ Test for editing recipe in category with one field """ response = self.register_user("Patrick", "Walukagga", "*****@*****.**", "telnetcmd123") # registered user login rep_login = self.login_user("*****@*****.**", "telnetcmd123") # valid token headers = dict(Authorization='Bearer ' + json.loads(rep_login.data.decode())['auth_token']) category = RecipeCategory(name="Breakfast", description="How to make breakfast", user_id=1) category.save() response = self.create_category("LunchBuffe", "How to make lunch buffe", headers) recipe = Recipe(name="Rolex for Lunch", cat_id=2, user_id=1, ingredients="oil, Onions, Tomatoes", description="How to make breakfast rolex") recipe.save() response = self.create_recipe_in_category(2, "Chicken Lunch Buffe", "oil, Onions,Tomatoes", "Fresh chicken", "Mix and boil", headers) recipe_data = json.dumps({"name": "Chicken Lunch Buffes"}) response = self.client.put('/recipe_category/2/recipes/1', headers=headers, data=recipe_data) self.assertEqual(response.status_code, 200) self.assertIn('Recipe has been updated', str(response.data)) response = self.client.get('/recipe_category/2/recipes/1', headers=headers) self.assertEqual(response.status_code, 200) self.assertIn('Chicken Lunch Buffes', str(response.data)) recipe_data = json.dumps({"ingredients": "oil, Onions"}) response = self.client.put('/recipe_category/2/recipes/1', headers=headers, data=recipe_data) self.assertEqual(response.status_code, 200) self.assertIn('Recipe has been updated', str(response.data)) recipe_data = json.dumps({"description": "Mix and boils"}) response = self.client.put('/recipe_category/2/recipes/1', headers=headers, data=recipe_data) self.assertEqual(response.status_code, 200) self.assertIn('Recipe has been updated', str(response.data))
def test_recipe_crud_when_not_logged_in(self): """ Test for recipe crud when not logged in """ response = self.register_user( "Patrick", "Walukagga", "*****@*****.**", "telnetcmd123" ) headers=dict(Authorization='Bearer ') category = RecipeCategory( name="Breakfast", description="How to make breakfast", user_id=1 ) category.save() response = self.create_category("LunchBuffe", "How to make lunch buffe", headers) self.assertEqual(response.status_code, 401) self.assertIn('Token is missing', str(response.data)) recipe = Recipe( name="Rolex for breakfast", cat_id=1, user_id=1, ingredients="oil, Onions, Tomatoes", description="How to make breakfast rolex" ) recipe.save() response = self.create_recipe_in_category(2, "Chicken Lunch Buffe", "oil, Onions,Tomatoes", "Mix and boil", headers ) response = self.client.delete('/recipe_category/2/recipes/2', headers=headers) self.assertEqual(response.status_code, 401) self.assertIn('Token is missing', str(response.data)) # delete recipe not yet in database response = self.client.delete('/recipe_category/2/recipes/4', headers=headers) self.assertEqual(response.status_code, 401) self.assertIn('Token is missing', str(response.data)) # delete recipe in category not yet in database response = self.client.delete('/recipe_category/3/recipes/1', headers=headers) self.assertEqual(response.status_code, 401) self.assertIn('Token is missing', str(response.data))
def post(current_user, self, category_id): """ Create a recipe --- tags: - recipes parameters: - in: path name: category_id required: true description: Category Id type: integer - in: body name: recipe required: true description: The title of the recipe type: string schema: id: recipe properties: title: type: string default: Meat soup body: type: string default: This is the process of making meat soup security: - TokenHeader: [] responses: 200: description: Create a recipe schema: id: recipe properties: title: type: string default: Meat soup body: type: string default: This is the process of making meat soup """ request_dict = request.get_json() if not request_dict: abort(status.HTTP_400_BAD_REQUEST, "All fields are required") errors = recipe_schema.validate(request_dict) if errors: abort(status.HTTP_400_BAD_REQUEST, errors) recipe_title = request_dict['title'].title() recipe_body = request_dict['body'].strip() if not Recipe.is_unique( id=0, title=recipe_title, user_id=current_user.id): abort(status.HTTP_409_CONFLICT, 'A recipe with the same title already exists') error, validated_title = Recipe.validate_recipe(ctx=recipe_title) if not validated_title: abort(400, error) error_body, validated_body = Recipe.validate_recipe(ctx=recipe_body) if not validated_body: abort(400, error_body) category = Category.query.filter_by(id=category_id, user_id=current_user.id).first() if category: recipe = Recipe(title=recipe_title, body=recipe_body, category_id=category.id, user=current_user) recipe.add(recipe) response = "Recipe uccessfully added!" return make_response(jsonify(response), status.HTTP_201_CREATED) else: abort(400, "A category with Id {0} does not exist".format(category_id))
def post(self, current_user, cat_id): auth_header = request.headers['Authorization'] if auth_header: auth_token = auth_header.split(" ")[1] else: auth_token = "" if auth_token: resp = current_user.decode_auth_token(auth_token) if not isinstance(resp, str): if not cat_id.isdigit(): responseObject = { 'error': 'Category ID must be an integer', 'status': "fail" } return make_response(jsonify(responseObject)), 400 category = RecipeCategory.query.filter_by(id=cat_id, user_id=\ current_user.id).\ first() if not category: responseObject = { 'message': 'Category not found in database' } return make_response(jsonify(responseObject)), 404 if not request.get_json(force=True): abort(400) data = request.get_json(force=True) if data: recipe_key_missing_in_body(data) if key_is_not_string(data): response_object = { 'error': 'Bad request, body field must be of type string' } return jsonify(response_object), 400 if data['name'] == "" or data["description"] == "" \ or data['ingredients'] == "" or data['directions'] == "": responseObject = { 'status': 'fail', 'message': 'field names not provided' } return make_response(jsonify(responseObject)), 200 if Recipe.query.filter_by(name=' '.join(data['name'].split()).capitalize(), cat_id=cat_id, user_id=current_user.id).\ first(): responseObject = { 'status': 'fail', 'message': 'Recipe already exists' } return make_response(jsonify(responseObject)), 202 recipe = Recipe(name=' '.join( data['name'].split()).capitalize(), cat_id=cat_id, user_id=current_user.id, ingredients=data['ingredients'], description=data['description'], directions=data['directions']) recipe.save() responseObject = { 'status': 'success', 'message': 'New recipe added to category' } return make_response(jsonify(responseObject)), 201 else: responseObject = { 'status': 'fail', 'message': 'New recipe not created!' } return make_response(jsonify(responseObject)), 200 else: responseObject = {'status': 'fail', 'message': resp} return make_response(jsonify(responseObject)), 401 else: responseObject = { 'status': 'fail', 'message': 'Provide a valid auth token.' } return make_response(jsonify(responseObject)), 403