public async Task GetAllUnreadReturnsCorrectMessages() { var options = new DbContextOptionsBuilder <NeedForCarsDbContext>() .UseInMemoryDatabase("GetAllUnreadMessagesDb") .Options; var context = new NeedForCarsDbContext(options); var messagesService = new MessagesService(context); var sender = new NeedForCarsUser { Email = "*****@*****.**", UserName = "******", PasswordHash = "HASHEDPASSWORD", FirstName = "First", LastName = "Last", PhoneNumber = "1234567890" }; var receiver = new NeedForCarsUser { Email = "*****@*****.**", UserName = "******", PasswordHash = "HASHEDPASSWORD", FirstName = "First", LastName = "Last", PhoneNumber = "1234567899" }; await context.Users.AddAsync(sender); await context.Users.AddAsync(receiver); await context.SaveChangesAsync(); var message1 = new Message { SenderId = sender.Id, ReceiverId = receiver.Id, Content = "content", Read = true }; var message2 = new Message { SenderId = sender.Id, ReceiverId = receiver.Id, Content = "content2", Read = false }; await context.Messages.AddAsync(message1); await context.Messages.AddAsync(message2); await context.SaveChangesAsync(); var result = await messagesService.GetAllUnreadForUser(receiver.Id).CountAsync(); Assert.Equal(1, result); }
private static async Task SeedUserInRoles(UserManager <NeedForCarsUser> userManager) { if (!userManager.Users.Any()) { var user = new NeedForCarsUser { UserName = "******", Email = "*****@*****.**", FirstName = "Admin", LastName = "Admin", EmailConfirmed = true, PhoneNumber = "0000000000", LockoutEnabled = false, ReceivedMessages = new HashSet <Message>(), SentMessages = new HashSet <Message>(), UserCars = new HashSet <UserCar>() }; var password = "******"; var result = await userManager.CreateAsync(user, password); if (result.Succeeded) { await userManager.AddToRoleAsync(user, ROLE_ADMIN); } } }
public async Task MarkAsReadMarksMessageAsRead() { var options = new DbContextOptionsBuilder <NeedForCarsDbContext>() .UseInMemoryDatabase("MarkAsReadMessagesDb") .Options; var context = new NeedForCarsDbContext(options); var messagesService = new MessagesService(context); var sender = new NeedForCarsUser { Email = "*****@*****.**", UserName = "******", PasswordHash = "HASHEDPASSWORD", FirstName = "First", LastName = "Last", PhoneNumber = "1234567890" }; var receiver = new NeedForCarsUser { Email = "*****@*****.**", UserName = "******", PasswordHash = "HASHEDPASSWORD", FirstName = "First", LastName = "Last", PhoneNumber = "1234567899" }; await context.Users.AddAsync(sender); await context.Users.AddAsync(receiver); await context.SaveChangesAsync(); var message = new Message { SenderId = sender.Id, ReceiverId = receiver.Id, Content = "content", Read = false }; await context.Messages.AddAsync(message); await context.SaveChangesAsync(); await messagesService.MarkAsRead(message); var result = await context.Messages.FirstAsync(); Assert.True(result.Read); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new NeedForCarsUser { FirstName = Input.FirstName, LastName = Input.LastName, UserName = Input.UserName, Email = Input.Email }; 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); 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 async Task SendMessageSendsMessageCorrectly() { var options = new DbContextOptionsBuilder <NeedForCarsDbContext>() .UseInMemoryDatabase("SendMessageDb") .Options; var context = new NeedForCarsDbContext(options); var messagesService = new MessagesService(context); var sender = new NeedForCarsUser { Email = "*****@*****.**", UserName = "******", PasswordHash = "HASHEDPASSWORD", FirstName = "First", LastName = "Last", PhoneNumber = "1234567890" }; var receiver = new NeedForCarsUser { Email = "*****@*****.**", UserName = "******", PasswordHash = "HASHEDPASSWORD", FirstName = "First", LastName = "Last", PhoneNumber = "1234567899" }; await context.Users.AddAsync(sender); await context.Users.AddAsync(receiver); await context.SaveChangesAsync(); await messagesService.SendMessageAsync(sender.Id, receiver.Id, "some message"); var result = await context.Messages.FirstAsync(); Assert.True(result.SenderId == sender.Id && result.ReceiverId == receiver.Id && result.Content == "some message"); }
public async Task GetAllPublicShouldOnlyReturnPublicUserCars() { var options = new DbContextOptionsBuilder <NeedForCarsDbContext>() .UseInMemoryDatabase("GetAllPublicUserCarDb") .Options; var context = new NeedForCarsDbContext(options); var userCarsService = new UserCarsService(context); var make = new Make { Name = "Make", Description = "Desc" }; await context.Makes.AddAsync(make); await context.SaveChangesAsync(); var model = new Model { MakeId = make.Id, Name = "Model" }; await context.Models.AddAsync(model); await context.SaveChangesAsync(); var generation = new Generation { ModelId = model.Id, BodyType = Models.Enums.BodyType.Convertible, Name = "Name", Seats = 5, Description = "Desc" }; await context.Generations.AddAsync(generation); await context.SaveChangesAsync(); var engine = new Engine { Name = "Name1", FuelType = Models.Enums.FuelType.Diesel, MaxHP = 100, Creator = "Creator" }; await context.AddAsync(engine); await context.SaveChangesAsync(); var car = new Car { GenerationId = generation.Id, EngineId = engine.Id, Transmission = Models.Enums.Transmission.Automatic, DriveWheel = Models.Enums.DriveWheel.AllWheelDrive, BeginningOfProductionYear = 2000, BeginningOfProductionMonth = 1 }; await context.Cars.AddAsync(car); await context.SaveChangesAsync(); var user = new NeedForCarsUser { Email = "*****@*****.**", UserName = "******", PasswordHash = "HASHEDPASSWORD", FirstName = "First", LastName = "Last", PhoneNumber = "1234567890" }; await context.Users.AddAsync(user); await context.SaveChangesAsync(); var userCar1 = new UserCar { OwnerId = user.Id, CarId = car.Id, Color = "color", ProductionDateYear = 2000, ProductionDateMonth = 1, Mileage = 0, IsPublic = true }; var userCar2 = new UserCar { OwnerId = user.Id, CarId = car.Id, Color = "color", ProductionDateYear = 2000, ProductionDateMonth = 1, Mileage = 0, IsPublic = false }; await context.UserCars.AddAsync(userCar1); await context.UserCars.AddAsync(userCar2); await context.SaveChangesAsync(); var result = await userCarsService.GetAllPublic().CountAsync(); Assert.Equal(1, result); }
public async Task GetRelatedEntitiesCountShouldReturnCorrectValues() { var options = new DbContextOptionsBuilder <NeedForCarsDbContext>() .UseInMemoryDatabase("RelatedEntitiesDb_models") .Options; var context = new NeedForCarsDbContext(options); var makesService = new MakesService(context); var modelsService = new ModelsService(context, makesService); var make = new Make { Name = "Make", Description = "Desc" }; await context.Makes.AddAsync(make); var model = new Model { MakeId = make.Id, Name = "Model1", }; await context.Models.AddAsync(model); var generation = new Generation { ModelId = model.Id, Name = "Model1" }; await context.Generations.AddAsync(generation); var engine = new Engine { Name = "engine", MaxHP = 100, FuelType = Models.Enums.FuelType.Diesel, Creator = "creator" }; await context.Engines.AddAsync(engine); var car = new Car { GenerationId = generation.Id, EngineId = engine.Id, Transmission = Models.Enums.Transmission.Automatic, DriveWheel = Models.Enums.DriveWheel.AllWheelDrive, BeginningOfProductionYear = 2000, BeginningOfProductionMonth = 1 }; await context.Cars.AddAsync(car); var user = new NeedForCarsUser { Email = "*****@*****.**", UserName = "******", PasswordHash = "HASHEDPASSWORD", FirstName = "First", LastName = "Last", PhoneNumber = "1234567890" }; await context.Users.AddAsync(user); var userCar = new UserCar { OwnerId = user.Id, CarId = car.Id, Color = "color", ProductionDateYear = 2000, ProductionDateMonth = 1, Mileage = 0 }; await context.UserCars.AddAsync(userCar); await context.SaveChangesAsync(); modelsService.GetRelatedEntitiesCount(model, out int generations, out int cars, out int userCars); Assert.True(generations == 1 && cars == 1 && userCars == 1); }