Skip to content

Commit

Permalink
refactor: extend configuration of firebase application; testing utili…
Browse files Browse the repository at this point in the history
…ties update accordingly
  • Loading branch information
0xjei committed Dec 23, 2022
1 parent fb2bd73 commit 6e7ee8a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/test-ci-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,4 @@ jobs:
key: ${{ runner.os }}-firebase-${{ steps.cache-settings.outputs.firebase-tools }}

- name: run test (unit & e2e)
run: yarn test:dev
env:
FIREBASE_API_KEY: ${{ secrets.FIREBASE_API_KEY }}
FIREBASE_PROJECT_ID: ${{ secrets.FIREBASE_PROJECT_ID }}
run: yarn test:dev
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"build": "lerna run build",
"test": "yarn test:dev",
"test:dev": "NODE_ENV=dev yarn workspace @zkmpc/backend emulator:exec-test",
"test:prod": "NODE_ENV=prod export GOOGLE_APPLICATION_CREDENTIALS=\"./packages/backend/serviceAccountKey.json\" && jest --config=jest.json --detectOpenHandles --forceExit --coverage",
"test:prod": "export GOOGLE_APPLICATION_CREDENTIALS=\"./packages/backend/serviceAccountKey.json\" && NODE_ENV=prod jest --config=jest.json --detectOpenHandles --forceExit --coverage",
"test:ci-prod": "NODE_ENV=prod jest --config=jest.json --detectOpenHandles --forceExit --coverage=false",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
Expand Down
22 changes: 15 additions & 7 deletions packages/actions/test/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { connectAuthEmulator, getAuth, signInAnonymously, UserCredential } from

dotenv.config({ path: `${__dirname}/../../.env.test` })

// Emulator data.
const emulatorApiKey = "AAaaAaAaaaAa11aAAAaAA_1AaAaAaAaAAAa1A1a"
// WARNING: DO NOT USE PROD HERE OR YOUR CONFIGS MAY BE EXPOSED.
// nb. This MUST match the "dev" project stored in `/packages/backend/.firebaserc`.
const emulatorProjectId = "demo-zkmpc"

/**
* Initialize and return the Admin SDK app and services.
* @returns <App, Firestore, Auth, SecurityRules> - the instance of the initialized admin app and services.
Expand All @@ -18,7 +24,9 @@ export const initializeAdminServices = (): {
adminSecurityRules: admin.securityRules.SecurityRules
} => {
// Init app.
const adminApp = admin.initializeApp({ projectId: process.env.FIREBASE_PROJECT_ID })
const adminApp = admin.initializeApp({
projectId: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_PROJECT_ID : emulatorProjectId
})

// Init services.
const adminFirestore = admin.firestore()
Expand All @@ -44,15 +52,15 @@ export const initializeUserServices = (): {
} => {
// Init app.
const userApp = initializeApp({
apiKey: process.env.FIREBASE_API_KEY,
projectId: process.env.FIREBASE_PROJECT_ID,
authDomain: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_AUTH_DOMAIN : "",
messagingSenderId: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_MESSAGING_SENDER_ID : "",
appId: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_APP_ID : ""
apiKey: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_API_KEY : emulatorApiKey, // dummy fallback for dev (emulator).
projectId: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_PROJECT_ID : emulatorProjectId,
authDomain: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_AUTH_DOMAIN : "", // not needed fallback for dev.
messagingSenderId: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_MESSAGING_SENDER_ID : "", // not needed fallback for dev.
appId: process.env.NODE_ENV === "prod" ? process.env.FIREBASE_APP_ID : "" // not needed fallback for dev.
})

// Init services.
const auth = getAuth()
const auth = getAuth(userApp)
const userFirestore = process.env.NODE_ENV === "prod" ? getFirestore(userApp) : getFirestore()
const userFunctions = process.env.NODE_ENV === "prod" ? getFunctions(userApp) : getFunctions(getApp())

Expand Down
3 changes: 2 additions & 1 deletion packages/backend/.firebaserc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"projects": {
"default": "mpc-phase2-suite-test"
"dev": "demo-zkmpc",
"prod": "mpc-phase2-suite-test"
}
}
4 changes: 3 additions & 1 deletion packages/backend/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"rules": "storage.rules"
},
"emulators": {
"singleProjectMode": true,
"auth": {
"port": 9099
},
Expand All @@ -30,7 +31,8 @@
"port": 9199
},
"ui": {
"enabled": true
"enabled": false,
"port": 4000
}
}
}
12 changes: 6 additions & 6 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
"firebase:login": "firebase login",
"firebase:logout": "firebase logout",
"firebase:init": "firebase init",
"firebase:deploy": "yarn firestore:get-indexes && firebase deploy",
"firebase:deploy-functions": "firebase deploy --only functions",
"firebase:deploy-firestore": "yarn firestore:get-indexes && firebase deploy --only firestore",
"firebase:deploy-storage": "firebase deploy --only storage",
"firebase:log-functions": "firebase functions:log",
"firebase:deploy": "yarn firestore:get-indexes && firebase --project prod deploy",
"firebase:deploy-functions": "firebase --project prod deploy --only functions",
"firebase:deploy-firestore": "yarn firestore:get-indexes && firebase --project prod deploy --only firestore",
"firebase:deploy-storage": "firebase --project prod deploy --only storage",
"firebase:log-functions": "firebase --project prod functions:log",
"firestore:get-indexes": "firebase firestore:indexes > firestore.indexes.json",
"emulator:serve": "yarn build && firebase emulators:start",
"emulator:serve-functions": "yarn build && firebase emulators:start --only functions",
"emulator:shell": "yarn build && firebase functions:shell",
"emulator:exec-test": "firebase emulators:exec \"yarn test:emulator\" && kill -9 $(lsof -ti:8085)",
"emulator:exec-test": "firebase --project dev emulators:exec \"yarn test:emulator\" && kill -9 $(lsof -ti:8085)",
"test:emulator": "jest --config=../../jest.json --detectOpenHandles --forceExit --coverage=false"
},
"devDependencies": {
Expand Down

0 comments on commit 6e7ee8a

Please sign in to comment.