DeleteRecipeTest_Given_That_Recipe_Was_Created_By_Some_One_Then_UnAuthorized_Exception_Should_Be_Thrown()
        {
            //Arrange
            var userId   = Guid.NewGuid();
            var recipeId = Guid.NewGuid();

            var recipe = new Recipe
            {
                RecipeId = recipeId,
                Steps    = new List <RecipeStep>
                {
                    new RecipeStep
                    {
                        Description = "Step 1"
                    },
                    new RecipeStep
                    {
                        Description = "Step 2"
                    },
                },
                DateCreated = DateTime.Now.AddDays(-1),
                CreatedById = Guid.NewGuid()
            };

            RecipeManagementContext.PrepareTestData(context => { context.Recipes.Add(recipe); });

            //Act
            var unauthorizedAccessException = Assert.Throws <UnauthorizedAccessException>(() =>
                                                                                          _recipeService.DeleteRecipe(recipeId, userId));

            //Assert
            Assert.IsNotNull(unauthorizedAccessException);
        }
Exemple #2
0
        public void UpdateUserTest()
        {
            //Arrange
            var userId = Guid.NewGuid();
            var user   = new User
            {
                UserId          = userId,
                Name            = "Old Name",
                Surname         = "Old Surname",
                EmailAddress    = "*****@*****.**",
                CellphoneNumber = "0720000000",
                DateCreated     = DateTime.Now.AddDays(-1)
            };

            RecipeManagementContext.PrepareTestData(context => { context.Users.Add(user); });

            //Act
            _userService.UpdateUser(userId, "Name", "Surname", "*****@*****.**", "0721234567");

            //Assert
            Assert.That(user.UserId, Is.EqualTo(userId));
            Assert.That(user.Name, Is.EqualTo("Name"));
            Assert.That(user.Surname, Is.EqualTo("Surname"));
            Assert.That(user.CellphoneNumber, Is.EqualTo("0721234567"));
            Assert.That(user.EmailAddress, Is.EqualTo("*****@*****.**"));
            Assert.That(RecipeManagementContext.Users.Count(), Is.EqualTo(1));
            Assert.That(user.DateCreated, Is.EqualTo(DateTime.Now.AddDays(-1)).Within(1).Minutes);
            Assert.That(user.DateUpdated, Is.EqualTo(DateTime.Now).Within(1).Minutes);

            RecipeManagementContext.VerifySave();
        }
        public void DeleteRecipeTest()
        {
            //Arrange
            var userId   = Guid.NewGuid();
            var recipeId = Guid.NewGuid();

            var recipe = new Recipe
            {
                RecipeId = recipeId,
                Steps    = new List <RecipeStep>
                {
                    new RecipeStep
                    {
                        Description = "Step 1"
                    },
                    new RecipeStep
                    {
                        Description = "Step 2"
                    },
                },
                DateCreated = DateTime.Now.AddDays(-1),
                CreatedById = userId
            };

            RecipeManagementContext.PrepareTestData(context => { context.Recipes.Add(recipe); });

            //Act
            _recipeService.DeleteRecipe(recipeId, userId);

            //Assert
            Assert.That(RecipeManagementContext.Recipes.Count(), Is.EqualTo(0));

            RecipeManagementContext.VerifySave();
        }
Exemple #4
0
        ChangePassword_Given_UserId_And_NewPassword_Then_Password_Should_Be_Changed_With_Newly_Generated_Salt()
        {
            //Arrange
            var          userId      = Guid.NewGuid();
            const string oldPassword = "******";
            const string newPassword = "******";
            const string oldSalt     = "OldSalt";

            RecipeManagementContext.PrepareTestData(context =>
            {
                context.Users.Add(new User
                {
                    UserId       = userId,
                    Password     = oldPassword,
                    PasswordSalt = oldSalt
                });
            });

            _cryptographyServiceMock.Setup(service => service.CreateSalt()).Returns("Salt");
            _cryptographyServiceMock.Setup(s => s.CreatePasswordHash(newPassword, "Salt")).Returns("PasswordHash");

            //Act
            _userService.ChangePassword(userId, newPassword);

            //Assert
            Assert.That(RecipeManagementContext.Users.First().Password, Is.EqualTo("PasswordHash"));
            Assert.That(RecipeManagementContext.Users.First().PasswordSalt, Is.EqualTo("Salt"));
            Assert.That(RecipeManagementContext.Users.First().DateUpdated, Is.EqualTo(DateTime.Now).Within(1).Minutes);

            //Verify
            _cryptographyServiceMock.Verify(v => v.CreatePasswordHash(newPassword, "Salt"));
            RecipeManagementContext.VerifySave();
        }
Exemple #5
0
        public void AuthenticateUserTest_Given_Email_And_Password_Then_True_Should_Be_Returned_If_Password_Match(
            string inputPassword, bool expectedResult)
        {
            //Arrange
            const string passwordSalt = "passwordSalt";

            var    cryptographyService = new CryptographyService();
            string password            = cryptographyService.CreatePasswordHash("password", passwordSalt);

            _cryptographyServiceMock.Setup(service => service.CreatePasswordHash("password", passwordSalt))
            .Returns(password);

            RecipeManagementContext.PrepareTestData(context =>
            {
                context.Users.Add(new User
                {
                    EmailAddress = "*****@*****.**",
                    Password     = password,
                    PasswordSalt = passwordSalt
                });
            });

            //Act
            bool isAuthenticated = _userService.AuthenticateUser("*****@*****.**", inputPassword);

            //Assert
            Assert.That(isAuthenticated, Is.EqualTo(expectedResult));
        }
Exemple #6
0
        public void UpdateUserTest_Given_That_User_With_The_Same_Email_Exists_Then_Exception_Should_Be_Thrown()
        {
            //Arrange
            var userId = Guid.NewGuid();
            var user   = new User
            {
                UserId          = userId,
                Name            = "Old Name",
                Surname         = "Old Surname",
                EmailAddress    = "*****@*****.**",
                CellphoneNumber = "0720000000"
            };

            RecipeManagementContext.PrepareTestData(context =>
            {
                context.Users.Add(user);
                context.Users.Add(new User
                {
                    UserId          = Guid.NewGuid(),
                    Name            = "Other Name",
                    Surname         = "Other Surname",
                    EmailAddress    = "*****@*****.**",
                    CellphoneNumber = "0721111111"
                });
            });

            //Act
            var exception = Assert.Throws <RecipeManagementException>(() =>
                                                                      _userService.UpdateUser(userId, "Name", "Surname", "*****@*****.**", "0721234567"));

            //Assert
            Assert.That(exception.Message, Is.EqualTo("User with specified email address already exists"));
        }
        public void UpdateRecipeTest()
        {
            //Arrange
            var userId      = Guid.NewGuid();
            var recipeId    = Guid.NewGuid();
            var recipeSteps = new List <RecipeStep>
            {
                new RecipeStep
                {
                    Description = "Step 1"
                },
                new RecipeStep
                {
                    Description = "Step 2"
                },
                new RecipeStep
                {
                    Description = "Step 3"
                }
            };

            var recipe = new Recipe
            {
                RecipeId = recipeId,
                Steps    = new List <RecipeStep>
                {
                    new RecipeStep
                    {
                        Description = "Step 1"
                    },
                    new RecipeStep
                    {
                        Description = "Step 2"
                    },
                },
                DateCreated = DateTime.Now.AddDays(-1),
                CreatedById = userId
            };

            RecipeManagementContext.PrepareTestData(context => { context.Recipes.Add(recipe); });

            //Act
            _recipeService.UpdateRecipe(recipeId, "Title", "Description", recipeSteps, "Notes", userId);

            //Assert
            Assert.That(recipe.Title, Is.EqualTo("Title"));
            Assert.That(recipe.Description, Is.EqualTo("Description"));
            Assert.That(recipe.Notes, Is.EqualTo("Notes"));
            Assert.That(recipe.Steps.Count, Is.EqualTo(3));
            Assert.That(recipe.Steps.First().StepNumber, Is.EqualTo(1));
            Assert.That(recipe.Steps.Last().StepNumber, Is.EqualTo(3));
            Assert.That(recipe.CreatedById, Is.EqualTo(userId));
            Assert.That(recipe.DateCreated, Is.EqualTo(DateTime.Now.AddDays(-1)).Within(1).Minutes);
            Assert.That(recipe.DateUpdated, Is.EqualTo(DateTime.Now).Within(1).Minutes);

            RecipeManagementContext.VerifySave();
        }
Exemple #8
0
        public void AddUserTest_Given_That_User_With_The_Same_Email_Exists_Then_Exception_Should_Be_Thrown()
        {
            //Arrange
            const string emailAddress = "*****@*****.**";

            RecipeManagementContext.PrepareTestData(context =>
            {
                context.Users.Add(new User
                {
                    UserId       = Guid.NewGuid(),
                    EmailAddress = emailAddress
                });
            });
            //Act
            var exception = Assert.Throws <RecipeManagementException>(() =>
                                                                      _userService.AddUser("Name", "Surname", "*****@*****.**", "0721234567", "Password123"));

            //Assert
            Assert.That(exception.Message, Is.EqualTo("User with specified email address already exists"));
        }
Exemple #9
0
        public void AuthenticateUserTest_Given_Email_And_Password_And_User_Is_Not_Active_Then_False_Should_Be_Returned()
        {
            //Arrange
            const string email = "*****@*****.**";

            RecipeManagementContext.PrepareTestData(context =>
            {
                context.Users.Add(new User
                {
                    EmailAddress = email,
                    Password     = "******",
                    PasswordSalt = "passwordSalt"
                });
            });

            //Act
            bool isAuthenticated = _userService.AuthenticateUser(email, "password");

            //Assert
            Assert.That(isAuthenticated, Is.EqualTo(false));
        }