public void EditPostShouldRedirectToDetailsWithTheIdFromTheMediatorIfModelStateIsValid()
        {
            var mockMediator = new Mock<IMediator>();

            var controller = new OrganizationController(mockMediator.Object);

            var mockContext = MockActionContextWithUser(SiteAdmin());
            controller.ActionContext = mockContext.Object;

            var model = new OrganizationEditModel();

            mockMediator.Setup(x => x.Send(It.Is<OrganizationEditCommand>(y => y.Organization == model))).Returns(Id);

            var result = (RedirectToActionResult)controller.Edit(model);

            Assert.Equal("Details", result.ActionName);
            Assert.Equal("Admin", result.RouteValues["area"]);
            Assert.Equal(Id, result.RouteValues["id"]);
        }
        public void EditOrganizationMediatorShouldBeCalledWithAppropriateDataWhenModelStateIsValid()
        {
            var mockMediator = new Mock<IMediator>();

            var controller = new OrganizationController(mockMediator.Object);

            var mockContext = MockActionContextWithUser(SiteAdmin());
            controller.ActionContext = mockContext.Object;

            controller.Edit(_organizationEditModel);

            mockMediator.Verify(x => x.Send(It.Is<OrganizationEditCommand>(y => y.Organization == _organizationEditModel)));
        }
        public void EditPostShouldReturnTheEditViewWithTheModelPassedInIfTheModelStateIsInvalid()
        {
            var controller = new OrganizationController(new Mock<IMediator>().Object);

            var mockContext = MockActionContextWithUser(SiteAdmin());
            controller.ActionContext = mockContext.Object;

            controller.ModelState.AddModelError("foo", "bar");

            var model = new OrganizationEditModel();

            var result = (ViewResult)controller.Edit(model);

            Assert.Equal("Edit", result.ViewName);
            Assert.Same(model, result.ViewData.Model);
        }
        public async Task EditPostSendsOrganizationEditCommandWithCorrectOrganizationEditModelWhenModelStateIsValidAndOrganizationNameIsUnique()
        {
            var mediator = new Mock<IMediator>();
            mediator.Setup(y => y.SendAsync(It.IsAny<OrganizationNameUniqueQueryAsync>())).ReturnsAsync(true);

            var controller = new OrganizationController(mediator.Object, SuccessValidator());
            await controller.Edit(_organizationEditModel);

            mediator.Verify(x => x.SendAsync(It.Is<EditOrganizationAsync>(y => y.Organization == _organizationEditModel)));
        }
        public async Task EditPostRedirectsToCorrectActionWithCorrectData_WhenModelStateIsValid_AndOrganizationNameIsUnique()
        {
            var model = new OrganizationEditViewModel();
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(y => y.SendAsync(It.IsAny<OrganizationNameUniqueQueryAsync>())).ReturnsAsync(true);
            mockMediator.Setup(x => x.SendAsync(It.Is<EditOrganizationAsync>(y => y.Organization == model))).ReturnsAsync(Id);            

            var controller = new OrganizationController(mockMediator.Object, SuccessValidator());
            var result = (RedirectToActionResult) await controller.Edit(model);

      Assert.Equal("Details", result.ActionName);
      Assert.Equal("Admin", result.RouteValues["area"]);
      Assert.Equal(Id, result.RouteValues["id"]);
    }
        public async Task EditPostReturnsTheCorrectViewAndViewModelWhenModelStateIsInvalid()
        {
            var model = new OrganizationEditViewModel();

            var controller = new OrganizationController(new Mock<IMediator>().Object, SuccessValidator());
            controller.ModelState.AddModelError("foo", "bar");
            var result = (ViewResult)await controller.Edit(model);

            Assert.Equal("Edit", result.ViewName);
            Assert.Same(model, result.ViewData.Model);
        }
        public async Task EditPostSendsOrganizationNameUniqueQueryWithCorrectParametersWhenModelStateIsValid()
        {
            var mediator = new Mock<IMediator>();
            var organizationEditModel = new OrganizationEditViewModel { Name = "name", Id = 1 };

            var controller = new OrganizationController(mediator.Object, SuccessValidator());
            await controller.Edit(organizationEditModel);

            mediator.Verify(x => x.SendAsync(It.Is<OrganizationNameUniqueQueryAsync>(y => y.OrganizationName == organizationEditModel.Name && y.OrganizationId == organizationEditModel.Id)), 
                Times.Once());
        }
        public async Task EditGetReturnsHttpNotFoundResult_WhenOrganizationIsNotFound()
        {
            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<OrganizationEditQueryAsync>())).ReturnsAsync(null);

            var sut = new OrganizationController(mediator.Object, null);
            var result = await sut.Edit(It.IsAny<int>());

            Assert.IsType<NotFoundResult>(result);
        }
 public async Task EditPostReturnsBadRequestResult_WhenModelIsNull()
 {
     var sut = new OrganizationController(null, null);
     var result = await sut.Edit(null);
     Assert.IsType<BadRequestResult>(result);
 }
        public async Task EditGetSendsOrganizationEditQueryWithCorrectOrganizationId()
        {
            const int organizationId = 1;
            var mediator = new Mock<IMediator>();

            var sut = new OrganizationController(mediator.Object, null);
            await sut.Edit(organizationId);

            mediator.Verify(x => x.SendAsync(It.Is<OrganizationEditQueryAsync>(y => y.Id == organizationId)), Times.Once);
        }
    public void EditOrganizationMediatorShouldBeCalledWithAppropriateDataWhenModelStateIsValid()
    {
      var mockMediator = new Mock<IMediator>();
      var controller = new OrganizationController(mockMediator.Object, SuccessValidator());

      var mockContext = MockControllerContextWithUser(SiteAdmin());
      controller.ControllerContext = mockContext.Object;
      mockMediator.Setup(y => y.Send(It.IsAny<OrganizationNameUniqueQuery>())).Returns(() => { return true; });
      controller.Edit(_organizationEditModel);

      mockMediator.Verify(x => x.Send(It.Is<OrganizationEditCommand>(y => y.Organization == _organizationEditModel)));
    }
        public async Task EditPostAddsErrorToModelState_WhenModelStateIsValid_AndOrganizationNameIsNotUnique()
        {
            //Arrange
            var organizationValidatorMock = new Mock<IOrganizationEditModelValidator>();
            var errors = new List<KeyValuePair<string, string>> { };
            organizationValidatorMock.Setup(o => o.Validate(It.IsAny<OrganizationEditViewModel>())).Returns(errors);

            var mediatorMock = new Mock<IMediator>();
            mediatorMock.Setup(m => m.SendAsync(It.IsAny<OrganizationNameUniqueQuery>())).ReturnsAsync(false);

            var sut = new OrganizationController(mediatorMock.Object, organizationValidatorMock.Object);

            //Act
            var result = await sut.Edit(_organizationEditModel) as ViewResult;

            //Assert
            result.ViewData.ModelState.ErrorCount.ShouldBe(1);
            result.ViewData.ModelState["Name"].ValidationState.ShouldBe(ModelValidationState.Invalid);
            result.ViewData.ModelState["Name"].Errors.Single().ErrorMessage
                  .ShouldBe("Organization with same name already exists. Please use different name.");
        }
        public async Task EditPostAddsTheCorrectErrorsToModelState_WhenOrganizationValidatorHasErrors_AndModelIsNotNull()
        {
            //Arrange
            var organizationValidatorMock = new Mock<IOrganizationEditModelValidator>();
            var errors = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("FakeProperty", "FakeError") };
            organizationValidatorMock.Setup(o => o.Validate(It.IsAny<OrganizationEditViewModel>())).Returns(errors);
            var sut = new OrganizationController(null, organizationValidatorMock.Object);

            //Act
            var result = await sut.Edit(_organizationEditModel) as ViewResult;

            //Assert
            result.ViewData.ModelState.ErrorCount.ShouldBe(1);
            result.ViewData.ModelState["FakeProperty"].ValidationState.ShouldBe(ModelValidationState.Invalid);
            result.ViewData.ModelState["FakeProperty"].Errors.Single().ErrorMessage.ShouldBe("FakeError");
        }
        public async Task EditPostInvokesOrganizationValidatorsValidateMethodWithTheCorrectOrganizationEditModel_WhenModelIsNotNull()
        {
            //Arrange
            var organizationValidatorMock = new Mock<IOrganizationEditModelValidator>();
            var errors = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("FakeProperty", "FakeError") };
            var organizationEditModel = new OrganizationEditViewModel {Id = 1, Name = "Test1"};
            organizationValidatorMock.Setup(o => o.Validate(organizationEditModel)).Returns(errors);
            var sut = new OrganizationController(null, organizationValidatorMock.Object);

            //Act
            var result = await sut.Edit(organizationEditModel) as ViewResult;

            //Assert
            organizationValidatorMock.Verify(o => o.Validate(organizationEditModel), Times.Once);
        }