Skip to content

Commit

Permalink
10007: Remove mockCognito, lean on localCognito exclusively.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Rogers committed Dec 6, 2023
1 parent b6314d8 commit 05472f6
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 168 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ bulk-import-log.txt
metadata.json
metadata-public.json
stats.html
other/
other/
.cognito/db/
1 change: 0 additions & 1 deletion esbuildHelper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const env = {
SESSION_TIMEOUT: process.env.SESSION_TIMEOUT,
SKIP_VIRUS_SCAN: process.env.SKIP_VIRUS_SCAN,
STAGE: process.env.STAGE,
USE_COGNITO_LOCAL: process.env.USE_COGNITO_LOCAL,
USTC_ENV: process.env.USTC_ENV,
WS_URL: process.env.WS_URL,
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@
"start:api:resume": "RESUME=true npm run start:api",
"start:api": "USTC_ENV=dev ./run-local.sh",
"start:api:docker": "USTC_ENV=dev ./run-api.sh",
"start:api:cognito-local": "USTC_ENV=dev USE_COGNITO_LOCAL=true ./run-local.sh",
"start:api:cognito-local": "USTC_ENV=dev ./run-local.sh",
"start:api:cognito-local:ci": "CI=true npm run start:api:cognito-local",
"start:client:ci": "CI=true SKIP_VIRUS_SCAN=true NODE_ENV=production npm run start:client",
"start:client:no-scanner": "NO_SCANNER=true npm run start:client",
"start:client": "npm run start:client-host && WATCH=true IS_LOCAL=true FILE_UPLOAD_MODAL_TIMEOUT=1 USTC_ENV=dev PDF_EXPRESS_LICENSE_KEY=OjkUB41bl1hJg6jvUEfn npm run start:client:esbuild",
"start:client:cognito-local": "USE_COGNITO_LOCAL=true npm run start:client",
"start:client:cognito-local": "npm run start:client",
"start:client-host": "servor dist index.html 1234 &",
"start:client:esbuild": "node esbuild.config.mjs",
"start:cognito-triggers-local": "cd cognito-triggers-sls && npx sls offline",
Expand Down
11 changes: 5 additions & 6 deletions run-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ else
fi
fi

if [ -n "${USE_COGNITO_LOCAL}" ]; then
echo "Starting local lambda for cognito triggers"
npm run start:cognito-triggers-local &
echo "Starting cognito-local"
CODE=123456 npx cognito-local &
fi
echo "Starting local lambda for cognito triggers"
npm run start:cognito-triggers-local &
echo "Starting cognito-local"
CODE=123456 npx cognito-local &


nodemon --delay 1 -e js,ts --ignore web-client/ --ignore dist/ --ignore dist-public/ --ignore cypress-integration/ --ignore cypress/helpers/ --ignore cypress-smoketests/ --ignore cypress-readonly --exec "npx ts-node --transpile-only web-api/src/app-local.ts"

Expand Down
4 changes: 0 additions & 4 deletions shared/src/sharedAppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export const getUniqueId = (): string => {
return uuidv4();
};

export const getCognitoLocalEnabled = () => {
return !!process.env.USE_COGNITO_LOCAL;
};

export const clerkOfCourtNameForSigning = 'Stephanie A. Servoss';

export const ERROR_MAP_429 = {
Expand Down
82 changes: 7 additions & 75 deletions web-api/src/applicationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
getEnvironment,
getUniqueId,
} from '../../shared/src/sharedAppContext';
import { cognitoLocalWrapper } from './cognitoLocalWrapper';
import { createLogger } from './createLogger';
import { documentUrlTranslator } from '../../shared/src/business/utilities/documentUrlTranslator';
import { exec } from 'child_process';
Expand All @@ -52,6 +51,10 @@ import {
getChromiumBrowser,
getChromiumBrowserAWS,
} from '../../shared/src/business/utilities/getChromiumBrowser';
import {
getCognito,
getLocalCognito,
} from '@web-api/persistence/cognito/getCognito';
import { getDocumentGenerators } from './getDocumentGenerators';
import { getPersistenceGateway } from './getPersistenceGateway';
import { getUseCaseHelpers } from './getUseCaseHelpers';
Expand All @@ -60,7 +63,6 @@ import { getUtilities } from './getUtilities';
import { isAuthorized } from '../../shared/src/authorization/authorizationClientService';
import { isCurrentColorActive } from './persistence/dynamo/helpers/isCurrentColorActive';
import { retrySendNotificationToConnections } from '../../shared/src/notifications/retrySendNotificationToConnections';
import { scan } from './persistence/dynamodbClientService';
import { sendBulkTemplatedEmail } from './dispatchers/ses/sendBulkTemplatedEmail';
import { sendEmailEventToQueue } from './persistence/messages/sendEmailEventToQueue';
import { sendNotificationOfSealing } from './dispatchers/sns/sendNotificationOfSealing';
Expand All @@ -70,9 +72,8 @@ import { sendSetTrialSessionCalendarEvent } from './persistence/messages/sendSet
import { sendSlackNotification } from './dispatchers/slack/sendSlackNotification';
import { sendUpdatePetitionerCasesMessage } from './persistence/messages/sendUpdatePetitionerCasesMessage';
import { updatePetitionerCasesInteractor } from '../../shared/src/business/useCases/users/updatePetitionerCasesInteractor';
import { v4 as uuidv4 } from 'uuid';
import type { ClientApplicationContext } from '../../web-client/src/applicationContext';
const { CognitoIdentityServiceProvider, DynamoDB, S3, SES, SQS } = AWS;
const { DynamoDB, S3, SES, SQS } = AWS;
const execPromise = util.promisify(exec);

const environment = {
Expand Down Expand Up @@ -229,78 +230,9 @@ export const createApplicationContext = (
},
getCognito: () => {
if (environment.stage === 'local') {
if (process.env.USE_COGNITO_LOCAL === 'true') {
return cognitoLocalWrapper(
new CognitoIdentityServiceProvider({
endpoint: 'http://localhost:9229/',
httpOptions: {
connectTimeout: 3000,
timeout: 5000,
},
maxRetries: 3,
region: 'local',
}),
);
} else {
return {
adminCreateUser: () => ({
promise: () => ({
User: {
Username: uuidv4(),
},
}),
}),
adminDisableUser: () => ({
promise: () => {},
}),
adminGetUser: ({ Username }) => ({
promise: async () => {
// TODO: this scan might become REALLY slow while doing a full integration
// test run.
const items = await scan({
applicationContext: {
environment,
getDocumentClient,
},
});
const users = items.filter(
({ pk, sk }) =>
pk.startsWith('user|') && sk.startsWith('user|'),
);
const foundUser = users.find(({ email }) => email === Username);
if (foundUser) {
return {
UserAttributes: [],
Username: foundUser.userId,
};
} else {
const error = new Error();
error.code = 'UserNotFoundException';
throw error;
}
},
}),
adminUpdateUserAttributes: () => ({
promise: () => {},
}),
listUsers: () => ({
promise: () => {
throw new Error(
'Please use cognito locally by running npm run start:api:cognito-local',
);
},
}),
};
}
return getLocalCognito();
} else {
return new CognitoIdentityServiceProvider({
httpOptions: {
connectTimeout: 3000,
timeout: 5000,
},
maxRetries: 3,
region: 'us-east-1',
});
return getCognito();
}
},
getConstants: () => ({
Expand Down
5 changes: 1 addition & 4 deletions web-api/src/getPersistenceGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { caseAdvancedSearch } from './persistence/elasticsearch/caseAdvancedSear
import { casePublicSearch as casePublicSearchPersistence } from './persistence/elasticsearch/casePublicSearch';
import { confirmAuthCode } from './persistence/cognito/confirmAuthCode';
import { confirmAuthCodeCognitoLocal } from '@web-api/persistence/cognito/confirmAuthCodeCognitoLocal';
import { confirmAuthCodeLocal } from './persistence/cognito/confirmAuthCodeLocal';
import { createCase } from './persistence/dynamo/cases/createCase';
import { createCaseDeadline } from './persistence/dynamo/caseDeadlines/createCaseDeadline';
import { createCaseTrialSortMappingRecords } from './persistence/dynamo/cases/createCaseTrialSortMappingRecords';
Expand Down Expand Up @@ -276,9 +275,7 @@ const gatewayMethods = {
caseAdvancedSearch,
casePublicSearch: casePublicSearchPersistence,
confirmAuthCode: process.env.IS_LOCAL
? process.env.USE_COGNITO_LOCAL
? confirmAuthCodeCognitoLocal
: confirmAuthCodeLocal
? confirmAuthCodeCognitoLocal
: confirmAuthCode,
createChangeOfAddressJob,
createLock,
Expand Down
90 changes: 90 additions & 0 deletions web-api/src/persistence/cognito/getCognito.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { CognitoIdentityServiceProvider } from 'aws-sdk';
import { cognitoLocalWrapper } from '@web-api/cognitoLocalWrapper';

let cognitoClientCache: CognitoIdentityServiceProvider;

export function getCognito() {
if (!cognitoClientCache) {
cognitoClientCache = new CognitoIdentityServiceProvider({
httpOptions: {
connectTimeout: 3000,
timeout: 5000,
},
maxRetries: 3,
region: 'us-east-1',
});
}
return cognitoClientCache;
}

export function getMockCognito() {
if (!cognitoClientCache) {
cognitoClientCache = {
adminCreateUser: () => ({
promise: () => ({
User: {
Username: uuidv4(),
},
}),
}),
adminDisableUser: () => ({
promise: () => {},
}),
adminGetUser: ({ Username }) => ({
promise: async () => {
// TODO: this scan might become REALLY slow while doing a full integration
// test run.
const items = await scan({
applicationContext: {
environment,
getDocumentClient,
},
});
const users = items.filter(
({ pk, sk }) => pk.startsWith('user|') && sk.startsWith('user|'),
);
const foundUser = users.find(({ email }) => email === Username);
if (foundUser) {
return {
UserAttributes: [],
Username: foundUser.userId,
};
} else {
const error = new Error();
error.code = 'UserNotFoundException';
throw error;
}
},
}),
adminUpdateUserAttributes: () => ({
promise: () => {},
}),
listUsers: () => ({
promise: () => {
throw new Error(
'Please use cognito locally by running npm run start:api:cognito-local',
);
},
}),
};
}

return cognitoClientCache;
}

export function getLocalCognito() {
if (!cognitoClientCache) {
cognitoClientCache = cognitoLocalWrapper(
new CognitoIdentityServiceProvider({
endpoint: 'http://localhost:9229/',
httpOptions: {
connectTimeout: 3000,
timeout: 5000,
},
maxRetries: 3,
region: 'local',
}),
);
}
return cognitoClientCache;
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,43 +318,6 @@ describe('createOrUpdatePractitionerUser', () => {
});
});

it('should call adminCreateUser with the correct params when USE_COGNITO_LOCAL is true', async () => {
process.env.USE_COGNITO_LOCAL = 'true';
process.env.USER_POOL_ID = 'localUserPoolId';

// setupNonExistingUserMock();

await createOrUpdatePractitionerUser({
applicationContext,
user: privatePractitionerUser as any,
});

expect(
applicationContext.getCognito().adminCreateUser,
).toHaveBeenCalledWith({
UserAttributes: [
{
Name: 'email_verified',
Value: 'True',
},
{
Name: 'email',
Value: '[email protected]',
},
{
Name: 'custom:role',
Value: 'privatePractitioner',
},
{
Name: 'name',
Value: 'Test Private Practitioner',
},
],
UserPoolId: 'localUserPoolId',
Username: '[email protected]',
});
});

describe('createUserRecords', () => {
it('attempts to persist a private practitioner user with name and barNumber mapping records', async () => {
await createUserRecords({
Expand Down
2 changes: 0 additions & 2 deletions web-client/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ const app = {
});
presenter.state.cognitoLoginUrl = applicationContext.getCognitoLoginUrl();
presenter.state.constants = applicationContext.getConstants();
presenter.state.cognitoLocalEnabled =
applicationContext.getCognitoLocalEnabled();

const shouldRefreshToken =
!wasAppLoadedFromACognitoLogin(window.location.href) &&
Expand Down
2 changes: 0 additions & 2 deletions web-client/src/applicationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
import {
ERROR_MAP_429,
clerkOfCourtNameForSigning,
getCognitoLocalEnabled,
getCognitoLoginUrl,
getEnvironment,
getPublicSiteUrl,
Expand Down Expand Up @@ -639,7 +638,6 @@ const applicationContext = {
getCognitoClientId: () => {
return process.env.COGNITO_CLIENT_ID || '6tu6j1stv5ugcut7dqsqdurn8q';
},
getCognitoLocalEnabled,
getCognitoLoginUrl,
getCognitoRedirectUrl: () => {
return process.env.COGNITO_REDIRECT_URI || 'http://localhost:1234/log-in';
Expand Down
Loading

0 comments on commit 05472f6

Please sign in to comment.