public async void AddReqeustsReturnModelContainsExpectedErrors_WhenModelHasValidationErrors()
        {
            var errorList = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("key1", "error1"),
                new KeyValuePair <string, string>("key2", "error2")
            };
            var model     = BuildValidModel();
            var mediator  = BuildValidMockMediator(model);
            var validator = new Mock <IUnlinkedRequestViewModelValidator>();

            validator.Setup(mock => mock.Validate(It.IsAny <UnlinkedRequestViewModel>()))
            .Returns(errorList);
            var sut = new UnlinkedRequestController(mediator.Object, validator.Object);

            sut.MakeUserAnOrgAdmin(OrganizationId.ToString());

            await sut.AddRequests(model);

            Assert.Equal(sut.ModelState.Count, errorList.Count);
            Assert.Equal(sut.ModelState.Keys, errorList.Select(error => error.Key));
            Assert.Equal(
                sut.ModelState.Select(
                    error => error.Value.Errors.Select(message => message.ErrorMessage).FirstOrDefault()),
                errorList.Select(error => error.Value));
        }
        public async void AddRequestsReturnsUnauthorised_WhenUserIsNotAnOrgAdmin()
        {
            var mediator  = new Mock <IMediator>();
            var validator = new Mock <IUnlinkedRequestViewModelValidator>();
            var model     = new Mock <UnlinkedRequestViewModel>();
            var sut       = new UnlinkedRequestController(mediator.Object, validator.Object);

            sut.MakeUserNotAnOrgAdmin();

            Assert.IsType <UnauthorizedResult>(await sut.AddRequests(model.Object));
        }
        public async void AddReqeustsReturnsRedirectResult_WhenModelStateIsValid()
        {
            var model     = BuildValidModel();
            var mediator  = BuildValidMockMediator(model);
            var validator = new Mock <IUnlinkedRequestViewModelValidator>();

            validator.Setup(mock => mock.Validate(It.IsAny <UnlinkedRequestViewModel>()))
            .Returns(new List <KeyValuePair <string, string> >());
            var sut = new UnlinkedRequestController(mediator.Object, validator.Object);

            sut.MakeUserAnOrgAdmin(OrganizationId.ToString());

            var result = await sut.AddRequests(model);

            Assert.IsType <RedirectToActionResult>(result);
            Assert.Equal(((RedirectToActionResult)result).ActionName, nameof(sut.List));
        }
        public async void AddReqeustsReturnModelContainsExpectedData_WhenModelHasValidationErrors()
        {
            var model = new UnlinkedRequestViewModel {
                EventId = EventId
            };
            var mediator       = BuildValidMockMediator(model);
            var expectedEvents = new List <SelectListItem>()
            {
                new SelectListItem {
                    Text = "testItem", Value = "testValue", Selected = true
                }
            };
            var expectedRequests = new List <RequestSelectViewModel>()
            {
                new RequestSelectViewModel {
                    Name = "testRequest", Id = Guid.NewGuid()
                }
            };

            mediator.Setup(x => x.SendAsync(It.Is <UnlinkedRequestListQuery>(y => y.OrganizationId == 1001)))
            .ReturnsAsync(new UnlinkedRequestViewModel()
            {
                Events   = expectedEvents,
                Requests = expectedRequests
            });

            var validator = new Mock <IUnlinkedRequestViewModelValidator>();

            validator.Setup(mock => mock.Validate(It.IsAny <UnlinkedRequestViewModel>()))
            .Returns(new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("test", "error")
            });
            var sut = new UnlinkedRequestController(mediator.Object, validator.Object);

            sut.MakeUserAnOrgAdmin(OrganizationId.ToString());


            var result = (ViewResult)await sut.AddRequests(model);

            var returnedModel = (UnlinkedRequestViewModel)result.Model;

            Assert.Equal(returnedModel.Requests, expectedRequests);
            Assert.Equal(returnedModel.Events, expectedEvents);
            Assert.Equal(returnedModel.EventId, EventId);
        }
        public async void AddReqeustsCallsCommandHandler_WhenModelStateIsValid()
        {
            var model    = BuildValidModel();
            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.Is <EventSummaryQuery>(y => y.EventId == EventId)))
            .ReturnsAsync(new EventSummaryViewModel()
            {
                OrganizationId = 1001
            });

            var validator = new Mock <IUnlinkedRequestViewModelValidator>();

            validator.Setup(mock => mock.Validate(It.IsAny <UnlinkedRequestViewModel>()))
            .Returns(new List <KeyValuePair <string, string> >());
            var sut = new UnlinkedRequestController(mediator.Object, validator.Object);

            sut.MakeUserAnOrgAdmin(OrganizationId.ToString());
            await sut.AddRequests(model);

            mediator.Verify(x => x.SendAsync(It.Is <AddRequestsToEventCommand>(y => y.EventId == EventId)), Times.Once);
        }
        public async void AddReqeustsReturnsExpectedViewName_WhenModelHasValidationErrors()
        {
            const string orgId     = "1001";
            var          errorList = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("test", "error")
            };
            var model    = BuildValidModel();
            var mediator = BuildValidMockMediator(model);

            var validator = new Mock <IUnlinkedRequestViewModelValidator>();

            validator.Setup(mock => mock.Validate(It.IsAny <UnlinkedRequestViewModel>()))
            .Returns(errorList);
            var sut = new UnlinkedRequestController(mediator.Object, validator.Object);

            sut.MakeUserAnOrgAdmin(orgId);

            var result = await sut.AddRequests(model);

            Assert.Equal(((ViewResult)result).ViewName, nameof(sut.List));
        }
        public async void AddReqeustsCallsQueryHandlerWithExpectedOrgId_WhenModelHasValidationErrors()
        {
            var errorList = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("test", "error")
            };
            var model = new UnlinkedRequestViewModel()
            {
                EventId = EventId,
            };
            var mediator = BuildValidMockMediator(model);

            var validator = new Mock <IUnlinkedRequestViewModelValidator>();

            validator.Setup(mock => mock.Validate(It.IsAny <UnlinkedRequestViewModel>()))
            .Returns(errorList);
            var sut = new UnlinkedRequestController(mediator.Object, validator.Object);

            sut.MakeUserAnOrgAdmin(OrganizationId.ToString());
            await sut.AddRequests(model);

            mediator.Verify(x => x.SendAsync(It.Is <UnlinkedRequestListQuery>(y => y.OrganizationId == 1001)), Times.Once);
        }
        public async void AddReqeustsReturnsUnauthorizedResult_WhenEventDoesNotBelongToCurrentOrgId()
        {
            var model    = BuildValidModel();
            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.Is <UnlinkedRequestListQuery>(y => y.OrganizationId == OrganizationId)))
            .ReturnsAsync(model);
            mediator.Setup(x => x.SendAsync(It.Is <EventSummaryQuery>(y => y.EventId == EventId)))
            .ReturnsAsync(new EventSummaryViewModel()
            {
                OrganizationId = 6783
            });
            var validator = new Mock <IUnlinkedRequestViewModelValidator>();

            validator.Setup(mock => mock.Validate(It.IsAny <UnlinkedRequestViewModel>()))
            .Returns(new List <KeyValuePair <string, string> >());
            var sut = new UnlinkedRequestController(mediator.Object, validator.Object);

            sut.MakeUserAnOrgAdmin(OrganizationId.ToString());

            var result = await sut.AddRequests(model);

            Assert.IsType <UnauthorizedResult>(result);
        }