コード例 #1
0
        public void GetByIdShouldReturnAnValidUser()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnAnValidUser))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var regValidator = new RegisterValidator();
                var crValidator  = new CreateValidator();
                var usersService = new UsersService(context, regValidator, crValidator, null, config);
                var added1       = new LabII.DTOs.RegisterPostModel
                {
                    FirstName = "firstName",
                    LastName  = "lastName",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

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

                Assert.NotNull(userById);
                Assert.AreEqual("firstName", userById.FirstName);
            }
        }
コード例 #2
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 LabII.DTOs.RegisterPostModel
                {
                    FirstName = "Catalin",
                    LastName  = "Albulescu",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                };
                var result = usersService.Register(added);

                var authenticated = new LabII.DTOs.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);
            }
        }
コード例 #3
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 LabII.DTOs.RegisterPostModel
                {
                    FirstName = "firstName",
                    LastName  = "lastName",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };
                var result = usersService.Register(added);

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


                //usersService.GetCurentUser(httpContext);

                Assert.IsNotNull(authresult);
            }
        }
コード例 #4
0
        public void ValidGetAllShouldReturnAllRegisteredUsers()
        {
            var options = new DbContextOptionsBuilder <ExpensesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(ValidGetAllShouldReturnAllRegisteredUsers))
                          .Options;

            using (var context = new ExpensesDbContext(options))
            {
                var regValidator = new RegisterValidator();
                var crValidator  = new CreateValidator();
                var usersService = new UsersService(context, regValidator, crValidator, null, config);
                var added1       = new LabII.DTOs.RegisterPostModel
                {
                    FirstName = "Catalin",
                    LastName  = "Albulescu",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                };
                var added2 = new LabII.DTOs.RegisterPostModel
                {
                    FirstName = "testfname",
                    LastName  = "testsname",
                    Username  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };
                usersService.Register(added1);
                usersService.Register(added2);

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

                Assert.NotZero(numberOfElements);
                Assert.AreEqual(2, numberOfElements);
            }
        }