public void Can_Get_Geolocation_Details_By_URL()
        {
            // Arrange
            GeolocationDetails sampleModel = new GeolocationDetails()
            {
                URL = "sample URL"
            };
            GetGeolocationDetailsByUrlReturnModel sampleViewModel = new GetGeolocationDetailsByUrlReturnModel(sampleModel);

            Mock <IGeolocationDetailsManager> mockManager = new Mock <IGeolocationDetailsManager>();

            mockManager.Setup(x => x.GetByUrl(It.IsAny <string>())).Returns(sampleViewModel);

            Mock <ILocationValidator> mockLocationValidator = new Mock <ILocationValidator>();

            mockLocationValidator.Setup(x => x.IsValidUrl(It.IsAny <string>())).Returns(true);

            var controller = new GeolocationController(mockManager.Object, mockLocationValidator.Object);

            // Act
            var actionResult = controller.Get(null, "url") as OkNegotiatedContentResult <GetGeolocationDetailsByUrlReturnModel>;

            // Assert
            Assert.IsNotNull(actionResult);
            Assert.AreEqual("sample URL", actionResult.Content.URL);
        }
        public void Can_Create_Geolocation_Details_With_URL()
        {
            // Arrange
            GeolocationDetails sampleDetails = new GeolocationDetails()
            {
                URL = "URL"
            };
            CreateGeolocationDetailsWithUrlReturnModel sampleControllerModel = new CreateGeolocationDetailsWithUrlReturnModel(sampleDetails);

            Mock <IGeolocationDetailsManager> mockManger = new Mock <IGeolocationDetailsManager>();

            mockManger.Setup(x => x.CreateWithUrlAsync(It.IsAny <string>())).Returns(Task.FromResult(sampleControllerModel));

            Mock <ILocationValidator> mockLocationValidator = new Mock <ILocationValidator>();

            mockLocationValidator.Setup(x => x.IsValidUrl(It.IsAny <string>())).Returns(true);

            var controller = new GeolocationController(mockManger.Object, mockLocationValidator.Object);

            // Act
            var actionResult = controller.PostAsync("test").Result as CreatedNegotiatedContentResult <CreateGeolocationDetailsWithUrlReturnModel>;

            // Assert
            Assert.IsNotNull(actionResult);
            Assert.AreEqual("URL", actionResult.Content.URL);
        }
        public void Can_Get_Geolocation_Details_By_IP()
        {
            // Arrange
            GeolocationDetails sampleModel = new GeolocationDetails()
            {
                IP = "sample IP"
            };
            GetGeolocationDetailsByIpReturnModel sampleViewModel = new GetGeolocationDetailsByIpReturnModel(sampleModel);

            Mock <IGeolocationDetailsManager> mockManager = new Mock <IGeolocationDetailsManager>();

            mockManager.Setup(x => x.GetByIp(It.IsAny <string>())).Returns(sampleViewModel);

            Mock <ILocationValidator> mockLocationValidator = new Mock <ILocationValidator>();

            mockLocationValidator.Setup(x => x.IsValidIpAddress(It.IsAny <string>())).Returns(true);

            var controller = new GeolocationController(mockManager.Object, mockLocationValidator.Object);

            // Act
            var actionResult = controller.Get("ip", null) as OkNegotiatedContentResult <GetGeolocationDetailsByIpReturnModel>;

            // Assert
            Assert.IsNotNull(actionResult);
            Assert.AreEqual("sample IP", actionResult.Content.IP);
        }
        private async Task <GeolocationDetails> CreateDetailsAsync(string ipOrUrl, GeolocationDetails newItem)
        {
            try
            {
                string accessKey = ipStackConfiguration.GetAccessKey();

                if (string.IsNullOrWhiteSpace(accessKey))
                {
                    throw new ApplicationException("Missing IP Stack access key setting.");
                }

                Interfaces.Data.GeolocationDetails details = await geolocationDetailsProvider.GetAsync(accessKey, ipOrUrl);

                if (details.Success == false)
                {
                    HandleUnsuccessfulDetailsRequest(details);
                }

                newItem.City        = details.City;
                newItem.CountryName = details.CountryName;
                newItem.ZipCode     = details.ZipCode;

                db.GeolocationDetails.Add(newItem);
                db.SaveChanges();

                return(newItem);
            }
            catch (Exception ex)
            {
                logger.LogError(this, ex);
                throw;
            }
        }
        public async Task <CreateGeolocationDetailsWithUrlReturnModel> CreateWithUrlAsync(string url)
        {
            var existingDetails = db.GeolocationDetails.SingleOrDefault(x => x.URL == url);

            if (existingDetails != null)
            {
                return(new CreateGeolocationDetailsWithUrlReturnModel(existingDetails));
            }

            var newItem = new GeolocationDetails()
            {
                URL = url
            };

            GeolocationDetails model = await CreateDetailsAsync(url, newItem);

            return(new CreateGeolocationDetailsWithUrlReturnModel(model));
        }
        public async Task <CreateGeolocationDetailsWithIpReturnModel> CreateWithIpAsync(string ip)
        {
            var existingDetails = db.GeolocationDetails.SingleOrDefault(x => x.IP == ip);

            if (existingDetails != null)
            {
                return(new CreateGeolocationDetailsWithIpReturnModel(existingDetails));
            }

            var newItem = new GeolocationDetails()
            {
                IP = ip
            };

            GeolocationDetails model = await CreateDetailsAsync(ip, newItem);

            return(new CreateGeolocationDetailsWithIpReturnModel(model));
        }
 public CreateGeolocationDetailsWithUrlReturnModel(GeolocationDetails model)
     : base(model)
 {
     URL = model.URL;
 }
Esempio n. 8
0
 public GetGeolocationDetailsByIpReturnModel(GeolocationDetails model) : base(model)
 {
     IP = model.IP;
 }
Esempio n. 9
0
 public GetGeolocationDetailsByUrlReturnModel(GeolocationDetails model) : base(model)
 {
     URL = model.URL;
 }
 protected AbstractGetGeolocationDetailsReturnModel(GeolocationDetails model)
 {
     City        = model.City;
     CountryName = model.CountryName;
     ZipCode     = model.ZipCode;
 }
 public CreateGeolocationDetailsWithIpReturnModel(GeolocationDetails model) : base(model)
 {
     IP = model.IP;
 }