Exemple #1
0
        public async Task GetARideController_Get_WithInvalidTokenAndValidLocationsInBody_MustReturnNewValidToken()
        {
            // Arrange
            var givenAccessToken   = "thisisaninvalidtokenusedfortesting1234";
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/getaride/");
            var mitGetARideRequest = new MitGetARideRequest()
            {
                AccessToken = givenAccessToken,
                AccessTokenExpirationDate = 0,
                From = { Latitude = 39.935912, Longitude = -86.162148 },
                To   = { Latitude = 39.956770, Longitude = -86.040315 }
            };
            var jsonRequest = JsonConvert.SerializeObject(mitGetARideRequest);

            httpRequestMessage.Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

            // Act
            var httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage);

            // Assert
            await EnsureSuccessStatus(httpResponseMessage);

            var mitGetARideResponse = JsonConvert.DeserializeObject <MitGetARideResponse>(await httpResponseMessage.Content.ReadAsStringAsync());

            Assert.AreNotEqual(mitGetARideResponse.AccessToken, givenAccessToken);
        }
        public async Task DomainFacade_GetAllRides_WithNonExpiredAuthorizationToken_ShouldReturnSameToken()
        {
            // Arrange
            var originalTokenValue      = "abcdefghijklmnopqrstuvwxyz";
            var originalTokenExpiration = (long)(DateTime.Now.AddHours(1) - DateTime.UnixEpoch).TotalSeconds;
            var expiredTokenRequest     = new MitGetARideRequest()
            {
                AccessToken = originalTokenValue,
                AccessTokenExpirationDate = originalTokenExpiration,
                From = { Latitude = 37.7763, Longitude = -122.3918 },
                To   = { Latitude = 37.7863, Longitude = -122.3904 }
            };

            _testMediator.SetHttpStatusCode(HttpStatusCode.OK);
            await SetAllExpectedResponsesFromTestJsonFiles();

            // Act
            var result = await _domainFacade.GetAllRides(expiredTokenRequest);

            // Assert
            Assert.IsTrue(result.AccessToken.Equals(originalTokenValue), "A non-expired access token was not used and instead a different" +
                          "token was returned. Had: " + originalTokenValue + " and was " +
                          "returned: " + result.AccessToken);
            Assert.IsTrue(result.AccessTokenExpirationDate == originalTokenExpiration, "A non-expired access token date was not returned " +
                          "in the response and a different date was returned. " +
                          "Expected: " + originalTokenExpiration + " and " +
                          "received back: " + result.AccessTokenExpirationDate);
        }
        private async Task <LyftRideEstimatesResponse> GetRideEstimatesResponse(MitGetARideRequest mitGetARideRequest)
        {
            var apiUrl = ApiUrl + $"cost?start_lat={mitGetARideRequest.From.Latitude}&start_lng={mitGetARideRequest.From.Longitude}&" +
                         $"end_lat={mitGetARideRequest.To.Latitude}&end_lng={mitGetARideRequest.To.Longitude}";

            return(await GetLyftObjectFromLyftApi <LyftRideEstimatesResponse>(apiUrl, mitGetARideRequest.AccessToken));
        }
 public async Task <MitGetARideResponse> Post([FromBody] MitGetARideRequest mitGetARideRequest)
 {
     using (var domain = new DomainFacade())
     {
         return(await domain.GetAllRides(mitGetARideRequest));
     }
 }
Exemple #5
0
        public async Task GetARideController_Get_WhenValidLocationsPassedInBody_MustReturnAtLeastOneRide()
        {
            // Arrange
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/getaride/");
            var mitGetARideRequest = new MitGetARideRequest()
            {
                AccessToken = string.Empty,
                AccessTokenExpirationDate = 0,
                From = { Latitude = 39.935912, Longitude = -86.162148 },
                To   = { Latitude = 39.956770, Longitude = -86.040315 }
            };
            var jsonRequest = JsonConvert.SerializeObject(mitGetARideRequest);

            httpRequestMessage.Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

            // Act
            var httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage);

            // Assert
            await EnsureSuccessStatus(httpResponseMessage);

            var mitGetARideResponse = JsonConvert.DeserializeObject <MitGetARideResponse>(await httpResponseMessage.Content.ReadAsStringAsync());

            Assert.IsTrue(mitGetARideResponse.RideDetails.Count > 0, "Got a response from the server successfully, but it did not contain any rides.");
        }
 public async Task <MitGetARideResponse> Get()
 {
     using (var domain = new DomainFacade())
     {
         var mitGetARideRequest = new MitGetARideRequest();
         return(await domain.GetAllRides(mitGetARideRequest));
     }
 }
        private bool DoesRequestContainsLocations(MitGetARideRequest mitGetARideRequest)
        {
            if (mitGetARideRequest.From.Latitude == 0.0 && mitGetARideRequest.From.Longitude == 0.0)
            {
                return(false);
            }

            return(true);
        }
        protected override async Task <MitGetARideResponse> GetRidesCore(MitGetARideRequest mitGetARideRequest)
        {
            if (string.IsNullOrEmpty(mitGetARideRequest.AccessToken) || AccessTokenIsExpired(mitGetARideRequest))
            {
                await GetAccessToken(mitGetARideRequest);
            }

            return(await GetResponseForRides(mitGetARideRequest));
        }
        private async Task <MitGetARideResponse> GetResponseForRides(MitGetARideRequest mitGetARideRequest)
        {
            var lyftRideTypesResponse = await GetRideTypesResponse(mitGetARideRequest);

            var lyftNearbyDriversResponse = await GetNearbyDriversResponse(mitGetARideRequest);

            var lyftRideEstimatesResponse = await GetRideEstimatesResponse(mitGetARideRequest);

            var lyftDriverEtaResponse = await GetDriverEtaResponse(mitGetARideRequest);

            return(LyftToMitGetARideMapper.GetMitGetARideResponseFromLyftResponses(mitGetARideRequest, lyftRideTypesResponse, lyftDriverEtaResponse, lyftRideEstimatesResponse, lyftNearbyDriversResponse));
        }
        protected override async Task <MitGetARideResponse> GetRidesCore(MitGetARideRequest mitGetARideRequest)
        {
            if (!DoesRequestContainsLocations(mitGetARideRequest))
            {
                return(GetErrorResponse(mitGetARideRequest));
            }

            if (string.IsNullOrEmpty(mitGetARideRequest.AccessToken) || AccessTokenIsExpired(mitGetARideRequest))
            {
                await GetAccessToken(mitGetARideRequest);
            }

            return(await GetResponseForRides(mitGetARideRequest));
        }
        private async Task GetAccessToken(MitGetARideRequest mitGetARideRequest)
        {
            using (var httpRequestMessage = GetAuthenticationHttpRequest())
            {
                var httpResponseMessage = await HttpClient.SendAsync(httpRequestMessage);
                await EnsureSuccessfulStatusCode(httpResponseMessage);

                var httpResponseString = await httpResponseMessage.Content.ReadAsStringAsync();

                var lyftAuthenticationResponse = JsonConvert.DeserializeObject <LyftAuthenticationResponse>(httpResponseString);

                mitGetARideRequest.AccessToken = lyftAuthenticationResponse.AccessToken;
                mitGetARideRequest.AccessTokenExpirationDate = GetCurrentTimeInSeconds() + lyftAuthenticationResponse.ExpiresIn;
            }
        }
        public async Task <MitGetARideResponse> Get(double startLatitude, double startLongitude, double endLatitude, double endLongitude)
        {
            using (var domain = new DomainFacade())
            {
                var rideRequest = new MitGetARideRequest()
                {
                    AccessToken = string.Empty,
                    AccessTokenExpirationDate = 0,
                    From = { Latitude = startLatitude, Longitude = startLongitude },
                    To   = { Latitude = endLatitude, Longitude = endLongitude }
                };

                return(await domain.GetAllRides(rideRequest));
            }
        }
        private MitGetARideResponse GetErrorResponse(MitGetARideRequest mitGetARideRequest)
        {
            var mitGetARideResponse = new MitGetARideResponse()
            {
                ServiceStatus        = "MSG00400",
                ServiceStatusMessage = "Unsupported error with the GetARide request"
            };

            if (!DoesRequestContainsLocations(mitGetARideRequest))
            {
                mitGetARideResponse.ServiceStatus        = "MSG00404";
                mitGetARideResponse.ServiceStatusMessage = "GetARide service requires a FROM and TO location; these values are missing in the request.";
            }

            return(mitGetARideResponse);
        }
        private MitGetARideRequest CreateTestMitGetARideRequest()
        {
            var mitGetARideRequest = new MitGetARideRequest
            {
                AccessToken = "",
                AccessTokenExpirationDate = 0,
                From = new MitGetARideGpsLocation()
                {
                    Latitude = 37.7763, Longitude = -122.3918
                },
                To = new MitGetARideGpsLocation()
                {
                    Latitude = 37.7972, Longitude = -122.4533
                }
            };

            return(mitGetARideRequest);
        }
Exemple #15
0
        public async Task GetARideController_Get_WhenInvalidLocationsPassedInBody_MustReturnTechnicalException()
        {
            // Arrange
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/getaride/");
            var mitGetARideRequest = new MitGetARideRequest()
            {
                AccessToken = string.Empty,
                AccessTokenExpirationDate = 0,
                From = { Latitude = 0.0, Longitude = 100.0 },
                To   = { Latitude = 100.0, Longitude = 0.0 }
            };
            var jsonRequest = JsonConvert.SerializeObject(mitGetARideRequest);

            httpRequestMessage.Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

            // Act
            var httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage);

            // Assert
            Assert.AreEqual(httpResponseMessage.StatusCode, HttpStatusCode.BadRequest);
            await AssertEx.AssertHttpResponseContains(new List <string>() { "no_service_in_area", "not inside a Lyft service area" }, httpResponseMessage);
        }
        public async Task DomainFacade_GetAllRides_WithExpiredAuthorizationToken_ShouldReturnNewToken()
        {
            // Arrange
            var originalTokenValue      = "abcdefghijklmnopqrstuvwxyz";
            var originalTokenExpiration = (long)Math.Floor((DateTime.Now - DateTime.UnixEpoch).TotalSeconds);
            var expiredTokenRequest     = new MitGetARideRequest()
            {
                AccessToken = originalTokenValue,
                AccessTokenExpirationDate = originalTokenExpiration,
                From = { Latitude = 37.7763, Longitude = -122.3918 },
                To   = { Latitude = 37.7863, Longitude = -122.3904 }
            };

            _testMediator.SetHttpStatusCode(HttpStatusCode.OK);
            await SetAllExpectedResponsesFromTestJsonFiles();

            // Act
            var result = await _domainFacade.GetAllRides(expiredTokenRequest);

            // Assert
            Assert.IsFalse(result.AccessTokenExpirationDate == originalTokenExpiration);
            Assert.IsFalse(result.AccessToken.Equals(originalTokenValue));
        }
 protected abstract Task <MitGetARideResponse> GetRidesCore(MitGetARideRequest mitGetARideRequest);
 public async Task <MitGetARideResponse> GetRides(MitGetARideRequest mitGetARideRequest)
 {
     return(await GetRidesCore(mitGetARideRequest));
 }
 private bool AccessTokenIsExpired(MitGetARideRequest mitGetARideRequest)
 {
     return(mitGetARideRequest.AccessTokenExpirationDate <= GetCurrentTimeInSeconds());
 }
        public static MitGetARideResponse GetMitGetARideResponseFromLyftResponses(MitGetARideRequest mitGetARideRequest,
                                                                                  LyftRideTypesResponse lyftRideTypesResponse, LyftDriverEtaResponse lyftDriverEtaResponse,
                                                                                  LyftRideEstimatesResponse lyftRideEstimatesResponse, LyftNearbyDriversResponse lyftNearbyDriversResponse)
        {
            var mitGetARideResponse = new MitGetARideResponse
            {
                AccessToken = mitGetARideRequest.AccessToken,
                AccessTokenExpirationDate            = mitGetARideRequest.AccessTokenExpirationDate,
                AccessManagementLoadBalalncingCookie = string.Empty,
                Alerts                = new List <string>(),
                CompletionCode        = string.Empty,
                CorrelationID         = string.Empty,
                CurrentDateTime       = DateTime.Now,
                ErrorID               = string.Empty,
                InputFieldDefinitions = new List <string>(),
                InsiteStatus          = string.Empty,
                NavigationOptions     = new List <string>(),
                RideDetails           = new List <MitGetARideRideDetail>(),
                ServiceStatus         = "MSG00000",
                ServiceStatusMessage  = string.Empty,
                UserSessionTokenID    = string.Empty
            };

            foreach (var rideType in lyftRideTypesResponse.RideTypes)
            {
                var mitGetARideRideDetail = new MitGetARideRideDetail
                {
                    DeepAppLink = GetDeepAppLink(mitGetARideRequest.From.Latitude, mitGetARideRequest.From.Longitude,
                                                 mitGetARideRequest.To.Latitude, mitGetARideRequest.To.Longitude, rideType.RideType),
                    Description     = rideType.Seats + " seats",
                    DisplayName     = rideType.DisplayName,
                    DriverLocations = new List <MitGetARideGpsDriverLocation>(),
                    RideType        = rideType.RideType
                };


                mitGetARideResponse.RideDetails.Add(mitGetARideRideDetail);
            }

            foreach (var rideType in mitGetARideResponse.RideDetails)
            {
                foreach (var etaType in lyftDriverEtaResponse.EtaEstimates)
                {
                    if (etaType.RideType.Equals(rideType.RideType))
                    {
                        rideType.EstimatedTimeOfArrival = "In " + Math.Floor(etaType.EtaSeconds / 60d) + " min";
                    }
                }
            }

            foreach (var rideType in mitGetARideResponse.RideDetails)
            {
                foreach (var rideEstimate in lyftRideEstimatesResponse.CostEstimates)
                {
                    if (rideEstimate.RideType.Equals(rideType.RideType))
                    {
                        var estimate = (rideEstimate.EstimatedCostCentsMax + rideEstimate.EstimatedCostCentsMin) / 2;
                        var dollars  = Math.Floor(estimate / 100d);
                        var cents    = estimate % 100;
                        rideType.EstimatedCost = $"${dollars}.{cents}";
                    }
                }
            }

            foreach (var rideType in mitGetARideResponse.RideDetails)
            {
                foreach (var nearbyDrivers in lyftNearbyDriversResponse.NearbyDrivers)
                {
                    if (nearbyDrivers.RideType.Equals(rideType.RideType))
                    {
                        foreach (var nearbyDriver in nearbyDrivers.Drivers)
                        {
                            var driverLocation = new MitGetARideGpsDriverLocation
                            {
                                Latitude  = nearbyDriver.Locations[(nearbyDriver.Locations.Count - 1)].Latitude,
                                Longitude = nearbyDriver.Locations[(nearbyDriver.Locations.Count - 1)].Longitude,
                                Direction = GetBearingOfDriver(nearbyDriver.Locations)
                            };
                            rideType.DriverLocations.Add(driverLocation);
                        }
                    }
                }
            }

            return(mitGetARideResponse);
        }
 public async Task <MitGetARideResponse> GetAllRides(MitGetARideRequest mitGetARideRequest)
 {
     return(await GetARideServiceGateway.GetRides(mitGetARideRequest));
 }
        private async Task <LyftDriverEtaResponse> GetDriverEtaResponse(MitGetARideRequest mitGetARideRequest)
        {
            var apiUrl = ApiUrl + $"eta?lat={mitGetARideRequest.From.Latitude}&lng={mitGetARideRequest.From.Longitude}";

            return(await GetLyftObjectFromLyftApi <LyftDriverEtaResponse>(apiUrl, mitGetARideRequest.AccessToken));
        }
Exemple #23
0
 public async Task <MitGetARideResponse> GetAllRides(MitGetARideRequest mitGetARideRequest)
 {
     return(await GetARideManager.GetAllRides(mitGetARideRequest));
 }