Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

#use firebase emulator for running e2e tests
USE_FIREBASE_EMULATOR=false

# use firebase emulator for running e2e tests
NEXT_PUBLIC_FIREBASE_EMULATOR=false
FIREBASE_AUTH_EMULATOR_HOST=127.0.0.1:9099
FIREBASE_STORAGE_EMULATOR_HOST=127.0.0.1:9199

# You already have access to basic local functionality (UI, authentication, database read access).

Expand Down
27 changes: 4 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,18 @@ jobs:
# "common/coverage/lcov.info" \
# "web/coverage/lcov.info" \
# > coverage/lcov.info



# - name: Build app
# env:
# DATABASE_URL: ${{ secrets.DATABASE_URL }}
# run: npm run build

# Optional: Playwright E2E tests
- name: Install Playwright deps
run: |
npx playwright install --with-deps
npx playwright install chromium
# npx playwright install --with-deps
# npm install @playwright/test

- name: Run E2E tests
env:
NEXT_PUBLIC_API_URL: localhost:8088
NEXT_PUBLIC_FIREBASE_ENV: DEV
NEXT_PUBLIC_FIREBASE_API_KEY: ${{ secrets.NEXT_PUBLIC_FIREBASE_API_KEY }}
NEXT_PUBLIC_SUPABASE_KEY: ${{ secrets.NEXT_PUBLIC_SUPABASE_KEY }}
run: |
npx nyc --reporter=lcov yarn --cwd=web serve &
npx nyc --reporter=lcov yarn --cwd=backend/api dev &
npx wait-on http://localhost:3000
npx playwright test tests/e2e
SERVER_PID=$(fuser -k 3000/tcp)
echo $SERVER_PID
kill $SERVER_PID
SERVER_PID=$(fuser -k 8088/tcp)
echo $SERVER_PID
kill $SERVER_PID
chmod +x scripts/e2e.sh
./scripts/e2e.sh

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
Expand Down
2 changes: 2 additions & 0 deletions backend/api/src/helpers/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export const parseCredentials = async (req: Request): Promise<Credentials> => {
try {
return {kind: 'jwt', data: await auth.verifyIdToken(payload)}
} catch (err) {
const raw = payload.split(".")[0];
console.log("JWT header:", JSON.parse(Buffer.from(raw, "base64").toString()));
// This is somewhat suspicious, so get it into the firebase console
console.error('Error verifying Firebase JWT: ', err, scheme, payload)
throw new APIError(500, 'Error validating token.')
Expand Down
15 changes: 6 additions & 9 deletions backend/shared/src/init-admin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import * as admin from 'firebase-admin'
import { getServiceAccountCredentials } from "shared/firebase-utils";
import { IS_LOCAL } from "common/hosting/constants";
import {getServiceAccountCredentials} from "shared/firebase-utils";
import {IS_LOCAL} from "common/hosting/constants";
import {IS_FIREBASE_EMULATOR} from "common/envs/constants";

export const initAdmin = () => {

if (IS_LOCAL && process.env.USE_FIREBASE_EMULATOR === "true") {
if (IS_LOCAL && IS_FIREBASE_EMULATOR) {
console.log("Using Firebase Emulator Suite.")

process.env.FIREBASE_AUTH_EMULATOR_HOST = "127.0.0.1:9099"
process.env.FIREBASE_STORAGE_EMULATOR_HOST = "127.0.0.1:9199" // Default Storage port

return admin.initializeApp({
projectId: "compass-57c3c",
storageBucket: "compass-130ba-public",
Expand All @@ -19,7 +16,7 @@ export const initAdmin = () => {
if (IS_LOCAL) {
try {
const serviceAccount = getServiceAccountCredentials()

if (!serviceAccount.project_id) {
console.debug(`GOOGLE_APPLICATION_CREDENTIALS not set, skipping admin firebase init.`)
return
Expand All @@ -34,7 +31,7 @@ export const initAdmin = () => {
console.error(err)
}
}

console.debug(`Initializing connection to default Firebase...`)
return admin.initializeApp()
}
3 changes: 3 additions & 0 deletions common/src/envs/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ console.debug(`Running in ${HOSTING_ENV} (${ENV})`,);
// throw new MissingKeyError('firebaseConfig.apiKey')
// }

export const IS_FIREBASE_EMULATOR = process.env.NEXT_PUBLIC_FIREBASE_EMULATOR === "true"
if (IS_FIREBASE_EMULATOR) console.log("Using Firebase emulator.")

export const LOCAL_WEB_DOMAIN = `localhost:3000`
export const LOCAL_BACKEND_DOMAIN = `${IS_WEBVIEW_DEV_PHONE ? '192.168.1.3' : IS_LOCAL_ANDROID ? '10.0.2.2' : 'localhost'}:8088`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"playwright:debug": "playwright test --debug",
"playwright:report": "npx playwright show-report tests/reports/playwright-report",
"postinstall": "./scripts/post_install.sh",
"emulate": "firebase emulators:start --only auth"
"emulate": "firebase emulators:start --only auth --project compass-57c3c"
},
"dependencies": {
"@capacitor/app": "7.1.0",
Expand Down
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineConfig, devices } from '@playwright/test';
import path from 'path';

export default defineConfig({
testDir: './tests/e2e',
Expand Down Expand Up @@ -27,5 +26,6 @@ export default defineConfig({
// use: { ...devices['Desktop Safari'] },
// },
],
timeout: 60000,

});
38 changes: 29 additions & 9 deletions scripts/e2e.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
#!/bin/bash

set -e
set -euo pipefail

# Function to clean up background processes
cleanup() {
echo "Stopping background processes..."
for pid in "${PIDS[@]:-}"; do
if kill -0 "$pid" 2>/dev/null; then
kill "$pid" || true
wait "$pid" 2>/dev/null || true
echo "Killed PID $pid"
fi
done
}

# Trap EXIT, INT, TERM to run cleanup automatically
trap cleanup EXIT INT TERM

cd "$(dirname "$0")"/..

npx playwright install chromium

export NEXT_PUBLIC_API_URL=localhost:8088
export NEXT_PUBLIC_FIREBASE_ENV=DEV
export NEXT_PUBLIC_FIREBASE_EMULATOR=true
export FIREBASE_AUTH_EMULATOR_HOST=127.0.0.1:9099
export FIREBASE_STORAGE_EMULATOR_HOST=127.0.0.1:9199
Comment on lines +27 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may also have been one key missing line.


# Start servers in background and store their PIDs
PIDS=()
npx nyc --reporter=lcov yarn --cwd=web serve & PIDS+=($!)
npx nyc --reporter=lcov yarn --cwd=backend/api dev & PIDS+=($!)
yarn emulate & PIDS+=($!)

npx nyc --reporter=lcov yarn --cwd=web serve &
npx nyc --reporter=lcov yarn --cwd=backend/api dev &
npx wait-on http://localhost:3000

npx tsx scripts/setup-auth.ts

npx playwright test tests/e2e
SERVER_PID=$(fuser -k 3000/tcp)
echo $SERVER_PID
kill $SERVER_PID

SERVER_PID=$(fuser -k 8088/tcp)
echo $SERVER_PID
kill $SERVER_PID
exit ${TEST_FAILED:-0}
2 changes: 2 additions & 0 deletions scripts/setup-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ async function createAuth() {
returnSecureToken: true
});

console.log('Auth created', config.USERS.DEV_1.EMAIL)

}
createAuth();
9 changes: 8 additions & 1 deletion web/lib/firebase/users.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import {type User} from 'common/user'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import {getAuth, GoogleAuthProvider, signInWithCredential, signInWithPopup} from 'firebase/auth'
import {getAuth, GoogleAuthProvider, signInWithCredential, signInWithPopup, connectAuthEmulator} from 'firebase/auth'

import {safeLocalStorage} from '../util/local'
import {app} from './init'
import {GOOGLE_CLIENT_ID} from "common/constants"
import {isAndroidWebView} from "web/lib/util/webview"
import {SocialLogin} from "@capgo/capacitor-social-login"
import {Capacitor} from "@capacitor/core"
import {IS_FIREBASE_EMULATOR} from "common/envs/constants"

dayjs.extend(utc)

export type {User}

export const auth = getAuth(app)

if (IS_FIREBASE_EMULATOR) {
connectAuthEmulator(auth, 'http://127.0.0.1:9099', { disableWarnings: true })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was one key missing line.

}

// console.log('auth:', auth)

export const CACHED_REFERRAL_USERNAME_KEY = 'CACHED_REFERRAL_KEY'

// Scenarios:
Expand Down
Loading