public void EditChatPostWithInvalidModelStateNotModifiesRepository()
        {
            var clientRepository  = RepositoryHelper.RepositoryLocalWithOneClient;
            var chatRepository    = new Mock <IRepository <Contact> >();
            var client            = clientRepository.All().First();
            var contactService    = new Mock <IContactService>();
            var restClient        = new Mock <IRestClient>();
            var userManager       = new Mock <IIdentityMembershipProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();


            var chat = new ChatViewModel()
            {
                Id          = 1,
                ClientId    = client.Id,
                ClientName  = client.Name,
                Date        = new DateTime(2013 - 1 - 02),
                Description = "this is the description"
            };

            var controller = new ChatController(
                chatRepository.Object,
                clientRepository,
                contactService.Object,
                new ServerTime(),
                restClient.Object, userManager.Object, contactAutoRating.Object);

            controller.ModelState.AddModelError("error", "SomeError");
            var result = controller.Edit(chat) as ViewResult;

            chatRepository.Verify(cr => cr.SaveChanges(), Times.Never);
        }
        public void EditChatPostModifiesDataInRepositoryAndIgnoresNullValues()
        {
            var clientRepository  = RepositoryHelper.RepositoryLocalWithOneClient;
            var chatRepository    = new LocalRepository <Contact>();
            var client            = clientRepository.All().First();
            var contactService    = new Mock <IContactService>();
            var restClient        = new Mock <IRestClient>();
            var userManager       = new Mock <IIdentityMembershipProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();

            var chat = new Contact()
            {
                Id       = 1,
                ClientId = client.Id,
                Date     = new DateTime(2013 - 1 - 02)
            };

            var chatProperty = new ContactProperty()
            {
                Id    = 1,
                Type  = "Description",
                Value = "this is the description"
            };
            var chatProperty2 = new ContactProperty()
            {
                Id    = 1,
                Type  = "Email",
                Value = "*****@*****.**"
            };
            var chatProperty3 = new ContactProperty()
            {
                Id    = 1,
                Type  = "Phone",
                Value = "08123456"
            };

            var chatProperty4 = new ContactProperty()
            {
                Id    = 1,
                Type  = "LiveChatId",
                Value = "LC123456"
            };

            chat.Property.Add(chatProperty);
            chat.Property.Add(chatProperty2);
            chat.Property.Add(chatProperty3);
            chat.Property.Add(chatProperty4);
            chatRepository.Add(chat);

            var chatViewModel = new ChatViewModel()
            {
                Id          = 1,
                ClientId    = client.Id,
                ClientName  = client.Name,
                Date        = new DateTime(2013 - 1 - 02),
                Description = "Description is Edited",
                Email       = "*****@*****.**",
                Phone       = "08654321",
                LiveChatId  = ""
            };

            var controller = new ChatController(
                chatRepository,
                clientRepository,
                contactService.Object,
                new ServerTime(),
                restClient.Object, userManager.Object, contactAutoRating.Object);

            var result = controller.Edit(chatViewModel) as ViewResult;

            Assert.AreEqual(1, chatRepository.All().Count());
            Assert.AreEqual("Description is Edited", chatRepository.Where(c => c.Id == chat.Id).First().Property.Single(lp => lp.Type == "Description").Value);
            Assert.AreEqual("*****@*****.**", chatRepository.Where(c => c.Id == chat.Id).First().Property.Single(lp => lp.Type == "Email").Value);
            Assert.AreEqual("08654321", chatRepository.Where(c => c.Id == chat.Id).First().Property.Single(lp => lp.Type == "Phone").Value);
            Assert.AreEqual(1, chatRepository.Where(c => c.Id == chat.Id).First().Property.Count(lp => lp.Type == "LiveChatId"));
            Assert.AreEqual("LC123456", chatRepository.Where(c => c.Id == chat.Id).First().Property.Single(lp => lp.Type == "LiveChatId").Value);
        }
        public void EditChatGetShowsCorrectData()
        {
            var clientRepository  = RepositoryHelper.RepositoryLocalWithOneClient;
            var chatRepository    = new LocalRepository <Contact>();
            var client            = clientRepository.All().First();
            var contactService    = new Mock <IContactService>();
            var restClient        = new Mock <IRestClient>();
            var userManager       = new Mock <IIdentityMembershipProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();

            var chat = new Contact()
            {
                Id       = 1,
                ClientId = client.Id,
                Date     = new DateTime(2013 - 1 - 02)
            };

            var chatProperty = new ContactProperty()
            {
                Id    = 1,
                Type  = "Description",
                Value = "this is the description"
            };
            var chatProperty2 = new ContactProperty()
            {
                Id    = 1,
                Type  = "Email",
                Value = "*****@*****.**"
            };
            var chatProperty3 = new ContactProperty()
            {
                Id    = 1,
                Type  = "Phone",
                Value = "08123456"
            };

            chat.Property.Add(chatProperty);
            chat.Property.Add(chatProperty2);
            chat.Property.Add(chatProperty3);
            chatRepository.Add(chat);

            var claim = new Claim("test", "AnyId");

            userManager.Setup(um => um.GetRoles(It.IsAny <string>())).Returns(new List <string> {
                "mockRole"
            });

            var controller = new ChatController(
                chatRepository,
                clientRepository,
                contactService.Object,
                new ServerTime(),
                restClient.Object, userManager.Object, contactAutoRating.Object);

            ModelHelper.SetClaimAsCurrentUserForController(claim, controller);
            var result = controller.Edit(chat.Id) as ViewResult;
            var model  = result.Model as ChatViewModel;

            Assert.AreEqual(model.ClientName, client.Name);
            Assert.AreEqual(model.Id, chat.Id);
            Assert.AreEqual("*****@*****.**", model.Email);
            Assert.AreEqual("08123456", model.Phone);
        }