Пример #1
0
        public void DefaultCountryUrlTest()
        {
            using var httpClient = new HttpClient();
            var dao = HttpDao.NewDao("env", new ApiKeyTokenClient("apiKey"), httpClient, null, null, null);

            Assert.AreEqual("https://portal-backend.incountry.com/countries", dao.CountriesEndPoint);
        }
Пример #2
0
        public void GenerateAudienceForTokenClientTest(string endpoint, string endpointMask,
                                                       FakeHttpServerResponse serverResponse, string midPopAudience, string miniPopAudience)
        {
            const string   midPopCountryCode  = "us";
            const string   miniPopCountryCode = "pu";
            const string   recordKey          = "someRecordKey";
            FakeHttpServer server             = null;

            if (!string.IsNullOrEmpty(serverResponse?.Body))
            {
                server = new FakeHttpServer(Port, new[] { serverResponse });
                server.Start();
            }

            using var httpClient = new HttpClient { Timeout = new TimeSpan(0, 0, 30) };
            var tokenClient = new FakeTokenClient();

            using var dao = HttpDao.NewDao(EnvId, tokenClient, httpClient, endpoint, endpointMask, s_endpoint);
            //midpop
            Assert.ThrowsAsync <StorageServerException>(async() =>
                                                        await dao.ReadRecordAsync(midPopCountryCode, recordKey).ConfigureAwait(false));
            Assert.AreEqual(midPopAudience, tokenClient.Audience);
            //minipop
            Assert.ThrowsAsync <StorageServerException>(async() =>
                                                        await dao.ReadRecordAsync(miniPopCountryCode, recordKey).ConfigureAwait(false));
            Assert.AreEqual(miniPopAudience, tokenClient.Audience);
            server?.Stop();
        }
Пример #3
0
        public void NetworkConnectionErrorTest()
        {
            const string country = "us";

            using var httpClient = new HttpClient { Timeout = new TimeSpan(0, 0, 1) };
            using var httpDao    = HttpDao.NewDao("envId", new ApiKeyTokenClient("apiKey"),
                                                  httpClient, s_endpoint);
            Assert.IsNotNull(httpDao);
            var exception = Assert.ThrowsAsync <StorageServerException>(async() =>
            {
                await httpDao.CreateBatchAsync(country, new List <TransferRecord> {
                    new TransferRecord("recordKey1")
                })
                .ConfigureAwait(false);
            });

            CheckNetworkException(exception);

            exception = Assert.ThrowsAsync <StorageServerException>(async() =>
            {
                await httpDao.CreateRecordAsync(country, new TransferRecord("recordKey1"))
                .ConfigureAwait(false);
            });
            CheckNetworkException(exception);

            exception = Assert.ThrowsAsync <StorageServerException>(async() =>
            {
                await httpDao.ReadRecordAsync(country, "recordKey1")
                .ConfigureAwait(false);
            });
            CheckNetworkException(exception);

            var filters =
                new Dictionary <string, object>
            {
                ["record_key"] = new StringFilter(new[] { "key" }).ToTransferObject()
            };

            exception = Assert.ThrowsAsync <StorageServerException>(async() =>
            {
                await httpDao.FindRecordsAsync(country, new TransferFilterContainer(filters, 1, 1, null))
                .ConfigureAwait(false);
            });
            CheckNetworkException(exception);

            exception = Assert.ThrowsAsync <StorageServerException>(async() =>
            {
                await httpDao.DeleteRecordAsync(country, "recordKey1")
                .ConfigureAwait(false);
            });
            CheckNetworkException(exception);
        }
Пример #4
0
        public void DefaultParametersTest()
        {
            var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, CountryLoadResponse) });

            server.Start();
            using var httpClient = new HttpClient();
            using var dao        = HttpDao.NewDao("envId", new ApiKeyTokenClient("apiKey"), httpClient, null, "test.localhost",
                                                  s_endpoint);
            var popList = dao.GetPopDictionaryClone();

            Assert.AreEqual(2, popList.Count);
            server.Stop();
        }
Пример #5
0
        public void ReLoadCountryListTest()
        {
            const string shortCountryListJson = "{\n" +
                                                "  \"countries\": [\n" +
                                                "    {\n" +
                                                "      \"direct\": true,\n" +
                                                "      \"id\": \"US\",\n" +
                                                "      \"name\": \"United States\",\n" +
                                                "      \"region\": \"amer\",\n" +
                                                "      \"status\": \"active\",\n" +
                                                "      \"type\": \"mid\"\n" +
                                                "    }]" +
                                                "}";
            var shortCountryListResponse = new FakeHttpServerResponse(200, shortCountryListJson);
            var longCountryListResponse  = new FakeHttpServerResponse(200, CountryLoadResponse);

            var server = new FakeHttpServer(Port,
                                            new[] { shortCountryListResponse, longCountryListResponse });

            using var httpClient = new HttpClient { Timeout = new TimeSpan(0, 0, 1) };
            server.Start();

            using var dao = HttpDao.NewDao(EnvId, new ApiKeyTokenClient("token"), httpClient,
                                           endPointMask: ".localhost",
                                           countriesEndPoint: s_endpoint, updateInterval: 1);
            var popList = dao.GetPopDictionaryClone();

            Assert.IsNotNull(popList);
            Assert.AreEqual(1, popList.Count);
            Assert.AreEqual("US", popList[0].Id);
            Thread.Sleep(2_000);
            Assert.ThrowsAsync <StorageServerException>(async() =>
                                                        await dao.DeleteRecordAsync("us", Guid.NewGuid().ToString()).ConfigureAwait(false));
            popList = dao.GetPopDictionaryClone();
            Assert.IsNotNull(popList);
            Assert.AreEqual(2, popList.Count);
            server.Stop();
        }
Пример #6
0
        public void GetEndPointTest()
        {
            var server = new FakeHttpServer(Port,
                                            new[]
            {
                new FakeHttpServerResponse(200, CountryLoadResponse),
                //for getting token to midpop
                new FakeHttpServerResponse(200, TokenResponse),
                //for getting token to minipop
                new FakeHttpServerResponse(200, TokenResponse)
            });

            using var httpClient = new HttpClient { Timeout = new TimeSpan(0, 0, 30) };
            server.Start();
            var tokenClient = new OAuthTokenClient(s_endpoint, null, EnvId, "clientId", "clientSecret", httpClient);

            using var dao = HttpDao.NewDao(EnvId, tokenClient, httpClient, null, "-localhost:123", s_endpoint);

            const string midPopCountry = "us";
            var          exception     = Assert.ThrowsAsync <StorageServerException>(async() =>
                                                                                     await dao.CreateRecordAsync(midPopCountry, new TransferRecord("recordKey")).ConfigureAwait(false));

            Assert.AreEqual("Unexpected error [https://us-localhost:123/v2/storage/records/us]", exception.Message);
            Assert.IsNotNull(exception.InnerException);
            Assert.IsInstanceOf <HttpRequestException>(exception.InnerException);

            const string miniPopCountry = "miniPopCountryCode";

            exception = Assert.ThrowsAsync <StorageServerException>(async() =>
                                                                    await dao.CreateRecordAsync(miniPopCountry, new TransferRecord("recordKey")).ConfigureAwait(false));
            Assert.AreEqual("Unexpected error [https://us-localhost:123/v2/storage/records/miniPopCountryCode]",
                            exception.Message);
            Assert.IsNotNull(exception.InnerException);
            Assert.IsInstanceOf <HttpRequestException>(exception.InnerException);

            server.Stop();
        }
Пример #7
0
        private Storage(StorageConfig config)
        {
            s_helper.Check <StorageClientException>(config == null, Messages.Storage.s_errNullConfig);
            _httpClient = new HttpClient();
            _httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(VersionInfo.ProductName,
                                                                                       VersionInfo.ProductVersion));
#pragma warning disable CA1062
            _httpClient.Timeout = new TimeSpan(0, 0, config.HttpTimeout);
#pragma warning restore CA1062
            ITokenClient tokenClient = null;
            if (!string.IsNullOrEmpty(config.ApiKey))
            {
                tokenClient = new ApiKeyTokenClient(config.ApiKey);
            }

            if (!(string.IsNullOrEmpty(config.ClientId) || string.IsNullOrEmpty(config.ClientSecret)))
            {
                tokenClient = new OAuthTokenClient(config.DefaultAuthEndpoint, config.AuthEndpoints,
                                                   config.EnvironmentId, config.ClientId, config.ClientSecret, _httpClient);
            }


            s_helper.Check <StorageClientException>(tokenClient == null, Messages.Storage.s_errNullCredentials);
            _cryptoProvider = config.CryptoProvider;
            if (config.CryptoProvider == null)
            {
                _cryptoProvider = new CryptoProvider();
            }

            _cryptoProvider.ValidateCustomCiphers(config.SecretKeyAccessor?.Invoke());
            _hashUtils   = new HashUtils(config.EnvironmentId, config.NormalizeKeys, Encoding.UTF8);
            _transformer = new DtoTransformer(_cryptoProvider, _hashUtils, config.HashSearchKeys,
                                              config.SecretKeyAccessor);
            _dao = HttpDao.NewDao(config.EnvironmentId, tokenClient, _httpClient, config.EndPoint, config.EndpointMask,
                                  config.CountriesEndPoint);
        }
Пример #8
0
        public async Task AuthorisationRetryTest()
        {
            const string tokenValue1    = "12345";
            const string tokenResponse1 =
                "{'access_token':'" + tokenValue1 + "' , 'expires_in':'300' , 'token_type':'bearer', 'scope':'" +
                EnvId +
                "'}";
            const string tokenValue2    = "67890";
            const string tokenResponse2 =
                "{'access_token':'" + tokenValue2 + "' , 'expires_in':'300' , 'token_type':'bearer', 'scope':'" +
                EnvId +
                "'}";
            var server = new FakeHttpServer(Port,
                                            new[]
            {
                new FakeHttpServerResponse(200, tokenResponse1),
                new FakeHttpServerResponse(401, "Unauthorized"),
                new FakeHttpServerResponse(200, tokenResponse2), new FakeHttpServerResponse(201, "OK")
            });

            using var httpClient = new HttpClient();
            server.Start();
            var tokenClient = new OAuthTokenClient(s_endpoint, null, EnvId, "clientId", "clientSecret", httpClient);

            using var dao = HttpDao.NewDao(EnvId, tokenClient, httpClient, s_endpoint, ".localhost:" + Port,
                                           s_endpoint);
            const string country = "us";
            await dao.CreateRecordAsync(country, new TransferRecord("recordKey")).ConfigureAwait(false);

            var finalToken = await tokenClient
                             .RefreshTokenAsync(false, s_endpoint + " https://" + country + ".localhost:" + Port, "emea")
                             .ConfigureAwait(false);

            Assert.AreEqual(tokenValue2, finalToken);
            server.Stop();
        }