public async Task EditGet_ReturnsViewResult_WhenRequestIsFoundAndUserIsOrgAdmin()
        {
            const int orgId = 1;

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EditRequestQuery>())).ReturnsAsync(new EditRequestViewModel { OrganizationId = orgId, Name = "test" });

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserAnOrgAdmin(orgId.ToString());

            var result = await sut.Edit(Guid.NewGuid());

            var objResult = Assert.IsType<ViewResult>(result);
            objResult.ViewName.ShouldBe("Edit");
            objResult.ViewData["Title"].ShouldBe("Edit test");
        }
        public async Task EditGet_ReturnsNotFound_WhenEditRequestQueryReturnsNull()
        {
            var eventId = Guid.NewGuid();

            EditRequestQuery eventSummaryQuery = null; // will be assigned from the Moq callback
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EditRequestQuery>())).ReturnsAsync(null).Callback<EditRequestQuery>(cmd => eventSummaryQuery = cmd).Verifiable();

            var sut = new RequestController(mockMediator.Object);

            var result = await sut.Edit(eventId);

            var objResult = Assert.IsType<NotFoundResult>(result);
            objResult.StatusCode.ShouldBe(StatusCodes.Status404NotFound);
        }
        public async Task EditGet_ReturnsHttpUnauthorizedResult_WhenUserIsNotOrgAdmin()
        {
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EditRequestQuery>())).ReturnsAsync(new EditRequestViewModel());

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserNotAnOrgAdmin();

            var result = await sut.Edit(Guid.NewGuid());

            var objResult = Assert.IsType<UnauthorizedResult>(result);
            objResult.StatusCode.ShouldBe(StatusCodes.Status401Unauthorized);
        }
        public async Task EditGet_SendsEditRequestQueryQuery_WithCorrectArguments()
        {
            var eventId = Guid.NewGuid();

            EditRequestQuery eventSummaryQuery = null; // will be assigned from the Moq callback
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EditRequestQuery>())).ReturnsAsync(null).Callback<EditRequestQuery>(cmd => eventSummaryQuery = cmd).Verifiable();

            var sut = new RequestController(mockMediator.Object);

            await sut.Edit(eventId);

            mockMediator.Verify(x => x.SendAsync(It.IsAny<EditRequestQuery>()), Times.Once);

            eventSummaryQuery.Id.ShouldBe(eventId);
        }
        public async Task EditPost_ReturnsRedirectToAction_WhenEventIsNotNullAndUserIsOrgAdmin()
        {
            const int eventId = 1;
            const int orgId = 1;
            var viewModel = new EventSummaryViewModel { Id = eventId, OrganizationId = orgId };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(viewModel);

            mockMediator.Setup(x => x.SendAsync(It.IsAny<EditRequestCommand>())).ReturnsAsync(Guid.NewGuid());

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserAnOrgAdmin(orgId.ToString());

            var result = await sut.Edit(new EditRequestViewModel { EventId = eventId });

            var objResult = Assert.IsType<RedirectToActionResult>(result);
            objResult.ActionName.ShouldBe("Requests");
            objResult.ControllerName.ShouldBe("Event");
        }
        public async Task EditPost_SendsEditRequestCommand_WhenEventIsNotNullAndUserIsOrgAdmin()
        {
            const int eventId = 1;
            const int orgId = 1;
            var viewModel = new EventSummaryViewModel { Id = eventId, OrganizationId = orgId };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(viewModel);

            EditRequestCommand editRequestCommand = null; // will be assigned from the Moq callback
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EditRequestCommand>())).ReturnsAsync(Guid.NewGuid()).Callback<EditRequestCommand>(cmd => editRequestCommand = cmd).Verifiable();

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserAnOrgAdmin(orgId.ToString());

            await sut.Edit(new EditRequestViewModel { EventId = eventId });

            mockMediator.Verify(x => x.SendAsync(It.IsAny<EditRequestCommand>()), Times.Once);

            editRequestCommand.RequestModel.EventId.ShouldBe(eventId);
        }
        public async Task EditPost_ReturnsViewResult_WhenModelStateIsNotValid()
        {
            const int eventId = 1;
            const int orgId = 1;
            var viewModel = new EventSummaryViewModel { Id = eventId, OrganizationId = orgId };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(viewModel);

            var sut = new RequestController(mockMediator.Object);
            sut.MakeUserAnOrgAdmin(orgId.ToString());
            sut.ModelState.AddModelError("test", "test error");

            var result = await sut.Edit(new EditRequestViewModel { EventId = eventId });

            var objResult = Assert.IsType<ViewResult>(result);
            objResult.ViewName.ShouldBe("Edit");
        }
        public async Task EditPost_ReturnsBadRequest_WhenEventSummaryQueryReturnsNull()
        {
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(mock => mock.SendAsync(It.IsAny<EventSummaryQuery>())).ReturnsAsync(null).Verifiable();

            var sut = new RequestController(mockMediator.Object);
            var result = await sut.Edit(new EditRequestViewModel { EventId = 1 });

            var objResult = Assert.IsType<BadRequestObjectResult>(result);
            objResult.StatusCode.ShouldNotBeNull();
            objResult.StatusCode.Value.ShouldBe(StatusCodes.Status400BadRequest);
        }