public void AuthenticateShouldLoginTheRegisteredUser() { var options = new DbContextOptionsBuilder <ExpensesDbContext>() .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLoginTheRegisteredUser)) .Options; using (var context = new ExpensesDbContext(options)) { var validator = new RegisterValidator(); var validatorUser = new UserRoleValidator(); var crValidator = new CreateValidator(); var userUserRoleService = new UserUserRoleService(validatorUser, context); var usersService = new UsersService(context, validator, crValidator, userUserRoleService, config); UserRole addUserRoleRegular = new UserRole { Name = "Regular", Description = "Created for test" }; context.UserRoles.Add(addUserRoleRegular); context.SaveChanges(); var added = new Lab6.Viewmodels.RegisterPostModel { FirstName = "Catalin", LastName = "Albulescu", Username = "******", Email = "*****@*****.**", Password = "******", }; var result = usersService.Register(added); var authenticated = new Lab6.Viewmodels.LoginPostModel { Username = "******", Password = "******" }; //valid authentification var authresult = usersService.Authenticate(added.Username, added.Password); Assert.IsNotNull(authresult); Assert.AreEqual(1, authresult.Id); Assert.AreEqual(authenticated.Username, authresult.Username); //invalid user authentification //var authresult1 = usersService.Authenticate("unknown", "abcdefg"); //Assert.IsNull(authresult1); } }
public void GetCurentUserShouldReturnAccesToKlaims() { var options = new DbContextOptionsBuilder <ExpensesDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetCurentUserShouldReturnAccesToKlaims)) .Options; using (var context = new ExpensesDbContext(options)) { var regValidator = new RegisterValidator(); var crValidator = new CreateValidator(); var validatorUser = new UserRoleValidator(); var userUserRoleService = new UserUserRoleService(validatorUser, context); var usersService = new UsersService(context, regValidator, crValidator, userUserRoleService, config); UserRole addUserRoleRegular = new UserRole { Name = "Regular", Description = "Created for test" }; context.UserRoles.Add(addUserRoleRegular); context.SaveChanges(); var added = new Lab6.Viewmodels.RegisterPostModel { FirstName = "firstName", LastName = "lastName", Username = "******", Email = "*****@*****.**", Password = "******" }; var result = usersService.Register(added); var authenticated = new Lab6.Viewmodels.LoginPostModel { Username = "******", Password = "******" }; var authresult = usersService.Authenticate(added.Username, added.Password); //usersService.GetCurentUser(httpContext); Assert.IsNotNull(authresult); } }