public async Task <IActionResult> CreateSpecification([FromBody] CreateSpecificationViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var fundingStreamIds = new List <string> {
                viewModel.FundingStreamId
            };

            IEnumerable <FundingStreamPermission> fundingStreamPermissions = await _authorizationHelper.GetUserFundingStreamPermissions(User);

            bool hasPermissionsOnAllTheseStreams = fundingStreamIds
                                                   .All(fundingStreamId => fundingStreamPermissions
                                                        .Any(x =>
                                                             x.FundingStreamId == fundingStreamId && x.CanCreateSpecification));

            if (!hasPermissionsOnAllTheseStreams)
            {
                return(new ForbidResult());
            }

            CreateSpecificationModel specification = new CreateSpecificationModel
            {
                Description         = viewModel.Description,
                Name                = viewModel.Name,
                FundingPeriodId     = viewModel.FundingPeriodId,
                FundingStreamIds    = fundingStreamIds,
                ProviderVersionId   = viewModel.ProviderVersionId,
                AssignedTemplateIds = viewModel.AssignedTemplateIds,
                ProviderSnapshotId  = viewModel.ProviderSnapshotId
            };

            ValidatedApiResponse <SpecificationSummary> result = await _specificationsApiClient.CreateSpecification(specification);

            if (result.IsBadRequest(out BadRequestObjectResult badRequest))
            {
                return(badRequest);
            }

            if (result.StatusCode.IsSuccess())
            {
                return(new OkObjectResult(result.Content));
            }

            return(new InternalServerErrorResult($"Unable to create specification - result '{result.StatusCode}'"));
        }
예제 #2
0
        public async Task CreateSpecification_ReturnsBadRequestObjectResultWhenApiResponseIsBadRequest()
        {
            SetupFundingStreamPermissions(new FundingStreamPermission
            {
                CanCreateSpecification = true
            });

            _specificationsApiClient.CreateSpecification(Arg.Any <CreateSpecificationModel>())
            .Returns(new ValidatedApiResponse <SpecificationSummary>(HttpStatusCode.BadRequest));


            IActionResult actionResult = await _specificationController.CreateSpecification(new CreateSpecificationViewModel());

            actionResult
            .Should()
            .BeOfType <BadRequestObjectResult>();
        }