Skip to content

Commit

Permalink
fix(geo): add validation for empty input arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Tré Ammatuna committed Mar 30, 2022
1 parent 56632af commit 3f6f0e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,23 @@ describe('AmazonLocationServiceProvider', () => {
);
});

test('should error if input is empty array', async () => {
jest.spyOn(Credentials, 'get').mockImplementation(() => {
return Promise.resolve(credentials);
});

LocationClient.prototype.send = jest
.fn()
.mockImplementation(mockBatchPutGeofenceCommand);

const locationProvider = new AmazonLocationServiceProvider();
locationProvider.configure(awsConfig.geo.amazon_location_service);

await expect(locationProvider.saveGeofences([])).rejects.toThrow(
'Geofence input array is empty'
);
});

test('should error if there are no geofenceCollections in config', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
Expand Down Expand Up @@ -950,6 +967,17 @@ describe('AmazonLocationServiceProvider', () => {
);
});

test('should error if input array is empty', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});
const locationProvider = new AmazonLocationServiceProvider();
locationProvider.configure(awsConfig.geo.amazon_location_service);
await expect(locationProvider.deleteGeofences([])).rejects.toThrow(
`GeofenceId input array is empty`
);
});

test('should error if there are no geofenceCollections in config', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
Expand Down
11 changes: 10 additions & 1 deletion packages/geo/src/Providers/AmazonLocationServiceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ export class AmazonLocationServiceProvider implements GeoProvider {
geofences: GeofenceInput[],
options?: AmazonLocationServiceGeofenceOptions
): Promise<SaveGeofencesResults> {
if (geofences.length < 1) {
throw new Error('Geofence input array is empty');
}

const credentialsOK = await this._ensureCredentials();
if (!credentialsOK) {
throw new Error('No credentials');
Expand Down Expand Up @@ -405,7 +409,8 @@ export class AmazonLocationServiceProvider implements GeoProvider {

while (PascalGeofences.length > 0) {
// Splice off 10 geofences from input clone due to Amazon Location Service API limit
batches.push(PascalGeofences.splice(0, 10));
const apiLimit = 10;
batches.push(PascalGeofences.splice(0, apiLimit));
}

await Promise.all(
Expand Down Expand Up @@ -618,6 +623,10 @@ export class AmazonLocationServiceProvider implements GeoProvider {
geofenceIds: string[],
options?: AmazonLocationServiceGeofenceOptions
): Promise<AmazonLocationServiceDeleteGeofencesResults> {
if (geofenceIds.length < 1) {
throw new Error('GeofenceId input array is empty');
}

const credentialsOK = await this._ensureCredentials();
if (!credentialsOK) {
throw new Error('No credentials');
Expand Down

0 comments on commit 3f6f0e9

Please sign in to comment.