public async Task StatusCode_PullKeys_ShouldUpdateOrNotLastUpdatedTimestamp(HttpStatusCode httpStatus) { ApiResponse <Stream> apiResponse = new ApiResponse <Stream>("test", HttpMethod.Get) { StatusCode = (int)httpStatus, }; ExposureNotificationWebService notificationWebService = Mock.Of <ExposureNotificationWebService>(b => b.GetFileAsStreamAsync(It.IsAny <string>()) == Task.FromResult(apiResponse)); Mock.Get(notificationWebService).CallBase = true; ApiResponse <Stream> response = await notificationWebService.GetDiagnosisKeys("dummyDate", CancellationToken.None); List <HttpStatusCode> errorStatusCodesList = new List <HttpStatusCode> { HttpStatusCode.InternalServerError, HttpStatusCode.BadRequest }; if (errorStatusCodesList.Contains(httpStatus)) { //MessageLastUpdateDateTime should not be updated Assert.Equal(DateTime.MinValue.ToLocalTime(), MessagesViewModel.LastUpdateDateTime); } else { //MessageLastUpdateDateTime must be updated Assert.NotEqual(DateTime.MinValue.ToLocalTime(), MessagesViewModel.LastUpdateDateTime); } }
public async void PullKeys_FetchKeys_NoNewKeysForToday() { ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4).HttpStatusCode(204), }); //Given today is day1 SystemTime.SetDateTime(day1); //Given last time we pulled was day, batch3. _helper.SetLastPulledDate(day1, 3); //When pulling keys List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //There were no new keys Assert.Empty(zipLocations); //A warning was logged List <LogSQLiteModel> logs = await _logManager.GetLogs(10); Assert.Single(logs); Assert.Contains("204 No Content", logs[0].Description); Assert.Equal(LogSeverity.WARNING.ToString(), logs[0].Severity); Assert.Contains("204 No Content", _developerTools.LastPullHistory); }
public async void PullKeys_FetchRestOfKeysFromYesterday_NoErrors() { ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4).HttpStatusCode(204), new PullKeysMockData(day2, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(false), new PullKeysMockData(day2, 3).HttpStatusCode(204), }); _preferences.Set(PULL_BATCH_TYPE, "all"); //Given today is day2 SystemTime.SetDateTime(day2); //Given last time we pulled was day3, batch2. _helper.SetLastPulledDate(day1, 2); //When pulling keys List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull the rest from day 1, and all from day 2 Assert.Equal(3, zipLocations.Count); Assert.Equal(2, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is saved from header Assert.False((await _logManager.GetLogs(10)).Any()); //And no errors were logged //Asuming submission of keys went well: LocalPreferencesHelper.UpdateLastPullKeysSucceededDateTime(); //The next day, when pulling again ExposureNotificationWebService mockedService2 = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4).HttpStatusCode(204), new PullKeysMockData(day2, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day2, 4).HttpStatusCode(204), new PullKeysMockData(day3, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day3, 4).HttpStatusCode(204), }); SystemTime.SetDateTime(day3); List <string> zipLocations2 = (await new ZipDownloader().PullNewKeys(mockedService2, new CancellationToken())).ToList(); //It will fetch the rest of day 2 (batch 3) and all from day 3 (batch 1-3). Assert.Equal(4, zipLocations2.Count); Assert.Equal(3, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is saved from header Assert.False((await _logManager.GetLogs(10)).Any()); //And no errors were logged }
[InlineData("2020-08-26 00:30 +2")] //This is still the 25th in UTC. public async void PullKeys_FetchZipsForMultipleDays_NoErrors(string todayString) { int lastBatchNumFromHeader = 5; //Given there are 3 batches for day1, 3 batches for day 2, 3 batches for day3 ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4).HttpStatusCode(204), new PullKeysMockData(day2, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day2, 4).HttpStatusCode(204), new PullKeysMockData(day3, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 3).HttpStatusCode(200).WithLastBatchHeader(lastBatchNumFromHeader).WithMoreBatchesExistHeader(false), new PullKeysMockData(day3, 4).HttpStatusCode(204), }); //Given today is day3 DateTime newToday = DateTime.ParseExact(todayString, "yyyy-MM-dd HH:mm z", CultureInfo.GetCultureInfo("nn-NO")); SystemTime.SetDateTime(newToday); _preferences.Set(PULL_BATCH_TYPE, "all"); //Given last time we pulled was day3, batch2. _helper.SetLastPulledDate(day1, 2); //When pulling keys _developerTools.StartPullHistoryRecord(); List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull the rest from day 1, all from day 2, and all from day 3 Assert.Equal(7, zipLocations.Count); //The last batch number is saved from header Assert.Equal(lastBatchNumFromHeader, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //And no errors were logged Assert.False((await _logManager.GetLogs(10)).Any()); //The history is stored for dev tools: string expected = $"Pulled the following keys (batches) at {newToday.ToUniversalTime().ToString("yyyy-MM-dd HH:mm")} UTC:\n" + $"* 2020-08-23_3_all.zip: 200 OK\n" + $"* 2020-08-24_1_all.zip: 200 OK\n" + $"* 2020-08-24_2_all.zip: 200 OK\n" + $"* 2020-08-24_3_all.zip: 200 OK\n" + $"* 2020-08-25_1_all.zip: 200 OK\n" + $"* 2020-08-25_2_all.zip: 200 OK\n" + $"* 2020-08-25_3_all.zip: 200 OK"; Assert.Equal(expected, _developerTools.LastPullHistory); Assert.Equal(expected, _developerTools.AllPullHistory); }
public PushKeysTests() { DependencyInjectionConfig.Init(); ApiStubHelper.StartServer(); JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Converters = new JsonConverter[] { new JsonMockConverter() } }; _developerToolsService = ServiceLocator.Current.GetInstance <IDeveloperToolsService>(); _exposureNotificationWebService = new ExposureNotificationWebService(); }
public async void PullKeys_FirstTime() { int lastBatchNumberFromHeader = 6; //Keys exist for more than 14 days back ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(fifteenDaysAgo, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(fifteenDaysAgo, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(fifteenDaysAgo, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(fifteenDaysAgo, 4).HttpStatusCode(204), new PullKeysMockData(forteenDaysAgo, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(forteenDaysAgo, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(forteenDaysAgo, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(forteenDaysAgo, 4).HttpStatusCode(204), new PullKeysMockData(day1, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4).HttpStatusCode(204), new PullKeysMockData(day2, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day2, 4).HttpStatusCode(204), new PullKeysMockData(day3, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 3).HttpStatusCode(200).WithLastBatchHeader(lastBatchNumberFromHeader).WithMoreBatchesExistHeader(false), new PullKeysMockData(day3, 4).HttpStatusCode(204), }); //Given today is day3 and there has never been pulled before. SystemTime.SetDateTime(day3); //When pulling keys List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then only today is pulled Assert.Equal(3, zipLocations.Count); Assert.Equal(lastBatchNumberFromHeader, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is saved from header Assert.False((await _logManager.GetLogs(10)).Any()); //And no errors were logged }
public async void PullKeys_LastFetch15DaysAgo() { //Keys exist for more than 14 days back ExposureNotificationWebService mockedService = _helper.MockedService(SixteenDaysOfKeys()); //Given today is day3 SystemTime.SetDateTime(day3); //Given last time we pulled was 14 days ago _helper.SetLastPulledDate(fifteenDaysAgo, 1); //When pulling keys List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then only pull the last 14 days' keys Assert.Equal(14 * 3, zipLocations.Count); Assert.Equal(3, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is saved from header Assert.False((await _logManager.GetLogs(10)).Any()); //And no errors were logged }
public async void DownloadZips_ShouldReturnProperState(int statusCode, bool isAborted, bool isNotificationShown) { _localNotificationsManager.HasBeenCalled = false; ExposureNotificationWebService exposureNotificationWebService = Mock.Of <ExposureNotificationWebService>( service => service.GetDiagnosisKeys( It.IsAny <string>(), It.IsAny <CancellationToken>()) == Task.FromResult(new ApiResponse <Stream>("", HttpMethod.Get) { Data = null, Endpoint = "", Exception = null, Headers = null, ResponseText = null, StatusCode = statusCode })); await MockZipDownloader().PullNewKeys(exposureNotificationWebService, CancellationToken.None); Assert.Equal(isNotificationShown, _localNotificationsManager.HasBeenCalled); _localNotificationsManager.HasBeenCalled = false; }
public async void PullKeys_LastFetch16DaysAgoWithGaps() { await _logManager.DeleteAll(); // Fetch one time successfully 16 days ago ExposureNotificationWebService mockedService1 = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(sixteenDaysAgoForGapsTest, 1, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(sixteenDaysAgoForGapsTest, 2, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(sixteenDaysAgoForGapsTest, 3, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(sixteenDaysAgoForGapsTest, 4, BatchType.NO).HttpStatusCode(204), }); SystemTime.SetDateTime(sixteenDaysAgoForGapsTest); List <string> zipLocations1 = (await new ZipDownloader().PullNewKeys(mockedService1, new CancellationToken())).ToList(); // Assert three files were downloaded 15 days ago Assert.Equal(3, zipLocations1.Count); // Update local preferenced Last pull suceeded time to simulate successful submission to EN API LocalPreferencesHelper.UpdateLastPullKeysSucceededDateTime(); //Keys exist for more than 14 days back and there are gaps ExposureNotificationWebService mockedService2 = _helper.MockedService(SixteenDaysOfKeysWithGaps()); //Given today is day4 SystemTime.SetDateTime(day4); //When pulling keys List <string> zipLocations2 = (await new ZipDownloader().PullNewKeys(mockedService2, new CancellationToken())).ToList(); //Then only pull the last 14 days' keys Assert.Equal(14 * 3 / 2, zipLocations2.Count); Assert.Equal(3, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is saved from header Assert.False((await _logManager.GetLogs(10)).Any()); //And no errors were logged }
/// <summary> /// Fetches the new keys if any. /// </summary> /// <returns>Paths to the temporary location where the zips are stored</returns> public async Task <IEnumerable <string> > PullNewKeys(ExposureNotificationWebService service, CancellationToken cancellationToken) { PullKeysParams requestParams = PullKeysParams.GenerateParams(); LocalPreferencesHelper.DidFirstFileOfTheDayEndedWith204 = false; List <string> zipLocations = new List <string>(); bool lastPull = false; int? lastBatchReceived = null; int? lastReceivedStatusCodeFromRequest = null; while (!lastPull) { string requestUrl = requestParams.ToBatchFileRequest(); ApiResponse <Stream> response = await service.GetDiagnosisKeys(requestUrl, cancellationToken); HttpHeaders headers = response.Headers; lastReceivedStatusCodeFromRequest = response.StatusCode; bool headersAreValid = true; if (response == null || (!response.IsSuccessfull)) { if (response?.StatusCode == 410) { NotificationsHelper.CreateNotification(NotificationsEnum.ApiDeprecated, 0); string warning = "410 Api was deprecated"; _developerTools.AddToPullHistoryRecord(warning, requestUrl); LogUtils.LogMessage(LogSeverity.WARNING, $"{_logPrefix}.{nameof(DownloadZips)}: {warning}"); } else { //Failed to fetch new keys due to server error. This is already logged in the webservice. _developerTools.AddToPullHistoryRecord($"{response.StatusCode} Server Error", requestUrl); } break; //Abort pulling } // If the server says 204: No Content, it means that there were no new keys (I.e. the request batch does not exist) if (response.StatusCode == 204) { if (requestParams.Date.Date < SystemTime.Now().Date) { //If there were no new keys for a day which is not today, then move on to fetch keys for the next date. requestParams.Date = requestParams.Date.AddDays(1); requestParams.BatchNumber = 1; lastPull = false; } else { //There were no new keys to fetch for today _developerTools.AddToPullHistoryRecord($"204 No Content - No new keys", requestUrl); string warning = $"API {response.Endpoint} returned 204 No Content - No new keys since last pull"; LogUtils.LogMessage(LogSeverity.WARNING, $"{_logPrefix}.{nameof(DownloadZips)}: {warning}"); lastPull = true; } } else { try { int lastBatchReceivedValue = int.Parse(headers.GetValues(LastBatchReturnedHeader).First()); bool moreBatchesExist = bool.Parse(headers.GetValues(MoreBatchesExistHeader).First()); //If both headers parse (no exceptions), then save lastBatchValue to be persisted lastBatchReceived = lastBatchReceivedValue; if (moreBatchesExist) { //There are still more batches to fetch for the given date requestParams.BatchNumber = (int)lastBatchReceived + 1; lastPull = false; } else if (requestParams.Date.Date < SystemTime.Now().Date) { //If there were no new keys for a day which is not today, then move on to fetch keys for the next date. requestParams.Date = requestParams.Date.AddDays(1); requestParams.BatchNumber = 1; lastPull = false; } else { //There are no more batches to fetch for today. Try again in some hours. lastPull = true; } } catch (Exception e) { headersAreValid = false; HandleErrorWhenPulling(e, $"Failed to parse {MoreBatchesExistHeader} or {LastBatchReturnedHeader} header.", requestUrl); break; //Abort pulling } } // Copy the zip stream in the response into a temp file if (response.StatusCode == 200 && headersAreValid) { try { _developerTools.AddToPullHistoryRecord("200 OK", requestUrl); string tmpFile = Path.Combine(ServiceLocator.Current.GetInstance <IFileSystem>().CacheDirectory, Guid.NewGuid() + ".zip"); FileStream tmpFileStream = File.Create(tmpFile); await response.Data.CopyToAsync(tmpFileStream); tmpFileStream.Close(); zipLocations.Add(tmpFile); } catch (Exception e) { HandleErrorWhenPulling(e, "Failed to save zip locally", requestUrl); break; //Abort pulling } } } if (zipLocations.Any() && lastBatchReceived != null) { //Persist the last batch that was fetched, to know which one to fetch next time the background task runs. LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted = (int)lastBatchReceived; //Also save the last batchtype fetched LocalPreferencesHelper.LastPulledBatchType = requestParams.BatchType; } // Edge case for when pulling across multiple days ends up in 204 for the first file if (requestParams.Date.Date == SystemTime.Now().Date && requestParams.BatchNumber == 1 && lastReceivedStatusCodeFromRequest == 204) { LocalPreferencesHelper.DidFirstFileOfTheDayEndedWith204 = true; } return(zipLocations); }
public async void PullKeys_NewTermsWereApproved() { int lastBatchNumNO = 4; int lastBatchNumALL = 5; //Both NO and EU keys exist for the last 3 days ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4, BatchType.NO).HttpStatusCode(204), new PullKeysMockData(day1, 1, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4, BatchType.ALL).HttpStatusCode(204), new PullKeysMockData(day2, 1, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 2, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 3, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day2, 4, BatchType.NO).HttpStatusCode(204), new PullKeysMockData(day2, 1, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 2, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 3, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day2, 4, BatchType.ALL).HttpStatusCode(204), new PullKeysMockData(day3, 1, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 2, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 3, BatchType.NO).HttpStatusCode(200).WithLastBatchHeader(lastBatchNumNO).WithMoreBatchesExistHeader(false), new PullKeysMockData(day3, 4, BatchType.NO).HttpStatusCode(204), new PullKeysMockData(day3, 5, BatchType.NO).HttpStatusCode(204), new PullKeysMockData(day3, 1, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 2, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 3, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(lastBatchNumALL).WithMoreBatchesExistHeader(false), new PullKeysMockData(day3, 4, BatchType.ALL).HttpStatusCode(204), new PullKeysMockData(day3, 5, BatchType.NO).HttpStatusCode(204), }); //Given today is day2 AND that the new terms have not been approved. SystemTime.SetDateTime(day3); //Given last time we pulled was day1, batch1. _helper.SetLastPulledDate(day1, 1); //When pulling keys List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull all NO keys Assert.Equal(8, zipLocations.Count); Assert.Equal(lastBatchNumNO, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is saved from header Assert.False((await _logManager.GetLogs(10)).Any()); //And no errors were logged //Prepare for new pull LocalPreferencesHelper.UpdateLastPullKeysSucceededDateTime(); await _logManager.DeleteAll(); _developerTools.ClearAllFields(); //When accepting the term OnboardingStatusHelper.Status = OnboardingStatus.CountriesOnboardingCompleted; //And then pull again zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull all EU keys but only for today Assert.Equal(3, zipLocations.Count); Assert.Equal(lastBatchNumALL, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is saved from header Assert.False((await _logManager.GetLogs(10)).Any()); //And no errors were logged //Next time it will also pull EU keys: LocalPreferencesHelper.UpdateLastPullKeysSucceededDateTime(); PullKeysParams newParams = PullKeysParams.GenerateParams(); Assert.Equal(BatchType.ALL, newParams.BatchType); }
public async void PullKeys_MissingOrBadHeader() { //Given that the MoreBatchesExist header is missing (batch 3) ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3).HttpStatusCode(200).WithLastBatchHeader(3), new PullKeysMockData(day1, 4).HttpStatusCode(204), }); //Given today is day1 SystemTime.SetDateTime(day1); //Given last time we pulled was day, batch3. _helper.SetLastPulledDate(day1, 1); //When pulling keys List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then the missing header is considered a failed pull, only batch 2 was successfull Assert.Single(zipLocations); Assert.Equal(2, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is saved from header //An error was logged List <LogSQLiteModel> logs = await _logManager.GetLogs(10); Assert.Single(logs); string expectedErrorMessage = $"Failed to parse {ZipDownloader.MoreBatchesExistHeader} or {ZipDownloader.LastBatchReturnedHeader} header."; Assert.Contains(expectedErrorMessage, logs[0].Description); Assert.Equal(LogSeverity.ERROR.ToString(), logs[0].Severity); Assert.Contains(expectedErrorMessage, _developerTools.LastPullHistory); //Prepare for new pull LocalPreferencesHelper.UpdateLastPullKeysSucceededDateTime(); await _logManager.DeleteAll(); _developerTools.ClearAllFields(); //If LastBatchHeader is missing ExposureNotificationWebService mockedService2 = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3).HttpStatusCode(200).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4).HttpStatusCode(204), }); //When pulling keys List <string> zipLocations2 = (await new ZipDownloader().PullNewKeys(mockedService2, new CancellationToken())).ToList(); //Then the missing header is considered a failed pull, there were no successfull new pulls Assert.Empty(zipLocations2); Assert.Equal(2, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //The last batch number is not updated //An error was logged logs = await _logManager.GetLogs(10); Assert.Single(logs); Assert.Contains(expectedErrorMessage, logs[0].Description); Assert.Equal(LogSeverity.ERROR.ToString(), logs[0].Severity); Assert.Contains(expectedErrorMessage, _developerTools.LastPullHistory); }
public async void PullKeys_FetchZipsForMultipleDays_UnknownErrorFromServer(int errorCode) { //Given there are 3 batches for day3 ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day3, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 3).HttpStatusCode(errorCode), new PullKeysMockData(day3, 4).HttpStatusCode(204), }); //Given today is day3 SystemTime.SetDateTime(day3); //Given last time we pulled was day3, batch2. _helper.SetLastPulledDate(day3, 1); //When pulling keys _developerTools.StartPullHistoryRecord(); List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull 1 good zip before the last one fails Assert.Single(zipLocations); //The last successful batch number is saved from header Assert.Equal(2, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //No error was logged because the service layer will handle that. Assert.False((await _logManager.GetLogs(10)).Any()); //But the error is printed to developer tools Assert.Contains("Server Error", _developerTools.LastPullHistory); //Asuming submission of keys went well: LocalPreferencesHelper.UpdateLastPullKeysSucceededDateTime(); //When pulling the next time, where no errors are returned. ExposureNotificationWebService mockedService2 = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day3, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day3, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day3, 4).HttpStatusCode(204), }); _developerTools.StartPullHistoryRecord(); List <string> zipLocations2 = (await new ZipDownloader().PullNewKeys(mockedService2, new CancellationToken())).ToList(); //Only the missing batches are fetched Assert.Single(zipLocations2); Assert.Equal(3, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //No errors Assert.False((await _logManager.GetLogs(10)).Any()); Assert.Contains("200 OK", _developerTools.LastPullHistory); //The history is stored for dev tools: string day3string = day3.ToUniversalTime().ToString("yyyy-MM-dd HH:mm"); string lastPullHistoryExpected = $"Pulled the following keys (batches) at {day3string} UTC:\n" + $"* 2020-08-25_3_no.zip: 200 OK"; Assert.Equal(lastPullHistoryExpected, _developerTools.LastPullHistory); string AllPullHistoryExpected = $"Pulled the following keys (batches) at {day3string} UTC:\n" + $"* 2020-08-25_2_no.zip: 200 OK\n" + $"* 2020-08-25_3_no.zip: {errorCode} Server Error\n\n" + $"Pulled the following keys (batches) at {day3string} UTC:\n" + $"* 2020-08-25_3_no.zip: 200 OK"; Assert.Equal(AllPullHistoryExpected, _developerTools.AllPullHistory); }
public async void PullKeys_FetchZipsTwoDays() { //Given there are 3 batches for day1, 3 batches for day 2, 3 batches for day3 ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 4).HttpStatusCode(204), new PullKeysMockData(day2, 1).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 2).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 3).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(false), new PullKeysMockData(day2, 4).HttpStatusCode(204), }); //Given today is day1 SystemTime.SetDateTime(day1); //Given last time we pulled was day1, batch1. _helper.SetLastPulledDate(day1, 1); //When pulling keys _developerTools.StartPullHistoryRecord(); List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull the rest from day 1 Assert.Equal(2, zipLocations.Count); //The last batch number is saved from header Assert.Equal(3, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //And no errors were logged Assert.False((await _logManager.GetLogs(10)).Any()); //The history is stored for dev tools: string expected = $"Pulled the following keys (batches) at {day1.ToString("yyyy-MM-dd HH:mm")} UTC:\n" + $"* 2020-08-23_2_no.zip: 200 OK\n" + $"* 2020-08-23_3_no.zip: 200 OK"; Assert.Equal(expected, _developerTools.LastPullHistory); LocalPreferencesHelper.UpdateLastPullKeysSucceededDateTime(); //Now it's the next day SystemTime.SetDateTime(day2); //And I pull again _developerTools.StartPullHistoryRecord(); zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull everything from day 2 Assert.Equal(3, zipLocations.Count); //The last batch number is saved from header Assert.Equal(3, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //And no errors were logged Assert.False((await _logManager.GetLogs(10)).Any()); string expected2 = $"Pulled the following keys (batches) at {day2.ToString("yyyy-MM-dd HH:mm")} UTC:\n" + $"* 2020-08-24_1_no.zip: 200 OK\n" + $"* 2020-08-24_2_no.zip: 200 OK\n" + $"* 2020-08-24_3_no.zip: 200 OK"; Assert.Equal(expected2, _developerTools.LastPullHistory); }
public async void PullKeys_FetchZipsForMultipleDaysWithEUFilesWithFreshInstall_FirstFileOfTodaysDateReturns204() { string todayString = "2020-08-24 01:30 +1"; int lastBatchNumFromHeader = 4; OnboardingStatusHelper.Status = OnboardingStatus.CountriesOnboardingCompleted; //Given there are 4 batches for day1, 0 batches for day 2 ExposureNotificationWebService mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day1, 1, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 2, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 3, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(3).WithMoreBatchesExistHeader(true), new PullKeysMockData(day1, 4, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(4).WithMoreBatchesExistHeader(false), new PullKeysMockData(day1, 5, BatchType.ALL).HttpStatusCode(204), new PullKeysMockData(day2, 1, BatchType.ALL).HttpStatusCode(204), }); //Given today is day2 DateTime newToday = DateTime.ParseExact(todayString, "yyyy-MM-dd HH:mm z", CultureInfo.GetCultureInfo("nn-NO")); SystemTime.SetDateTime(newToday); //Given last time we pulled was day1, batch2. _helper.SetLastPulledDate(day1, 2); //When pulling keys _developerTools.StartPullHistoryRecord(); List <string> zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull the rest from day 1, none from day 2 Assert.Equal(4, zipLocations.Count); //The last batch number is saved as one since today's first pull ended up in 204 Assert.Equal(lastBatchNumFromHeader, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //And 1 warning was logged List <LogSQLiteModel> log = await _logManager.GetLogs(10); Assert.Single(log); // Should be one log entry indicating 204 on today's date //The history is stored for dev tools: string expected = $"Pulled the following keys (batches) at {newToday.ToUniversalTime().ToString("yyyy-MM-dd HH:mm")} UTC:\n" + $"* 2020-08-23_1_all.zip: 200 OK\n" + $"* 2020-08-23_2_all.zip: 200 OK\n" + $"* 2020-08-23_3_all.zip: 200 OK\n" + $"* 2020-08-23_4_all.zip: 200 OK\n" + $"* 2020-08-24_1_all.zip: 204 No Content - No new keys"; Assert.Equal(expected, _developerTools.LastPullHistory); Assert.Equal(expected, _developerTools.AllPullHistory); // Emulate successful submission of the keys to EN API _developerTools.AddToPullHistoryRecord("Zips were successfully submitted to EN API."); if (LocalPreferencesHelper.DidFirstFileOfTheDayEndedWith204) { LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted = 0; } LocalPreferencesHelper.UpdateLastPullKeysSucceededDateTime(); LocalPreferencesHelper.DidFirstFileOfTheDayEndedWith204 = false; Assert.Equal(0, LocalPreferencesHelper.LastPullKeysBatchNumberSuccessfullySubmitted); // Emulate pull in couple of hours that ends up in 200 OK SystemTime.SetDateTime(SystemTime.Now().AddHours(5)); lastBatchNumFromHeader = 2; mockedService = _helper.MockedService(new List <PullKeysMockData> { new PullKeysMockData(day2, 1, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(1).WithMoreBatchesExistHeader(true), new PullKeysMockData(day2, 2, BatchType.ALL).HttpStatusCode(200).WithLastBatchHeader(2).WithMoreBatchesExistHeader(false), new PullKeysMockData(day2, 3, BatchType.ALL).HttpStatusCode(204), }); zipLocations = (await new ZipDownloader().PullNewKeys(mockedService, new CancellationToken())).ToList(); //Then we pull the rest from day 1, none from day 2 Assert.Equal(2, zipLocations.Count); //The last batch number is saved as one since today's first pull ended up in 204 Assert.Equal(lastBatchNumFromHeader, LocalPreferencesHelper.LastPullKeysBatchNumberNotSubmitted); //Clean up log await _logManager.DeleteAll(); }