Пример #1
0
        public HttpResponseMessage PostShipment(ShipmentRequestModel requestModel) {

            var createdShipmentResult = 
                _shipmentService.AddShipment(requestModel.ToShipment());

            if (!createdShipmentResult.IsSuccess) {

                return new HttpResponseMessage(HttpStatusCode.Conflict);
            }

            var response = Request.CreateResponse(HttpStatusCode.Created,
                createdShipmentResult.Entity.ToShipmentDto());

            response.Headers.Location = new Uri(
                Url.Link(RouteName, new {
                    key = createdShipmentResult.Entity.Key
                })
            );

            return response;
        }
                Returns_400_If_Request_Authorized_But_Invalid()
            {

                // Arange
                Guid[] availableShipmentTypeKeys = new[] { 
                    Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
                };

                var config = IntegrationTestHelper
                    .GetInitialIntegrationTestConfig(GetContainer(availableShipmentTypeKeys));

                var shipmentRequestModel = new ShipmentRequestModel
                {
                    ReceiverName = "ANameWhichIsMoreThan50CharsANameWhichIsMoreThan50Chars",
                    ReceiverSurname = "ASurnameWhichIsMoreThan50CharsASurnameWhichIsMoreThan50Chars",
                    ReceiverAddress = "AnAddressWhichIsMoreThan50CharsAnAddressWhichIsMoreThan50Chars",
                    ReceiverCity = "ACityWhichIsMoreThan50CharsACityWhichIsMoreThan50Chars",
                    ReceiverTelephone = "ATelephoneWhichIsMoreThan50CharsATelephoneWhichIsMoreThan50Chars",
                    ReceiverEmail = "fooexample.com"
                };

                var request = HttpRequestMessageHelper
                    .ConstructRequest(
                        httpMethod: HttpMethod.Post,
                        uri: string.Format(
                            "https://localhost/{0}",
                            ApiBaseRequestPath),
                        mediaType: "application/json",
                        username: Constants.ValidAdminUserName,
                        password: Constants.ValidAdminPassword);

                request.Content = new ObjectContent<ShipmentRequestModel>(
                    shipmentRequestModel, new JsonMediaTypeFormatter());

                var httpError = await IntegrationTestHelper.
                    GetResponseMessageBodyAsync<HttpError>(
                        config, request, HttpStatusCode.BadRequest);

                var modelState = (HttpError)httpError["ModelState"];
                var affiliateKeyError = modelState["requestModel.AffiliateKey"] as string[];
                var shipmentTypeKeyError = modelState["requestModel.ShipmentTypeKey"] as string[];
                var priceError = modelState["requestModel.Price"] as string[];
                var receiverNameError = modelState["requestModel.ReceiverName"] as string[];
                var receiverSurnameError = modelState["requestModel.ReceiverSurname"] as string[];
                var receiverAddressError = modelState["requestModel.ReceiverAddress"] as string[];
                var receiverCityError = modelState["requestModel.ReceiverCity"] as string[];
                var receiverCountryError = modelState["requestModel.ReceiverCountry"] as string[];
                var receiverTelephoneError = modelState["requestModel.ReceiverTelephone"] as string[];
                var receiverEmailError = modelState["requestModel.ReceiverEmail"] as string[];
                var receiverZipCodeError = modelState["requestModel.ReceiverZipCode"] as string[];

                // Assert
                Assert.NotNull(affiliateKeyError);
                Assert.NotNull(shipmentTypeKeyError);
                Assert.NotNull(priceError);
                Assert.NotNull(receiverNameError);
                Assert.NotNull(receiverSurnameError);
                Assert.NotNull(receiverAddressError);
                Assert.NotNull(receiverCityError);
                Assert.NotNull(receiverCountryError);
                Assert.NotNull(receiverTelephoneError);
                Assert.NotNull(receiverEmailError);
                Assert.NotNull(receiverZipCodeError);
            }
                Returns_409_If_Request_Authorized_But_Conflicted()
            {

                // Arange
                Guid[] availableShipmentTypeKeys = new[] { 
                    Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
                };

                var config = IntegrationTestHelper
                    .GetInitialIntegrationTestConfig(GetContainer(availableShipmentTypeKeys));

                var shipmentRequestModel = new ShipmentRequestModel
                {
                    ShipmentTypeKey = Guid.NewGuid(),
                    AffiliateKey = Guid.NewGuid(),
                    Price = 12.23M,
                    ReceiverName = "Receiver 1 Name",
                    ReceiverSurname = "Receiver 1 Surname",
                    ReceiverAddress = "Receiver 1 Address",
                    ReceiverCity = "Receiver 1 City",
                    ReceiverCountry = "Receiver 1 Country",
                    ReceiverTelephone = "Receiver 1 Country",
                    ReceiverZipCode = "12345",
                    ReceiverEmail = "*****@*****.**"
                };

                var request = HttpRequestMessageHelper
                    .ConstructRequest(
                        httpMethod: HttpMethod.Post,
                        uri: string.Format(
                            "https://localhost/{0}",
                            ApiBaseRequestPath),
                        mediaType: "application/json",
                        username: Constants.ValidAdminUserName,
                        password: Constants.ValidAdminPassword);

                request.Content = new ObjectContent<ShipmentRequestModel>(
                    shipmentRequestModel, new JsonMediaTypeFormatter());

                // Act
                var response = await IntegrationTestHelper
                    .GetResponseAsync(config, request);

                // Assert
                Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
            }