public async Task UpdateShouldUpdateRoles()
        {
            var role = new Role
            {
                Name = _random.Next().ToString()
            };

            _roleList.Add(role);

            var user = new User
            {
                Id    = _random.Next(),
                Roles = new List <UserRole>
                {
                    new UserRole()
                }
            };

            _userList.Add(user);

            var model = new UdpateUserModel
            {
                LastName  = _random.Next().ToString(),
                FirstName = _random.Next().ToString(),
                Roles     = new[] { role.Name }
            };

            var result = await _query.Update(user.Id, model);

            result.Roles.Should().HaveCount(1);
            result.Roles.Should().Contain(x => x.User == result && x.Role == role);
        }
Example #2
0
        public async Task <User> Update(int id, UdpateUserModel updateUserModel)
        {
            var isUsrExist = GetUserQuery().Any(x => x.Id == id);

            if (!isUsrExist)
            {
                throw new NotFoundException("USer does not exist");
            }

            var usrToUpdate = GetUserQuery().Where(x => x.Id == id).FirstOrDefault();

            //usrToUpdate.Roles.Clear();

            //var isRoleExist = usrToUpdate.Roles.Any(x => x.Role == usrToUpdate.Roles);
            //if (!isRoleExist)
            //{
            //    foreach (var r in updateUserModel.Roles)
            //    {
            //        var role = _uow.Query<UserRole>().Where(x => x.Role.Name == r).FirstOrDefault();
            //        usrToUpdate.Roles.Add(role);
            //    }
            //}

            // add role
            AddUserRoles(usrToUpdate, updateUserModel.Roles);

            // update details
            usrToUpdate.Username  = updateUserModel.Username;
            usrToUpdate.LastName  = updateUserModel.LastName;
            usrToUpdate.FirstName = updateUserModel.FirstName;

            _uow.Update <User>(usrToUpdate);

            await _uow.CommitAsync();

            return(usrToUpdate);
        }
        public async Task UpdateShouldUpdateUserFields()
        {
            var user = new User {
                Id = _random.Next()
            };

            _userList.Add(user);

            var model = new UdpateUserModel
            {
                Username  = _random.Next().ToString(),
                LastName  = _random.Next().ToString(),
                FirstName = _random.Next().ToString(),
            };

            var result = await _query.Update(user.Id, model);

            result.Should().Be(user);
            result.Username.Should().Be(model.Username);
            result.LastName.Should().Be(model.LastName);
            result.FirstName.Should().Be(model.FirstName);

            _uow.Verify(x => x.CommitAsync());
        }