Example #1
0
        public async Task UpdateKitchenIngredientAsync_ValidKitchenIngredient_ReturnsOkAndOnlyUpdatesQty()
        {
            KitchenUser       kitchenUser        = _userManager.TestUser.KitchenUser.FirstOrDefault();
            KitchenIngredient ingredientToUpdate = kitchenUser?.Kitchen.KitchenIngredient.FirstOrDefault();

            if (ingredientToUpdate == null)
            {
                throw new Exception("not setup for testing");
            }

            KitchenIngredientDto updateDto = new KitchenIngredientDto(ingredientToUpdate);

            string noteBeforeUpdate = ingredientToUpdate.Note;
            int    expectedQty      = 5;

            updateDto.Note     = null;
            updateDto.Quantity = expectedQty;
            DateTime timeBeforeUpdate = DateTime.UtcNow;


            var updateResult = await _controller.UpdateKitchenIngredientAsync(updateDto.KitchenIngredientId, updateDto);

            KitchenIngredient updatedIngredient = _context.KitchenIngredient.Where(ki => ki.KitchenIngredientId == updateDto.KitchenIngredientId).FirstOrDefault();

            Assert.IsType <OkResult>(updateResult);
            Assert.Equal(noteBeforeUpdate, updatedIngredient.Note);
            Assert.Equal(expectedQty, updatedIngredient.Quantity);
            Assert.True(updatedIngredient.LastUpdated >= timeBeforeUpdate);
        }
        public KitchenIngredientDto(KitchenIngredient kitchenIngredient)
        {
            if (kitchenIngredient == null)
            {
                return;
            }

            KitchenIngredientId  = kitchenIngredient.KitchenIngredientId;
            IngredientId         = kitchenIngredient.IngredientId;
            KitchenId            = kitchenIngredient.KitchenId;
            AddedByKitchenUserId = kitchenIngredient.AddedByKitchenUserId;
            CategoryId           = kitchenIngredient.CategoryId;
            LastUpdated          = kitchenIngredient.LastUpdated;
            Quantity             = kitchenIngredient.Quantity;
            Note = kitchenIngredient.Note;


            Ingredient = new IngredientDto(kitchenIngredient.Ingredient);
            Kitchen    = new KitchenDto(kitchenIngredient.Kitchen);

            if (kitchenIngredient.Category != null)
            {
                Category = new CategoryDto(kitchenIngredient.Category);
            }
        }
        public async Task <ActionResult <KitchenIngredient> > DeleteKitchenIngredientAsync(long id)
        {
            PantryPlannerUser user;

            try
            {
                user = await _userManager.GetUserFromCookieOrJwtAsync(this.User);

                KitchenIngredient deletedIngredient = _service.DeleteKitchenIngredient(id, user);
                return(Ok(new KitchenIngredientDto(deletedIngredient)));
            }
            catch (ArgumentNullException e)
            {
                return(BadRequest(e.Message));
            }
            catch (IngredientNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (PermissionsException e)
            {
                return(Unauthorized(e.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
Example #4
0
        public void AddKitchenIngredient_InvalidUser_ThrowsUserNotFoundException()
        {
            Ingredient ingredient = _context.Ingredient.Where(i => i.Name.Contains("butter")).FirstOrDefault();

            Kitchen kitchen = _testUser.KitchenUser.FirstOrDefault().Kitchen;

            if (ingredient == null || kitchen == null)
            {
                throw new ArgumentNullException("ingredient or kitchen is not setup for testing");
            }

            KitchenIngredient ingredientToAdd = new KitchenIngredient()
            {
                KitchenId    = kitchen.KitchenId,
                IngredientId = ingredient.IngredientId,
            };


            Assert.Throws <UserNotFoundException>(() =>
            {
                _service.AddKitchenIngredient(ingredientToAdd, new PantryPlannerUser {
                    Id = "fakeuser"
                });
            });
        }
        public async Task <ActionResult <KitchenIngredientDto> > AddKitchenIngredientAsync([FromBody] KitchenIngredientDto kitchenIngredient)
        {
            PantryPlannerUser user;

            try
            {
                user = await _userManager.GetUserFromCookieOrJwtAsync(this.User);

                KitchenIngredient kitchenIngredientToAdd = KitchenIngredientDto.Create(kitchenIngredient);

                KitchenIngredient newIngredient = _service.AddKitchenIngredient(kitchenIngredientToAdd, user);
                return(Ok(new KitchenIngredientDto(newIngredient)));
            }
            catch (ArgumentNullException e)
            {
                return(BadRequest(e.Message));
            }
            catch (UserNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (InvalidOperationException e)
            {
                return(StatusCode(StatusCodes.Status405MethodNotAllowed, e.Message));
            }
            catch (PermissionsException e)
            {
                return(Unauthorized(e.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
Example #6
0
        public async System.Threading.Tasks.Task UpdateKitchenIngredient_Valid_ContextIsUpdatedAsync()
        {
            Kitchen           kitchen            = _testUser.KitchenUser.FirstOrDefault().Kitchen;
            KitchenIngredient ingredientToUpdate = kitchen?.KitchenIngredient.FirstOrDefault();

            if (ingredientToUpdate == null || kitchen == null)
            {
                throw new ArgumentNullException("ingredient or kitchen is not setup for testing");
            }


            string expectedNote = "hello world";
            int    expectedQty  = 2;

            ingredientToUpdate.Note     = expectedNote;
            ingredientToUpdate.Quantity = expectedQty;

            await _service.UpdateKitchenIngredientAsync(ingredientToUpdate, _testUser);

            KitchenIngredient updatedIngredient = _context.KitchenIngredient.Where(k => k.KitchenIngredientId == ingredientToUpdate.KitchenIngredientId).FirstOrDefault();

            Assert.Equal(expectedQty, updatedIngredient.Quantity);
            Assert.Equal(expectedNote, updatedIngredient.Note);
            Assert.True(updatedIngredient.LastUpdated.Date == DateTime.Now.Date);
        }
Example #7
0
        public void AddKitchenIngredient_NullUserOrIngredient_ThrowsArgumentNullException()
        {
            Ingredient ingredient = _context.Ingredient.Where(i => i.Name.Contains("butter")).FirstOrDefault();

            Kitchen kitchen = _testUser.KitchenUser.FirstOrDefault().Kitchen;

            if (ingredient == null || kitchen == null)
            {
                throw new ArgumentNullException("ingredient or kitchen is not setup for testing");
            }

            KitchenIngredient ingredientToAdd = new KitchenIngredient()
            {
                KitchenId    = kitchen.KitchenId,
                IngredientId = ingredient.IngredientId,
            };


            Assert.Throws <ArgumentNullException>(() =>
            {
                _service.AddKitchenIngredient(null, _testUser);
            });


            Assert.Throws <ArgumentNullException>(() =>
            {
                _service.AddKitchenIngredient(ingredientToAdd, null);
            });
        }
Example #8
0
        public void AddKitchenIngredient_ValidIngredientAndUser_ContextIsUpdated()
        {
            Kitchen kitchen = _testUser.KitchenUser.FirstOrDefault().Kitchen;

            List <long> existingIngredients = kitchen.KitchenIngredient.Select(ki => ki.IngredientId).ToList();

            Ingredient ingredient = _context.Ingredient.Where(i => existingIngredients.Contains(i.IngredientId) == false).FirstOrDefault();


            if (ingredient == null || kitchen == null)
            {
                throw new ArgumentNullException("ingredient or kitchen is not setup for testing");
            }

            KitchenIngredient ingredientToAdd = new KitchenIngredient()
            {
                KitchenId    = kitchen.KitchenId,
                IngredientId = ingredient.IngredientId,
            };

            int countBeforeAdd = kitchen.KitchenIngredient.Count;

            _service.AddKitchenIngredient(ingredientToAdd, _testUser);

            Assert.Equal(countBeforeAdd + 1, kitchen.KitchenIngredient.Count);
            Assert.NotNull(_context.KitchenIngredient.Where(i => i.KitchenIngredientId == ingredientToAdd.KitchenIngredientId).FirstOrDefault());
        }
Example #9
0
        public async Task DeleteKitchenIngredient_NoRightsToKitchenIngredient_ReturnsUnauthorized()
        {
            List <long>       myKitchenIds       = _userManager.TestUser.KitchenUser.Select(k => k.KitchenId).ToList();
            Kitchen           notMyKitchen       = _context.Kitchen.Where(k => myKitchenIds.Contains(k.KitchenId) == false).FirstOrDefault();
            KitchenIngredient ingredientToDelete = notMyKitchen.KitchenIngredient.FirstOrDefault();

            if (ingredientToDelete == null)
            {
                throw new Exception("not setup for testing");
            }


            var deleteResult = await _controller.DeleteKitchenIngredientAsync(ingredientToDelete.KitchenIngredientId);

            Assert.IsType <UnauthorizedObjectResult>(deleteResult.Result);
        }
Example #10
0
        public async Task GetKitchenIngredient_NoRightsToKitchenIngredient_ReturnsUnauthorized()
        {
            List <long>       myKitchenIds    = _userManager.TestUser.KitchenUser.Select(k => k.KitchenId).ToList();
            Kitchen           notMyKitchen    = _context.Kitchen.Where(k => myKitchenIds.Contains(k.KitchenId) == false).FirstOrDefault();
            KitchenIngredient notMyIngredient = notMyKitchen.KitchenIngredient.FirstOrDefault();


            if (notMyKitchen == null || notMyIngredient == null)
            {
                throw new Exception("kitchen or ingredient is not setup for testing");
            }

            ActionResult <KitchenIngredientDto> actualResult = await _controller.GetKitchenIngredient(notMyIngredient.KitchenIngredientId);

            Assert.IsType <UnauthorizedObjectResult>(actualResult.Result);
        }
Example #11
0
        public async void UpdateKitchenIngredient_UserNoRights_ThrowsPermissionsException()
        {
            Kitchen           kitchen    = _testUser.KitchenUser.FirstOrDefault().Kitchen;
            KitchenIngredient ingredient = kitchen.KitchenIngredient.FirstOrDefault();
            PantryPlannerUser otherUserTryingToUpdate = InMemoryDataGenerator.AddNewRandomUser(_context);


            if (ingredient == null || kitchen == null || otherUserTryingToUpdate == null)
            {
                throw new ArgumentNullException("ingredient, kitchen, or user is not setup for testing");
            }

            await Assert.ThrowsAsync <PermissionsException>(async() =>
            {
                await _service.UpdateKitchenIngredientAsync(ingredient, otherUserTryingToUpdate);
            });
        }
Example #12
0
        public async Task UpdateKitchenIngredientAsync_KitchenIngredientNotExists_ReturnsNotFound()
        {
            KitchenUser       kitchenUser        = _userManager.TestUser.KitchenUser.FirstOrDefault();
            KitchenIngredient ingredientToUpdate = kitchenUser?.Kitchen.KitchenIngredient.FirstOrDefault();

            if (ingredientToUpdate == null)
            {
                throw new Exception("not setup for testing");
            }

            KitchenIngredientDto updateDto = new KitchenIngredientDto(ingredientToUpdate);

            updateDto.KitchenIngredientId = -5;

            var updateResult = await _controller.UpdateKitchenIngredientAsync(updateDto.KitchenIngredientId, updateDto);

            Assert.IsType <NotFoundObjectResult>(updateResult);
        }
Example #13
0
        public void DeleteKitchenIngredient_Valid_ReturnsKitchenIngredientDeleted()
        {
            Kitchen           kitchen           = _testUser.KitchenUser.FirstOrDefault().Kitchen;
            KitchenIngredient kitchenIngredient = kitchen?.KitchenIngredient.FirstOrDefault();

            if (kitchenIngredient == null || kitchen == null)
            {
                throw new ArgumentNullException("ingredient, kitchen is not setup for testing");
            }


            int countBeforeDelete = kitchen.KitchenIngredient.Count;

            KitchenIngredient deletedIngredient = _service.DeleteKitchenIngredient(kitchenIngredient, _testUser);

            Assert.Equal(kitchenIngredient, deletedIngredient);
            Assert.Equal(countBeforeDelete - 1, kitchen.KitchenIngredient.Count);
        }
Example #14
0
        public async Task DeleteKitchenIngredient_ValidKitchenIngredient_ReturnsOkAndDeletedKitchenIngredient()
        {
            KitchenUser       kitchenUser        = _userManager.TestUser.KitchenUser.FirstOrDefault();
            KitchenIngredient ingredientToDelete = kitchenUser?.Kitchen.KitchenIngredient.FirstOrDefault();

            if (ingredientToDelete == null)
            {
                throw new Exception("not setup for testing");
            }


            var deleteResult = await _controller.DeleteKitchenIngredientAsync(ingredientToDelete.KitchenIngredientId);

            Assert.IsType <OkObjectResult>(deleteResult.Result);

            var actualDeletedIngredient = (deleteResult.Result as OkObjectResult).Value;

            Assert.IsType <KitchenIngredientDto>(actualDeletedIngredient);
            Xunit.Asserts.Compare.DeepAssert.Equals(new KitchenIngredientDto(ingredientToDelete), (actualDeletedIngredient as KitchenIngredientDto));
        }
Example #15
0
        public async Task UpdateKitchenIngredientAsync_UserNoRightsToKitchenIngredient_ReturnsUnauthorized()
        {
            List <long>       myKitchenIds       = _userManager.TestUser.KitchenUser.Select(k => k.KitchenId).ToList();
            Kitchen           notMyKitchen       = _context.Kitchen.Where(k => myKitchenIds.Contains(k.KitchenId) == false).FirstOrDefault();
            KitchenIngredient ingredientToUpdate = notMyKitchen.KitchenIngredient.FirstOrDefault();

            if (ingredientToUpdate == null)
            {
                throw new Exception("not setup for testing");
            }

            KitchenIngredientDto updateDto = new KitchenIngredientDto(ingredientToUpdate);

            updateDto.Note     = "froopyland is real";
            updateDto.Quantity = 3;

            var updateResult = await _controller.UpdateKitchenIngredientAsync(updateDto.KitchenIngredientId, updateDto);

            Assert.IsType <UnauthorizedObjectResult>(updateResult);
        }
Example #16
0
        public void DeleteKitchenIngredient_NullArguments_ThrowsArgumentNullException()
        {
            Kitchen           kitchen            = _testUser.KitchenUser.FirstOrDefault().Kitchen;
            KitchenIngredient ingredientToDelete = kitchen.KitchenIngredient.FirstOrDefault();

            if (ingredientToDelete == null || kitchen == null)
            {
                throw new ArgumentNullException("ingredient or kitchen is not setup for testing");
            }


            Assert.Throws <ArgumentNullException>(() =>
            {
                _service.DeleteKitchenIngredient(null, _testUser);
            });

            Assert.Throws <ArgumentNullException>(() =>
            {
                _service.DeleteKitchenIngredient(ingredientToDelete, null);
            });
        }
Example #17
0
        public async Task GetKitchenIngredient_ValidKitchenIngredient_ReturnsOkAndCorrectResult()
        {
            Kitchen           kitchen            = _userManager.TestUser.KitchenUser.FirstOrDefault()?.Kitchen;
            KitchenIngredient expectedIngredient = kitchen.KitchenIngredient.FirstOrDefault();

            if (kitchen == null || expectedIngredient == null)
            {
                throw new Exception("kitchen is not setup for testing");
            }

            ActionResult <KitchenIngredientDto> actualResult = await _controller.GetKitchenIngredient(expectedIngredient.KitchenIngredientId);


            Assert.IsType <OkObjectResult>(actualResult.Result);

            var actualKitchenIngredient = (actualResult.Result as OkObjectResult).Value;

            Assert.IsType <KitchenIngredientDto>(actualKitchenIngredient);

            Xunit.Asserts.Compare.DeepAssert.Equals(new KitchenIngredientDto(expectedIngredient), (actualKitchenIngredient as KitchenIngredientDto));
        }
Example #18
0
        public void AddKitchenIngredient_AddDuplicateIngredient_ThrowsInvalidOperationException()
        {
            Kitchen    kitchen    = _testUser.KitchenUser.FirstOrDefault().Kitchen;
            Ingredient ingredient = kitchen.KitchenIngredient.FirstOrDefault()?.Ingredient;

            if (ingredient == null || kitchen == null)
            {
                throw new ArgumentNullException("ingredient or kitchen is not setup for testing");
            }


            Assert.Throws <InvalidOperationException>(() =>
            {
                KitchenIngredient duplicateIngredient = new KitchenIngredient()
                {
                    KitchenId    = kitchen.KitchenId,
                    IngredientId = ingredient.IngredientId,
                };

                _service.AddKitchenIngredient(duplicateIngredient, _testUser);
            });
        }
        /// <summary>
        /// Loop over every Kitchen available and add a random amount of ingredients to each one
        /// </summary>
        internal static void InitializeKitchenIngredients(PantryPlannerContext context)
        {
            if (context.Ingredient.Count() == 0)
            {
                throw new Exception("Ingredient must be populated to initialize");
            }

            Random randomGen = new Random();

            foreach (Kitchen kitchen in context.Kitchen)
            {
                int         numOfIngredientsToAdd = randomGen.Next(1, 25);
                int         randomOffset          = randomGen.Next(100);
                KitchenUser someUserInKitchen     = kitchen.KitchenUser.FirstOrDefault();

                for (int i = 0; i < numOfIngredientsToAdd; i++)
                {
                    Ingredient ingredient = context.Ingredient.Skip(randomOffset)?.ToList()[i];

                    if (ingredient == null)
                    {
                        break;
                    }

                    KitchenIngredient kitchenIngredient = new KitchenIngredient()
                    {
                        KitchenId            = kitchen.KitchenId,
                        IngredientId         = ingredient.IngredientId,
                        LastUpdated          = DateTime.Now,
                        AddedByKitchenUserId = someUserInKitchen?.KitchenUserId
                    };

                    context.KitchenIngredient.Add(kitchenIngredient);
                }
            }

            context.SaveChanges();
        }
Example #20
0
        public void AddKitchenIngredient_UserNoRights_ThrowsPermissionsException()
        {
            Ingredient        ingredient           = _context.Ingredient.Where(i => i.Name.Contains("butter")).FirstOrDefault();
            Kitchen           kitchen              = _testUser.KitchenUser.FirstOrDefault().Kitchen;
            PantryPlannerUser otherUserTryingToAdd = _context.Users.Where(u => u.Id != _testUser.Id).FirstOrDefault();

            if (ingredient == null || kitchen == null || otherUserTryingToAdd == null)
            {
                throw new ArgumentNullException("ingredient, kitchen, or user is not setup for testing");
            }

            KitchenIngredient ingredientToAdd = new KitchenIngredient()
            {
                KitchenId    = kitchen.KitchenId,
                IngredientId = ingredient.IngredientId,
            };


            Assert.Throws <PermissionsException>(() =>
            {
                _service.AddKitchenIngredient(ingredientToAdd, otherUserTryingToAdd);
            });
        }
Example #21
0
        public async void UpdateKitchenIngredient_NullArguments_ThrowsArgumentNullException()
        {
            Kitchen kitchen = _testUser.KitchenUser.FirstOrDefault()?.Kitchen;

            if (kitchen == null)
            {
                throw new ArgumentNullException("ingredient or kitchen is not setup for testing");
            }


            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                KitchenIngredient ingredient = null;

                await _service.UpdateKitchenIngredientAsync(ingredient, _testUser);
            });

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                KitchenIngredient ingredient = kitchen.KitchenIngredient.FirstOrDefault();

                await _service.UpdateKitchenIngredientAsync(ingredient, null);
            });
        }
        public static bool KitchenIngredientExists(this PantryPlannerContext context, KitchenIngredient ingredient)
        {
            if (ingredient == null)
            {
                return(false);
            }

            return(context.KitchenIngredientExists(ingredient.KitchenIngredientId));
        }