예제 #1
0
        private async Task <VISITORS> PopulateVisitorInformationAsync(VISITORS visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }
            if (string.IsNullOrWhiteSpace(visitor.IP))
            {
                throw new ArgumentException("Cannot be space, empty or null", nameof(visitor.IP));
            }

            var httpRequest  = new HttpRequestMessage(HttpMethod.Get, $"{_baseUrl}/json/{visitor.IP}");
            var httpClient   = _httpClientFactory.CreateClient();
            var httpResponse = await httpClient.SendAsync(httpRequest);

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                return(visitor);
            }

            JObject jobject = JObject.Parse(await httpResponse.Content.ReadAsStringAsync());

            visitor.CITY    = jobject.GetValue("city", StringComparison.OrdinalIgnoreCase)?.Value <string>();
            visitor.COUNTRY = jobject.GetValue("countryCode", StringComparison.OrdinalIgnoreCase)?.Value <string>();
            //visitor.IP = jobject.GetValue("ip", StringComparison.OrdinalIgnoreCase)?.Value<string>();
            visitor.ISP    = jobject.GetValue("isp", StringComparison.OrdinalIgnoreCase)?.Value <string>();
            visitor.REGION = jobject.GetValue("region", StringComparison.OrdinalIgnoreCase)?.Value <string>();
            //visitor.CREATED_ON_UTC = DateTime.UtcNow;

            return(visitor);
        }
예제 #2
0
        public void WhenValidVisitorViewModel_ExpectValidVisitor()
        {
            // Assemble
            var expectedVisitor = new VISITORS()
            {
                CITY              = "a",
                COUNTRY           = "b",
                IP                = "c",
                ISP               = "d",
                REGION            = "e",
                CLIENT_BROWSER    = "f",
                CLIENT_HEADERS    = "g",
                CLIENT_OS         = "h",
                CLIENT_USER_AGENT = "i",
                CREATED_ON_UTC    = DateTime.UtcNow
            };

            var viewModel = new VisitorViewModel()
            {
                City            = expectedVisitor.CITY,
                ClientBrowser   = expectedVisitor.CLIENT_BROWSER,
                ClientHeaders   = expectedVisitor.CLIENT_HEADERS,
                ClientOS        = expectedVisitor.CLIENT_OS,
                ClientUserAgent = expectedVisitor.CLIENT_USER_AGENT,
                Country         = expectedVisitor.COUNTRY,
                CreatedUtc      = expectedVisitor.CREATED_ON_UTC,
                Ip     = expectedVisitor.IP,
                Isp    = expectedVisitor.ISP,
                Region = expectedVisitor.REGION
            };

            // Act
            var actualVisitor = _mapper.Map <VISITORS>(viewModel);

            // Assert
            Assert.NotNull(actualVisitor);
            Assert.Equal(expectedVisitor.CITY, actualVisitor.CITY);
            Assert.Equal(expectedVisitor.CLIENT_BROWSER, actualVisitor.CLIENT_BROWSER);
            Assert.Equal(expectedVisitor.CLIENT_HEADERS, actualVisitor.CLIENT_HEADERS);
            Assert.Equal(expectedVisitor.CLIENT_OS, actualVisitor.CLIENT_OS);
            Assert.Equal(expectedVisitor.COUNTRY, actualVisitor.COUNTRY);
            Assert.Equal(expectedVisitor.IP, actualVisitor.IP);
            Assert.Equal(expectedVisitor.REGION, actualVisitor.REGION);
            Assert.Equal(expectedVisitor.CLIENT_USER_AGENT, actualVisitor.CLIENT_USER_AGENT);
            Assert.Equal(expectedVisitor.ISP, actualVisitor.ISP);
            Assert.Equal(expectedVisitor.CREATED_ON_UTC, actualVisitor.CREATED_ON_UTC);
        }
예제 #3
0
        public void WhenValidVisitor_ExpectValidVisitorViewModel()
        {
            // Assemble
            var visitor = new VISITORS()
            {
                CITY              = "a",
                COUNTRY           = "b",
                IP                = "c",
                ISP               = "d",
                REGION            = "e",
                CLIENT_BROWSER    = "f",
                CLIENT_HEADERS    = "g",
                CLIENT_OS         = "h",
                CLIENT_USER_AGENT = "i",
                CREATED_ON_UTC    = DateTime.UtcNow
            };

            var expectedVisitorViewModel = new VisitorViewModel()
            {
                City            = visitor.CITY,
                ClientBrowser   = visitor.CLIENT_BROWSER,
                ClientHeaders   = visitor.CLIENT_HEADERS,
                ClientOS        = visitor.CLIENT_OS,
                ClientUserAgent = visitor.CLIENT_USER_AGENT,
                Country         = visitor.COUNTRY,
                CreatedUtc      = visitor.CREATED_ON_UTC,
                Ip     = visitor.IP,
                Isp    = visitor.ISP,
                Region = visitor.REGION
            };

            // Act
            var actualVisitorViewModel = _mapper.Map <VisitorViewModel>(visitor);

            // Assert
            Assert.NotNull(actualVisitorViewModel);
            Assert.Equal(expectedVisitorViewModel.City, actualVisitorViewModel.City);
            Assert.Equal(expectedVisitorViewModel.ClientBrowser, actualVisitorViewModel.ClientBrowser);
            Assert.Equal(expectedVisitorViewModel.ClientHeaders, actualVisitorViewModel.ClientHeaders);
            Assert.Equal(expectedVisitorViewModel.ClientOS, actualVisitorViewModel.ClientOS);
            Assert.Equal(expectedVisitorViewModel.Country, actualVisitorViewModel.Country);
            Assert.Equal(expectedVisitorViewModel.Ip, actualVisitorViewModel.Ip);
            Assert.Equal(expectedVisitorViewModel.Region, actualVisitorViewModel.Region);
            Assert.Equal(expectedVisitorViewModel.ClientUserAgent, actualVisitorViewModel.ClientUserAgent);
            Assert.Equal(expectedVisitorViewModel.Isp, actualVisitorViewModel.Isp);
            Assert.Equal(expectedVisitorViewModel.CreatedUtc, actualVisitorViewModel.CreatedUtc);
        }
예제 #4
0
        public async Task HandleVisitorAsync(VISITORS visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }

            // Checks if localhost / already exists
            // if (visitor.IP == "localhost" || visitor.IP == "::1") return;
            if (visitor.IP == "localhost" || visitor.IP == "::1")
            {
                visitor.IP = GetRandomIpAddress();
            }
            if (_visitorRepository.Exists(visitor.IP))
            {
                return;
            }

            visitor = await PopulateVisitorInformationAsync(visitor);

            await _visitorRepository.AddAsync(visitor);
        }
예제 #5
0
        public void WhenValidIP_ExpectValidVisitor()
        {
            // Assemble
            VISITORS expectedVisitor = new VISITORS()
            {
                CITY    = "string",
                COUNTRY = "string",
                IP      = "string",
                ISP     = "string",
                REGION  = "string"
            };

            HttpResponseMessage httpResponse = new HttpResponseMessage();

            httpResponse.Content    = new StringContent(expectedVisitor.ToJson());
            httpResponse.StatusCode = HttpStatusCode.OK;

            var httpHandler = new Mock <DelegatingHandler>();

            httpHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(httpResponse)
            .Verifiable();
            httpHandler.As <IDisposable>().Setup(s => s.Dispose());

            var httpClient = new HttpClient(httpHandler.Object);

            _httpClientFactory.Setup(cf => cf.CreateClient(It.IsAny <string>())).Returns(httpClient);

            var service = new VisitorService(_configuration, _httpClientFactory.Object, _visitorRepository.Object);

            // Act
            service.HandleVisitor(expectedVisitor);

            // Assert
            // Not useful test - simply does not throws exception
        }
예제 #6
0
        // TODO: use unit of work instead to save changes
        public async Task <int> AddAsync(VISITORS visitor)
        {
            await _dbSet.AddAsync(visitor);

            return(await _db.SaveChangesAsync());
        }
예제 #7
0
 // TODO: use unit of work instead to save changes
 public int Add(VISITORS visitor)
 {
     _dbSet.Add(visitor);
     return(_db.SaveChanges());
 }