Skip to content

Commit 506f351

Browse files
committed
Consume new endpoint to return last 14 days keys after onboarding
1 parent f9c766f commit 506f351

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ APP_VERSION_NAME=1.0
66
SUBMIT_URL=https://submission.covidshield.app
77
RETRIEVE_URL=https://retrieval.covidshield.app
88
HMAC_KEY=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
9-
REGION=302
10-
MCC_CODE=302;
9+
REGION=302;
1110
TRANSMISSION_RISK_LEVEL=2
1211
MINIMUM_FETCH_INTERVAL=15
1312

src/services/BackendService/BackendService.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {downloadDiagnosisKeysFile} from '../../bridge/CovidShield';
12
import {TemporaryExposureKey} from '../../bridge/ExposureNotification';
23

34
import {BackendService} from './BackendService';
@@ -89,7 +90,7 @@ describe('BackendService', () => {
8990

9091
describe('reportDiagnosisKeys', () => {
9192
it('returns last 14 keys if there is more than 14', async () => {
92-
const backendService = new BackendService('http://localhost', 'https://localhost', 'mock', 0);
93+
const backendService = new BackendService('http://localhost', 'https://localhost', 'mock', 302);
9394
const keys = generateRandomKeys(20);
9495

9596
await backendService.reportDiagnosisKeys(
@@ -115,4 +116,26 @@ describe('BackendService', () => {
115116
});
116117
});
117118
});
119+
120+
describe('retrieveDiagnosisKeys', () => {
121+
it('returns keys file for set period', async () => {
122+
const backendService = new BackendService('http://localhost', 'https://localhost', 'mock', 302);
123+
124+
await backendService.retrieveDiagnosisKeys(18457);
125+
126+
expect(downloadDiagnosisKeysFile).toHaveBeenCalledWith(
127+
'http://localhost/retrieve/302/18457/c4d9820c20f7073e47f54cc1bd24475fb98c8ab9ffc0ea81dded3f8ebfb48b67',
128+
);
129+
});
130+
131+
it('returns keys file for 14 days if period is 0', async () => {
132+
const backendService = new BackendService('http://localhost', 'https://localhost', 'mock', 302);
133+
134+
await backendService.retrieveDiagnosisKeys(0);
135+
136+
expect(downloadDiagnosisKeysFile).toHaveBeenCalledWith(
137+
'http://localhost/retrieve/302/00000/ca365ad512568f4292953403590b398e3f1336efcb4f1acedb20926c35408bd9',
138+
);
139+
});
140+
});
118141
});

src/services/BackendService/BackendService.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {TemporaryExposureKey} from 'bridge/ExposureNotification';
66
import nacl from 'tweetnacl';
77
import {getRandomBytes, downloadDiagnosisKeysFile} from 'bridge/CovidShield';
88
import {blobFetch} from 'shared/fetch';
9-
import {TRANSMISSION_RISK_LEVEL, REGION} from 'env';
9+
import {TRANSMISSION_RISK_LEVEL} from 'env';
1010
import {captureMessage} from 'shared/log';
1111

1212
import {covidshield} from './covidshield';
@@ -15,6 +15,9 @@ import {BackendInterface, SubmissionKeySet} from './types';
1515
const MAX_UPLOAD_KEYS = 14;
1616
const FETCH_HEADERS = {headers: {'Cache-Control': 'no-store'}};
1717

18+
// See https://github.com/cds-snc/covid-shield-server/pull/176
19+
const LAST_14_DAYS_PERIOD = '00000';
20+
1821
export class BackendService implements BackendInterface {
1922
retrieveUrl: string;
2023
submitUrl: string;
@@ -29,15 +32,16 @@ export class BackendService implements BackendInterface {
2932
}
3033

3134
async retrieveDiagnosisKeys(period: number) {
32-
const message = `${this.region}:${period}:${Math.floor(Date.now() / 1000 / 3600)}`;
35+
const periodStr = `${period > 0 ? period : LAST_14_DAYS_PERIOD}`;
36+
const message = `${this.region}:${periodStr}:${Math.floor(Date.now() / 1000 / 3600)}`;
3337
const hmac = hmac256(message, encHex.parse(this.hmacKey)).toString(encHex);
34-
const url = `${this.retrieveUrl}/retrieve/${this.region}/${period}/${hmac}`;
38+
const url = `${this.retrieveUrl}/retrieve/${this.region}/${periodStr}/${hmac}`;
3539
captureMessage('retrieveDiagnosisKeys', {period, url});
3640
return downloadDiagnosisKeysFile(url);
3741
}
3842

3943
async getExposureConfiguration() {
40-
const url = `${this.retrieveUrl}/exposure-configuration/${REGION}.json`;
44+
const url = `${this.retrieveUrl}/exposure-configuration/${this.region}.json`;
4145
captureMessage('getExposureConfiguration', {url});
4246
return (await fetch(url, FETCH_HEADERS)).json();
4347
}

src/services/ExposureNotificationService/ExposureNotificationService.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('ExposureNotificationService', () => {
8282
.mockImplementation((args: any) => new OriginalDate(args));
8383

8484
await service.updateExposureStatus();
85-
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(14);
85+
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(1);
8686
});
8787

8888
it('backfills the right amount of keys for current day', async () => {
@@ -98,7 +98,7 @@ describe('ExposureNotificationService', () => {
9898
},
9999
});
100100
await service.updateExposureStatus();
101-
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(0);
101+
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(1);
102102

103103
server.retrieveDiagnosisKeys.mockClear();
104104

@@ -109,7 +109,7 @@ describe('ExposureNotificationService', () => {
109109
},
110110
});
111111
await service.updateExposureStatus();
112-
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(1);
112+
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(2);
113113

114114
server.retrieveDiagnosisKeys.mockClear();
115115

@@ -120,7 +120,7 @@ describe('ExposureNotificationService', () => {
120120
},
121121
});
122122
await service.updateExposureStatus();
123-
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(2);
123+
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(3);
124124
});
125125

126126
it('serializes status update', async () => {

src/services/ExposureNotificationService/ExposureNotificationService.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {Platform} from 'react-native';
21
import ExposureNotification, {
32
ExposureSummary,
43
Status as SystemStatus,
@@ -263,10 +262,20 @@ export class ExposureNotificationService {
263262
): AsyncGenerator<{keysFileUrl: string; period: number} | null> {
264263
const runningDate = new Date();
265264

266-
const lastCheckedPeriod =
267-
_lastCheckedPeriod || periodSinceEpoch(addDays(runningDate, -EXPOSURE_NOTIFICATION_CYCLE), HOURS_PER_PERIOD);
268265
let runningPeriod = periodSinceEpoch(runningDate, HOURS_PER_PERIOD);
269266

267+
if (!_lastCheckedPeriod) {
268+
try {
269+
const keysFileUrl = await this.backendInterface.retrieveDiagnosisKeys(0);
270+
yield {keysFileUrl, period: runningPeriod};
271+
} catch (error) {
272+
captureException('Error while downloading batch files', error);
273+
}
274+
return;
275+
}
276+
277+
const lastCheckedPeriod = Math.max(_lastCheckedPeriod - 1, runningPeriod - EXPOSURE_NOTIFICATION_CYCLE);
278+
270279
while (runningPeriod > lastCheckedPeriod) {
271280
try {
272281
const keysFileUrl = await this.backendInterface.retrieveDiagnosisKeys(runningPeriod);
@@ -339,12 +348,7 @@ export class ExposureNotificationService {
339348
if (!value) continue;
340349
const {keysFileUrl, period} = value;
341350
keysFileUrls.push(keysFileUrl);
342-
343-
// Temporarily disable persisting lastCheckPeriod on Android
344-
// Ref https://github.com/cds-snc/covid-shield-mobile/issues/453
345-
if (Platform.OS !== 'android') {
346-
lastCheckedPeriod = Math.max(lastCheckedPeriod || 0, period);
347-
}
351+
lastCheckedPeriod = Math.max(lastCheckedPeriod || 0, period);
348352
}
349353

350354
captureMessage('performExposureStatusUpdate', {
@@ -365,11 +369,12 @@ export class ExposureNotificationService {
365369
lastCheckedPeriod,
366370
);
367371
}
372+
return finalize({}, lastCheckedPeriod);
368373
} catch (error) {
369374
captureException('performExposureStatusUpdate', error);
370375
}
371376

372-
return finalize({}, lastCheckedPeriod);
377+
return finalize();
373378
}
374379

375380
private async processPendingExposureSummary() {

0 commit comments

Comments
 (0)