public async Task IndexShouldSendOrganizationListQuery()
        {
            var mediator = new Mock<IMediator>();
            var sut = new OrganizationController(mediator.Object, null);
            await sut.Index();

            mediator.Verify(x => x.SendAsync(It.IsAny<OrganizationListQueryAysnc>()), Times.Once);
        }
 public void CreateNewOrganizationPostReturnsBadRequestForNullOrganization()
 {
     //arrange
     OrganizationEditModel viewmodel = null;
     var sut = new OrganizationController(MockMediatorCreateOrganization().Object);
     //act
     var result = sut.Create(viewmodel);
     //assert 
     Assert.IsType<BadRequestResult>(result);
 }
 public void CreateNewOrganizationInvalidModelReturnsCreateView()
 {
     //arrange
     var sut = new OrganizationController(MockMediatorCreateOrganization().Object);
     sut.ModelState.AddModelError("foo", "bar");
     //act
     var result = sut.Create(_stubViewModel);
     //assert
     Assert.IsType<ViewResult>(result);
     Assert.Equal("Create", ((ViewResult) result).ViewName);
 }
        public async Task IndexShouldReturnAViewWithTheCorrectViewModel()
        {
            var mediator = new Mock<IMediator>();
            var organizationSummaryModel = new List<OrganizationSummaryViewModel>();
            mediator.Setup(x => x.SendAsync(It.IsAny<OrganizationListQueryAysnc>())).ReturnsAsync(organizationSummaryModel);

            var sut = new OrganizationController(mediator.Object, null);
            var result = (ViewResult) await sut.Index();

            Assert.IsType<ViewResult>(result);
            Assert.Same(organizationSummaryModel, result.ViewData.Model);
        }
 public void CreateNewOrganizationRedirectsToOrganizationList()
 {
     //arrange
     var sut = new OrganizationController(MockMediatorCreateOrganization().Object);
     var expectedRouteValues = new {controller = "Organization", action = "Index"};
     //act
     var result = sut.Create(_stubViewModel);
     //assert
     Assert.IsType<RedirectToRouteResult>(result);
     Assert.Equal("areaRoute", ((RedirectToRouteResult) result).RouteName);
     Assert.Equal("Organization", ((RedirectToRouteResult)result).RouteValues["controller"]); 
     Assert.Equal("Index",((RedirectToRouteResult)result).RouteValues["action"]); 
 }
        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 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 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 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 EditPostReturnsBadRequestResult_WhenModelIsNull()
 {
     var sut = new OrganizationController(null, null);
     var result = await sut.Edit(null);
     Assert.IsType<BadRequestResult>(result);
 }
        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 DeleteGetAssignsCorrectTitleToViewModel()
        {
            var viewModel = new DeleteViewModel { Title = "Title" };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<DeleteQuery>())).ReturnsAsync(viewModel);

            var sut = new OrganizationController(mediator.Object, null);
            var result = await sut.Delete(It.IsAny<int>()) as ViewResult;
            var modelResult = result.Model as DeleteViewModel;

            Assert.Equal(modelResult.Title, $"Delete {viewModel.Name}");
        }
        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 async Task DeleteGetSendsDeleteQueryWithTheCorrectOrgId()
        {
            const int orgId = 1;

            var mediator = new Mock<IMediator>();
            var sut = new OrganizationController(mediator.Object, null);

            await sut.Delete(orgId);

            mediator.Verify(x => x.SendAsync(It.Is<DeleteQuery>(y => y.OrgId == orgId)), 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 DeleteConfirmedShouldRedirectToTheCorrectActionAndArea()
        {
            var routeValueDictionary = new RouteValueDictionary { ["area"] = "Admin" };

            var sut = new OrganizationController(Mock.Of<IMediator>(), null);
            var result = await sut.DeleteConfirmed(It.IsAny<int>()) as RedirectToActionResult;

            Assert.Equal(result.ActionName, nameof(OrganizationController.Index));
            Assert.Equal(result.RouteValues, routeValueDictionary);
        }
        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 void CreateReturnsCorrectView()
 {
     var sut = new OrganizationController(null, null);
     var result = (ViewResult)sut.Create();
     Assert.Equal("Edit", result.ViewName);         
 }
        public async Task DeleteGetReturnsNotFoundResultWhenViewModelIsNull()
        {
            var sut = new OrganizationController(Mock.Of<IMediator>(), null);

            var result = await sut.Delete(It.IsAny<int>());

            Assert.IsType<NotFoundResult>(result);
        }
        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 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);
        }
        private static void CreateSut()
        {
            _mediator = new Mock<IMediator>();

            _sut = new OrganizationController(_mediator.Object);
        }
        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);
        }