Пример #1
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 UserUserRolesService(validatorUser, context);
                var usersService        = new UsersService(context, validator, userUserRoleService, config);

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

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

                var authenticated = new ExamenNet.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);
            }
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
        public void GetCurentUserShouldReturnAccesToKlaims()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetCurentUserShouldReturnAccesToKlaims))
                          .Options;

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

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

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

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

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

                Assert.IsNotNull(authresult);
            }
        }
Пример #4
0
        public void CreateShouldAddAnUserUserRole()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldAddAnUserUserRole))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var userUserRolesService = new UserUserRolesService(null, context);

                User userToAdd = new User
                {
                    Email            = "*****@*****.**",
                    LastName         = "Anca",
                    FirstName        = "Anca",
                    Password         = "******",
                    RegistrationDate = DateTime.Now,
                    UserUserRoles    = new List <UserUserRole>()
                };
                context.Users.Add(userToAdd);

                UserRole addUserRole = new UserRole
                {
                    Name        = "Newcomer",
                    Description = "..."
                };
                context.UserRoles.Add(addUserRole);
                context.SaveChanges();

                context.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRole,
                    StartTime = DateTime.Now,
                    EndTime   = null
                });
                context.SaveChanges();


                Assert.NotNull(userToAdd);
                Assert.AreEqual("Newcomer", addUserRole.Name);
                Assert.AreEqual("Anca", userToAdd.FirstName);
            }
        }
Пример #5
0
        public void GetUserRoleNameByIdShouldReturnUserRoleName()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetUserRoleNameByIdShouldReturnUserRoleName))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var userUserRolesService = new UserUserRolesService(null, context);

                User userToAdd = new User
                {
                    Email            = "*****@*****.**",
                    LastName         = "Marcus",
                    FirstName        = "Ana",
                    Password         = "******",
                    RegistrationDate = DateTime.Now,
                    UserUserRoles    = new List <UserUserRole>()
                };
                context.Users.Add(userToAdd);

                UserRole addUserRole = new UserRole
                {
                    Name        = "Newcomer",
                    Description = "A new guy..."
                };
                context.UserRoles.Add(addUserRole);
                context.SaveChanges();

                context.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRole,
                    StartTime = DateTime.Now,
                    EndTime   = null
                });
                context.SaveChanges();

                string userRoleName = userUserRolesService.GetUserRoleNameById(userToAdd.Id);
                Assert.AreEqual("Newcomer", userRoleName);
                Assert.AreEqual("Ana", userToAdd.FirstName);
            }
        }
Пример #6
0
        public void GetByIdShouldReturnUserRole()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnUserRole))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var userUserRolesService = new UserUserRolesService(null, context);

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

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

                context.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRole,
                    StartTime = DateTime.Now,
                    EndTime   = null
                });
                context.SaveChanges();

                var userUserRoleGetModels = userUserRolesService.GetHistoryRoleById(1);
                Assert.IsNotNull(userUserRoleGetModels.FirstOrDefaultAsync(uur => uur.EndTime == null));
            }
        }
Пример #7
0
        public void GetUserRoleNameByIdShouldReturnUserRoleName()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetUserRoleNameByIdShouldReturnUserRoleName))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var userUserRolesService = new UserUserRolesService(null, context);

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

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

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

                string userRoleName = userUserRolesService.GetUserRoleNameById(1);
                Assert.AreEqual("Regular", userRoleName);
            }
        }
Пример #8
0
        public void GetByIdShouldReturnUserRole()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnUserRole))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var userUserRolesService = new UserUserRolesService(null, context);

                User userToAdd = new User
                {
                    Email            = "*****@*****.**",
                    LastName         = "Anca",
                    FirstName        = "Anca",
                    Password         = "******",
                    RegistrationDate = DateTime.Now,
                    UserUserRoles    = new List <UserUserRole>()
                };
                context.Users.Add(userToAdd);

                UserRole addUserRole = new UserRole
                {
                    Name        = "Newcomer",
                    Description = "..."
                };
                context.UserRoles.Add(addUserRole);
                context.SaveChanges();

                context.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRole,
                    StartTime = DateTime.Now,
                    EndTime   = null
                });
                context.SaveChanges();

                var userUserRoleGet = userUserRolesService.GetById(1);
                Assert.IsNotNull(userUserRoleGet.FirstOrDefaultAsync(uurole => uurole.EndTime == null));
            }
        }
Пример #9
0
        public void UpsertShouldModifyFildsValues()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpsertShouldModifyFildsValues))
                          .Options;

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

                var added = new UserPostDTO()

                {
                    FirstName = "Julia",
                    LastName  = "Bush",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

                var IsIn = usersService.Create(added);

                var added1 = new UserPostDTO()

                {
                    FirstName = "Maya",
                    LastName  = "Bush",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };


                Assert.IsNull(IsIn);
                Assert.AreNotEqual(added.FirstName, added1.FirstName);
            }
        }
Пример #10
0
        public void CreateShouldAddTheUserUserRole()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldAddTheUserUserRole))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var validator            = new UserRoleValidator();
                var userUserRolesService = new UserUserRolesService(validator, context);

                User userToAdd = new User
                {
                    Email          = "*****@*****.**",
                    LastName       = "Ion",
                    FirstName      = "POpescu",
                    Password       = "******",
                    DataRegistered = DateTime.Now,
                    UserUserRoles  = new List <UserUserRole>()
                };
                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.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRoleRegular,
                    StartTime = DateTime.Parse("2019-06-13T00:00:00"),
                    EndTime   = null
                });
                context.SaveChanges();

                //sectiunea de schimbare valori invalidata de catre UserRoleValidator
                var uurpm = new UserUserRolePostModel
                {
                    UserId       = userToAdd.Id,
                    UserRoleName = "Admin"
                };
                var result1 = userUserRolesService.Create(uurpm);
                Assert.IsNotNull(result1);   //User role nu exista in baza de date dupa validare, ==> exista erori la validare

                //sectiunea de schimbare valori validata de catre UserRoleValidator
                var uurpm1 = new UserUserRolePostModel
                {
                    UserId       = userToAdd.Id,
                    UserRoleName = "AdminDeTest"
                };
                var result2 = userUserRolesService.Create(uurpm1);
                Assert.IsNull(result2);   //User role exista si se face upsert
            }
        }
Пример #11
0
        public void CreateShouldAddAnUserUserRole()
        {
            var options = new DbContextOptionsBuilder <TasksDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldAddAnUserUserRole))
                          .Options;

            using (var context = new TasksDbContext(options))
            {
                var validator            = new UserRoleValidator();
                var userUserRolesService = new UserUserRolesService(validator, context);

                User userToAdd = new User
                {
                    Email            = "*****@*****.**",
                    LastName         = "Marcus",
                    FirstName        = "Ana",
                    Password         = "******",
                    RegistrationDate = DateTime.Now,
                    UserUserRoles    = new List <UserUserRole>()
                };
                context.Users.Add(userToAdd);

                UserRole addUserRole = new UserRole
                {
                    Name        = "Newcomer",
                    Description = "A new guy..."
                };
                UserRole addUserRole1 = new UserRole
                {
                    Name        = "Roug",
                    Description = "A new guy?"
                };
                context.UserRoles.Add(addUserRole);
                context.UserRoles.Add(addUserRole1);
                context.SaveChanges();

                context.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRole,
                    StartTime = DateTime.Now,
                    EndTime   = null
                });
                context.SaveChanges();

                var newUUR = new UserUserRolePostDTO
                {
                    UserId       = userToAdd.Id,
                    UserRoleName = "Admin"
                };

                var result = userUserRolesService.Create(newUUR);
                Assert.IsNotNull(result);

                var newUUR1 = new UserUserRolePostDTO
                {
                    UserId       = userToAdd.Id,
                    UserRoleName = "Admin1"
                };
                var result1 = userUserRolesService.Create(newUUR1);
                Assert.NotNull(result1);
            }
        }