Example #1
0
        public async Task RemoveRole_WithGivenExistingRoleWithName_ShouldRemoveRoleSuccessfully()
        {
            string onNotNullErrorMessage = "The role was not removed succesfully from the roleStore.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var userManager = UserManagerInitializer.InitializeMockedUserManager();
            var roleManager = this.ConfigureRoleManagerWithRoleStore(context);

            var roleService = new RoleService(context, userManager.Object, roleManager);

            var             existingRole = "Moderator";
            ApplicationRole role         = new ApplicationRole();

            role.Name = existingRole;

            // Creating the role so it will exist before the service method is called
            await roleManager.CreateAsync(role);

            var methodResult = await roleService.RemoveRole(existingRole);

            Assert.True(methodResult, "The method returned false upon valid input data for removal.");

            var roleExistInRoleStore = await roleManager.FindByNameAsync(existingRole);

            AssertExtensions.NullWithMessage(roleExistInRoleStore, onNotNullErrorMessage);
        }
        public async Task Remove_WithNonExistingProduct_MethodShouldReturnFalse()
        {
            string onTrueErrorMessage = "The method does not return false upon non-existing product.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            // Gets the user(Usermanager accepts the id and returns the whole user) and a fake productId
            var user      = this.GetUser();
            var productId = "FakeProductId";

            var userManager = UserManagerInitializer.InitializeMockedUserManager();

            userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user);

            var favoritesService = new FavoritesService(context, userManager.Object);

            // Seeding user, product and favorite product (not matching productId)
            await this.SeedUser(context);

            await this.SeedProduct(context);

            await this.SeedFavoriteProduct(context);

            var methodResult = await favoritesService.Remove(productId, user.Id);

            Assert.False(methodResult, onTrueErrorMessage);
        }
        public async Task Remove_WithCorrectData_ShouldRemoveProductSuccessfully()
        {
            string onFalseMethodReturnErrorMessage   = "The method does not return true upon successfull remove.";
            string onFalseDatabaseReturnErrorMessage = "The favorite product is not deleted from the database.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            // Gets the user(Usermanager accepts the id and returns the whole user) and a fake productId
            var user      = this.GetUser();
            var productId = this.GetProduct().Id;

            var userManager = UserManagerInitializer.InitializeMockedUserManager();

            userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user);

            var favoritesService = new FavoritesService(context, userManager.Object);

            // Seeding user, product and favorite product
            await this.SeedUser(context);

            await this.SeedProduct(context);

            await this.SeedFavoriteProduct(context);

            var methodResult = await favoritesService.Remove(productId, user.Id);

            Assert.True(methodResult, onFalseMethodReturnErrorMessage);

            var doesExistInDatabase = context
                                      .UsersFavoriteProducts
                                      .FirstOrDefaultAsync(x => x.ProductId == productId && x.ApplicationUserId == user.Id);

            Assert.True(doesExistInDatabase.Result == null, onFalseDatabaseReturnErrorMessage);
        }
        public async Task Add_WithCorrectData_ShouldProvideCorrectResult()
        {
            string onFalseErrorMessage = "Service method returned false.";
            string onNullErrorMessage  = "The favorite product not found in database.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            // Gets the user(Usermanager accepts the id and returns the whole user) and productId only
            var user      = this.GetUser();
            var productId = this.GetProduct().Id;

            var userManager = UserManagerInitializer.InitializeMockedUserManager();

            userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user);

            var favoritesService = new FavoritesService(context, userManager.Object);

            // Seed both user and product
            await this.SeedUser(context);

            await this.SeedProduct(context);

            var methodResult = await favoritesService.Add(productId, user.Id);

            Assert.True(methodResult, onFalseErrorMessage);

            var favoriteProductFromDb = context
                                        .UsersFavoriteProducts
                                        .SingleOrDefaultAsync(x => x.ProductId == productId && x.ApplicationUserId == user.Id);

            AssertExtensions.NotNullWithMessage(favoriteProductFromDb, onNullErrorMessage);
        }
Example #5
0
        public async Task GetAllRoles_WithNoSeededRoles_ShouldReturnAnEmptyCollection()
        {
            string onFalseErrorMessage = "The method did not return an empty collection.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var userManager = UserManagerInitializer.InitializeMockedUserManager();
            var roleManager = this.ConfigureRoleManagerWithRoleStore(context);

            var roleService = new RoleService(context, userManager.Object, roleManager);

            var resultRoles = roleService.GetAllRoles();

            var expectedCount = 0;

            Assert.True(resultRoles.Result.Count() == expectedCount, onFalseErrorMessage);
        }
Example #6
0
        public async Task RemoveRole_WithGivenNullInput_ShouldReturnFalse()
        {
            string onTrueErrorMessage = "The method returned true upon invalid input data for removal.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var userManager = UserManagerInitializer.InitializeMockedUserManager();
            var roleManager = this.ConfigureRoleManagerWithRoleStore(context);

            var roleService = new RoleService(context, userManager.Object, roleManager);

            string invalidRoleName = null;

            var methodResult = await roleService.RemoveRole(invalidRoleName);

            Assert.False(methodResult, onTrueErrorMessage);
        }
        public async Task GetUserFavorites_WithCorrectData_ShouldReturnAllFavoriteProducts()
        {
            string onFalseErrorMessage           = "The method did not return the correct products.";
            string onCountDifferenceErrorMessage = "The count of the returned products is not correct";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            // Gets the user and products
            var user     = this.GetUser();
            var products = this.GetMultipleProducts();

            // Seed the needed data
            await this.SeedUser(context);

            await this.SeedMultipleProducts(context);

            await this.SeedMultipleFavoriteProducts(context);

            var userManager = UserManagerInitializer.InitializeMockedUserManager();

            userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user);

            var favoritesService = new FavoritesService(context, userManager.Object);

            var methodResult = await favoritesService.GetUserFavorites(user.Id);

            var expectedCountOfProducts = products.Count;
            var actualCountOfProducts   = 0;

            foreach (var pr in methodResult)
            {
                actualCountOfProducts++;
                var productId   = pr.Id;
                var productName = pr.Name;

                Assert.True(
                    products.Any(p => p.Id == productId && p.Name == productName),
                    onFalseErrorMessage);
            }

            AssertExtensions.EqualCountWithMessage(
                expectedCountOfProducts,
                actualCountOfProducts,
                onCountDifferenceErrorMessage);
        }
Example #8
0
        public async Task GetAllRoles_WithSeededRoles_ShouldReturnCorrectRoles()
        {
            string onFalseErrorMessage = "The returned roles are not correct.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var userManager = UserManagerInitializer.InitializeMockedUserManager();
            var roleManager = this.ConfigureRoleManagerWithRoleStore(context);

            await this.SeedRoles(roleManager);

            var roleService = new RoleService(context, userManager.Object, roleManager);

            var resultRoles = roleService.GetAllRoles();

            string[] expectedNames = new string[] { "Moderator", "Admin", "User" };

            Assert.True(resultRoles.Result.Any(x => expectedNames.Contains(x.Text)), onFalseErrorMessage);
        }
        public async Task GetUserFavorites_WithNonExistingUser_ShouldReturnNull()
        {
            string onNonNullErrorMessage = "The method did not return a null object.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            // Sets a fake (non-existing user)
            ApplicationUser user   = null;
            var             userId = "fakeUserId";

            var userManager = UserManagerInitializer.InitializeMockedUserManager();

            userManager.Setup(x => x.FindByIdAsync(userId)).ReturnsAsync(user);

            var favoritesService = new FavoritesService(context, userManager.Object);

            var methodResult = await favoritesService.GetUserFavorites(userId);

            AssertExtensions.NullWithMessage(methodResult, onNonNullErrorMessage);
        }
Example #10
0
        public async Task AddRole_WithGivenValidRoleName_ShouldCreateRoleSuccessfully()
        {
            string onFalseErrorMessage = "The method returned false upon valid input data for creation";
            string onNullErrorMessage  = "The role was not added succesfully into the roleStore (returned null)";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var userManager = UserManagerInitializer.InitializeMockedUserManager();
            var roleManager = this.ConfigureRoleManagerWithRoleStore(context);

            var roleService = new RoleService(context, userManager.Object, roleManager);

            var validRoleName = "Moderator";
            var methodResult  = await roleService.AddRole(validRoleName);

            Assert.True(methodResult, onFalseErrorMessage);

            var roleExistInRoleStore = await roleManager.FindByNameAsync(validRoleName);

            AssertExtensions.NotNullWithMessage(roleExistInRoleStore, onNullErrorMessage);
        }
Example #11
0
        public async Task AddRole_WithExistingRoleWithName_ShouldReturnFalse()
        {
            string onTrueErrorMessage = "The method returned true upon existing role with same name for creation.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var userManager = UserManagerInitializer.InitializeMockedUserManager();
            var roleManager = this.ConfigureRoleManagerWithRoleStore(context);

            var roleService = new RoleService(context, userManager.Object, roleManager);

            string          roleName = "Moderator";
            ApplicationRole role     = new ApplicationRole();

            role.Name = roleName;

            // Creating the role so it will exist before the service method is called
            await roleManager.CreateAsync(role);

            var methodResult = await roleService.AddRole(roleName);

            Assert.False(methodResult, onTrueErrorMessage);
        }
        public async Task GetUserFavorites_WithNoFavoriteProducts_ShouldReturnEmptyCollection()
        {
            string onNonEmptyCollectionErrorMessage = "The method did not return an empty collection.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            // Gets the user
            var user = this.GetUser();

            // Seed the needed data
            await this.SeedUser(context);

            await this.SeedMultipleProducts(context);

            var userManager = UserManagerInitializer.InitializeMockedUserManager();

            userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user);

            var favoritesService = new FavoritesService(context, userManager.Object);

            var methodResult = await favoritesService.GetUserFavorites(user.Id);

            AssertExtensions.EmptyWithMessage(methodResult, onNonEmptyCollectionErrorMessage);
        }
        public async Task Add_WithNonExistingUser_MethodShouldReturnFalse()
        {
            string onTrueErrorMessage = "Method not returning false on non-existing user.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            // Gets the user(Usermanager accepts the fake id and returns null) and productId only
            ApplicationUser user      = null;
            var             userId    = "fakeUserId";
            var             productId = this.GetProduct().Id;

            var userManager = UserManagerInitializer.InitializeMockedUserManager();

            userManager.Setup(x => x.FindByIdAsync(userId)).ReturnsAsync(user);

            var favoritesService = new FavoritesService(context, userManager.Object);

            // Seed only product
            await this.SeedProduct(context);

            var methodResult = await favoritesService.Add(productId, userId);

            Assert.False(methodResult, onTrueErrorMessage);
        }