Exemplo n.º 1
0
        public void GetCurrentRoleShouldReturnCurrentRoleForUser()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetCurrentRoleShouldReturnCurrentRoleForUser))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                context.Roles.Add(roleAdmin);
                context.SaveChanges();

                var userService = new UserService(context, config, registerValidator, roleValidator);

                userService.Register(user);

                var addedUser = context.Users.Last();

                RoleIdModel roleAdminModel = new RoleIdModel
                {
                    RoleId = roleAdmin.Id
                };

                userService.GiveNewRoleToUser(addedUser.Id, roleAdminModel);

                Role currentRoleForUser = userService.GetCurrentRoleForUser(addedUser);

                Assert.AreEqual(currentRoleForUser, roleAdmin);
            }
        }
Exemplo n.º 2
0
        public ErrorsCollection GiveNewRoleToUser(int userId, RoleIdModel roleIdModel)
        {
            var errors = roleValidator.ValidateRole(roleIdModel, context);

            if (errors != null)
            {
                return(errors);
            }

            var now = DateTime.Now;

            UserRole newUserRole = new UserRole
            {
                UserId    = userId,
                RoleId    = roleIdModel.RoleId,
                StartTime = now,
                EndTime   = null
            };

            UserRole userRoleForUser = context.UserRoles.FirstOrDefault(r => r.UserId == userId && r.EndTime == null);

            userRoleForUser.EndTime = now;

            context.UserRoles.Add(newUserRole);
            context.SaveChanges();
            return(null);
        }
Exemplo n.º 3
0
        public ErrorsCollection ValidateRole(RoleIdModel roleIdModel, ExpensesDbContext context)
        {
            ErrorsCollection errors = new ErrorsCollection
            {
                Entity = nameof(RolePostModel)
            };

            UserRole role = context.UserRoles.FirstOrDefault(r => r.Id == roleIdModel.RoleId);

            if (role == null)
            {
                errors.ErrorMessages.Add($"The role {roleIdModel.RoleId} does not exist!");
            }
            return(null);
        }
Exemplo n.º 4
0
        public IActionResult GiveNewRoleToUser(int userId, [FromBody] RoleIdModel roleIdModel)
        {
            User user = _userService.GetUserById(userId);

            if (user == null)
            {
                return(NotFound());
            }

            //userRoleModel.UserId = userId;

            var errors = _userService.GiveNewRoleToUser(userId, roleIdModel);

            if (errors != null)
            {
                return(BadRequest(errors));
            }

            return(Ok());
        }
Exemplo n.º 5
0
        public void GiveRoleShouldGiveNewRoleToUser()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GiveRoleShouldGiveNewRoleToUser))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var userService = new UserService(context, config, registerValidator, roleValidator);

                var user = new RegisterPostModel
                {
                    Email     = "*****@*****.**",
                    FirstName = "alina",
                    LastName  = "demian",
                    Password  = "******",
                    Username  = "******"
                };

                userService.Register(user);

                var addedUser = context.Users.Last();

                var role = new RoleIdModel
                {
                    RoleId = 5
                };

                var result = userService.GiveNewRoleToUser(addedUser.Id, role);

                Assert.IsNull(result);

                var roleForUser = context.UserRoles.Last();

                Assert.AreEqual(role.RoleId, roleForUser.RoleId);
            }
        }