Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        public void GetAllShouldReturnAllRegisteredUsers()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetAllShouldReturnAllRegisteredUsers))
                          .Options;

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

                int numberOfElements = usersService.GetAll().Count();

                Assert.NotZero(numberOfElements);
                Assert.AreEqual(2, numberOfElements);
            }
        }
Exemplo n.º 3
0
        public void InvalidRegisterShouldReturnErrorsCollection()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(InvalidRegisterShouldReturnErrorsCollection))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, null, config);
                var added        = new Lab3Movie.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "lastName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"   //invalid password should invalidate register
                };

                var result = usersService.Register(added);

                Assert.IsNotNull(result);
                Assert.AreEqual(1, result.ErrorMessages.Count());
            }
        }
Exemplo n.º 4
0
        public void ValidRegisterShouldCreateNewUser()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidRegisterShouldCreateNewUser))// "ValidRegisterShouldCreateANewUser")
                          .Options;

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

                var result = usersService.Register(added);

                Assert.IsNull(result);
                Assert.AreEqual(added.UserName, context.Users.FirstOrDefault(u => u.Id == 1).Username);
                Assert.AreEqual(1, context.UserUserRole.FirstOrDefault(uur => uur.Id == 1).UserId);
            }
        }
Exemplo n.º 5
0
        public void DeleteShouldEmptyTheDb()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(DeleteShouldEmptyTheDb))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, null, config);
                var added1       = new Lab3Movie.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "firstName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

                var userCreated = usersService.Create(added1);

                Assert.IsNull(userCreated);
                Assert.AreEqual(1, usersService.GetAll().Count());

                var userDeleted = usersService.Delete(1);

                Assert.NotNull(userDeleted);
                Assert.AreEqual(0, usersService.GetAll().Count());
            }
        }
Exemplo n.º 6
0
        public void CreateShouldReturnNullIfValidUserGetModel()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldReturnNullIfValidUserGetModel))
                          .Options;

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

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

                var added1 = new Lab3Movie.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "firstName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

                var userCreated = usersService.Create(added1);

                Assert.IsNull(userCreated);
            }
        }
Exemplo n.º 7
0
        public void GetByIdShouldReturnAnValidUser()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnAnValidUser))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, null, config);
                var added1       = new Lab3Movie.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "firstName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

                usersService.Register(added1);
                var userById = usersService.GetById(1);

                Assert.NotNull(userById);
                Assert.AreEqual("firstName1", userById.FirstName);
            }
        }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
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 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 = "******"
                };
                var authresult = usersService.Authenticate(added.UserName, added.Password);

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

                Assert.IsNotNull(authresult);
            }
        }
Exemplo n.º 10
0
        public void ValidRegisterShouldCreateANewUser()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidRegisterShouldCreateANewUser))// "ValidRegisterShouldCreateANewUser")
                          .Options;

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

                Assert.IsNotNull(result);
                Assert.AreEqual(added.Username, result.Username);
            }
        }
Exemplo n.º 11
0
        public void UpsertShouldModifyFildsValues()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpsertShouldModifyFildsValues))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, null, config);
                var added22      = new Lab3Movie.ViewModels.RegisterPostModel
                {
                    FirstName = "Nume",
                    LastName  = "Prenume",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

                usersService.Create(added22);

                var updated = new Lab3Movie.ViewModels.UserPostModel
                {
                    FirstName = "Alin",
                    LastName  = "Popescu",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

                var userUpdated = usersService.Upsert(1, updated);

                Assert.NotNull(userUpdated);
                Assert.AreEqual("Alin", userUpdated.FirstName);
                Assert.AreEqual("Popescu", userUpdated.LastName);
            }
        }