public void CreateCompanyWithInvalidUserShouldReturnFalse() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "CreateCompanyFalse_Users_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; XeonUser user = null; var mockUserStore = new Mock <IUserStore <XeonUser> >(); var userManager = new Mock <UserManager <XeonUser> >(mockUserStore.Object, null, null, null, null, null, null, null, null); userManager.Setup(m => m.FindByNameAsync(username)) .Returns(Task.FromResult <XeonUser>(user)); var usersService = new UsersService(dbContext, userManager.Object); var company = new Company { Name = "Computers Ltd" }; var isCreated = usersService.CreateCompany(company, username); Assert.False(isCreated); }
public void CreateWhithTheSameUserRequestShouldNotCreateNewRequest() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "CreateTheSame_PartnerRequest_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; var user = new XeonUser { UserName = username }; var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(username)) .Returns(user); var partnerRequestService = new PartnerRequestsService(userService.Object, dbContext); partnerRequestService.Create(username); partnerRequestService.Create(username); var partnerRequests = dbContext.PartnerRequests.ToList(); Assert.Single(partnerRequests); }
public void AddProductInShoppingCartWithInvalidProductShouldNotAddProduct() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "AddProductInShoppingCartWithInvalidProduct_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; var user = new XeonUser { UserName = username, ShoppingCart = new ShoppingCart() }; dbContext.Users.Add(user); dbContext.SaveChanges(); var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(username)) .Returns(dbContext.Users.FirstOrDefault(x => x.UserName == username)); var productId = 1; Product product = null; var productService = new Mock <IProductsService>(); productService.Setup(p => p.GetProductById(productId)) .Returns(product); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); shoppingCartsService.AddProductInShoppingCart(productId, username); var shoppingCartProducts = dbContext.ShoppingCartProducts.ToList(); Assert.Empty(shoppingCartProducts); }
public void GetUserCompanyByUsernameShouldReturnCurrentUserCompany() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "GetUserCompanyByUsername_Users_Database") .Options; var dbContext = new XeonDbContext(options); var mockUserStore = new Mock <IUserStore <XeonUser> >(); var userManager = new Mock <UserManager <XeonUser> >(mockUserStore.Object, null, null, null, null, null, null, null, null); var usersService = new UsersService(dbContext, userManager.Object); var user = new XeonUser { UserName = "******", Company = new Company { Name = "Computers Ltd" } }; dbContext.Users.Add(user); dbContext.SaveChanges(); var company = usersService.GetUserCompanyByUsername(user.UserName); Assert.Equal(user.Company.Name, company.Name); }
public void AnyProductsShouldReturnFalseWhenThereAreNotAnyProducts() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: $"АnyProductsFalse_Database") .Options; var dbContext = new XeonDbContext(options); var user = new XeonUser { UserName = "******", ShoppingCart = new ShoppingCart() }; dbContext.Users.Add(user); var product = new Product { Name = "USB Cable" }; dbContext.Products.Add(product); dbContext.SaveChanges(); var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(user.UserName)) .Returns(user); var productService = new Mock <IProductsService>(); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); var areThereAnyProducts = shoppingCartsService.AnyProducts(user.UserName); Assert.False(areThereAnyProducts); }
public void EditLastNameRequstsShouldEditLastName() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "EditLastName_Users_Database") .Options; var dbContext = new XeonDbContext(options); var user = new XeonUser { UserName = "******", LastName = "AdminLastName" }; dbContext.Users.Add(user); dbContext.SaveChanges(); var store = new Mock <IUserStore <XeonUser> >(); var userManager = new Mock <UserManager <XeonUser> >(store.Object, null, null, null, null, null, null, null, null); var usersService = new UsersService(dbContext, userManager.Object); var lastName = "UserLastName"; usersService.EditLastName(user, lastName); Assert.Equal(lastName, user.LastName); }
public void CreateCompanyShouldCreateUserCompanyAndReturTrue() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "CreateCompany_Users_Database") .Options; var dbContext = new XeonDbContext(options); var user = new XeonUser { UserName = "******", }; dbContext.Users.Add(user); dbContext.SaveChanges(); var mockUserStore = new Mock <IUserStore <XeonUser> >(); var userManager = new Mock <UserManager <XeonUser> >(mockUserStore.Object, null, null, null, null, null, null, null, null); userManager.Setup(m => m.FindByNameAsync(user.UserName)) .Returns(Task.FromResult <XeonUser>(user)); var usersService = new UsersService(dbContext, userManager.Object); var company = new Company { Name = "Computers Ltd" }; var isCreated = usersService.CreateCompany(company, user.UserName); Assert.True(isCreated); Assert.Equal(company.Name, user.Company.Name); }
public void CreateOrderShouldCreateOrder() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "CreateOrder_Orders_Database") .Options; var dbContext = new XeonDbContext(options); var shoppingCartsService = new Mock <IShoppingCartsService>(); var username = "******"; var user = new XeonUser { UserName = username }; var usersService = new Mock <IUsersService>(); usersService.Setup(u => u.GetUserByUsername(username)) .Returns(user); var ordersService = new OrdersService(usersService.Object, shoppingCartsService.Object, dbContext); ordersService.CreateOrder(username); var orders = dbContext.Orders.ToList(); Assert.Single(orders); }
public void RemoveShouldRemovePartnerRequest() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "Remove_PartnerRequest_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; var user = new XeonUser { UserName = username }; var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(username)) .Returns(user); var partnerRequestService = new PartnerRequestsService(userService.Object, dbContext); partnerRequestService.Create(username); var partnerRequest = dbContext.PartnerRequests.FirstOrDefault(x => x.XeonUser.UserName == username); partnerRequestService.Remove(partnerRequest.Id); var partnerRequests = dbContext.PartnerRequests.ToList(); Assert.Empty(partnerRequests); }
public void AddProductInShoppingCartWithInvalidUserShouldNotAddProduct() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "AddProductInShoppingCartWithInvalidUser_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; XeonUser user = null; var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(username)) .Returns(user); var productId = 1; var productService = new Mock <IProductsService>(); productService.Setup(p => p.GetProductById(productId)) .Returns(new Product { Name = "USB Cable" }); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); shoppingCartsService.AddProductInShoppingCart(productId, username); var shoppingCartProducts = dbContext.ShoppingCartProducts.ToList(); Assert.Empty(shoppingCartProducts); }
public void GetAllShoppingCartProductsShouldReturnAllShoppingCartProductsForUser() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: $"GetAllShoppingCartProducts_Database") .Options; var dbContext = new XeonDbContext(options); var products = new List <Product> { new Product { Name = "USB 1.0" }, new Product { Name = "USB 2.0" }, new Product { Name = "USB 3.0" }, new Product { Name = "USB 4.0" } }; dbContext.Products.AddRange(products); var shoppingCartProducts = new List <ShoppingCartProduct> { new ShoppingCartProduct { Product = products.First() }, new ShoppingCartProduct { Product = products.Last() }, }; var user = new XeonUser { UserName = "******", ShoppingCart = new ShoppingCart { ShoppingCartProducts = shoppingCartProducts } }; dbContext.Users.Add(user); dbContext.SaveChanges(); var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(user.UserName)) .Returns(user); var productService = new Mock <IProductsService>(); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); var shoppingCatrProducts = shoppingCartsService.GetAllShoppingCartProducts(user.UserName); Assert.Equal(2, shoppingCatrProducts.Count()); }
public void EditFirstName(XeonUser user, string firstName) { if (user == null) { return; } user.FirstName = firstName; this.db.SaveChanges(); }
public void EditLastName(XeonUser user, string lastName) { if (user == null) { return; } user.LastName = lastName; this.db.SaveChanges(); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new XeonUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirsName, LastName = Input.LastName, ShoppingCart = new ShoppingCart() }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); var cart = SessionHelper.GetObjectFromJson <List <ShoppingCartProductsViewModel> >(HttpContext.Session, GlobalConstants.SESSION_SHOPPING_CART_KEY); if (cart != null) { foreach (var product in cart) { _shoppingCartService.AddProductInShoppingCart(product.Id, Input.Email, product.Quantity); } HttpContext.Session.Remove(GlobalConstants.SESSION_SHOPPING_CART_KEY); } return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public void EditProductQuantityInShoppingCartShouldEditProductQuantity(int quantity, int expectedQuantity) { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "EditProductQuantityInShoppingCart_Database") .Options; var dbContext = new XeonDbContext(options); var product = new Product { Name = "USB 1.0" }; dbContext.Products.AddRange(product); var shoppingCart = new ShoppingCart { ShoppingCartProducts = new List <ShoppingCartProduct> { new ShoppingCartProduct { Product = product, Quantity = 1 }, } }; var username = "******"; var user = new XeonUser { UserName = username, ShoppingCart = shoppingCart }; dbContext.Users.Add(user); dbContext.SaveChanges(); var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(username)) .Returns(user); var productService = new Mock <IProductsService>(); productService.Setup(p => p.GetProductById(product.Id)) .Returns(product); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); shoppingCartsService.EditProductQuantityInShoppingCart(product.Id, username, quantity); var shoppingCartProduct = dbContext.ShoppingCartProducts .FirstOrDefault(x => x.ProductId == product.Id && x.ShoppingCartId == user.ShoppingCartId); Assert.Equal(expectedQuantity, shoppingCartProduct.Quantity); }
public void AddAddressToUserShouldAddAddressDataCorrectly() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "Address_AddAddressToUser_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; var user = new XeonUser { UserName = username }; dbContext.Users.Add(user); dbContext.SaveChanges(); var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(username)) .Returns(dbContext.Users.FirstOrDefault(x => x.UserName == username)); var addressService = new AddressesService(userService.Object, dbContext); var address = new Address { Street = "Ivan Vazov", Description = "106", BuildingNumber = "A", City = new City { Name = "Burgas", Postcode = "8000" }, Country = "Bulgaria" }; addressService.AddAddressToUser(username, address); var userAddress = dbContext.Users .FirstOrDefault(x => x.UserName == username) .Addresses .FirstOrDefault(); Assert.Equal(address.Street, userAddress.Street); Assert.Equal(address.Description, userAddress.Description); Assert.Equal(address.BuildingNumber, userAddress.BuildingNumber); Assert.Equal(address.Country, userAddress.Country); Assert.Equal(address.City.Name, userAddress.City.Name); Assert.Equal(address.City.Postcode, userAddress.City.Postcode); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new XeonUser { UserName = Input.Email, Email = Input.Email, ShoppingCart = new ShoppingCart() }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public void ProcessOrderShouldProcessOrder() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "ProcessOrder_Orders_Database") .Options; var dbContext = new XeonDbContext(options); var shoppingCartsService = new Mock <IShoppingCartsService>(); var orderId = 3; var user = new XeonUser { UserName = "******", Orders = new List <Order> { new Order { Id = 1, Status = OrderStatus.Processing }, new Order { Id = 2, Status = OrderStatus.Delivered }, new Order { Id = orderId, Status = OrderStatus.Unprocessed }, } }; dbContext.Users.Add(user); dbContext.SaveChanges(); var usersService = new Mock <IUsersService>(); usersService.Setup(u => u.GetUserByUsername(user.UserName)) .Returns(dbContext.Users.FirstOrDefault(x => x.UserName == user.UserName)); var ordersService = new OrdersService(usersService.Object, shoppingCartsService.Object, dbContext); ordersService.ProcessOrder(orderId); var order = dbContext.Orders.FirstOrDefault(x => x.Id == orderId); Assert.Equal(orderId, order.Id); Assert.Equal(OrderStatus.Processed, order.Status); }
public void GetProcessedOrdersShouldReturnAllProcessedOrders() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "GetProcessedOrders_Orders_Database") .Options; var dbContext = new XeonDbContext(options); var shoppingCartsService = new Mock <IShoppingCartsService>(); var user = new XeonUser { UserName = "******", Orders = new List <Order> { new Order { Status = OrderStatus.Processed }, new Order { Status = OrderStatus.Delivered }, new Order { Status = OrderStatus.Processed }, } }; dbContext.Users.Add(user); dbContext.SaveChanges(); var usersService = new Mock <IUsersService>(); usersService.Setup(u => u.GetUserByUsername(user.UserName)) .Returns(dbContext.Users.FirstOrDefault(x => x.UserName == user.UserName)); var ordersService = new OrdersService(usersService.Object, shoppingCartsService.Object, dbContext); var unprocessedOrders = ordersService.GetProcessedOrders(); Assert.Equal(2, unprocessedOrders.Count()); }
public void AddProductInShoppingCartWhithExistingProductShouldNotAddProduct() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "AddProductInShoppingCartExistingProduct_Database") .Options; var dbContext = new XeonDbContext(options); var user = new XeonUser { UserName = "******", ShoppingCart = new ShoppingCart() }; dbContext.Users.Add(user); var product = new Product { Name = "USB Cable" }; dbContext.Products.Add(product); dbContext.SaveChanges(); var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(user.UserName)) .Returns(user); var productService = new Mock <IProductsService>(); productService.Setup(p => p.GetProductById(product.Id)) .Returns(product); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); shoppingCartsService.AddProductInShoppingCart(product.Id, user.UserName); shoppingCartsService.AddProductInShoppingCart(product.Id, user.UserName); var shoppingCartProducts = dbContext.ShoppingCartProducts.ToList(); Assert.Single(shoppingCartProducts); }
public void GetAllShoppingCartProductsWhithInvalidUserShouldReturnNull() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: $"GetAllShoppingCartProductsNull_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; XeonUser user = null; var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(username)) .Returns(user); var productService = new Mock <IProductsService>(); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); var shoppingCatrProducts = shoppingCartsService.GetAllShoppingCartProducts(username); Assert.Null(shoppingCatrProducts); }
public void AddProductInShoppingCartShouldAddDefaultOrSubmittedProductQuantity(int?quantity, int expectedQuantity) { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: $"AddProductInShoppingCartWhithQuantity_{expectedQuantity}_Database") .Options; var dbContext = new XeonDbContext(options); var user = new XeonUser { UserName = "******", ShoppingCart = new ShoppingCart() }; dbContext.Users.Add(user); var product = new Product { Name = "USB Cable" }; dbContext.Products.Add(product); dbContext.SaveChanges(); var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(user.UserName)) .Returns(user); var productService = new Mock <IProductsService>(); productService.Setup(p => p.GetProductById(product.Id)) .Returns(product); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); shoppingCartsService.AddProductInShoppingCart(product.Id, user.UserName, quantity); var shoppingCartProducts = dbContext.ShoppingCartProducts.ToList(); Assert.Equal(expectedQuantity, shoppingCartProducts.First().Quantity); }
private static async Task SeedUserInRoles(UserManager <XeonUser> userManager) { if (!userManager.Users.Any()) { var user = new XeonUser { UserName = "******", Email = "*****@*****.**", FirstName = "AdminFirstName", LastName = "AdminLastName", ShoppingCart = new ShoppingCart() }; var password = "******"; var result = await userManager.CreateAsync(user, password); if (result.Succeeded) { await userManager.AddToRoleAsync(user, Role.Admin.ToString()); } } }
public void GetUserOrdersShouldReturnAllUserOrders() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "GetUserOrders_Orders_Database") .Options; var dbContext = new XeonDbContext(options); var shoppingCartsService = new Mock <IShoppingCartsService>(); var user = new XeonUser { UserName = "******" }; var orders = new List <Order> { new Order { Status = OrderStatus.Processed, XeonUser = user }, new Order { Status = OrderStatus.Delivered, XeonUser = user }, new Order { Status = OrderStatus.Unprocessed, XeonUser = new XeonUser { UserName = "******" } }, }; dbContext.Orders.AddRange(orders); dbContext.SaveChanges(); var usersService = new Mock <IUsersService>(); var ordersService = new OrdersService(usersService.Object, shoppingCartsService.Object, dbContext); var userOrders = ordersService.GetUserOrders(user.UserName); Assert.Equal(2, userOrders.Count()); }
public void RemoveUserFromToRoleShouldReturnTrue() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "RemoveUserFromToRole_Users_Database") .Options; var dbContext = new XeonDbContext(options); var user = new XeonUser { UserName = "******", }; dbContext.Users.Add(user); var roleName = "Admin"; var role = new IdentityRole { Name = roleName }; dbContext.Roles.Add(role); dbContext.UserRoles.Add(new IdentityUserRole <string> { UserId = user.Id, RoleId = role.Id }); dbContext.SaveChanges(); var store = new Mock <IUserStore <XeonUser> >(); var userManager = new Mock <UserManager <XeonUser> >(store.Object, null, null, null, null, null, null, null, null); userManager.Setup(m => m.FindByNameAsync(user.UserName)) .Returns(Task.FromResult <XeonUser>(user)); var usersService = new UsersService(dbContext, userManager.Object); var isUserRomeveFromRole = usersService.RemoveUserFromToRole(user.UserName, roleName); Assert.True(isUserRomeveFromRole); }
public void RemoveUserFromToRoleWithInvalidUserShouldReturnFalse() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: "RemoveUserFromToRoleFalse_Users_Database") .Options; var dbContext = new XeonDbContext(options); var username = "******"; XeonUser user = null; var store = new Mock <IUserStore <XeonUser> >(); var userManager = new Mock <UserManager <XeonUser> >(store.Object, null, null, null, null, null, null, null, null); userManager.Setup(m => m.FindByNameAsync(username)) .Returns(Task.FromResult <XeonUser>(user)); var usersService = new UsersService(dbContext, userManager.Object); var role = "Admin"; var isUserRomeveFromRole = usersService.RemoveUserFromToRole(username, role); Assert.False(isUserRomeveFromRole); }
public void CompleteProcessingOrderShouldCompleteProcessingOrder(bool isPartnerOrAdmin, decimal totalPrice, int quantity) { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: $"CompleteProcessingOrder{totalPrice}_Orders_Database") .Options; var dbContext = new XeonDbContext(options); var user = new XeonUser { UserName = "******", Orders = new List <Order> { new Order { Status = OrderStatus.Processing } }, ShoppingCart = new ShoppingCart() }; var shoppingCartProducts = new List <ShoppingCartProduct> { new ShoppingCartProduct { Product = new Product { Name = "USB 1.0", Price = 10, ParnersPrice = 15 }, ShoppingCart = user.ShoppingCart, Quantity = quantity }, new ShoppingCartProduct { Product = new Product { Name = "USB 2.0", Price = 5, ParnersPrice = 10 }, ShoppingCart = user.ShoppingCart, Quantity = quantity } }; dbContext.Users.Add(user); dbContext.ShoppingCartProducts.AddRange(shoppingCartProducts); dbContext.SaveChanges(); var shoppingCartsService = new Mock <IShoppingCartsService>(); shoppingCartsService.Setup(s => s.GetAllShoppingCartProducts(user.UserName)) .Returns(shoppingCartProducts); var usersService = new Mock <IUsersService>(); usersService.Setup(u => u.GetUserByUsername(user.UserName)) .Returns(dbContext.Users.FirstOrDefault(x => x.UserName == user.UserName)); var ordersService = new OrdersService(usersService.Object, shoppingCartsService.Object, dbContext); ordersService.CompleteProcessingOrder(user.UserName, isPartnerOrAdmin); var order = dbContext.Orders.FirstOrDefault(x => x.XeonUser.UserName == user.UserName); Assert.Equal(OrderStatus.Unprocessed, order.Status); Assert.Equal(2, order.OrderProducts.Count()); Assert.Equal(PaymentStatus.Unpaid, order.PaymentStatus); Assert.Equal(totalPrice, order.TotalPrice); }
public void DeleteProductFromShoppingCartShoulDeleteExactProductFromCurrentUser() { var options = new DbContextOptionsBuilder <XeonDbContext>() .UseInMemoryDatabase(databaseName: $"DeleteProductFromShoppingCart_Database") .Options; var dbContext = new XeonDbContext(options); var products = new List <Product> { new Product { Name = "USB 1.0" }, new Product { Name = "USB 2.0" }, new Product { Name = "USB 3.0" }, new Product { Name = "USB 4.0" } }; dbContext.Products.AddRange(products); var shoppingCartProducts = new List <ShoppingCartProduct> { new ShoppingCartProduct { Product = products.First() }, new ShoppingCartProduct { Product = products.Last() }, }; var user = new XeonUser { UserName = "******", ShoppingCart = new ShoppingCart { ShoppingCartProducts = shoppingCartProducts } }; dbContext.Users.Add(user); dbContext.SaveChanges(); var userService = new Mock <IUsersService>(); userService.Setup(r => r.GetUserByUsername(user.UserName)) .Returns(user); var productForDelete = products.First(); var productService = new Mock <IProductsService>(); productService.Setup(p => p.GetProductById(productForDelete.Id)) .Returns(productForDelete); var shoppingCartsService = new ShoppingCartsService(dbContext, productService.Object, userService.Object); shoppingCartsService.DeleteProductFromShoppingCart(productForDelete.Id, user.UserName); var shoppingCartProduct = dbContext.ShoppingCartProducts.Where(x => x.ShoppingCartId == user.ShoppingCartId && x.ProductId == productForDelete.Id).ToList(); Assert.Empty(shoppingCartProduct); }