예제 #1
0
        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));
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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));
        }
예제 #6
0
        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));
        }
예제 #7
0
        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);
        }
예제 #8
0
        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"));
            }
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
 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));
     }
 }
예제 #12
0
        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);
        }
예제 #13
0
        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"));
            }
        }
예제 #14
0
 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);
 }
예제 #15
0
        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);
        }