public async Task CreateDraftTemplate_FailsIfUserWithoutPermissionsToCreateTemplates()
        {
            ITemplateBuilderApiClient apiClient = Substitute.For <ITemplateBuilderApiClient>();
            TemplateCreateModel       model     = new TemplateCreateModel
            {
                Description     = "Test Description",
                FundingStreamId = "TEST",
                FundingPeriodId = "TEST"
            };
            string templateId = Guid.NewGuid().ToString();

            apiClient
            .CreateDraftTemplate(Arg.Any <TemplateCreateCommand>())
            .Returns(new ValidatedApiResponse <string>(HttpStatusCode.Created, templateId));
            var authHelper = Substitute.For <IAuthorizationHelper>();

            authHelper.GetUserFundingStreamPermissions(Arg.Any <ClaimsPrincipal>(), Arg.Is(model.FundingStreamId))
            .Returns(new FundingStreamPermission {
                CanCreateTemplates = false, FundingStreamId = model.FundingStreamId
            });
            TemplateBuildController controller = new TemplateBuildController(apiClient, authHelper, Substitute.For <ILogger>());

            IActionResult result = await controller.CreateDraftTemplate(model);

            result
            .Should()
            .BeAssignableTo <ForbidResult>();

            apiClient.Received(0);
        }
        public async Task GetTemplateVersions_ReturnsCorrectResult_AllStatuses()
        {
            ITemplateBuilderApiClient apiClient = Substitute.For <ITemplateBuilderApiClient>();
            string templateId = Guid.NewGuid().ToString();
            List <TemplateStatus>          statuses        = new List <TemplateStatus>();
            List <TemplateSummaryResource> returnedContent = new List <TemplateSummaryResource>
            {
                new TemplateSummaryResource
                {
                    TemplateId           = "123",
                    FundingPeriodId      = "ABC",
                    FundingStreamId      = "XYZ",
                    Description          = "Test",
                    LastModificationDate = DateTime.Now,
                    SchemaVersion        = "1.1",
                    AuthorId             = "author",
                    AuthorName           = "name",
                    Comments             = "A comment",
                    Status       = TemplateStatus.Published,
                    Version      = 1,
                    MinorVersion = 1,
                    MajorVersion = 0
                }
            };

            apiClient
            .GetTemplateVersions(templateId, statuses, Arg.Any <int>(), Arg.Any <int>())
            .Returns(new ApiResponse <TemplateVersionListResponse>(HttpStatusCode.OK, new TemplateVersionListResponse {
                PageResults = returnedContent
            }));

            var authHelper = Substitute.For <IAuthorizationHelper>();
            TemplateBuildController controller = new TemplateBuildController(apiClient, authHelper, Substitute.For <ILogger>());

            IActionResult result = await controller.GetTemplateVersions(templateId, statuses, 1, 20);

            result
            .Should()
            .BeAssignableTo <OkObjectResult>();

            TemplateVersionListResponse results = (result as OkObjectResult).Value as TemplateVersionListResponse;

            results.PageResults
            .Should()
            .Equal(returnedContent);
        }
        public async Task CreateDraftTemplate_ReturnsCorrectResult()
        {
            ITemplateBuilderApiClient apiClient = Substitute.For <ITemplateBuilderApiClient>();
            TemplateCreateModel       model     = new TemplateCreateModel
            {
                Description     = "Test Description",
                FundingStreamId = "TEST",
                FundingPeriodId = "TEST"
            };
            string templateId = Guid.NewGuid().ToString();

            apiClient
            .CreateDraftTemplate(Arg.Any <TemplateCreateCommand>())
            .Returns(new ValidatedApiResponse <string>(HttpStatusCode.Created, templateId));
            var authHelper = Substitute.For <IAuthorizationHelper>();

            authHelper.GetUserFundingStreamPermissions(Arg.Any <ClaimsPrincipal>(), Arg.Is(model.FundingStreamId))
            .Returns(new FundingStreamPermission {
                CanCreateTemplates = true, FundingStreamId = model.FundingStreamId
            });
            TemplateBuildController controller = new TemplateBuildController(apiClient, authHelper, Substitute.For <ILogger>());

            IActionResult result = await controller.CreateDraftTemplate(model);

            result
            .Should()
            .BeAssignableTo <CreatedResult>();

            string resultId = (result as CreatedResult)?.Value as string;

            resultId
            .Should()
            .Be(templateId);

            string resultLocation = (result as CreatedResult)?.Location;

            resultLocation
            .Should()
            .Be($"api/templates/build/{templateId}");
        }