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_user_retrieves_recipe_categories_with_limit(self): """ Test for user retrieves recipe categories with limit pagination """ with self.client: 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="Morningfast", description="How to make morningfast", user_id=1) category.save() response = self.create_category("Breakfast", "How to make breakfast", headers) response = self.create_category("Lunchfast", "How to make lunchfast", headers) response = self.client.get('/recipe_category?limit=2', headers=headers) self.assertEqual(response.status_code, 200) self.assertIn('How to make morningfast', str(response.data)) self.assertIn('How to make breakfast', str(response.data)) self.assertNotIn('How to make lunchfast', str(response.data))
def test_recipe_creation_with_name_has_numbers(self): """ Test for recipe creation with name has numbers """ 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) response = self.create_recipe_in_category(2, 1273839393, "oil, Onions,Tomatoes", "Mix and boil", headers ) self.assertEqual(response.status_code, 400) self.assertIn('Bad request, body field must be of type string', str(response.data))
def test_category_creation_which_exists(self): """ Test for category creation which already exists """ with self.client: 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("Breakfast", "How to make breakfast", headers) self.assertEqual(response.status_code, 200) self.assertIn('Category already exists', str(response.data)) self.assertIn('fail', str(response.data))
def test_recipe_creation_in_category(self): """ Test for recipe creation 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) response = self.create_recipe_in_category(2, "Chicken Lunch Buffe", "oil, Onions,Tomatoes", "Mix and boil", headers ) self.assertEqual(response.status_code, 201) self.assertIn('New recipe added to category', str(response.data)) # create recipe with same name response = self.create_recipe_in_category(2, "Chicken Lunch Buffe", "oil, Onions,Tomatoes", "Mix and boil", headers ) self.assertEqual(response.status_code, 200) self.assertIn('Recipe already exists', str(response.data)) # create recipe in category which doesnot exit response = self.create_recipe_in_category(3, "Chicken Lunch Buffe", "oil, Onions,Tomatoes", "Mix and boil", headers ) self.assertEqual(response.status_code, 404) self.assertIn('Category not found in database', str(response.data)) # create recipe with empty fields response = self.create_recipe_in_category(2, "", "", "", headers) self.assertEqual(response.status_code, 200) self.assertIn('field names not provided', str(response.data))
def post(self, current_user): 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 request.get_json(force=True): abort(400) data = request.get_json(force=True) if data: category_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"] == "": responseObject = { 'status': 'fail', 'message': 'field names not provided' } return make_response(jsonify(responseObject)), 200 if RecipeCategory.query.filter_by( user_id=current_user.id, name=data['name']).first(): responseObject = { 'status': 'fail', 'message': 'Category already exists' } return make_response(jsonify(responseObject)), 200 category = RecipeCategory(name=data['name'], description=data['description'], user_id=current_user.id) category.save() responseObject = { 'status': 'success', 'message': 'New recipe category created!' } return make_response(jsonify(responseObject)), 201 else: responseObject = { 'status': 'fail', 'message': 'New recipe category 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
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_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_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 test_get_paginated_categories(self): """ Test for pagination of categories """ 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']) # create categories category = RecipeCategory(name="Breakfast", description="How to make breakfast", user_id=1) category.save() category = RecipeCategory(name="Lunch", description="How to make Lunch", user_id=1) category.save() category = RecipeCategory(name="Dinner", description="How to make Dinner", user_id=1) category.save() category = RecipeCategory(name="Chapati Fried", description="How to make fried chapatis", user_id=1) category.save() category = RecipeCategory(name="Luwombo", description="How to make Luwombo", user_id=1) category.save() response = self.client.get('/recipe_category?limit=2&page=1', headers=headers) self.assertEqual(response.status_code, 200) self.assertIn('How to make breakfast', str(response.data)) self.assertIn('How to make Lunch', str(response.data)) self.assertNotIn('How to make Dinner', str(response.data)) response = self.client.get('/recipe_category?limit=2&page=2', headers=headers) self.assertEqual(response.status_code, 200) self.assertNotIn('How to make breakfast', str(response.data)) self.assertNotIn('How to make lunchfast', str(response.data)) self.assertIn('How to make Dinner', str(response.data)) response = self.client.get('/recipe_category?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))