Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fcm): Fix error throw when data property exists but is undefined #1139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/messaging/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ export class Messaging implements MessagingInterface {
}

// Validate the data payload object does not contain blacklisted properties
if ('data' in payloadCopy) {
if ('data' in payloadCopy && payloadCopy.data !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be just if (payloadCopy.data)

BLACKLISTED_DATA_PAYLOAD_KEYS.forEach((blacklistedKey) => {
if (blacklistedKey in payloadCopy.data!) {
throw new FirebaseMessagingError(
Expand Down
100 changes: 100 additions & 0 deletions test/unit/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,12 @@ describe('Messaging', () => {
{ token: 'mock-token' },
).should.eventually.be.rejected.and.have.property('code', 'messaging/invalid-argument');
});

it('should not throws when the payload contains the data property with undefined value', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not throw when

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is actually irrelevant to the actual fix. The fix only applies to legacy FCM APIs like sendToDevice(). It has no effect on send(). We can remove this.

mockedRequests.push(mockSendRequest());
const message = { token: 'a', data: undefined };
return messaging.send(message).should.eventually.equal('projects/projec_id/messages/message_id');
})
});

describe('sendAll()', () => {
Expand Down Expand Up @@ -823,6 +829,29 @@ describe('Messaging', () => {
[validMessage],
).should.eventually.be.rejected.and.have.property('code', 'app/invalid-credential');
});

it('should not throws when the payload contains the data property with undefined value', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove. Not relevant to the fix.

const messageIds = [
'projects/projec_id/messages/1',
'projects/projec_id/messages/2',
'projects/projec_id/messages/3',
];
mockedRequests.push(mockBatchRequest(messageIds));
const validMessageWithUndefinedDataProp = { ...validMessage, data: undefined }
return messaging.sendAll([
validMessageWithUndefinedDataProp,
validMessageWithUndefinedDataProp,
validMessageWithUndefinedDataProp
]).then((response: BatchResponse) => {
expect(response.successCount).to.equal(3);
expect(response.failureCount).to.equal(0);
response.responses.forEach((resp, idx) => {
expect(resp.success).to.be.true;
expect(resp.messageId).to.equal(messageIds[idx]);
expect(resp.error).to.be.undefined;
});
});
})
});

describe('sendMulticast()', () => {
Expand Down Expand Up @@ -1118,6 +1147,31 @@ describe('Messaging', () => {
).should.eventually.be.rejected.and.have.property('code', 'app/invalid-credential');
});

it('should not throws when the payload contains the data property with undefined value', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

const messageIds = [
'projects/projec_id/messages/1',
'projects/projec_id/messages/2',
'projects/projec_id/messages/3',
];
mockedRequests.push(mockBatchRequest(messageIds));
return messaging.sendMulticast({
tokens: ['a', 'b', 'c'],
android: { ttl: 100 },
apns: { payload: { aps: { badge: 42 } } },
data: undefined,
notification: { title: 'test title' },
webpush: { data: { webKey: 'webValue' } },
}).then((response: BatchResponse) => {
expect(response.successCount).to.equal(3);
expect(response.failureCount).to.equal(0);
response.responses.forEach((resp, idx) => {
expect(resp.success).to.be.true;
expect(resp.messageId).to.equal(messageIds[idx]);
expect(resp.error).to.be.undefined;
});
});
})

function checkSendResponseSuccess(response: SendResponse, messageId: string): void {
expect(response.success).to.be.true;
expect(response.messageId).to.equal(messageId);
Expand Down Expand Up @@ -1457,6 +1511,16 @@ describe('Messaging', () => {
expect(mockOptionsClone).to.deep.equal(mocks.messaging.options);
});
});

it('should not throws when the payload contains the data property with undefined value', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not throw when...

mockedRequests.push(mockSendToDeviceStringRequest());

return messaging.sendToDevice(
mocks.messaging.registrationToken,
{ ...mocks.messaging.payload, data: undefined },
mocks.messaging.options,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need an assertion here. May be at least .should.eventually.be.fulfilled.

})
});

describe('sendToDeviceGroup()', () => {
Expand Down Expand Up @@ -1708,6 +1772,15 @@ describe('Messaging', () => {
expect(mockOptionsClone).to.deep.equal(mocks.messaging.options);
});
});

it('should not throws when the payload contains the data property with undefined value', () => {
mockedRequests.push(mockSendToDeviceGroupRequest());

return messaging.sendToDeviceGroup(
mocks.messaging.notificationKey,
{ ...mocks.messaging.payloadDataOnly, data: undefined },
);
});
});

describe('sendToTopic()', () => {
Expand Down Expand Up @@ -1935,6 +2008,15 @@ describe('Messaging', () => {
expect(mockOptionsClone).to.deep.equal(mocks.messaging.options);
});
});

it('should not throws when the payload contains the data property with undefined value', () => {
mockedRequests.push(mockSendToTopicRequest());

return messaging.sendToTopic(
mocks.messaging.topic,
{ ...mocks.messaging.payload, data: undefined },
);
});
});

describe('sendToCondition()', () => {
Expand Down Expand Up @@ -2116,6 +2198,15 @@ describe('Messaging', () => {
expect(mockOptionsClone).to.deep.equal(mocks.messaging.options);
});
});

it('should not throws when the payload contains the data property with undefined value', () => {
mockedRequests.push(mockSendToConditionRequest());

return messaging.sendToCondition(
mocks.messaging.condition,
{ ...mocks.messaging.payloadDataOnly, data: undefined },
);
});
});

describe('Payload validation', () => {
Expand Down Expand Up @@ -2341,6 +2432,15 @@ describe('Messaging', () => {
});
});

it('should not throws when the payload contains the data property with undefined value', () => {
mockedRequests.push(mockSendToDeviceStringRequest());

return messaging.sendToDevice(mocks.messaging.registrationToken, {
...mocks.messaging.payloadDataOnly,
data: undefined,
});
});

const invalidImages = ['', 'a', 'foo', 'image.jpg'];
invalidImages.forEach((imageUrl) => {
it(`should throw given an invalid imageUrl: ${imageUrl}`, () => {
Expand Down