public async void DownloadDiagnosisKeysAsyncTests_Success() { var content = new ByteArrayContent(Encoding.UTF8.GetBytes("")); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(@"application/zip"); var client = HttpClientUtils.CreateHttpClient( HttpStatusCode.OK, content ); mockClientService.Setup(x => x.Create()).Returns(client); var entry = new DiagnosisKeyEntry() { Region = 1, Url = "https://example.com/1.zip", Created = 12345678 }; var tmpPath = Path.GetTempPath(); var unitUnderTest = CreateRepository(); var result = await unitUnderTest.DownloadDiagnosisKeysAsync(entry, tmpPath, default); Assert.Equal(Path.Combine(tmpPath, "1.zip"), result); if (File.Exists(result)) { File.Delete(result); } }
public async void GetDiagnosisKeysListAsyncTests_Success() { var client = HttpClientUtils.CreateHttpClient( HttpStatusCode.OK, new StringContent( "[" + " {region: 1, url:\"https://example.com/1\", created: 12345678}, " + " {region: 2, url:\"https://example.com/2\", created: 87654321}" + "]", Encoding.UTF8, "application/json" ) ); mockClientService.Setup(x => x.Create()).Returns(client); var unitUnderTest = CreateRepository(); var(_, result) = await unitUnderTest.GetDiagnosisKeysListAsync("https://example.com", default); Assert.Equal(2, result.Count); Assert.Equal(1, result[0].Region); Assert.Equal("https://example.com/1", result[0].Url); Assert.Equal(12345678, result[0].Created); Assert.Equal(2, result[1].Region); Assert.Equal("https://example.com/2", result[1].Url); Assert.Equal(87654321, result[1].Created); }
public async void DownloadDiagnosisKeysAsyncTests_HttpError(System.Net.HttpStatusCode statusCode) { var content = new ByteArrayContent(Encoding.UTF8.GetBytes("")); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(@"application/zip"); var client = HttpClientUtils.CreateHttpClient( statusCode, content ); mockClientService.Setup(x => x.Create()).Returns(client); var entry = new DiagnosisKeyEntry() { Region = 1, Url = "https://example.com/1.zip", Created = 12345678 }; var tmpPath = Path.GetTempPath(); await Assert.ThrowsAsync <HttpRequestException>(async() => { var unitUnderTest = CreateRepository(); string result = await unitUnderTest.DownloadDiagnosisKeysAsync(entry, tmpPath, default); }); var outputPath = Path.Combine(tmpPath, "1.zip"); if (File.Exists(outputPath)) { File.Delete(outputPath); } }
public async Task GetExposureRiskConfigurationTest_preferCache() { string existConfigurationJson = GetTestJson(JSON_EXPOSURE_RISK_CONFIGURATION1); File.WriteAllText(CURRENT_EXPOSURE_RISK_CONFIGURATION_FILE_PATH, existConfigurationJson); V1ExposureRiskCalculationConfiguration expect = JsonConvert.DeserializeObject <V1ExposureRiskCalculationConfiguration>(existConfigurationJson); string newConfigurationJson = GetTestJson(JSON_EXPOSURE_RISK_CONFIGURATION2); var jsonContent = new StringContent( newConfigurationJson, Encoding.UTF8, "application/json" ); var client = HttpClientUtils.CreateHttpClient(HttpStatusCode.OK, jsonContent); mockClientService.Setup(x => x.Create()).Returns(client); mockLocalPathService.Setup(x => x.ExposureConfigurationDirPath).Returns("./"); mockLocalPathService.Setup(x => x.CurrentExposureRiskCalculationConfigurationPath).Returns(CURRENT_EXPOSURE_RISK_CONFIGURATION_FILE_PATH); mockServerConfigurationRepository.Setup(x => x.ExposureRiskCalculationConfigurationUrl).Returns("https://example.com/exposure_risk_configuration.json"); var unitUnderTest = CreateRepository(); var result = await unitUnderTest.GetExposureRiskCalculationConfigurationAsync(preferCache : true); mockServerConfigurationRepository.Verify(s => s.LoadAsync(), Times.Never()); Assert.NotNull(result); Assert.Equal(result, expect); }
public async void GetDiagnosisKeysListAsyncTests_HttpError(System.Net.HttpStatusCode statusCode) { var client = HttpClientUtils.CreateHttpClient( statusCode, new StringContent("", Encoding.UTF8, "application/json") ); mockClientService.Setup(x => x.Create()).Returns(client); var unitUnderTest = CreateRepository(); var(_, result) = await unitUnderTest.GetDiagnosisKeysListAsync("https://example.com", default); Assert.Equal(0, result.Count); }
public async Task GetExposureConfigurationTest_not_updated_mapping_and_cache_expired() { var date = Date; var cacheExpireDate = date + TimeSpan.FromDays(AppConstants.MinimumDiagnosisKeysDataMappingApplyIntervalDays) + TimeSpan.FromSeconds(1); string testJson1 = GetTestJson(JSON_EXPOSURE_CONFIGURATION1); using (var writer = File.CreateText(CURRENT_EXPOSURE_CONFIGURATION_FILE_PATH)) { await writer.WriteAsync(testJson1); } ExposureConfiguration result1 = JsonConvert.DeserializeObject <ExposureConfiguration>(testJson1); string testJson3 = GetTestJson(JSON_EXPOSURE_CONFIGURATION1); ExposureConfiguration expectedResult = JsonConvert.DeserializeObject <ExposureConfiguration>(testJson3); var jsonContent = new StringContent( testJson3, Encoding.UTF8, "application/json" ); var client = HttpClientUtils.CreateHttpClient(HttpStatusCode.OK, jsonContent); mockClientService.Setup(x => x.Create()).Returns(client); mockLocalPathService.Setup(x => x.ExposureConfigurationDirPath).Returns("./"); mockLocalPathService.Setup(x => x.CurrentExposureConfigurationPath).Returns(CURRENT_EXPOSURE_CONFIGURATION_FILE_PATH); mockServerConfigurationRepository.Setup(x => x.ExposureConfigurationUrl).Returns("https://example.com/exposure_configuration.json"); mockDateTimeUtility.Setup(x => x.UtcNow).Returns(cacheExpireDate); mockPreferencesService.Setup(x => x.GetValue(PreferenceKey.ExposureConfigurationAppliedEpoch, It.IsAny <long>())).Returns(date.ToUnixEpoch()); mockPreferencesService.Setup(x => x.GetValue(PreferenceKey.ExposureConfigurationDownloadedEpoch, It.IsAny <long>())).Returns(date.ToUnixEpoch()); mockPreferencesService.Setup(x => x.GetValue(PreferenceKey.IsDiagnosisKeysDataMappingConfigurationUpdated, false)).Returns(false); var unitUnderTest = CreateRepository(); var result2 = await unitUnderTest.GetExposureConfigurationAsync(); mockPreferencesService.Verify(s => s.SetValue(PreferenceKey.ExposureConfigurationDownloadedEpoch, cacheExpireDate.ToUnixEpoch()), Times.Once()); mockPreferencesService.Verify(s => s.SetValue(PreferenceKey.ExposureConfigurationAppliedEpoch, cacheExpireDate.ToUnixEpoch()), Times.Never()); mockPreferencesService.Verify(s => s.SetValue(PreferenceKey.IsDiagnosisKeysDataMappingConfigurationUpdated, true), Times.Never()); Assert.Equal(result1, result2); Assert.Equal(expectedResult, result2); }
private async void IsUpdate(bool isIos, string version, bool expected) { string content = GetTestJson(JSON_VERSION1); var jsonContent = new StringContent( content, Encoding.UTF8, "application/json" ); var client = HttpClientUtils.CreateHttpClient(HttpStatusCode.OK, jsonContent); _mockClientService.Setup(x => x.Create()).Returns(client); _mockEssentialsService.Setup(x => x.IsIos).Returns(isIos); _mockEssentialsService.Setup(x => x.AppVersion).Returns(version); ICheckVersionService service = CreateService(); bool isUpdated = await service.IsUpdateVersionExistAsync(); Assert.Equal(expected, isUpdated); }
public async Task GetExposureConfigurationTest_firsttime() { var date = Date; string content = GetTestJson(JSON_EXPOSURE_CONFIGURATION1); var jsonContent = new StringContent( content, Encoding.UTF8, "application/json" ); var client = HttpClientUtils.CreateHttpClient(HttpStatusCode.OK, jsonContent); mockClientService.Setup(x => x.Create()).Returns(client); mockLocalPathService.Setup(x => x.ExposureConfigurationDirPath).Returns("./"); mockLocalPathService.Setup(x => x.CurrentExposureConfigurationPath).Returns(CURRENT_EXPOSURE_CONFIGURATION_FILE_PATH); mockServerConfigurationRepository.Setup(x => x.ExposureConfigurationUrl).Returns("https://example.com/exposure_configuration.json"); mockDateTimeUtility.Setup(x => x.UtcNow).Returns(date); var unitUnderTest = CreateRepository(); var result = await unitUnderTest.GetExposureConfigurationAsync(); mockServerConfigurationRepository.Verify(s => s.LoadAsync(), Times.Once()); mockPreferencesService.Verify(s => s.SetValue(PreferenceKey.ExposureConfigurationDownloadedEpoch, date.ToUnixEpoch()), Times.AtLeastOnce()); mockPreferencesService.Verify(s => s.SetValue(PreferenceKey.IsDiagnosisKeysDataMappingConfigurationUpdated, true), Times.AtLeastOnce()); Assert.NotNull(result); Assert.NotNull(result.GoogleExposureConfig); Assert.NotNull(result.AppleExposureConfigV1); // Google ExposureWindow mode Assert.NotNull(result.GoogleDailySummariesConfig); Assert.NotNull(result.GoogleDiagnosisKeysDataMappingConfig); // Apple ENv2 Assert.NotNull(result.AppleExposureConfigV2); }
public async Task GetExposureRiskConfigurationTest_not_updated() { string currentConfigurationJson = GetTestJson(JSON_EXPOSURE_RISK_CONFIGURATION1); File.WriteAllText(CURRENT_EXPOSURE_RISK_CONFIGURATION_FILE_PATH, currentConfigurationJson); V1ExposureRiskCalculationConfiguration currentConfiguration = JsonConvert.DeserializeObject <V1ExposureRiskCalculationConfiguration>(currentConfigurationJson); string newConfigurationJson = GetTestJson(JSON_EXPOSURE_RISK_CONFIGURATION2); V1ExposureRiskCalculationConfiguration newConfiguration = JsonConvert.DeserializeObject <V1ExposureRiskCalculationConfiguration>(newConfigurationJson); var jsonContent = new StringContent( newConfigurationJson, Encoding.UTF8, "application/json" ); var client = HttpClientUtils.CreateHttpClient(HttpStatusCode.OK, jsonContent); mockClientService.Setup(x => x.Create()).Returns(client); mockLocalPathService.Setup(x => x.ExposureConfigurationDirPath).Returns("./"); mockLocalPathService.Setup(x => x.CurrentExposureRiskCalculationConfigurationPath).Returns(CURRENT_EXPOSURE_RISK_CONFIGURATION_FILE_PATH); mockServerConfigurationRepository.Setup(x => x.ExposureRiskCalculationConfigurationUrl).Returns("https://example.com/exposure_risk_configuration.json"); var unitUnderTest = CreateRepository(); var result = await unitUnderTest.GetExposureRiskCalculationConfigurationAsync(preferCache : false); result = await unitUnderTest.GetExposureRiskCalculationConfigurationAsync(preferCache : false); mockServerConfigurationRepository.Verify(s => s.LoadAsync(), Times.Exactly(2)); mockLoggerService.Verify(x => x.Info("ExposureRiskCalculationConfiguration have not been changed.", It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()), Times.Once); Assert.NotNull(result); Assert.NotEqual(result, currentConfiguration); Assert.Equal(result, newConfiguration); }