public async Task CreateUproccessedOrder_ThrowsException_WhenShoppingCartIsEmptyOrNull()
        {
            MapperInitializer.InitializeMapper();
            var context                   = ApplicationDbContextInMemoryFactory.InitializeContext();
            var orderRepository           = new EfDeletableEntityRepository <Order>(context);
            var userTestSeeder            = new UserTestSeeder();
            var shoppingCartProductSeeder = new ShoppingCartProductSeeder();

            //seed user
            await userTestSeeder.SeedUsersWithAddressesAsync(context);

            var userWithoutProducts = context.Users.First(x => x.UserName == "UserWithoutAddresses");

            var orderCreateInputModel = new OrderCreateInputModel()
            {
            };

            var orderService = this.GetOrderService(orderRepository, context);

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await orderService.CreateUproccessedOrder(orderCreateInputModel, null);
            });

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await orderService.CreateUproccessedOrder(orderCreateInputModel, userWithoutProducts.ShoppingCart.Id);
            });
        }
        public async Task GetIdByUserId_FunctionsCorrectly()
        {
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var shoppingCartRepository = new EfDeletableEntityRepository <ShoppingCart>(context);
            var shoppingCartService    = new ShoppingCartService(shoppingCartRepository);
            var userSeeder             = new UserTestSeeder();
            await userSeeder.SeedUsersWithAddressesAsync(context);

            var user = context.Users.First(x => x.UserName == "UserWithoutAddresses");

            var result = await shoppingCartService.GetIdByUserId(user.Id);

            Assert.Equal(user.ShoppingCart.Id, result);

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await shoppingCartService.GetIdByUserId(null);
            });
        }
Exemple #3
0
        public async Task GetUserWithAllPropertiesById_WorksCorectly()
        {
            MapperInitializer.InitializeMapper();
            var context        = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService    = this.GetUserService(userRepository, context);
            var userTestSeeder = new UserTestSeeder();

            await userTestSeeder.SeedUsersWithAddressesAsync(context);

            var result = await userService.GetUserWithAllPropertiesById("UserId1");

            Assert.NotNull(result);

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await userService.GetUserWithAllPropertiesById(null);
            });
        }
        public async Task CreateUnproccessedOrder_RemovesProductsFromShoppingCart_ProperlyCreatesAnOrderWithTheProducts()
        {
            MapperInitializer.InitializeMapper();
            var context                   = ApplicationDbContextInMemoryFactory.InitializeContext();
            var orderRepository           = new EfDeletableEntityRepository <Order>(context);
            var userTestSeeder            = new UserTestSeeder();
            var shoppingCartProductSeeder = new ShoppingCartProductSeeder();
            var productsTestSeeder        = new ProductsTestSeeder();

            //seed products
            await productsTestSeeder.SeedProducts(context);

            //seed users
            await userTestSeeder.SeedUsersWithAddressesAsync(context);

            var userWithProducts = context.Users.First(x => x.UserName == "UserWithoutAddresses");

            //seed products in shoppingCart for user
            await shoppingCartProductSeeder.SeedProductsToUser(context, userWithProducts);

            var orderCreateInputModel = new OrderCreateInputModel()
            {
                UserId = userWithProducts.Id,
            };

            var orderService = this.GetOrderService(orderRepository, context);

            await orderService.CreateUproccessedOrder(orderCreateInputModel, userWithProducts.ShoppingCart.Id);

            var actualShoppingCartCount = userWithProducts.ShoppingCart.ShoppingCartProducts.Count();
            var actualOrderCount        = context.Orders.Count();
            var order = context.Orders.FirstOrDefault();

            var deliveryPrice = userWithProducts.ShoppingCart.ShoppingCartProducts.Sum(x => x.Quantity * x.Product.Price) >= 20m ? 0m : 3.5m;
            var totalPrice    = userWithProducts.ShoppingCart.ShoppingCartProducts.Sum(x => x.Quantity * x.Product.Price) + deliveryPrice;

            Assert.Equal(OrderStatus.UnProccessed, order.OrderStatus);
            Assert.Equal(deliveryPrice, order.DeliveryPrice);
            Assert.Equal(totalPrice, order.TotalPrice);
            Assert.Equal(0, actualShoppingCartCount);
            Assert.Equal(1, actualOrderCount);
        }
Exemple #5
0
        public async Task UserHasThreeAdresses_ReturnsCorrectBooleanResult()
        {
            var errorMessage = "UserService UserHasThreeAddresses() method does not return the correct boolean result";

            var context        = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService    = this.GetUserService(userRepository, context);
            var seeder         = new UserTestSeeder();
            await seeder.SeedUsersWithAddressesAsync(context);

            var userWithoutAddresses   = context.Users.First(x => x.UserName == "UserWithoutAddresses");
            var userWithThreeAddresses = context.Users.First(x => x.UserName == "UserWithThreeAddresses");

            var resultNoAdresses = await userService.UserHasThreeAddresses(userWithoutAddresses.Id);

            var resultThreeAdresses = await userService.UserHasThreeAddresses(userWithThreeAddresses.Id);

            Assert.False(resultNoAdresses, errorMessage);
            Assert.True(resultThreeAdresses, errorMessage);
        }
Exemple #6
0
        public async Task EditProfileAsync_ReturnsFalse_IfUsernameAlreadyExists()
        {
            var errorMessage = "EditProfileAsync did not return false when a user tried to change his username to an already existing one.";

            var context        = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService    = this.GetUserService(userRepository, context);
            var seeder         = new UserTestSeeder();
            await seeder.SeedUsersWithAddressesAsync(context);

            var userWithoutAddresses = context.Users.First(x => x.UserName == "UserWithoutAddresses");

            var profileEditInputModel = new ProfileEdintInputModel()
            {
                Id       = userWithoutAddresses.Id,
                Username = "******",
            };

            var result = await userService.EditProfileAsync(profileEditInputModel);

            Assert.False(result, errorMessage);
        }
Exemple #7
0
        public async Task DeleteAsync_FunctionsCorrectly()
        {
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userAddressRepository = new EfDeletableEntityRepository <UserAddress>(context);
            var userAddressService    = new UsersAddressesService(userAddressRepository);
            var userSeeder            = new UserTestSeeder();
            await userSeeder.SeedUsersWithAddressesAsync(context);

            var user    = context.Users.First(x => x.UserName == "UserWithThreeAddresses");
            var address = context.Addresses.First(x => x.City == "FirstCity");

            var booleanResult = await userAddressService.DeleteAsync(address.Id, user.Id);

            var actualAddressesCount = user.UserAddresses.Count();

            Assert.True(booleanResult, "Method should return true after deleting the address");
            Assert.Equal(2, actualAddressesCount);

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await userAddressService.DeleteAsync(null, null);
            });
        }
Exemple #8
0
        public async Task CreateAsync_AddsAddress_ReturnsTrueAndAddsAddress_WithCorrectData()
        {
            var errorMessage = "Method did not return true upon adding an address to user;";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userAddressRepository = new EfDeletableEntityRepository <UserAddress>(context);
            var userAddressService    = new UsersAddressesService(userAddressRepository);
            var userSeeder            = new UserTestSeeder();
            await userSeeder.SeedUsersWithAddressesAsync(context);

            var addressSeeder = new AddressTestSeeder();
            await addressSeeder.SeedOneAddress(context);

            var user    = context.Users.First(x => x.UserName == "UserWithoutAddresses");
            var address = context.Addresses.First(x => x.City == "FourthCity");

            var booleanResult = await userAddressService.CreateAsync(user.Id, address.Id);

            var actualAddressesCount = user.UserAddresses.Count();

            Assert.True(booleanResult, errorMessage);
            Assert.Equal(1, actualAddressesCount);
        }
Exemple #9
0
        public async Task EditProfileAsync_ReturnsTrueAndMakesChanges_IfTheModelIsCorrect()
        {
            var errorMessageBool = "EditProfileAsync did not return true";

            MapperInitializer.InitializeMapper();
            var context        = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userService    = this.GetUserService(userRepository, context);
            var seeder         = new UserTestSeeder();
            await seeder.SeedUsersWithAddressesAsync(context);

            var userWithoutAddresses = context.Users.First(x => x.UserName == "UserWithoutAddresses");

            var profileEditInputModel = new ProfileEdintInputModel()
            {
                Id       = userWithoutAddresses.Id,
                Username = "******",
            };

            var result = await userService.EditProfileAsync(profileEditInputModel);

            Assert.True(result, errorMessageBool);
            Assert.Equal("Changed", userWithoutAddresses.UserName);
        }