def test_delete_with_all_recipes(self): with self.app_context(): with self.app() as client: self.assertEqual( RecipeModel.count_all()[0], 3, "The number of recipes saved to db is not correct.") self.assertEqual( len(FavouriteRecipesModel.show_mine(1)), 3, "The number of favourite recipes saved to db is not correct." ) response = client.post( f"{URL}/login", data=json.dumps({ "username": "******", "password": "******" }), headers={"Content-Type": "application/json"}) access_token = json.loads(response.data)['access_token'] response = client.delete(f"{URL}/delete_account/TestUsername", headers={ "Content-Type": "application/json", "Authorization": f"Bearer {access_token}" }) expected = { "message": f"User's account (username: TestUsername) deleted successfully!", "access_token": None, "refresh_token": None } self.assertEqual( response.status_code, 200, "Status code after deleting a user is not correct.") self.assertEqual( json.loads(response.data), expected, "Message returned after deleting a user is not correct.") self.assertEqual( RecipeModel.count_all()[0], 0, "The number of recipes saved to db after deleting a user is not correct." ) self.assertEqual( len(FavouriteRecipesModel.show_mine(1)), 0, "The number of favourite recipes saved to db after deleting a user is not correct." )
def test_count_all(self): with self.app_context(): recipe1 = RecipeModel("Title1", "Url1", "Ingredients1, test1") recipe2 = RecipeModel("Title2", "Url2", "Ingredients1, test2") recipe3 = RecipeModel("Title3", "Url3", "Ingredients1, test3") recipe1.save_to_db() recipe2.save_to_db() recipe3.save_to_db() no_of_recipes = RecipeModel.count_all()[0] self.assertEqual( no_of_recipes, 3, "RecipeModel.count_all() failed to count a proper number of recipes" )
def get(self): claims = get_jwt_claims() if not claims["admin"]: return {"message": "Admin privileges required"}, 403 number_unique_recipes_in_db = RecipeModel.count_all()[0] recipes_stats = FavouriteRecipesModel.show_stats() recipes_stats_display = [{ "recipe": r.json(), "number_of_users_who_have_it_saved_to_favourites": n, "ids_of_users_who_saved_it_to_favourites": [x.user_id for x in r.users] } for r, n in recipes_stats] return { "a number of recipes saved into the table 'recipes'": number_unique_recipes_in_db, "details of the recipes and a number of people " "who saved each of them as their favourites": recipes_stats_display }, 200
def test_delete_when_admin(self): with self.app_context(): with self.app() as client: UserModel("TestAdmin", "TestPwd1!", admin=1).save_to_db() UserModel("TestUsernameToBeDeleted", "TestPwd1!").save_to_db() self.current_time = datetime.utcnow() self.recipe_id1, _ = RecipeModel( "Title1", "Url1", "Ingredients1, test1").save_to_db() recipe_id2, _ = RecipeModel( "Title2", "Url2", "Ingredients2, test2").save_to_db() recipe_id3, _ = RecipeModel( "Title3", "Url3", "Ingredients3, test3").save_to_db() FavouriteRecipesModel( 2, self.recipe_id1, self.current_time, "Test category - salad", "Test comment - for Friday's dinner").save_to_db() FavouriteRecipesModel( 2, recipe_id2, self.current_time, "Test category - salad", "Test comment - for Friday's dinner").save_to_db() FavouriteRecipesModel( 2, recipe_id3, self.current_time, "Test category - salad", "Test comment - for Friday's dinner").save_to_db() FavouriteRecipesModel(1, self.recipe_id1, self.current_time, "Category of the second user", "User2's comment").save_to_db() self.assertEqual( RecipeModel.count_all()[0], 3, "The number of recipes saved to db is not correct.") self.assertEqual( len(FavouriteRecipesModel.show_mine(2)), 3, "The number of favourite recipes saved to db is not correct." ) self.assertEqual( len(FavouriteRecipesModel.show_mine(1)), 1, "The number of favourite recipes saved to db is not correct." ) response = client.post( f"{URL}/login", data=json.dumps({ "username": "******", "password": "******" }), headers={"Content-Type": "application/json"}) access_token = json.loads(response.data)['access_token'] response = client.delete( f"{URL}/admin_delete_account/2", headers={"Authorization": f"Bearer {access_token}"}) expected = { "message": f"User's account (user_id: 2) deleted successfully!" } self.assertEqual(response.status_code, 200) self.assertDictEqual(json.loads(response.data), expected) self.assertEqual( RecipeModel.count_all()[0], 1, "The number of recipes saved to db is not correct.") self.assertEqual( len(FavouriteRecipesModel.show_mine(2)), 0, "The number of favourite recipes saved to db is not correct." ) self.assertEqual( len(FavouriteRecipesModel.show_mine(1)), 1, "The number of favourite recipes saved to db is not correct." )