Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        public void RemoveAUSer()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(RemoveAUSer))
                          .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          expected             = new RegisterPostModel()

                {
                    Email     = "*****@*****.**",
                    FirstName = "User",
                    LastName  = "Test",
                    Password  = "******",
                    Username  = "******"
                };
                var resultAdded = usersService.Create(expected);

                Assert.NotNull(expected);
                Assert.AreEqual(1, usersService.GetAll().Count());

                var userDeleted = usersService.Delete(1);

                //Assert.NotNull(userDeleted);
                //Assert.AreEqual(userDeleted.FirstName, expected.FirstName);

                //Assert.AreEqual(0, usersService.GetAll().Count());
            }
        }
Esempio n. 3
0
        public void GetByIdShouldReturnAValidUser()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnAValidUser))
                          .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
                {
                    Id        = 1,
                    Email     = "*****@*****.**",
                    FirstName = "User",
                    LastName  = "Test",
                    Password  = "******",
                    Username  = "******"
                };
                usersService.Register(added);
                var userById = usersService.GetById(added.Id);

                Assert.IsNotNull(userById);
                Assert.AreEqual(1, userById.Id);
                Assert.AreEqual("User", userById.FirstName);
            }
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
0
        public void GetCurentUserShouldReturnAccesToKlaims()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetCurentUserShouldReturnAccesToKlaims))
                          .Options;

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

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

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

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

                //nu stiu sa instantiez un HttpContext
                //usersService.GetCurentUser(httpContext);

                Assert.IsNotNull(authresult);
            }
        }
Esempio n. 6
0
        public void GetUserRoleNameByIdShouldReturnTheUserName()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetUserRoleNameByIdShouldReturnTheUserName))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator            = new UserRoleValidator();
                var user_userRoleService = new User_UserRoleService(validator, context);

                User added = new User
                {
                    Email     = "*****@*****.**",
                    FirstName = "User",
                    LastName  = "Test",
                    Password  = "******",
                    Username  = "******"
                };
                context.Users.Add(added);

                UserRole userRoleAdded = new UserRole
                {
                    Name        = "NewUser",
                    Description = "New user for test"
                };
                context.UserRoles.Add(userRoleAdded);
                context.SaveChanges();

                context.User_UserRoles.Add(new User_UserRole
                {
                    User      = added,
                    UserRole  = userRoleAdded,
                    StartTime = DateTime.Now,
                    EndTime   = null
                });
                context.SaveChanges();

                string userRoleName = user_userRoleService.GetUserRoleNameById(added.Id);
                Assert.AreEqual("NewUser", userRoleName);
                Assert.AreEqual("User", added.FirstName);
            }
        }
Esempio n. 7
0
        public void UpsertShouldModifyFieldsValues()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpsertShouldModifyFieldsValues))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var          user = new User();
                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          expected             = new RegisterPostModel
                {
                    FirstName = "Ion",
                    LastName  = "Ion",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                    //UserRoles = "UserManager"
                };

                usersService.Create(expected);

                var updated = new UserPostModel
                {
                    FirstName = "ana",
                    LastName  = "ana",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                    // UserRoles = "Admin"
                };

                var userUpdated = usersService.Upsert(6, updated);  //id 6 ca daca e 1 da as no tracking

                Assert.NotNull(userUpdated);
                Assert.AreEqual("ana", userUpdated.FirstName);
                Assert.AreEqual("ana", userUpdated.LastName);
            }
        }
Esempio n. 8
0
        public void ValidRegisterShouldCreateANewUser()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidRegisterShouldCreateANewUser))
                          .Options;
            var registerValidator = new RegisterValidator();

            using (var context = new ExpensesDbContext(options))
            {
                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()
                {
                    FirstName = "User",
                    LastName  = "Test",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                };

                //ErrorsCollection errorsCollection = new ErrorsCollection
                //{
                //    Entity = nameof(RegisterPostModel),
                //    ErrorMessages = new List<string> { "The password must contain at least two digits!" }

                //};

                var addedResult = usersService.Register(added);
                //context.SaveChanges();
                Assert.IsNull(addedResult);
                //Assert.AreEqual(errorsCollection, addedResult);
                //Assert.AreEqual(added, addedResult);
            }
        }
Esempio n. 9
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);
            }
        }
Esempio n. 10
0
        public void GetByIdShouldReturnAValidUser_UserRole()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnAValidUser_UserRole))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator = new UserRoleValidator();
                User_UserRoleService user_userRoleService = new User_UserRoleService(validator, context);
                var added = new Labo2.ViewModels.User_UserRolePostModel
                {
                    UserId       = 1,
                    UserRoleName = "Regular",
                    StartTime    = DateTime.Now,
                    EndTime      = null
                };

                var userById = user_userRoleService.GetById(added.UserId);
                Assert.IsNotNull(userById);
            }
        }
Esempio n. 11
0
        public void GetAllShouldReturnAllRegisteredUsers()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetAllShouldReturnAllRegisteredUsers))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator            = new UserRoleValidator();
                var user_userRoleService = new User_UserRoleService(validator, context);
                var registerValidator    = new RegisterValidator();
                var usersService         = new UsersService(context, registerValidator, config, user_userRoleService);
                var added = new RegisterPostModel()
                {
                    FirstName = "User",
                    LastName  = "Test",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                };
                var added2 = new RegisterPostModel()
                {
                    FirstName = "User2",
                    LastName  = "Test2",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                };
                //var result = usersService.GetAll();
                //Assert.IsNotNull(result);
                usersService.Create(added);
                usersService.Create(added2);
                int number = usersService.GetAll().Count();
                Assert.IsNotNull(number);
                Assert.AreEqual(2, number);
            }
        }
Esempio n. 12
0
        public void CreateShouldAddTheUserUserRole()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldAddTheUserUserRole))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var validator            = new UserRoleValidator();
                var userUserRolesService = new User_UserRoleService(validator, context);

                User userToAdd = new User
                {
                    Email          = "*****@*****.**",
                    LastName       = "Ion",
                    FirstName      = "POpescu",
                    Password       = "******",
                    DataRegistered = DateTime.Now,
                    User_UserRoles = new List <User_UserRole>()
                };
                context.Users.Add(userToAdd);

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

                context.User_UserRoles.Add(new User_UserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRoleRegular,
                    StartTime = DateTime.Parse("2019-06-13T00:00:00"),
                    EndTime   = null
                });
                context.SaveChanges();


                var u_urpm = new User_UserRolePostModel
                {
                    UserId       = userToAdd.Id,
                    UserRoleName = "Admin"
                };
                var result1 = userUserRolesService.Create(u_urpm);
                Assert.IsNotNull(result1);   //User role nu exista in baza de date dupa validare, ==> exista erori la validare


                var u_urpm1 = new User_UserRolePostModel
                {
                    UserId       = userToAdd.Id,
                    UserRoleName = "AdminDeTest"
                };
                var result2 = userUserRolesService.Create(u_urpm1);
                Assert.IsNull(result2);   //User role exista si se face upsert
            }
        }