public IActionResult Post([FromBody] UserModelIn userIn) { try { if (ModelState.IsValid) { User user = userIn.TransformToEntity(); user = userService.CreateUser(user); UserModelOut userModel = new UserModelOut(user); return(CreatedAtRoute("Get", new { id = user.UserId }, userModel)); } else { return(BadRequest(ModelState)); } } catch (NotUniqueException ex) { return(BadRequest(ex.Message)); } catch (EmailFormatException ex) { return(BadRequest(ex.Message)); } }
public void CreateValidUserTest() { var modelIn = new UserModelIn() { Name = "Juan", LastName = " Perez", Email = "*****@*****.**", UserName = "******", Password = "******", IsAdministrator = true }; var fakeUser = new User("Juan", "Perez", "Alberto", "pass", "*****@*****.**", true); fakeUser.UserId = 1; var modelOut = new UserModelOut(fakeUser); var userServiceMock = new Mock <IUserService>(); userServiceMock.Setup(userService => userService.CreateUser(fakeUser)); var controller = new UsersController(userServiceMock.Object); var result = controller.Post(modelIn); var createdResult = result as CreatedAtRouteResult; var modelOut2 = createdResult.Value as UserModelOut; userServiceMock.VerifyAll(); Assert.IsNotNull(createdResult); Assert.AreEqual("Get", createdResult.RouteName); Assert.AreEqual(201, createdResult.StatusCode); }
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 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 IActionResult TryAddUser(UserModelIn user) { UserDto toAdd = BuildUser(user); UserDto added = userService.AddUser(toAdd); UserModelOut modelOut = CreateModelOut(added); return(CreatedAtRoute("GetUserById", new { username = toAdd.username }, modelOut)); }
private IActionResult TryToAddUser(UserModelIn userModel) { UserDTO user = userModel.ToServicesDTO(); userServices.CreateUser(user); var addedUser = new UserModelOut(user); return(CreatedAtRoute("GetUser", new { userId = addedUser.UserName }, addedUser)); }
private UserModelOut CreateModelOut(UserDto user) { UserModelOut built = new UserModelOut() { Username = user.username, Name = user.name, Surname = user.surname, Email = user.email, IsAdmin = user.isAdmin }; return(built); }
public IActionResult Get(int id) { User user = userService.GetUserById(id); if (user != null) { UserModelOut model = new UserModelOut(user); return(Ok(model)); } else { return(BadRequest("No existe el usuario buscado")); } }
private UserModelOut TryGetUser(string username) { UserDto queried = userService.GetUser(username); UserModelOut toReturn = new UserModelOut { Name = queried.name, Surname = queried.surname, Username = queried.username, Email = queried.email, IsAdmin = queried.isAdmin }; return(toReturn); }
public IActionResult Get(string username) { IActionResult result; try { SetSession(); UserModelOut toReturn = TryGetUser(username); result = Ok(toReturn); } catch (ServiceException e) { result = errors.GenerateError(e); } return(result); }
public IActionResult Post([FromBody] UserModelIn userIn) { if (ModelState.IsValid) { var user = new User(userIn.Username, userIn.Password, userIn.Age); userService.SignUp(user); var addedUser = new UserModelOut() { Id = user.Id, Username = userIn.Username, Age = userIn.Age }; return(CreatedAtRoute("GetById", new { id = addedUser.Id }, addedUser)); } else { return(BadRequest(ModelState)); } }
public void CreateValidUserTest() { //Arrange. service.Setup(us => us.AddUser(It.IsAny <UserDto>())).Returns(GetFakeUser()); //Act. IActionResult result = controller.Post(input); CreatedAtRouteResult createdResult = result as CreatedAtRouteResult; UserModelOut modelOut = createdResult.Value as UserModelOut; //Assert. service.Verify(us => us.AddUser(It.IsAny <UserDto>()), Times.Once); Assert.IsNotNull(result); Assert.IsNotNull(createdResult); Assert.AreEqual(201, createdResult.StatusCode); Assert.AreEqual("GetUserById", createdResult.RouteName); Assert.AreEqual(modelOut.Username, input.Username); }
public IActionResult Get() { IEnumerable <User> users = userService.GetAllUsers(); if (users.Count() > 0) { List <UserModelOut> modelUsers = new List <UserModelOut>(); foreach (User user in users) { UserModelOut model = new UserModelOut(user); modelUsers.Add(model); } return(Ok(modelUsers)); } else { return(Ok("No existen usuarios en el sistema")); } }
public void TestInitialize() { webApiMapper = new WebApiMapper(); user = new UserEntity { Id = 1, CompleteName = "Nahuel Kleiman", Mail = "*****@*****.**", Password = "******", Requests = new List <RequestEntity>(), IsAdmin = false }; request = new RequestEntity { Id = 1, Detail = "Un contenedor roto en la esquina de Av. Italia y Bolivia", ApplicantName = "Nahuel Kleiman", Mail = "*****@*****.**", Phone = "098555666", RequestTypeEntityId = 1 }; userModelOut = new UserModelOut() { Id = 1, CompleteName = "Nahuel Kleiman", Mail = "*****@*****.**", Password = "******", IsAdmin = false }; userModelIn = new UserModelIn() { Id = 1, CompleteName = "Nahuel Kleiman", Mail = "*****@*****.**", Password = "******", IsAdmin = false }; userLogicMock = new Mock <IUserLogic>(); userController = new UserController(userLogicMock.Object, webApiMapper); }
public void GetTest() { //Arrange. UserDto fake = GetFakeUser(); service.Setup(us => us.GetUser(fake.username)).Returns(fake); //Act. IActionResult result = controller.Get(fake.username); OkObjectResult okResult = result as OkObjectResult; UserModelOut modelOut = okResult.Value as UserModelOut; //Assert. service.Verify(us => us.GetUser(fake.username), Times.Once); Assert.IsNotNull(result); Assert.IsNotNull(okResult); Assert.AreEqual(200, okResult.StatusCode); Assert.IsNotNull(modelOut); Assert.AreEqual(modelOut.Username, fake.username); Assert.AreEqual(modelOut.Email, fake.email); Assert.AreEqual(modelOut.Name, fake.name); Assert.AreEqual(modelOut.Surname, fake.surname); Assert.IsFalse(modelOut.IsAdmin); }