Skip to content

Commit

Permalink
Consume new endpoint to return last 14 days keys after onboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
henrytao-me committed Aug 28, 2020
1 parent f9c766f commit 6bc368b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ APP_VERSION_NAME=1.0
SUBMIT_URL=https://submission.covidshield.app
RETRIEVE_URL=https://retrieval.covidshield.app
HMAC_KEY=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
REGION=302
MCC_CODE=302;
REGION=302;
TRANSMISSION_RISK_LEVEL=2
MINIMUM_FETCH_INTERVAL=15

Expand Down
2 changes: 1 addition & 1 deletion src/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const RETRIEVE_URL = Config.RETRIEVE_URL;

export const HMAC_KEY = Config.HMAC_KEY;

export const REGION = parseInt(Config.REGION, 10);
export const REGION = parseInt(Config.REGION, 10) || 302;

export const TRANSMISSION_RISK_LEVEL = parseInt(Config.TRANSMISSION_RISK_LEVEL, 10);

Expand Down
25 changes: 24 additions & 1 deletion src/services/BackendService/BackendService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {downloadDiagnosisKeysFile} from '../../bridge/CovidShield';
import {TemporaryExposureKey} from '../../bridge/ExposureNotification';

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

describe('reportDiagnosisKeys', () => {
it('returns last 14 keys if there is more than 14', async () => {
const backendService = new BackendService('http://localhost', 'https://localhost', 'mock', 0);
const backendService = new BackendService('http://localhost', 'https://localhost', 'mock', undefined);
const keys = generateRandomKeys(20);

await backendService.reportDiagnosisKeys(
Expand All @@ -115,4 +116,26 @@ describe('BackendService', () => {
});
});
});

describe('retrieveDiagnosisKeys', () => {
it('returns keys file for set period', async () => {
const backendService = new BackendService('http://localhost', 'https://localhost', 'mock', undefined);

await backendService.retrieveDiagnosisKeys(18457);

expect(downloadDiagnosisKeysFile).toHaveBeenCalledWith(
'http://localhost/retrieve/302/18457/c4d9820c20f7073e47f54cc1bd24475fb98c8ab9ffc0ea81dded3f8ebfb48b67',
);
});

it('returns keys file for 14 days if period is 0', async () => {
const backendService = new BackendService('http://localhost', 'https://localhost', 'mock', undefined);

await backendService.retrieveDiagnosisKeys(0);

expect(downloadDiagnosisKeysFile).toHaveBeenCalledWith(
'http://localhost/retrieve/302/00000/ca365ad512568f4292953403590b398e3f1336efcb4f1acedb20926c35408bd9',
);
});
});
});
8 changes: 6 additions & 2 deletions src/services/BackendService/BackendService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {BackendInterface, SubmissionKeySet} from './types';
const MAX_UPLOAD_KEYS = 14;
const FETCH_HEADERS = {headers: {'Cache-Control': 'no-store'}};

// See https://github.com/cds-snc/covid-shield-server/pull/176
const LAST_14_DAYS_PERIOD = '00000';

export class BackendService implements BackendInterface {
retrieveUrl: string;
submitUrl: string;
Expand All @@ -29,9 +32,10 @@ export class BackendService implements BackendInterface {
}

async retrieveDiagnosisKeys(period: number) {
const message = `${this.region}:${period}:${Math.floor(Date.now() / 1000 / 3600)}`;
const periodStr = `${period > 0 ? period : LAST_14_DAYS_PERIOD}`;
const message = `${REGION}:${periodStr}:${Math.floor(Date.now() / 1000 / 3600)}`;
const hmac = hmac256(message, encHex.parse(this.hmacKey)).toString(encHex);
const url = `${this.retrieveUrl}/retrieve/${this.region}/${period}/${hmac}`;
const url = `${this.retrieveUrl}/retrieve/${REGION}/${periodStr}/${hmac}`;
captureMessage('retrieveDiagnosisKeys', {period, url});
return downloadDiagnosisKeysFile(url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('ExposureNotificationService', () => {
.mockImplementation((args: any) => new OriginalDate(args));

await service.updateExposureStatus();
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(14);
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(1);
});

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

server.retrieveDiagnosisKeys.mockClear();

Expand All @@ -109,7 +109,7 @@ describe('ExposureNotificationService', () => {
},
});
await service.updateExposureStatus();
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(1);
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(2);

server.retrieveDiagnosisKeys.mockClear();

Expand All @@ -120,7 +120,7 @@ describe('ExposureNotificationService', () => {
},
});
await service.updateExposureStatus();
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(2);
expect(server.retrieveDiagnosisKeys).toHaveBeenCalledTimes(3);
});

it('serializes status update', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,20 @@ export class ExposureNotificationService {
): AsyncGenerator<{keysFileUrl: string; period: number} | null> {
const runningDate = new Date();

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

if (!_lastCheckedPeriod) {
try {
const keysFileUrl = await this.backendInterface.retrieveDiagnosisKeys(0);
yield {keysFileUrl, period: runningPeriod};
} catch (error) {
captureException('Error while downloading batch files', error);
}
return;
}

const lastCheckedPeriod = Math.max(_lastCheckedPeriod - 1, runningPeriod - EXPOSURE_NOTIFICATION_CYCLE);

while (runningPeriod > lastCheckedPeriod) {
try {
const keysFileUrl = await this.backendInterface.retrieveDiagnosisKeys(runningPeriod);
Expand Down Expand Up @@ -339,12 +349,7 @@ export class ExposureNotificationService {
if (!value) continue;
const {keysFileUrl, period} = value;
keysFileUrls.push(keysFileUrl);

// Temporarily disable persisting lastCheckPeriod on Android
// Ref https://github.com/cds-snc/covid-shield-mobile/issues/453
if (Platform.OS !== 'android') {
lastCheckedPeriod = Math.max(lastCheckedPeriod || 0, period);
}
lastCheckedPeriod = Math.max(lastCheckedPeriod || 0, period);
}

captureMessage('performExposureStatusUpdate', {
Expand All @@ -365,11 +370,12 @@ export class ExposureNotificationService {
lastCheckedPeriod,
);
}
return finalize({}, lastCheckedPeriod);
} catch (error) {
captureException('performExposureStatusUpdate', error);
}

return finalize({}, lastCheckedPeriod);
return finalize();
}

private async processPendingExposureSummary() {
Expand Down

0 comments on commit 6bc368b

Please sign in to comment.