public async void Create_ReturnsCreated()
        {
            // Arrange
            var controller = new UserController(_userService.Object);
            var expected   = GetSampleUser();
            var sampleUser = new InputUserDto
            {
                Email     = expected.Email,
                FirstName = expected.FirstName,
                LastName  = expected.LastName,
                UserName  = expected.UserName
            };

            _userService.Setup(mock => mock.CreateAsync(It.IsAny <User>())).ReturnsAsync(expected);

            // Act
            var result = await controller.Create(sampleUser);

            // Assert
            _userService.Verify(mock => mock.CreateAsync(It.IsAny <User>()), Times.Once);

            Assert.IsType <CreatedAtActionResult>(result);
            var createdResult = result as CreatedAtActionResult;
            var user          = createdResult.Value as UserDto;

            Assert.Equal(expected.Id, user.Id);
            Assert.Equal(expected.FirstName, user.FirstName);
            Assert.Equal(expected.LastName, user.LastName);
            Assert.Equal(expected.UserName, user.UserName);
            Assert.Equal(expected.Email, user.Email);
        }
Example #2
0
        public async Task Create_ReturnsCreated()
        {
            // Arrange
            var sampleUser = new InputUserDto()
            {
                Email     = "*****@*****.**",
                FirstName = "testName",
                LastName  = "testLastName",
                UserName  = "******"
            };

            // Act
            var result = await _httpClient.PostAsync(ResouceUri, CreateContent(sampleUser));

            // Assert
            Assert.True(result.IsSuccessStatusCode);
            Assert.Equal(HttpStatusCode.Created, result.StatusCode);

            var user = JsonConvert.DeserializeObject <UserDto>(await result.Content.ReadAsStringAsync());

            Assert.Equal(sampleUser.FirstName, user.FirstName);
            Assert.Equal(sampleUser.LastName, user.LastName);
            Assert.Equal(sampleUser.UserName, user.UserName);
            Assert.Equal(sampleUser.Email, user.Email);
        }
Example #3
0
        public async Task <string> PutUser(InputUserDto input)
        {
            var usr = await _userManager.FindByIdAsync(input.Id);

            try
            {
                if (usr == null)
                {
                    return(null);
                }
                usr.Email              = input.Email;
                usr.NormalizedEmail    = input.Email.ToUpper();
                usr.PhoneNumber        = input.Phone;
                usr.UserName           = input.UserName;
                usr.NormalizedUserName = input.UserName.ToUpper();
                usr.FullName           = input.FullName;
                usr.IsAdmin            = input.IsAdmin;
                var IR = await _userManager.UpdateAsync(usr);

                if (!IR.Succeeded)
                {
                    return(IR.Errors.FirstOrDefault().Code);
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return("Success");
        }
Example #4
0
        public async Task <IActionResult> Update(Guid id, [FromBody] InputUserDto user)
        {
            // TODO: Fix validation attribute, it's not working as expected.
            if (user == null)
            {
                return(BadRequest());
            }

            var userToUpdate = new User
            {
                Id        = id,
                Email     = user.Email,
                FirstName = user.FirstName,
                LastName  = user.LastName,
                UserName  = user.UserName
            };

            var result = await _userService.UpdateAsync(userToUpdate);

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

            return(NoContent());
        }
Example #5
0
        public async Task <User> PutUser(InputUserDto input)
        {
            var usr = await _userManager.FindByIdAsync(input.Id);

            try
            {
                if (usr == null)
                {
                    return(null);
                }
                usr.Email                 = input.Email;
                usr.PhoneNumber           = input.Phone;
                usr.UserName              = input.UserName;
                usr.IsAdmin               = input.IsAdmin;
                usr.HasWrittingAccess     = input.HasWrittingAccess;
                usr.IsCustomer            = input.IsCustomer;
                _context.Entry(usr).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(usr);
        }
        public IHttpActionResult UserInfoGet([FromBody] InputUserDto request)
        {
            _defaultOpenApiClient = new DefaultOpenApiClient(_url, _appKey, _sysSecret, ApiGlobalConst.MethodConst.OpenApiUserInfoGet);
            _defaultOpenApiClient.SetBizContent(request.ToJson());
            var resultHtml = _defaultOpenApiClient.Excute();
            var result     = resultHtml.FromJson <OutputResponseDto>();

            return(Json(result));
        }
Example #7
0
        public async Task <IActionResult> Register(
            [FromServices] ICreateUserUseCase useCase,
            [FromServices] CreateUserPresenter presenter,
            [FromBody] InputUserDto input)
        {
            await useCase.Execute(new CreateUserInput(
                                      input.Name, input.Surname, input.Email, input.Password));

            return(presenter.ViewModel);
        }
        public async Task <IHttpActionResult> UserInfoGetAsync([FromBody] InputUserDto request)
        {
            _defaultOpenApiClient = new DefaultOpenApiClient(_url, _appKey, _sysSecret, ApiGlobalConst.MethodConst.OpenApiUserInfoGet);
            _defaultOpenApiClient.SetBizObject(request);
            var resultHtml = await _defaultOpenApiClient.ExcuteAsync();

            var result = resultHtml.FromJson <OutputResponseDto>();

            return(Json(result));
        }
        public async void Update_ReturnsBadRequest()
        {
            // Arrange
            var controller = new UserController(_userService.Object);
            var user       = new InputUserDto
            {
                Email = "invalid-address"
            };

            // Act
            var result = await controller.Update(Guid.NewGuid(), user);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
Example #10
0
        public async Task Create_ReturnsBadRequest_WhenInvalidInput()
        {
            // Arrange
            var sampleUser = new InputUserDto
            {
                Email = "invalid-address"
            };

            // Act
            var result = await _httpClient.PostAsync(ResouceUri, CreateContent(sampleUser));

            // Assert
            Assert.False(result.IsSuccessStatusCode);
            Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
        }
Example #11
0
        public async Task <string> Post(InputUserDto input)
        {
            IdentityResult IR   = null;
            var            user = new User {
                UserName = input.UserName, NormalizedUserName = input.UserName.ToUpper(), Email = input.Email, NormalizedEmail = input.Email.ToUpper(), PhoneNumber = input.Phone, IsAdmin = input.IsAdmin, FullName = input.FullName
            };

            IR = await _userManager.CreateAsync(user, input.Password);

            if (!IR.Succeeded)
            {
                return(IR.Errors.FirstOrDefault().Code);
            }
            user = await _userManager.FindByIdAsync(user.Id);

            return("Success");
        }
Example #12
0
        public async Task <User> Post(InputUserDto input)
        {
            IdentityResult IR   = null;
            var            user = new User {
                UserName = input.UserName, Email = input.Email, PhoneNumber = input.Phone, IsCustomer = input.IsCustomer, IsAdmin = input.IsAdmin, HasWrittingAccess = input.HasWrittingAccess
            };

            IR = await _userManager.CreateAsync(user, input.Password);

            if (!IR.Succeeded)
            {
                return(null);
            }
            user = await _userManager.FindByIdAsync(user.Id);

            return(user);
        }
Example #13
0
        public async Task Update_ReturnsNotFound_WhenUserNotExists()
        {
            // Arrange
            var userId       = Guid.NewGuid();
            var userToUpdate = new InputUserDto()
            {
                Email     = "*****@*****.**",
                FirstName = "different-first-name",
                LastName  = "different-last-name",
                UserName  = "******"
            };

            // Act
            var result = await _httpClient.PutAsync($"{ResouceUri}{userId.ToString()}", CreateContent(userToUpdate));

            // Assert
            Assert.False(result.IsSuccessStatusCode);
            Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
        }
Example #14
0
        public async Task <IActionResult> Create([FromBody] InputUserDto user)
        {
            // TODO: Fix validation attribute, it's not working as expected.
            if (user == null)
            {
                return(BadRequest());
            }

            var userToCreate = new User
            {
                Email     = user.Email,
                FirstName = user.FirstName,
                LastName  = user.LastName,
                UserName  = user.UserName
            };

            var result = await _userService.CreateAsync(userToCreate);

            return(CreatedAtAction(nameof(Get), new { userId = result.Id }, new UserDto(result)));
        }
        public async void Update_ReturnsNoContent()
        {
            // Arrange
            var controller = new UserController(_userService.Object);
            var expected   = GetSampleUser();
            var sampleUser = new InputUserDto
            {
                Email     = expected.Email,
                FirstName = expected.FirstName,
                LastName  = expected.LastName,
                UserName  = expected.UserName
            };

            _userService.Setup(mock => mock.UpdateAsync(It.IsAny <User>())).ReturnsAsync(expected);

            // Act
            var result = await controller.Update(expected.Id, sampleUser);

            // Assert
            _userService.Verify(mock => mock.UpdateAsync(It.IsAny <User>()), Times.Once);
            Assert.IsType <NoContentResult>(result);
        }
Example #16
0
        public async Task Update_ReturnsNoContent()
        {
            // Arrange
            var sampleUser = GetSampleUser();

            using (var context = CreateContext())
            {
                await context.Users.AddAsync(sampleUser);

                await context.SaveChangesAsync();
            }

            var userToUpdate = new InputUserDto()
            {
                Email     = "*****@*****.**",
                FirstName = "different-first-name",
                LastName  = "different-last-name",
                UserName  = "******"
            };

            // Act
            var result = await _httpClient.PutAsync($"{ResouceUri}{sampleUser.Id.ToString()}", CreateContent(userToUpdate));

            // Assert
            Assert.True(result.IsSuccessStatusCode);
            Assert.Equal(HttpStatusCode.NoContent, result.StatusCode);

            User updatedUser;

            using (var context = CreateContext())
            {
                updatedUser = await context.Users.FindAsync(sampleUser.Id);
            }

            Assert.Equal(userToUpdate.Email, updatedUser.Email);
            Assert.Equal(userToUpdate.FirstName, updatedUser.FirstName);
            Assert.Equal(userToUpdate.LastName, updatedUser.LastName);
            Assert.Equal(userToUpdate.UserName, updatedUser.UserName);
        }
Example #17
0
        public async Task Update_ReturnsBadRequest_WhenInputIsInvalid()
        {
            // Arrange
            var sampleUser = GetSampleUser();

            using (var context = CreateContext())
            {
                await context.Users.AddAsync(sampleUser);

                await context.SaveChangesAsync();
            }

            var userToUpdate = new InputUserDto()
            {
                Email = "invalid-email"
            };

            // Act
            var result = await _httpClient.PutAsync($"{ResouceUri}{sampleUser.Id.ToString()}", CreateContent(userToUpdate));

            // Assert
            Assert.False(result.IsSuccessStatusCode);
            Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
        }