Пример #1
0
        public void PatchPerson()
        {
            #region Owin Context
            var owinMock = new Mock <IOwinContext>();

            var userStoreMock = new Mock <IUserStore <ApplicationUser> >();
            userStoreMock.Setup(s => s.FindByNameAsync("*****@*****.**")).ReturnsAsync(new ApplicationUser {
                Id          = "ed980470-9c0f-47f7-a967-0adc9eb2325e",
                Email       = "*****@*****.**",
                UserName    = "******",
                PhoneNumber = "012345678",
                Person      = new Person()
                {
                    Id = "ed980470-9c0f-47f7-a967-0adc9eb2325e", FirstName = "Pablo Emilio", LastName = "Escobar Gaviria", Gender = "Mr.", Address = new UserAddress()
                    {
                        Id = "ed980470-9c0f-47f7-a967-0adc9eb2325e", City = "Medellín", Country = "Colombia",
                    }, ApplicationUser = new ApplicationUser()
                    {
                        Id = "ed980470-9c0f-47f7-a967-0adc9eb2325e", UserName = "******", Email = "*****@*****.**", PhoneNumber = "012345678"
                    }
                },
            });
            var applicationUserManager = new ApplicationUserManager(userStoreMock.Object);

            owinMock.Setup(o => o.Get <ApplicationUserManager>(It.IsAny <string>())).Returns(applicationUserManager);
            #endregion

            var repo = new Mock <IRepository <Person> >();
            // Arrange
            PeopleRepository rep        = new PeopleRepository();
            PeopleController controller = new PeopleController(rep.Repo);
            controller.Request = new HttpRequestMessage();
            controller.Request.SetOwinContext(owinMock.Object);
            controller.Configuration = new HttpConfiguration();
            controller.User          = new ClaimsPrincipal(new GenericPrincipal(new GenericIdentity("*****@*****.**"), new string[] { Utils.AppRoles.Admin }));

            PersonExtendedDTO person = new PersonExtendedDTO()
            {
                UserName = "******",
                Address1 = "Hacienda Nápoles",
                City     = "Puerto Triunfo",
            };

            // Act
            IHttpActionResult result = controller.Patch(person.UserName, person);
            var contentResult        = result as OkNegotiatedContentResult <PersonExtendedDTO>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
            Assert.AreEqual("Puerto Triunfo", contentResult.Content.City);
            Assert.AreEqual("Hacienda Nápoles", contentResult.Content.Address1);
            Assert.AreEqual("Colombia", contentResult.Content.Country);
        }
Пример #2
0
        public IHttpActionResult Patch(string username, PersonExtendedDTO person)
        {
            //WARNING: from the moment Person has a non-nullable property, replace PersonExtendedDTO by PersonExtendedPatchDTO
            #region Validation
            //If not admin, user can only edit himself/herself
            if (!User.IsInRole(AppRoles.Admin) && person.UserName != User.Identity.GetUserName())
            {
                return(BadRequest("You do not have sufficient rights to edit anyone but yourself"));
            }
            if (username != person.UserName)
            {
                return(BadRequest("username and object param doesn't match"));
            }
            #endregion

            //Get user manager
            var userManager = Request.GetOwinContext().GetUserManager <ApplicationUserManager>();
            //Get user associated
            ApplicationUser user = userManager.FindByName(person.UserName);

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

            //Cast for database storage
            Person model = person.ToPatchModel(user);

            //Insert in db
            userManager.Update(model.ApplicationUser);
            //peopleRepository.Update(model);
            //peopleRepository.Save();

            //Cast for transport
            PersonExtendedDTO result = new PersonExtendedDTO(model);

            return(Ok(result));
        }
Пример #3
0
        public IHttpActionResult Post(PersonExtendedDTO person)
        {
            //If not admin, user can only edit himself/herself
            if (!User.IsInRole(AppRoles.Admin) && person.UserName != User.Identity.GetUserName())
            {
                return(BadRequest("You do not have sufficient rights to edit anyone but yourself"));
            }

            //Get user manager
            var userManager = Request.GetOwinContext().GetUserManager <ApplicationUserManager>();
            //Get user associated
            ApplicationUser user = userManager.FindByName(person.UserName);

            #region Validation
            if (user == null)
            {
                return(BadRequest("Person you try to create has no user to associate with!"));
            }
            if (user.Person != null)
            {
                return(BadRequest("Person you try to create already exists!"));
            }
            #endregion

            //Cast for database storage
            Person model = person.ToModel(user);

            //Insert in db
            userManager.Update(model.ApplicationUser);
            //peopleRepository.Add(model);
            //peopleRepository.Save();

            //Cast for transport
            PersonExtendedDTO result = new PersonExtendedDTO(model);

            return(Ok(result));
        }