예제 #1
0
        public async Task <IActionResult> CreateOrderItemAsync(
            CallOffId callOffId,
            CatalogueItemId catalogueItemId,
            CreateOrderItemModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var order = await orderItemService.GetOrder(callOffId);

            if (order is null)
            {
                return(NotFound());
            }

            var validationResult = await createOrderItemService.CreateAsync(order, catalogueItemId, model);

            if (validationResult.Success)
            {
                return(CreatedAtAction(
                           nameof(GetAsync).TrimAsync(),
                           new { callOffId = callOffId.ToString(), catalogueItemId = catalogueItemId.ToString() },
                           null));
            }

            foreach ((string key, string errorMessage) in validationResult.ToModelErrors())
            {
                ModelState.AddModelError(key, errorMessage);
            }

            return(ValidationProblem(ModelState));
        }
예제 #2
0
        public static async Task CreateOrderItemAsync_OrderExists_ReturnsCreatedAtActionResult(
            [Frozen] Mock <ICreateOrderItemService> createOrderItemService,
            [Frozen] Mock <IOrderItemService> orderItemService,
            [Frozen] CallOffId callOffId,
            CatalogueItemId catalogueItemId,
            Order order,
            CreateOrderItemModel model,
            OrderItemsController controller)
        {
            Expression <Func <ICreateOrderItemService, Task <AggregateValidationResult> > > createAsync = s => s.CreateAsync(
                It.Is <Order>(o => o.Equals(order)),
                It.Is <CatalogueItemId>(i => i == catalogueItemId),
                It.Is <CreateOrderItemModel>(m => m == model));

            orderItemService.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order);
            createOrderItemService.Setup(createAsync).ReturnsAsync(new AggregateValidationResult());

            var result = await controller.CreateOrderItemAsync(callOffId, catalogueItemId, model);

            result.Should().BeOfType <CreatedAtActionResult>();
            result.As <CreatedAtActionResult>().Should().BeEquivalentTo(new
            {
                ActionName  = "Get",
                RouteValues = new RouteValueDictionary
                {
                    { nameof(callOffId), callOffId.ToString() },
                    { nameof(catalogueItemId), catalogueItemId.ToString() },
                },
            });
        }
예제 #3
0
        public async Task <IActionResult> AddOrUpdateAsync(
            CallOffId callOffId,
            CatalogueItemId catalogueItemId,
            DefaultDeliveryDateModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            Order order = await defaultDeliveryDateService.GetOrder(callOffId, catalogueItemId);

            if (order is null)
            {
                return(NotFound());
            }

            (bool isValid, ErrorsModel errors) = validator.Validate(model, order.CommencementDate);

            if (!isValid)
            {
                return(BadRequest(errors));
            }

            // ReSharper disable once PossibleInvalidOperationException (covered by model validation)
            DeliveryDateResult addedOrUpdated = await defaultDeliveryDateService.SetDefaultDeliveryDate(callOffId, catalogueItemId, model.DeliveryDate.Value);

            return(addedOrUpdated == DeliveryDateResult.Added
                ? CreatedAtAction(nameof(GetAsync).TrimAsync(), new { callOffId = callOffId.ToString(), catalogueItemId = catalogueItemId.ToString() }, null)
                : Ok());
        }
        public static async Task AddOrUpdateAsync_Add_ReturnsExpectedStatusCode(
            [Frozen] Mock <IDefaultDeliveryDateService> service,
            [Frozen] CallOffId callOffId,
            [Frozen] CatalogueItemId catalogueItemId,
            Order order,
            DefaultDeliveryDateModel defaultDeliveryDate,
            [Frozen] Mock <IDefaultDeliveryDateValidator> validator,
            DefaultDeliveryDateController controller)
        {
            service.Setup(o => o.GetOrder(callOffId, catalogueItemId)).ReturnsAsync(order);
            service.Setup(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)).Callback(() =>
            {
                order.SetDefaultDeliveryDate(catalogueItemId, defaultDeliveryDate.DeliveryDate !.Value);
            });

            validator.Setup(v => v.Validate(defaultDeliveryDate, order.CommencementDate)).Returns((true, null));

            var response = await controller.AddOrUpdateAsync(callOffId, catalogueItemId, defaultDeliveryDate);

            service.Verify(o => o.GetOrder(callOffId, catalogueItemId));
            service.Verify(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value));

            response.Should().BeOfType <CreatedAtActionResult>();
            response.As <CreatedAtActionResult>().Should().BeEquivalentTo(new
            {
                ActionName  = "Get",
                RouteValues = new RouteValueDictionary
                {
                    { nameof(callOffId), callOffId.ToString() },
                    { nameof(catalogueItemId), catalogueItemId.ToString() },
                },
            });
        }
예제 #5
0
        public static async Task OnAuthorizationAsync_UserHasSamePrimaryOrganisationId_ReturnsExpectedValue(
            [Frozen] ApplicationDbContext dbContext,
            [Frozen] CallOffId callOffId,
            Order order,
            OrderLookupOrganisationAuthorizationFilter filter)
        {
            dbContext.Order.Add(order);
            await dbContext.SaveChangesAsync();

            const string parameterName = OrderLookupOrganisationAuthorizationFilter.DefaultParameterName;

            var user = ClaimsPrincipalBuilder.Create()
                       .WithClaim(ApplicationClaimTypes.Ordering)
                       .WithClaim(UserClaimTypes.PrimaryOrganisationId, order.OrderingParty.Id.ToString())
                       .Build();

            var actionDescriptor = new ActionDescriptor
            {
                EndpointMetadata = new object[] { new AuthorizeOrganisationAttribute() },
                Parameters       = new[] { new ParameterDescriptor {
                                               Name = parameterName
                                           } },
            };

            var context = AuthorizationFilterContextBuilder.Create()
                          .WithActionDescription(actionDescriptor)
                          .WithRouteValue(parameterName, callOffId.ToString())
                          .WithUser(user)
                          .Build();

            await filter.OnAuthorizationAsync(context);

            context.Result.Should().BeNull();
        }
예제 #6
0
        public static void ToString_ReturnsExpectedResult()
        {
            var id = new CallOffId(1, 1);

            var idAsString = id.ToString();

            idAsString.Should().Be("C000001-01");
        }
예제 #7
0
        public static async Task BindModelAsync_ReturnsSuccessWithOrder(
            [Frozen] ApplicationDbContext dbContext,
            [Frozen] CallOffId callOffId,
            Order order,
            Mock <IValueProvider> valueProviderMock,
            Mock <ModelBindingContext> contextMock,
            OrderModelBinder modelBinder)
        {
            dbContext.Order.Add(order);
            await dbContext.SaveChangesAsync();

            contextMock.SetupAllProperties();
            contextMock.Setup(c => c.ValueProvider).Returns(valueProviderMock.Object);
            valueProviderMock
            .Setup(v => v.GetValue(It.IsAny <string>()))
            .Returns(new ValueProviderResult(new StringValues(callOffId.ToString())));

            var context = contextMock.Object;

            await modelBinder.BindModelAsync(context);

            context.Result.Should().Be(ModelBindingResult.Success(order));
        }