示例#1
0
        public async Task WillSetLocationFromEnvironmentInfoInfo()
        {
            var resolver = await GetResolverAsync(Log);

            if (resolver is NullGeoIpService)
            {
                return;
            }

            var plugin = new GeoPlugin(resolver, _options);
            var ev     = new PersistentEvent();

            ev.SetEnvironmentInfo(new EnvironmentInfo {
                IpAddress = $"127.0.0.1,{GREEN_BAY_IP}"
            });
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject())
            });

            Assert.NotNull(ev.Geo);

            var location = ev.GetLocation();

            Assert.Equal("US", location?.Country);
            Assert.Equal("WI", location?.Level1);
            Assert.Equal("Green Bay", location?.Locality);
        }
示例#2
0
        public async Task WillSetFromSingleGeo()
        {
            var resolver = await GetResolverAsync(Log);

            if (resolver is NullGeoIpService)
            {
                return;
            }

            var plugin = new GeoPlugin(resolver, _options);

            var contexts = new List <EventContext> {
                new EventContext(new PersistentEvent {
                    Geo = GREEN_BAY_IP
                }, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject()),
                new EventContext(new PersistentEvent {
                    Geo = GREEN_BAY_IP
                }, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject())
            };

            await plugin.EventBatchProcessingAsync(contexts);

            foreach (var context in contexts)
            {
                AssertCoordinatesAreEqual(GREEN_BAY_COORDINATES, context.Event.Geo);

                var location = context.Event.GetLocation();
                Assert.Equal("US", location?.Country);
                Assert.Equal("WI", location?.Level1);
                Assert.Equal("Green Bay", location?.Locality);
            }
        }
示例#3
0
        public async Task WillSetMultipleFromEmptyGeo()
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log));

            var ev            = new PersistentEvent();
            var greenBayEvent = new PersistentEvent();

            greenBayEvent.SetEnvironmentInfo(new EnvironmentInfo {
                IpAddress = GREEN_BAY_IP
            });
            var irvingEvent = new PersistentEvent();

            irvingEvent.SetEnvironmentInfo(new EnvironmentInfo {
                IpAddress = IRVING_IP
            });
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev, OrganizationData.GenerateSampleOrganization(), ProjectData.GenerateSampleProject()),
                new EventContext(greenBayEvent, OrganizationData.GenerateSampleOrganization(), ProjectData.GenerateSampleProject()),
                new EventContext(irvingEvent, OrganizationData.GenerateSampleOrganization(), ProjectData.GenerateSampleProject())
            });

            Assert.Equal(GREEN_BAY_COORDINATES, greenBayEvent.Geo);
            var location = greenBayEvent.GetLocation();

            Assert.Equal("US", location?.Country);
            Assert.Equal("WI", location?.Level1);
            Assert.Equal("Green Bay", location?.Locality);

            Assert.Equal(IRVING_COORDINATES, irvingEvent.Geo);
            location = irvingEvent.GetLocation();
            Assert.Equal("US", location?.Country);
            Assert.Equal("TX", location?.Level1);
            Assert.Equal("Irving", location?.Locality);
        }
        internal Settings GetDefautSettings(IPAddress remoteIpAddress)
        {
            Settings result = new Settings();

            string ip = (remoteIpAddress == null || remoteIpAddress.Equals(IPAddress.IPv6Loopback) || remoteIpAddress.Equals(IPAddress.Loopback) ? GetRemoteIPAdress() : remoteIpAddress.ToString());

            GeoPlugin geo = GetIPGeoPlugin(ip);

            result.Language = GetLanguages(0, 0, string.IsNullOrEmpty(geo.CountryCode) ? "EN" : geo.CountryCode).FirstOrDefault();
            if (result.Language == null)
            {
                result.Language = GetLanguages(0, 0, "EN").FirstOrDefault();
            }
            if (result.Language == null)
            {
                result.Language = new Models.GlobalParts.Lang()
                {
                    Id = 0, Code = "EN", Name = "English"
                }
            }
            ;

            result.Country  = GetCountries(result.Language.Id, 0, string.IsNullOrEmpty(geo.CountryCode) ? "US" : geo.CountryCode.ToLower() == "en" ? "US" : geo.CountryCode).FirstOrDefault();
            result.Currency = GetCurrencies(result.Language.Id, 0, string.IsNullOrEmpty(geo.CurrencyCode) ? "USD" : geo.CurrencyCode).FirstOrDefault();

            return(result);
        }
    }
示例#5
0
        public async Task WillSetFromSingleGeo()
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log));

            var contexts = new List <EventContext> {
                new EventContext(new PersistentEvent {
                    Geo = GREEN_BAY_IP
                }, OrganizationData.GenerateSampleOrganization(), ProjectData.GenerateSampleProject()),
                new EventContext(new PersistentEvent {
                    Geo = GREEN_BAY_IP
                }, OrganizationData.GenerateSampleOrganization(), ProjectData.GenerateSampleProject())
            };

            await plugin.EventBatchProcessingAsync(contexts);

            foreach (var context in contexts)
            {
                Assert.Equal(GREEN_BAY_COORDINATES, context.Event.Geo);

                var location = context.Event.GetLocation();
                Assert.Equal("US", location?.Country);
                Assert.Equal("WI", location?.Level1);
                Assert.Equal("Green Bay", location?.Locality);
            }
        }
示例#6
0
        public async Task WillNotSetFromMultipleGeo()
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log));

            var ev            = new PersistentEvent();
            var greenBayEvent = new PersistentEvent {
                Geo = GREEN_BAY_IP
            };
            var irvingEvent = new PersistentEvent {
                Geo = IRVING_IP
            };
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev),
                new EventContext(greenBayEvent),
                new EventContext(irvingEvent)
            });

            Assert.Equal(GREEN_BAY_COORDINATES, greenBayEvent.Geo);
            var location = greenBayEvent.GetLocation();

            Assert.Equal("US", location?.Country);
            Assert.Equal("WI", location?.Level1);
            Assert.Equal("Green Bay", location?.Locality);

            Assert.Equal(IRVING_COORDINATES, irvingEvent.Geo);
            location = irvingEvent.GetLocation();
            Assert.Equal("US", location?.Country);
            Assert.Equal("TX", location?.Level1);
            Assert.Equal("Irving", location?.Locality);
        }
示例#7
0
        public async Task WillNotSetFromMultipleGeo()
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log), _options);

            var ev            = new PersistentEvent();
            var greenBayEvent = new PersistentEvent {
                Geo = GREEN_BAY_IP
            };
            var irvingEvent = new PersistentEvent {
                Geo = IRVING_IP
            };
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject()),
                new EventContext(greenBayEvent, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject()),
                new EventContext(irvingEvent, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject())
            });

            Assert.Equal(GREEN_BAY_COORDINATES, greenBayEvent.Geo);
            var location = greenBayEvent.GetLocation();

            Assert.Equal("US", location?.Country);
            Assert.Equal("WI", location?.Level1);
            Assert.Equal("Green Bay", location?.Locality);

            Assert.Equal(IRVING_COORDINATES, irvingEvent.Geo);
            location = irvingEvent.GetLocation();
            Assert.Equal("US", location?.Country);
            Assert.Equal("TX", location?.Level1);
            Assert.Equal("Irving", location?.Locality);
        }
示例#8
0
        public async Task WillNotSetLocation()
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log));
            var ev     = new PersistentEvent {
                Geo = GREEN_BAY_COORDINATES
            };
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev, OrganizationData.GenerateSampleOrganization(), ProjectData.GenerateSampleProject())
            });

            Assert.Equal(GREEN_BAY_COORDINATES, ev.Geo);
            Assert.Null(ev.GetLocation());
        }
示例#9
0
        public async Task WillNotSetLocation()
        {
            var plugin = new GeoPlugin(await GetResolverAsync());
            var ev     = new PersistentEvent {
                Geo = GREEN_BAY_COORDINATES
            };
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev)
            });

            Assert.Equal(GREEN_BAY_COORDINATES, ev.Geo);
            Assert.Null(ev.GetLocation());
        }
示例#10
0
        public async Task WillResetLocation(string geo)
        {
            var plugin = new GeoPlugin(await GetResolverAsync());

            var ev = new PersistentEvent {
                Geo = geo
            };
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev)
            });

            Assert.Null(ev.Geo);
            Assert.Null(ev.GetLocation());
        }
示例#11
0
        public async Task WillResetLocation(string geo)
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log));

            var ev = new PersistentEvent {
                Geo = geo
            };
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev, OrganizationData.GenerateSampleOrganization(), ProjectData.GenerateSampleProject())
            });

            Assert.Null(ev.Geo);
            Assert.Null(ev.GetLocation());
        }
示例#12
0
        public async Task WillSetLocationFromGeo()
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log));
            var ev     = new PersistentEvent {
                Geo = GREEN_BAY_IP
            };
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev, OrganizationData.GenerateSampleOrganization(), ProjectData.GenerateSampleProject())
            });

            Assert.NotNull(ev.Geo);
            Assert.NotEqual(GREEN_BAY_IP, ev.Geo);

            var location = ev.GetLocation();

            Assert.Equal("US", location?.Country);
            Assert.Equal("WI", location?.Level1);
            Assert.Equal("Green Bay", location?.Locality);
        }
示例#13
0
        public async Task WillSetLocationFromRequestInfo()
        {
            var plugin = new GeoPlugin(await GetResolverAsync());
            var ev     = new PersistentEvent();

            ev.AddRequestInfo(new RequestInfo {
                ClientIpAddress = GREEN_BAY_IP
            });
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev)
            });

            Assert.NotNull(ev.Geo);

            var location = ev.GetLocation();

            Assert.Equal("US", location?.Country);
            Assert.Equal("WI", location?.Level1);
            Assert.Equal("Green Bay", location?.Locality);
        }
示例#14
0
        public async Task WillSetLocationFromRequestInfo()
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log), _options);
            var ev     = new PersistentEvent();

            ev.AddRequestInfo(new RequestInfo {
                ClientIpAddress = GREEN_BAY_IP
            });
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject())
            });

            Assert.NotNull(ev.Geo);

            var location = ev.GetLocation();

            Assert.Equal("US", location?.Country);
            Assert.Equal("WI", location?.Level1);
            Assert.Equal("Green Bay", location?.Locality);
        }
示例#15
0
        public async Task WillSetLocationFromEnvironmentInfoInfo()
        {
            var plugin = new GeoPlugin(await GetResolverAsync(Log));
            var ev     = new PersistentEvent();

            ev.SetEnvironmentInfo(new EnvironmentInfo {
                IpAddress = $"127.0.0.1,{GREEN_BAY_IP}"
            });
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev)
            });

            Assert.NotNull(ev.Geo);

            var location = ev.GetLocation();

            Assert.Equal("US", location?.Country);
            Assert.Equal("WI", location?.Level1);
            Assert.Equal("Green Bay", location?.Locality);
        }
示例#16
0
        public async Task WillResetLocation(string geo)
        {
            var resolver = await GetResolverAsync(Log);

            if (resolver is NullGeoIpService)
            {
                return;
            }

            var plugin = new GeoPlugin(resolver, _options);
            var ev     = new PersistentEvent {
                Geo = geo
            };
            await plugin.EventBatchProcessingAsync(new List <EventContext> {
                new EventContext(ev, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject())
            });

            Assert.Null(ev.Geo);
            Assert.Null(ev.GetLocation());
        }
        private GeoPlugin GetIPGeoPlugin(string ip)
        {
            GeoPlugin result = null;

            TryCatch(() =>
            {
                string data = "";
                var req     = WebRequest.Create(@"http://www.geoplugin.net/json.gp?ip=" + ip);
                {
                    using (var reader = new StreamReader(req.GetResponse().GetResponseStream()))
                    {
                        data = reader.ReadToEnd();
                    }
                }
                result = JsonSerializer.Deserialize <GeoPlugin>(data);
                if (result.Status != 200)
                {
                    if (string.IsNullOrEmpty(result.CountryCode))
                    {
                        result.CountryCode = "EN";
                    }
                    if (string.IsNullOrEmpty(result.CurrencyCode))
                    {
                        result.CurrencyCode = "USD";
                    }
                }
            },
                     (ex) =>
            {
                result = new GeoPlugin()
                {
                    CountryCode = "EN", CurrencyCode = "USD"
                };
            });
            return(result);
        }