Example #1
0
        public void ShouldAuthentificate()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ShouldAuthentificate))// "ValidRegisterShouldCreateANewUser")
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, config);
                var added        = new Lab2Expense.ViewModels.RegisterPostModel
                {
                    Email     = "[email protected]",
                    FirstName = "fdsfsdfs",
                    LastName  = "fdsfs",
                    Password  = "******",
                    Username  = "******"
                };
                var result = usersService.Register(added);
                var auth   = new Lab2Expense.ViewModels.LoginPostModel
                {
                    Username = added.Username,
                    Password = added.Password
                };

                var resultAuth     = usersService.Authenticate(auth.Username, auth.Password);
                var CevaCeNuExista = usersService.Authenticate("FDDFDSF", "SDHKSJFD");
                Assert.IsNotNull(resultAuth.Token);
                Assert.IsNull(CevaCeNuExista);
            }
        }
        public async Task Authenticate_with_good_credential()
        {
            //Act
            var u = await usersService.Create(user);

            var u1 = await usersService.Authenticate("stratege", "password");

            //Assert
            Assert.IsNotNull(u1);
            Assert.IsNotNull(u1.token);
            Assert.IsNull(u1.password);
        }
Example #3
0
        public void AuthenticateShouldLogTheRegisteredUser()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLogTheRegisteredUser))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var validator           = new RegisterValidator();
                var validatorUser       = new UserRoleValidator();
                var userUserRoleService = new UserUserRoleService(validatorUser, context);
                var usersService        = new UsersService(context, validator, userUserRoleService, config);

                UserRole addUserRoleRegular = new UserRole
                {
                    Name        = "Regular",
                    Description = "Creat pentru testare"
                };
                context.UserRole.Add(addUserRoleRegular);
                context.SaveChanges();

                var added = new Lab3Movie.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "lastName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };
                var result = usersService.Register(added);

                var authenticated = new Lab3Movie.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);
            }
        }
Example #4
0
        public void ValidDeleteIsRemovedShoulBeTrue()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidDeleteIsRemovedShoulBeTrue))// "ValidRegisterShouldCreateANewUser")
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, config);

                var added = new Lab2Expense.ViewModels.RegisterPostModel
                {
                    Email     = "[email protected]",
                    FirstName = "fdsfsdfs",
                    LastName  = "fdsfs",
                    Password  = "******",
                    Username  = "******"
                };
                var resultAdded  = usersService.Register(added);
                var resultAuth   = usersService.Authenticate(added.Username, added.Password);
                var resultDelete = usersService.Delete(resultAuth.Id);
                var resultNull   = usersService.Delete(238);
                Assert.IsNull(resultNull);

                Assert.AreEqual(true, resultDelete.isRemoved);
            }
        }
Example #5
0
        public void AuthenticateShouldLoginAUser()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLoginAUser))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var usersService = new UsersService(context, config);
                var added        = new Lab_2_webapi.ViewModels.RegisterPostModel

                {
                    FirstName = "pomihai2",
                    LastName  = "popmihai2",
                    Username  = "******",
                    Email     = "x@x",
                    Password  = "******"
                };
                var result        = usersService.Register(added);
                var authenticated = new Lab_2_webapi.ViewModels.LoginPostModel
                {
                    Username = "******",
                    Password = "******"
                };
                var authresult = usersService.Authenticate(added.Username, added.Password);

                Assert.IsNotNull(authresult);
                Assert.AreEqual(1, authresult.Id);
                Assert.AreEqual(authenticated.Username, authresult.Username);
            }
        }
Example #6
0
        public void ValidGetHistoryRole()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidGetHistoryRole))// "ValidRegisterShouldCreateANewUser")
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, config);
                var added        = new Lab2Expense.ViewModels.RegisterPostModel
                {
                    Email     = "[email protected]",
                    FirstName = "fdsfsdfs",
                    LastName  = "fdsfs",
                    Password  = "******",
                    Username  = "******"
                };
                var resultAdded          = usersService.Register(added);
                var resultAuthentificate = usersService.Authenticate(added.Username, added.Password);



                var userRole = usersService.GetHistoryRoles(resultAuthentificate.Id);
                Assert.IsNotNull(userRole);
            }
        }
Example #7
0
        public void AuthenticateShouldLoginSuccessfullyTheUser()
        {
            var options = new DbContextOptionsBuilder <UsersDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLoginSuccessfullyTheUser))// "ValidUsernameAndPasswordShouldLoginSuccessfully")
                          .Options;

            using (var context = new UsersDbContext(options))
            {
                var usersService = new UsersService(context, config);

                var added = new examen_web_application.Viewmodels.RegisterPostModel

                {
                    Email     = "*****@*****.**",
                    FirstName = "ovi1",
                    LastName  = "ovi1",
                    Password  = "******",
                    Username  = "******",
                };
                usersService.Register(added);
                var loggedIn = new examen_web_application.Viewmodels.LoginPostModel
                {
                    Username = "******",
                    Password = "******"
                };
                var authoresult = usersService.Authenticate(added.Username, added.Password);

                Assert.IsNotNull(authoresult);
                Assert.AreEqual(1, authoresult.Id);
                Assert.AreEqual(loggedIn.Username, authoresult.Username);
            }
        }
Example #8
0
        public void AuthenticateShouldLogTheUser()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLogTheUser))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var usersService = new UsersService(context, config);
                var added        = new TaskAgendaProj.ViewModels.RegisterPostModel
                {
                    Email     = "*****@*****.**",
                    FirstName = "FirstNameTest3",
                    LastName  = "LastNameTest3",
                    Password  = "******",
                    Username  = "******"
                };
                var result = usersService.Register(added);

                var authenticate = new TaskAgendaProj.ViewModels.LoginPostModel
                {
                    Username = "******",
                    Password = "******",
                };

                var authenticateresult = usersService.Authenticate(added.Username, added.Password);

                Assert.IsNotNull(authenticateresult);
                Assert.AreEqual(1, authenticateresult.Id);
                Assert.AreEqual(authenticate.Username, authenticateresult.Username);
            }
        }
        public void AuthenticateShouldLoginSuccessfullyTheUser()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLoginSuccessfullyTheUser))// "ValidUsernameAndPasswordShouldLoginSuccessfully")
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var usersService = new UsersService(context, config);

                var added = new LabII.DTOs.RegisterPostModel

                {
                    Email     = "*****@*****.**",
                    FirstName = "Petre",
                    LastName  = "Popica",
                    Password  = "******",
                    Username  = "******",
                };
                usersService.Register(added);
                var loggedIn = new LabII.DTOs.LoginPostModel
                {
                    Username = "******",
                    Password = "******"
                };
                var authoresult = usersService.Authenticate(added.Username, added.Password);

                Assert.IsNotNull(authoresult);
                Assert.AreEqual(1, authoresult.Id);
                Assert.AreEqual(loggedIn.Username, authoresult.Username);
                //Assert.AreEqual(loggedIn.Password, UsersService.);
            }
        }
Example #10
0
        public void InvalidAuthenticationShouldNotAuthenticateUserWithInvalidPassword()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(InvalidAuthenticationShouldNotAuthenticateUserWithInvalidPassword))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var userService = new UsersService(context, config);

                var addedUser = new Laborator2.ViewModels.RegisterPostModel
                {
                    Email     = "[email protected]",
                    FirstName = "ggg",
                    LastName  = "hhh",
                    Password  = "******",
                    Username  = "******"
                };

                var addResult = userService.Register(addedUser);

                Assert.IsNotNull(addResult);
                Assert.AreEqual(addedUser.Username, addResult.Username);

                var authenticate = new Laborator2.ViewModels.UserGetModel
                {
                    Email    = "[email protected]",
                    Username = "******"
                };

                var result = userService.Authenticate(addedUser.Username, "11111111");

                Assert.AreEqual(null, result);
            }
        }
Example #11
0
        public void AuthenticateShouldLogInAUser()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLogInAUser))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var usersService = new UsersService(context, config);
                var added        = new LabIV.DTO.RegisterPostDTO

                {
                    FirstName = "Julia",
                    LastName  = "Bush",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };
                var result        = usersService.Register(added);
                var authenticated = new LabIV.DTO.LoginPostDTO
                {
                    Username = "******",
                    Password = "******"
                };
                var authresult = usersService.Authenticate(added.Username, added.Password);

                Assert.IsNotNull(authresult);
                Assert.AreEqual(1, authresult.Id);
                Assert.AreEqual(authenticated.Username, authresult.Username);
            }
        }
        public void AddMessageToRoomTestAsync()
        {
            var user = new UserAccountDTO()
            {
                Login    = "******",
                Password = "******"
            };
            var user1 = new User()
            {
                Login    = "******",
                Password = "******"
            };

            var appContext = new Mock <ApplicationContext>();
            var map        = new Mock <IMapper>();
            var email      = new Mock <IEmailService>();

            var  service = new UsersService(map.Object, appContext.Object, email.Object);
            User user11  = null;
            var  mock    = new Mock <IUsersService>();

            mock.Setup(x => x.Authenticate(user.Login, user.Password)).ReturnsAsync(user);
            mock.Setup(x => x.GetUserByLogin(user.Login)).ReturnsAsync(user1);
            mock.Setup(x => x.GetUserByLogin(user1.Login)).ReturnsAsync(user11);

            var result = service.Authenticate(user.Login, user.Password);

            Assert.Equal(user.Login, user.Login);
            mock.Verify();
        }
        public void AuthenticateShouldLogTheRegisteredUser()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLogTheRegisteredUser))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var usersService = new UsersService(context, config);
                var added        = new Lab3Movie.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "lastName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };
                var result = usersService.Register(added);

                var authenticated = new Lab3Movie.ViewModels.LoginPostModel
                {
                    Username = "******",
                    Password = "******"
                };
                var authresult = usersService.Authenticate(added.UserName, added.Password);

                Assert.IsNotNull(authresult);
                Assert.AreEqual(1, authresult.Id);
                Assert.AreEqual(authenticated.Username, authresult.UserName);
            }
        }
Example #14
0
        public void InvalidAuthentificationShouldNotAuthenticateUserWithInvalidPassword()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(InvalidAuthentificationShouldNotAuthenticateUserWithInvalidPassword))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var userService = new UsersService(context, config);

                var addedUser = new WebApplication3.ViewModels.RegisterPostModel
                {
                    Email     = "*****@*****.**",
                    FirstName = "ovi1",
                    LastName  = "todea1",
                    Password  = "******",
                    Username  = "******"
                };

                var addResult = userService.Register(addedUser);

                Assert.IsNotNull(addResult);
                Assert.AreEqual(addedUser.Username, addResult.UserName);

                var authentificate = new WebApplication3.ViewModels.LoginGetModel
                {
                    Email    = "*****@*****.**",
                    Username = "******"
                };

                var result = userService.Authenticate(addedUser.Username, "012345");

                Assert.AreEqual(null, result);
            }
        }
Example #15
0
        public void ValidAuthenticate()
        {
            var options = new DbContextOptionsBuilder <MovieDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidAuthenticate))// "ValidRegisterShouldCreateANewUser")
                          .Options;

            using (var context = new MovieDbContext(options))
            {
                var usersService = new UsersService(context, config);
                var added        = new Seminar2.ViewModel.RegisterPostModel
                {
                    Email     = "[email protected]",
                    FirstName = "fdsfsdfs",
                    LastName  = "fdsfs",
                    Password  = "******",
                    Username  = "******"
                };
                usersService.Register(added);
                var loggedIn = new Seminar2.ViewModel.LoginPostModel
                {
                    Password = "******",
                    Username = "******"
                };
                var result = usersService.Authenticate(added.Username, added.Password);

                Assert.IsNotNull(result);
                Assert.AreEqual(1, result.Id);
                Assert.AreEqual(loggedIn.Username, result.Username);
                Assert.IsNotNull(result.Token);
            }
        }
Example #16
0
        public void GetValidCurrentRole()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetValidCurrentRole))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var          registerValidator    = new RegisterValidator();
                var          validator            = new UserRoleValidator();
                var          user_userRoleService = new User_UserRoleService(validator, context);
                UsersService usersService         = new UsersService(context, registerValidator, config, user_userRoleService);
                var          added = new RegisterPostModel
                {
                    Email     = "*****@*****.**",
                    FirstName = "User",
                    LastName  = "Test",
                    Password  = "******",
                    Username  = "******"
                };
                var resultAdded          = usersService.Register(added);
                var resultAuthentificate = usersService.Authenticate(added.Username, added.Password);

                var user = context.Users.FirstOrDefault(user_userRole => user_userRole.Id == resultAuthentificate.Id);

                var userRole = usersService.GetCurrentUserRole(user);

                Assert.IsNull(user);
                //Assert.AreEqual("Regular", userRole.Name);
            }
        }
        public void AuthenticateShouldLoginAUser()
        {
            var options = new DbContextOptionsBuilder <UsersDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLoginAUser))
                          .Options;

            using (var context = new UsersDbContext(options))
            {
                var usersService = new UsersService(context, registerValidator, config);
                var added        = new PostUserDto

                {
                    FirstName = "test_first",
                    LastName  = "test_last",
                    Username  = "******",
                    Password  = "******"
                };
                var result        = usersService.Register(added);
                var authenticated = new PostLoginDto
                {
                    Username = "******",
                    Password = "******"
                };

                var authresult = usersService.Authenticate(added.Username, added.Password);
                Assert.IsNotNull(authresult);
                Assert.AreEqual(authenticated.Username, authresult.Username);
            }
        }
Example #18
0
        public async Task Authenticate_WithGivenEmailAndPassword_ReturnsUser()
        {
            //Arrange
            _mockTokenGenerator
            .Setup(x => x.Generate(It.IsAny <AppSettings>(), It.IsAny <UserTokenDataModel>()))
            .Returns(GetTokenString());

            _mockMediator
            .Setup(x => x.Send(It.IsAny <GetUserByEmailAndPasswordQuery>(), CancellationToken.None))
            .Returns(Task.FromResult(GetUserGetViewModel()));

            //Act
            var user = await _usersService.Authenticate("*****@*****.**", "1234566");

            //Assert
            user.Should().NotBeNull();
        }
Example #19
0
        public void Authenticate_Valid_User()
        {
            var email    = "*****@*****.**";
            var password = "******";

            var user = UsersService.Authenticate(email, password);

            Assert.IsNotNull(user);
        }
        public IActionResult Authenticate([FromBody] AuthenticateModel model)
        {
            var user = _userService.Authenticate(model.Username, model.Password);

            if (user == null)
            {
                return(NotFound(new { message = "Usuário ou senha inválidos" }));
            }

            return(Ok(user));
        }
        public async Task <ActionResult <LoginOutput> > Authenticate([FromBody] LoginInput model)
        {
            var user = await _usersService.Authenticate(model);

            if (user == null)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            return(Ok(user));
        }
Example #22
0
        public async Task <ActionResult> Login([FromBody] UserCredentials userCredentials)
        {
            var token = usersService.Authenticate(userCredentials.Email, userCredentials.Password);

            if (token == null)
            {
                return(Unauthorized());
            }

            var user = await usersService.GetByEmailAsync(userCredentials.Email);

            return(Ok(new { token, user.Id, user.post }));
        }
Example #23
0
        [HttpPost("authenticate")]//ROUTE
        public IActionResult Authenticate([FromBody] UserDto userDto)
        {
            try
            {
                var userAdminView = UsersService.Authenticate(userDto.Username, userDto.Password);

                if (userAdminView.User == null)
                {
                    return(BadRequest(new { message = "Username or password is incorrect" }));
                }
                if (!userAdminView.User.IsAccountActive)
                {
                    return(BadRequest(new { message = "User is inactive" }));
                }

                var tokenHandler    = new JwtSecurityTokenHandler();
                var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new Claim[]
                    {
                        new Claim(ClaimTypes.Name, userAdminView.User.IdUser.ToString())
                    }),
                    Expires            = DateTime.UtcNow.AddDays(7),
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                };
                var token       = tokenHandler.CreateToken(tokenDescriptor);
                var tokenString = tokenHandler.WriteToken(token);

                // return basic user info (without password) and token to store client side
                return(Ok(new
                {
                    IdUser = userAdminView.User.IdUser,
                    Username = userAdminView.User.Username,
                    Email = userAdminView.User.Email,
                    FirstName = userAdminView.User.FirstName,
                    LastName = userAdminView.User.LastName,
                    NrMatricol = userAdminView.User.NrMatricol,
                    IsAccountActive = userAdminView.User.IsAccountActive,
                    Role = userAdminView.Role.RoleName,
                    College = userAdminView.Colleges[0].IdCollege,
                    Department = userAdminView.Departments[0].IdDepartment,
                    Token = tokenString
                }));
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
Example #24
0
        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);
            }
        }
Example #25
0
        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);
            }
        }
Example #26
0
        public void GetCurrentUser()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetCurrentUser))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var          registervalidator    = new RegisterValidator();
                var          validator            = new UserRoleValidator();
                var          user_userRoleService = new User_UserRoleService(validator, context);
                UsersService usersService         = new UsersService(context, registervalidator, config, user_userRoleService);

                //UsersController usersController = new UsersController(usersService, null);
                //usersController.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext();
                //usersController.ControllerContext.HttpContext = new DefaultHttpContext();
                // usersController.ControllerContext.HttpContext.Items.Add("user-Name", "Ghita");
                UserRole addUserRoleRegular = new UserRole
                {
                    Name        = "Regular",
                    Description = "Creat pentru testare"
                };
                context.UserRoles.Add(addUserRoleRegular);
                context.SaveChanges();


                var added = new RegisterPostModel
                {
                    Email     = "*****@*****.**",
                    FirstName = "User",
                    LastName  = "Test",
                    Password  = "******",
                    Username  = "******"
                };
                var resultAdded          = usersService.Register(added);
                var resultAuthentificate = usersService.Authenticate(added.Username, added.Password);

                var user = context.Users.FirstOrDefault(user_userRole => user_userRole.Id == resultAuthentificate.Id);

                //User userRole = usersService.GetCurrentUser(user);

                Assert.IsNotNull(user);
                //Assert.AreEqual(resultAuthentificate, userRole.Id);
            }
        }
        public void AuthenticateShouldLogInAUser()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLogInAUser))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var validator           = new RegisterValidator();
                var validatorUser       = new UserRoleValidator();
                var userUserRoleService = new UserUserRolesService(validatorUser, context);
                var usersService        = new UsersService(context, validator, null, userUserRoleService, config);

                UserRole addUserRoleRegular = new UserRole
                {
                    Name        = "Regular",
                    Description = "For testing..."
                };
                context.UserRoles.Add(addUserRoleRegular);
                context.SaveChanges();


                var added = new LabIV.DTO.RegisterPostDTO

                {
                    FirstName = "Julia",
                    LastName  = "Bush",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };
                var result        = usersService.Register(added);
                var authenticated = new LabIV.DTO.LoginPostDTO
                {
                    Username = "******",
                    Password = "******"
                };
                var authresult = usersService.Authenticate(added.Username, added.Password);

                Assert.IsNotNull(authresult);
                Assert.AreEqual(1, authresult.Id);
                Assert.AreEqual(authenticated.Username, authresult.Username);
            }
        }
Example #28
0
        public void AuthenticateShouldLogTheUser()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLogTheUser))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var          registerValidator    = new RegisterValidator();
                var          validator            = new UserRoleValidator();
                var          user_userRoleService = new User_UserRoleService(validator, context);
                UsersService usersService         = new UsersService(context, registerValidator, config, user_userRoleService);
                var          userRole             = new UserRole
                {
                    Name = "Regular"
                };
                context.UserRoles.Add(userRole);
                context.SaveChanges();
                var added = new RegisterPostModel()
                {
                    Email     = "*****@*****.**",
                    FirstName = "User",
                    LastName  = "Test",
                    Password  = "******",
                    Username  = "******"
                };
                var result = usersService.Register(added);

                var authenticate = new LoginPostModel()
                {
                    Username = added.Username,
                    Password = added.Password,
                };
                var authenticateresult = usersService.Authenticate(added.Username, added.Password);

                Assert.IsNotNull(authenticateresult);
                //var isNotRegistered = usersService.Authenticate("ABCD", "ABCDERF");
                //Assert.IsNotNull(authenticateresult.Token);
                //Assert.IsNotNull(isNotRegistered);
            }
        }
Example #29
0
        public void ValidAuthenticationShouldAuthenticateValidUser()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidAuthenticationShouldAuthenticateValidUser))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var userService = new UsersService(context, config);

                var addedUser = new Laborator2.ViewModels.RegisterPostModel
                {
                    Email     = "[email protected]",
                    FirstName = "ddd",
                    LastName  = "eee",
                    Password  = "******",
                    Username  = "******"
                };

                var addResult = userService.Register(addedUser);

                Assert.IsNotNull(addResult);
                Assert.AreEqual(addedUser.Username, addResult.Username);

                var authenticate = new Laborator2.ViewModels.UserGetModel
                {
                    Email    = "[email protected]",
                    Username = "******"
                };

                var result = userService.Authenticate(addedUser.Username, addedUser.Password);

                Assert.IsNotNull(result);

                Assert.AreEqual(authenticate.Username, result.Username);
            }
        }
Example #30
0
        public void ValidGetCurentRole()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidGetCurentRole))// "ValidRegisterShouldCreateANewUser")
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, config);
                var added        = new Lab2Expense.ViewModels.RegisterPostModel
                {
                    Email     = "[email protected]",
                    FirstName = "fdsfsdfs",
                    LastName  = "fdsfs",
                    Password  = "******",
                    Username  = "******"
                };

                var userRoleService = new UserRoleService(context);
                var addedRole       = new Lab2Expense.ViewModels.UserRolePostModel
                {
                    Name        = "Regular",
                    Description = "jskds"
                };
                var result = userRoleService.Create(addedRole);

                var resultAdded          = usersService.Register(added);
                var resultAuthentificate = usersService.Authenticate(added.Username, added.Password);

                var user = context.Users.FirstOrDefault(u => u.Id == resultAuthentificate.Id);

                var userRole = usersService.GetCurrentUserRole(user);
                Assert.AreEqual("Regular", userRole.Name);
            }
        }