public IActionResult Put(string username, [FromBody] UpdateUserModelIn input) { IActionResult result; UserDto toModify = BuildUser(username, input); try { SetSession(); UserDto modified = userService.ModifyUser(toModify); result = Ok(CreateModelOut(modified)); } catch (ServiceException e) { if (e.Error.Equals(ErrorType.ENTITY_NOT_FOUND)) { UserDto added = userService.AddUser(toModify); result = CreatedAtRoute("GetUserById", new { username = toModify.username }, CreateModelOut(added)); } else { result = errors.GenerateError(e); } } return(result); }
public void PutCreateTest() { //Arrange. var modelIn = new UpdateUserModelIn() { Name = "name1", Surname = "surname1", Password = "******", Email = "*****@*****.**" }; service.Setup(us => us.ModifyUser(It.IsAny <UserDto>())).Throws(new ServiceException("", ErrorType.ENTITY_NOT_FOUND)); service.Setup(us => us.AddUser(It.IsAny <UserDto>())).Returns(GetFakeUser()); //Act. IActionResult result = controller.Put("username", modelIn); CreatedAtRouteResult createdResult = result as CreatedAtRouteResult; UserModelOut added = createdResult.Value as UserModelOut; Assert.IsNotNull(result); Assert.IsNotNull(createdResult); Assert.IsNotNull(added); Assert.AreEqual("GetUserById", createdResult.RouteName); Assert.AreEqual(201, createdResult.StatusCode); Assert.AreEqual("username", added.Username); }
public void PutUserNoDataAccessTest() { //Arrange. UpdateUserModelIn modelIn = new UpdateUserModelIn() { Name = "name1", Surname = "surname1", Password = "******", Email = "*****@*****.**" }; Exception internalEx = new DataInaccessibleException(); Exception toThrow = new ServiceException(internalEx.Message, ErrorType.DATA_INACCESSIBLE); service.Setup(us => us.ModifyUser(It.IsAny <UserDto>())).Throws(toThrow); //Act. IActionResult result = controller.Put("username", modelIn); ObjectResult noData = result as ObjectResult; ErrorModelOut error = noData.Value as ErrorModelOut; //Assert. Assert.IsNotNull(result); Assert.IsNotNull(noData); Assert.AreEqual(500, noData.StatusCode); Assert.IsNotNull(error); Assert.AreEqual(toThrow.Message, error.ErrorMessage); }
public void PutModifyTest() { //Arrange. var modelIn = new UpdateUserModelIn() { Name = "name1", Surname = "surname1", Password = "******", Email = "*****@*****.**", IsAdmin = true }; service.Setup(us => us.ModifyUser(It.IsAny <UserDto>())).Returns(GetFakeUser()); //Act. IActionResult result = controller.Put("username", modelIn); OkObjectResult okResult = result as OkObjectResult; UserModelOut modified = okResult.Value as UserModelOut; //Assert. service.Verify(us => us.ModifyUser(It.IsAny <UserDto>()), Times.Once); Assert.IsNotNull(result); Assert.IsNotNull(okResult); Assert.IsNotNull(modified); }
private UserDto BuildUser(string username, UpdateUserModelIn modelIn) { UserDto built = new UserDto() { name = modelIn.Name, surname = modelIn.Surname, username = username, password = modelIn.Password, email = modelIn.Email, isAdmin = modelIn.IsAdmin }; return(built); }