示例#1
0
        public static ResultCode AddGeolocation(string ipOrUrl)
        {
            if (GeolocationDao.GetGeolocationByIpOrHost(ipOrUrl) != null)
            {
                return(ResultCode.RecordAlreadyExists);
            }
            string response = null;

            try
            {
                response = IpStackService.GetIpStack(ipOrUrl).Result;
            }
            catch (Exception e)
            {
                log.Error("Failed to get geolocation from IPStack", e);
                return(ResultCode.ServiceUnavailable);
            }
            if (response == null)
            {
                return(ResultCode.ServiceUnavailable);
            }
            IpStack ipStack = null;

            try
            {
                ipStack = IpStack.FromJson(response);
            }
            catch (Exception e)
            {
                log.Error("Failed to parse json from IPStack", e);
                return(ResultCode.UnexpectedError);
            }
            if (ipStack.Latitude == null || ipStack.Longitude == null)
            {
                return(ResultCode.UrlNotFound);
            }
            var geolocation = IpStackService.GetGeolocationFromIpStack(ipStack, ipOrUrl);

            try
            {
                if (GeolocationDao.Insert(geolocation))
                {
                    return(ResultCode.OK);
                }
            }
            catch (Exception)
            {
                return(ResultCode.DatabaseError);
            }
            return(ResultCode.UnexpectedError);
        }
示例#2
0
        public void Test()
        {
            var testGeolocation = new Geolocation()
            {
                Ip        = "ip",
                Latitude  = 1,
                Longitude = 2,
                Location  = "secret test location"
            };
            var result = GeolocationDao.Insert(testGeolocation);

            Assert.IsTrue(result);
            var geolocationResult = GeolocationDao.GetGeolocationByIp("ip");

            Assert.AreEqual(testGeolocation.Ip, geolocationResult.Ip);
            Assert.AreEqual(testGeolocation.Host, geolocationResult.Host);
            Assert.AreEqual(testGeolocation.Latitude, geolocationResult.Latitude);
            Assert.AreEqual(testGeolocation.Longitude, geolocationResult.Longitude);
            Assert.AreEqual(testGeolocation.Location, geolocationResult.Location);

            Assert.IsNull(GeolocationDao.GetGeolocationByIpOrHost(null));
            Assert.IsNull(GeolocationDao.GetGeolocationByIpOrHost(""));
            Assert.IsNull(GeolocationDao.GetGeolocationByIpOrHost("ip2"));

            Assert.IsFalse(GeolocationDao.DeleteByHost(null));
            Assert.IsFalse(GeolocationDao.DeleteByHost(""));
            Assert.IsFalse(GeolocationDao.DeleteByHost("ip"));
            Assert.IsFalse(GeolocationDao.DeleteByIp(null));
            Assert.IsFalse(GeolocationDao.DeleteByIp(""));
            Assert.IsFalse(GeolocationDao.DeleteByIp("test ip"));
            Assert.IsTrue(GeolocationDao.DeleteByIp("ip"));

            Assert.IsNull(GeolocationDao.GetGeolocationByIp("ip"));

            testGeolocation = new Geolocation()
            {
                Ip        = "ip",
                Host      = "host",
                Latitude  = 1,
                Longitude = 2,
                Location  = "secret test location"
            };
            result = GeolocationDao.Insert(testGeolocation);
            Assert.IsTrue(result);

            Assert.IsNotNull(GeolocationDao.GetGeolocationByIpOrHost("host"));
            Assert.IsTrue(GeolocationDao.DeleteByHost("host"));
            Assert.IsNull(GeolocationDao.GetGeolocationByIpOrHost("host"));
        }
示例#3
0
        public void DeleteGeolocationTest()
        {
            var geolocation = new Geolocation()
            {
                Host      = "host",
                Ip        = "ip2",
                Latitude  = 0,
                Longitude = 0,
                Location  = "location"
            };

            GeolocationDao.Insert(geolocation);
            var response = _controller.DeleteGeolocation(geolocation.Host);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsFalse(GeolocationDao.DeleteByHost(geolocation.Host));
        }
示例#4
0
        public void GetGeolocationTest()
        {
            var geolocation = new Geolocation()
            {
                Host      = "host",
                Ip        = "ip",
                Latitude  = 0,
                Longitude = 0,
                Location  = "location"
            };

            GeolocationDao.Insert(geolocation);
            var response = _controller.GetGeolocation(geolocation.Host);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            var result = response.Content.ReadAsAsync <Geolocation>().Result;

            Assert.AreEqual(geolocation.Host, result.Host);
            Assert.AreEqual(geolocation.Ip, result.Ip);
            Assert.AreEqual(geolocation.Latitude, result.Latitude);
            Assert.AreEqual(geolocation.Longitude, result.Longitude);
            Assert.AreEqual(geolocation.Location, result.Location);
        }