public HttpResponseMessage PostShipment(Guid key, ShipmentByAffiliateRequestModel requestModel)
        {

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

            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.AffiliateKey,
                    shipmentKey = createdShipmentResult.Entity.Key
                })
            );

            return response;
        }
Example #2
0
        public async Task<ActionResult> Create_Post(ShipmentByAffiliateRequestModel requestModel) {

            if (ModelState.IsValid) {

                ShipmentDto shipment =
                    await _shipmentsClient.AddShipmentAsync(requestModel);

                return RedirectToAction("Details", new { id = shipment.Key });
            }

            await GetAndSetShipmentTypesAsync();
            return View(requestModel);
        }
            private static Tuple<HttpConfiguration, HttpRequestMessage> GetConfigAndRequestMessage(
                Guid[] shipmentKeys, Guid[] affiliateKeys,
                Guid affiliateRequestKey, Guid shipmentRequestKey,
                ShipmentByAffiliateRequestModel requestModel) {

                var config = IntegrationTestHelper
                    .GetInitialIntegrationTestConfig(
                            GetContainer(shipmentKeys, affiliateKeys));

                var request = HttpRequestMessageHelper
                    .ConstructRequest(
                        httpMethod: HttpMethod.Put,
                        uri: string.Format(
                            "https://localhost/{0}/{1}",
                            string.Format(ApiBaseRequestPathFormat, affiliateRequestKey), shipmentRequestKey),
                        mediaType: "application/json",
                        username: Constants.ValidAffiliateUserName,
                        password: Constants.ValidAffiliatePassword);

                if (requestModel != null) {

                    request.Content = new ObjectContent<ShipmentByAffiliateRequestModel>(
                        requestModel, new JsonMediaTypeFormatter());
                }

                return Tuple.Create(config, request);
            }
            public async Task Returns_200_And_Updated_Shipment_If_Request_Authorized_And_Request_Is_Valid() {

                // Arrange
                var shipmentKeys = IntegrationTestHelper.GetKeys(9);
                var affiliateKeys = IntegrationTestHelper.GetKeys(3);

                // Except for first key, all keys are not 
                // related to current Affiliate user
                var validAffiliateKey = affiliateKeys[0];

                // There are totatly 9 shipments inside the fake collection.
                // First three belong to affiliateKeys[0]. Take one of them
                var validShipmetKey = shipmentKeys[2];

                var shipmentRequestModel = new ShipmentByAffiliateRequestModel {
                    ShipmentTypeKey = 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 = "*****@*****.**"
                };

                // Get the config and request
                var configAndRequest = GetConfigAndRequestMessage(shipmentKeys, 
                    affiliateKeys, validAffiliateKey, validShipmetKey, shipmentRequestModel);

                // Act
                var shipmentDto = await IntegrationTestHelper.
                    GetResponseMessageBodyAsync<ShipmentDto>(
                        configAndRequest.Item1, 
                        configAndRequest.Item2, 
                        HttpStatusCode.OK);

                // Assert
                Assert.Equal(validShipmetKey, shipmentDto.Key);
                Assert.Equal(validAffiliateKey, shipmentDto.AffiliateKey);
                Assert.Equal(shipmentRequestModel.Price, shipmentDto.Price);
                Assert.Equal(shipmentRequestModel.ReceiverEmail, shipmentDto.ReceiverEmail);
            }
            public async Task Returns_401_If_Request_Authorized_But_Requested_Shipment_Is_Not_Related_To_Affiliate() {

                // Arrange
                var shipmentKeys = IntegrationTestHelper.GetKeys(9);
                var affiliateKeys = IntegrationTestHelper.GetKeys(3);

                // Except for first key, all keys are not 
                // related to current Affiliate user
                var validAffiliateKey = affiliateKeys[0];

                // There are totatly 9 shipments inside the fake collection.
                // First three belong to affiliateKeys[0]. Take one of the others
                var invalidShipmetKey = shipmentKeys[5];

                var shipmentRequestModel = new ShipmentByAffiliateRequestModel {
                    ShipmentTypeKey = 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 = "*****@*****.**"
                };

                // Get the config and request
                var configAndRequest = GetConfigAndRequestMessage(
                    shipmentKeys, affiliateKeys, validAffiliateKey, invalidShipmetKey, shipmentRequestModel);

                // Act
                var response = await IntegrationTestHelper
                    .GetResponseAsync(
                        configAndRequest.Item1, configAndRequest.Item2);

                // Assert
                Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
            }
            public async Task Returns_404_If_Request_Authorized_But_Shipment_Does_Not_Exist() {

                // Arrange
                var shipmentKeys = IntegrationTestHelper.GetKeys(9);
                var affiliateKeys = IntegrationTestHelper.GetKeys(3);

                // Except for first key, all keys are not 
                // related to current Affiliate user
                var validAffiliateKey = affiliateKeys[0];

                var invalidShipmetKey = Guid.NewGuid();

                var shipmentRequestModel = new ShipmentByAffiliateRequestModel {
                    ShipmentTypeKey = 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 = "*****@*****.**"
                };

                // Get the config and request
                var configAndRequest = GetConfigAndRequestMessage(
                    shipmentKeys, affiliateKeys, validAffiliateKey, invalidShipmetKey, shipmentRequestModel);

                // Act
                var response = await IntegrationTestHelper
                    .GetResponseAsync(
                        configAndRequest.Item1, configAndRequest.Item2);

                // Assert
                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }
            public async Task Returns_201_And_Shipment_If_Request_Authorized_And_Success() {

                // Arange
                var availableShipmentTypeKeys = IntegrationTestHelper.GetKeys(3);
                var shipmentKeys = IntegrationTestHelper.GetKeys(9);
                var affiliateKeys = IntegrationTestHelper.GetKeys(3);

                // Except for first key, all keys are not 
                // related to current Affiliate user
                var validAffiliateKey = affiliateKeys[0];

                var shipmentRequestModel = new ShipmentByAffiliateRequestModel {
                    ShipmentTypeKey = availableShipmentTypeKeys[1],
                    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 configAndRequest = GetConfigAndRequestMessage(shipmentKeys, 
                    affiliateKeys, availableShipmentTypeKeys, validAffiliateKey, shipmentRequestModel);

                // Act
                var shipmentDto = await IntegrationTestHelper
                    .GetResponseMessageBodyAsync<ShipmentDto>(
                        configAndRequest.Item1,
                        configAndRequest.Item2, 
                        HttpStatusCode.Created);

                // Assert
                Assert.Equal(shipmentRequestModel.Price, shipmentDto.Price);
                Assert.Equal(shipmentRequestModel.ReceiverName, shipmentDto.ReceiverName);
                Assert.Equal(shipmentRequestModel.ReceiverEmail, shipmentDto.ReceiverEmail);
                Assert.NotNull(shipmentDto.ShipmentType);
                Assert.True(shipmentDto.ShipmentStates.Count() > 0);
            }