From 05472f611f7501400c2c997010cd727f49babfc9 Mon Sep 17 00:00:00 2001 From: Zachary Rogers Date: Wed, 6 Dec 2023 11:34:32 -0800 Subject: [PATCH] 10007: Remove mockCognito, lean on localCognito exclusively. --- .gitignore | 3 +- esbuildHelper.mjs | 1 - package.json | 4 +- run-local.sh | 11 ++- shared/src/sharedAppContext.ts | 4 - web-api/src/applicationContext.ts | 82 ++--------------- web-api/src/getPersistenceGateway.ts | 5 +- web-api/src/persistence/cognito/getCognito.ts | 90 +++++++++++++++++++ .../createOrUpdatePractitionerUser.test.ts | 37 -------- web-client/src/app.tsx | 2 - web-client/src/applicationContext.ts | 2 - web-client/src/views/LogIn.tsx | 55 +++++------- 12 files changed, 128 insertions(+), 168 deletions(-) create mode 100644 web-api/src/persistence/cognito/getCognito.ts diff --git a/.gitignore b/.gitignore index 562bb2f3951..066108260d7 100644 --- a/.gitignore +++ b/.gitignore @@ -69,4 +69,5 @@ bulk-import-log.txt metadata.json metadata-public.json stats.html -other/ \ No newline at end of file +other/ +.cognito/db/ \ No newline at end of file diff --git a/esbuildHelper.mjs b/esbuildHelper.mjs index ede564cb12d..5781d5489cb 100644 --- a/esbuildHelper.mjs +++ b/esbuildHelper.mjs @@ -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, }; diff --git a/package.json b/package.json index cccdaae0771..5dff1f6b97f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/run-local.sh b/run-local.sh index 7a2051ef9f0..2279d38deec 100755 --- a/run-local.sh +++ b/run-local.sh @@ -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" diff --git a/shared/src/sharedAppContext.ts b/shared/src/sharedAppContext.ts index d14c81e281c..df4f321f515 100644 --- a/shared/src/sharedAppContext.ts +++ b/shared/src/sharedAppContext.ts @@ -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 = { diff --git a/web-api/src/applicationContext.ts b/web-api/src/applicationContext.ts index da7dfa5ad60..527fc64084e 100644 --- a/web-api/src/applicationContext.ts +++ b/web-api/src/applicationContext.ts @@ -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'; @@ -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'; @@ -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'; @@ -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 = { @@ -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: () => ({ diff --git a/web-api/src/getPersistenceGateway.ts b/web-api/src/getPersistenceGateway.ts index 3d16618d486..49e76163eef 100644 --- a/web-api/src/getPersistenceGateway.ts +++ b/web-api/src/getPersistenceGateway.ts @@ -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'; @@ -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, diff --git a/web-api/src/persistence/cognito/getCognito.ts b/web-api/src/persistence/cognito/getCognito.ts new file mode 100644 index 00000000000..cc0038db634 --- /dev/null +++ b/web-api/src/persistence/cognito/getCognito.ts @@ -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; +} diff --git a/web-api/src/persistence/dynamo/users/createOrUpdatePractitionerUser.test.ts b/web-api/src/persistence/dynamo/users/createOrUpdatePractitionerUser.test.ts index 108f5657ba8..f8e4481e57d 100644 --- a/web-api/src/persistence/dynamo/users/createOrUpdatePractitionerUser.test.ts +++ b/web-api/src/persistence/dynamo/users/createOrUpdatePractitionerUser.test.ts @@ -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: 'test@example.com', - }, - { - Name: 'custom:role', - Value: 'privatePractitioner', - }, - { - Name: 'name', - Value: 'Test Private Practitioner', - }, - ], - UserPoolId: 'localUserPoolId', - Username: 'test@example.com', - }); - }); - describe('createUserRecords', () => { it('attempts to persist a private practitioner user with name and barNumber mapping records', async () => { await createUserRecords({ diff --git a/web-client/src/app.tsx b/web-client/src/app.tsx index ceee7199052..e17b6f782cb 100644 --- a/web-client/src/app.tsx +++ b/web-client/src/app.tsx @@ -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) && diff --git a/web-client/src/applicationContext.ts b/web-client/src/applicationContext.ts index 0a2c32b79e7..1e49938f2b7 100644 --- a/web-client/src/applicationContext.ts +++ b/web-client/src/applicationContext.ts @@ -25,7 +25,6 @@ import { import { ERROR_MAP_429, clerkOfCourtNameForSigning, - getCognitoLocalEnabled, getCognitoLoginUrl, getEnvironment, getPublicSiteUrl, @@ -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'; diff --git a/web-client/src/views/LogIn.tsx b/web-client/src/views/LogIn.tsx index 453e91936a9..bc2a3cc1f7f 100644 --- a/web-client/src/views/LogIn.tsx +++ b/web-client/src/views/LogIn.tsx @@ -10,16 +10,12 @@ const Button = getView('Button'); export const LogIn = connect( { - cognitoLocalEnabled: state.cognitoLocalEnabled, form: state.form, - loginWithCodeSequence: sequences.loginWithCodeSequence, loginWithCognitoLocalSequence: sequences.loginWithCognitoLocalSequence, updateFormValueSequence: sequences.updateFormValueSequence, }, function LogIn({ - cognitoLocalEnabled, form, - loginWithCodeSequence, loginWithCognitoLocalSequence, updateFormValueSequence, }) { @@ -33,15 +29,10 @@ export const LogIn = connect( id="log-in" onSubmit={event => { event.preventDefault(); - !cognitoLocalEnabled && - loginWithCodeSequence({ - code: form.email, - }); - cognitoLocalEnabled && - loginWithCognitoLocalSequence({ - code: form.email, - password: form.password, - }); + loginWithCognitoLocalSequence({ + code: form.email, + password: form.password, + }); }} >
@@ -63,27 +54,23 @@ export const LogIn = connect( }); }} /> - {cognitoLocalEnabled && ( - <> - - { - updateFormValueSequence({ - key: e.target.name, - value: e.target.value, - }); - }} - /> - - )} + + { + updateFormValueSequence({ + key: e.target.name, + value: e.target.value, + }); + }} + />