Skip to content

Commit

Permalink
fix(amplify-category-notifications): trim whitespace from fcm keys (#…
Browse files Browse the repository at this point in the history
…7456)

this commit prevents a copy/paste issue where an extra newline character comes along with an fcm
key.

fix #7440
  • Loading branch information
johnpc committed Jul 12, 2021
1 parent 34560cd commit 155a071
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
const inquirer = require('inquirer');
const channelName = 'FCM';
const channelFCM = require('../../lib/channel-FCM');

const mockInquirer = answers => {
inquirer.prompt = async prompts => {
[].concat(prompts).forEach(function (prompt) {
if (!(prompt.name in answers) && typeof prompt.default !== 'undefined') {
answers[prompt.name] = prompt.default;
}
});

return answers;
};
};

describe('channel-FCM', () => {
const mockServiceOutput = {};
const mockChannelOutput = { Enabled: true };
mockServiceOutput[channelName] = mockChannelOutput;

const mockPinpointResponseErr = new Error('channel-FCM.test.js error');
const mockPinpointResponseData = {
FCMChannelResponse: {},
};

const mockPinpointClient = {
updateGcmChannel: jest.fn((_, cb) => {
return new Promise(() => {
cb(null, mockPinpointResponseData);
});
}),
};

const mockPinpointClientReject = {
updateGcmChannel: jest.fn((_, cb) => {
return new Promise(() => {
cb(mockPinpointResponseErr);
});
}),
};

let mockContext = {
exeInfo: {
serviceMeta: {
output: mockServiceOutput,
},
pinpointClient: mockPinpointClient,
},
print: {
info: jest.fn(),
error: jest.fn(),
},
};

let mockContextReject = {
exeInfo: {
serviceMeta: {
output: mockServiceOutput,
},
pinpointClient: mockPinpointClientReject,
},
print: {
info: jest.fn(),
error: jest.fn(),
},
};

test('configure', async () => {
mockChannelOutput.Enabled = true;
mockInquirer({ disableChannel: true });

await channelFCM.configure(mockContext).then(() => {
expect(mockPinpointClient.updateGcmChannel).toBeCalled();
});

mockChannelOutput.Enabled = true;
mockInquirer({ disableChannel: false });
await channelFCM.configure(mockContext).then(() => {
expect(mockPinpointClient.updateGcmChannel).toBeCalled();
});

mockChannelOutput.Enabled = false;
mockInquirer({ enableChannel: true });
await channelFCM.configure(mockContext).then(() => {
expect(mockPinpointClient.updateGcmChannel).toBeCalled();
});
});

test('enable', async () => {
mockInquirer({ ApiKey: 'ApiKey-abc123' });
await channelFCM.enable(mockContext, 'successMessage').then(data => {
expect(mockPinpointClient.updateGcmChannel).toBeCalled();
expect(data).toEqual(mockPinpointResponseData);
});
});

test('enable with newline', async () => {
mockInquirer({ ApiKey: 'ApiKey-abc123\n' });
await channelFCM.enable(mockContext, 'successMessage').then(data => {
expect(mockPinpointClient.updateGcmChannel).toBeCalledWith(
{
ApplicationId: undefined,
GCMChannelRequest: {
ApiKey: 'ApiKey-abc123',
Enabled: true,
},
},
expect.anything(),
);
expect(data).toEqual(mockPinpointResponseData);
});
});

test('enable unsuccessful', async () => {
mockInquirer({ ApiKey: 'ApiKey-abc123' });
await channelFCM.enable(mockContextReject, 'successMessage').catch(err => {
expect(mockPinpointClient.updateGcmChannel).toBeCalled();
expect(err).toEqual(mockPinpointResponseErr);
});
});

test('disable', async () => {
await channelFCM.disable(mockContext).then(data => {
expect(mockPinpointClient.updateGcmChannel).toBeCalled();
expect(data).toEqual(mockPinpointResponseData);
});
});

test('disable unsuccessful', async () => {
await expect(channelFCM.disable(mockContextReject)).rejects.toThrow(mockPinpointResponseErr);
});
});
13 changes: 11 additions & 2 deletions packages/amplify-category-notifications/lib/channel-FCM.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function enable(context, successMessage) {
default: channelOutput.ApiKey,
},
];
answers = await inquirer.prompt(questions);
answers = trimAnswers(await inquirer.prompt(questions));
}

const params = {
Expand Down Expand Up @@ -104,7 +104,7 @@ async function disable(context) {
default: channelOutput.ApiKey,
},
];
answers = await inquirer.prompt(questions);
answers = trimAnswers(await inquirer.prompt(questions));
}

const params = {
Expand Down Expand Up @@ -154,6 +154,15 @@ function pull(context, pinpointApp) {
});
}

function trimAnswers(answers) {
for (const [key, value] of Object.entries(answers)) {
if (typeof answers[key] === 'string') {
answers[key] = value.trim();
}
}
return answers;
}

module.exports = {
configure,
enable,
Expand Down

0 comments on commit 155a071

Please sign in to comment.