Skip to content

Commit

Permalink
fix: pull issue with multi-env notifications #6475 (#6525)
Browse files Browse the repository at this point in the history
* fix: pull issue with multi-env notifications #6475
* fix notification category multi-env handling
* fix to ask for `API key` for FCM on `remove notification` command
* add E2E for notification testing
  • Loading branch information
Attila Hajdrik committed Mar 2, 2021
1 parent 7f89462 commit b0803d1
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 233 deletions.
493 changes: 261 additions & 232 deletions .circleci/config.yml

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion packages/amplify-category-notifications/lib/channel-FCM.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,30 @@ function validateInputParams(channelInput) {
return channelInput;
}

function disable(context) {
async function disable(context) {
let answers;
if (context.exeInfo.pinpointInputParams && context.exeInfo.pinpointInputParams[channelName]) {
answers = validateInputParams(context.exeInfo.pinpointInputParams[channelName]);
} else {
let channelOutput = {};
if (context.exeInfo.serviceMeta.output[channelName]) {
channelOutput = context.exeInfo.serviceMeta.output[channelName];
}
const questions = [
{
name: 'ApiKey',
type: 'input',
message: 'ApiKey',
default: channelOutput.ApiKey,
},
];
answers = await inquirer.prompt(questions);
}

const params = {
ApplicationId: context.exeInfo.serviceMeta.output.Id,
GCMChannelRequest: {
...answers,
Enabled: false,
},
};
Expand Down
27 changes: 27 additions & 0 deletions packages/amplify-category-notifications/lib/multi-env-manager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const fs = require('fs-extra');
const _ = require('lodash');
const sequential = require('promise-sequential');
const pinpointHelper = require('./pinpoint-helper');
const constants = require('./constants');
const notificationManager = require('./notifications-manager');
const { stateManager } = require('amplify-cli-core');

async function initEnv(context) {
const pinpointNotificationsMeta = await constructPinpointNotificationsMeta(context);
Expand All @@ -19,6 +21,31 @@ async function constructPinpointNotificationsMeta(context) {
let serviceBackendConfig;
let pinpointNotificationsMeta;

// For pull we have to get the pinpoint application for notifications category
// from cloud meta and as no new resources are created during pull, we should not look for
// Pinpoint app in analytics category.
const isPulling = context.input.command === 'pull' || (context.input.command === 'env' && context.input.subCommands[0] === 'pull');

if (isPulling) {
const currentAmplifyMeta = stateManager.getCurrentMeta(undefined, {
throwIfNotExist: false,
});

if (currentAmplifyMeta) {
const notificationsMeta = currentAmplifyMeta[constants.CategoryName];

// We only support single resource for notificaitons
if (notificationsMeta && Object.keys(notificationsMeta).length > 0) {
const pinpointResource = _.get(notificationsMeta, Object.keys(notificationsMeta)[0], undefined);
pinpointApp = {
Id: pinpointResource.output.Id,
};
pinpointApp.Name = pinpointResource.output.Name || pinpointResource.output.appName;
pinpointApp.Region = pinpointResource.output.Region;
}
}
}

const { teamProviderInfo, localEnvInfo, amplifyMeta } = context.exeInfo;

const { envName } = localEnvInfo;
Expand Down
2 changes: 2 additions & 0 deletions packages/amplify-category-notifications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"dependencies": {
"fs-extra": "^8.1.0",
"inquirer": "^7.3.3",
"lodash": "^4.17.19",
"open": "^7.0.0",
"ora": "^4.0.3",
"promise-sequential": "^1.1.1"
},
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-e2e-core/src/categories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * from './hosting';
export * from './interactions';
export * from './lambda-function';
export * from './lambda-layer';
export * from './notifications';
export * from './predictions';
export * from './storage';
26 changes: 26 additions & 0 deletions packages/amplify-e2e-core/src/categories/notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { nspawn as spawn, getCLIPath, singleSelect } from '..';

export type NotificationSettings = {
resourceName: string;
};

export const addSMSNotification = async (cwd: string, settings: NotificationSettings): Promise<void> => {
return new Promise((resolve, reject) => {
let chain = spawn(getCLIPath(), ['add', 'notification'], { cwd, stripColors: true });

singleSelect(chain.wait('Choose the push notification channel to enable'), 'SMS', ['APNS', 'FCM', 'Email', 'SMS']);

chain
.wait('Provide your pinpoint resource name')
.sendLine(settings.resourceName)
.wait('The SMS channel has been successfully enabled')
.sendEof()
.run((err: Error) => {
if (!err) {
resolve(undefined);
} else {
reject(err);
}
});
});
};
87 changes: 87 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/notifications.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import _ from 'lodash';
import {
addSMSNotification,
amplifyPull,
amplifyPushAuth,
createNewProjectDir,
deleteProject,
deleteProjectDir,
getAppId,
getTeamProviderInfo,
initJSProjectWithProfile,
} from 'amplify-e2e-core';
import { checkoutEnvironment, removeEnvironment } from '../environment/env';
import { getShortId } from '../import-helpers';

const profileName = 'amplify-integ-test-user';

describe('notification category test', () => {
const projectPrefix = 'notification';

const projectSettings = {
name: projectPrefix,
};

let projectRoot: string;
let ignoreProjectDeleteErrors: boolean = false;

beforeEach(async () => {
projectRoot = await createNewProjectDir(projectPrefix);
ignoreProjectDeleteErrors = false;
});

afterEach(async () => {
try {
await deleteProject(projectRoot);
} catch (error) {
// In some tests where project initialization fails it can lead to errors on cleanup which we
// can ignore if set by the test
if (!ignoreProjectDeleteErrors) {
throw error;
}
}
deleteProjectDir(projectRoot);
});

it('add notifications and pull to empty dir to compare values', async () => {
await initJSProjectWithProfile(projectRoot, {
...projectSettings,
disableAmplifyAppCreation: false,
});

const shortId = getShortId();

const settings = {
resourceName: `${projectPrefix}${shortId}`,
};

await addSMSNotification(projectRoot, settings);

await amplifyPushAuth(projectRoot);

const appId = getAppId(projectRoot);
expect(appId).toBeDefined();

let projectRootPull;

try {
projectRootPull = await createNewProjectDir('notification-pull');

await amplifyPull(projectRootPull, { override: false, emptyDir: true, appId });

expectLocalAndPulledTeamNotificationMatching(projectRoot, projectRootPull);
} finally {
deleteProjectDir(projectRootPull);
}
});

const expectLocalAndPulledTeamNotificationMatching = (projectRoot: string, pulledProjectRoot: string) => {
const team = getTeamProviderInfo(projectRoot);
const pulledTeam = getTeamProviderInfo(pulledProjectRoot);

const pinpointApp = _.get(team, ['integtest', 'categories', 'notifications']);
const pulledPinpointApp = _.get(pulledTeam, ['integtest', 'categories', 'notifications']);

expect(pinpointApp).toMatchObject(pulledPinpointApp);
};
});
1 change: 1 addition & 0 deletions scripts/split-e2e-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const KNOWN_SUITES_SORTED_ACCORDING_TO_RUNTIME = [
'src/__tests__/amplify-configure.test.ts',
'src/__tests__/init.test.ts',
'src/__tests__/tags.test.ts',
'src/__tests__/notifications.test.ts',
//<15m
'src/__tests__/schema-versioned.test.ts',
'src/__tests__/schema-data-access-patterns.test.ts',
Expand Down

0 comments on commit b0803d1

Please sign in to comment.