Skip to content

Commit

Permalink
Merge branch 'main' into note-import-export
Browse files Browse the repository at this point in the history
  • Loading branch information
codyzu committed Jul 12, 2023
2 parents 823d243 + d6e4338 commit e9ac542
Show file tree
Hide file tree
Showing 26 changed files with 168 additions and 38 deletions.
2 changes: 0 additions & 2 deletions .env

This file was deleted.

4 changes: 4 additions & 0 deletions .env.emulator
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
VITE_FIREBASE_API_KEY=doesnotexist
VITE_FIREBASE_PROJECT_ID=demo-test
VITE_FIREBASE_STORAGE_BUCKET=demo-test.appspot.com
NODE_ENV=development
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --mode staging",
"dev": "firebase emulators:exec --project demo-test --only auth,firestore,storage --import test-data 'vite --mode emulator'",
"dev:update": "firebase emulators:exec --project demo-test --only auth,firestore,storage --import test-data 'vite --mode emulator' --export-on-exit",
"build": "tsc && vite build",
"lint": "xo",
"preview": "vite preview",
Expand All @@ -14,10 +15,10 @@
"build:all": "pwa-assets-generator && pnpm run build && pnpm run build:functions",
"deploy": "firebase deploy --only functions:renderForBot,auth,firestore,storage,hosting",
"generate-pwa-assets": "pwa-assets-generator",
"test": "firebase emulators:exec --project demo-test --only auth,firestore,storage vitest",
"test:once": "firebase emulators:exec --project demo-test --only auth,firestore,storage 'vitest --run'",
"test:ui": "firebase emulators:exec --project demo-test --only auth,firestore,storage 'vitest --ui'",
"coverage": "firebase emulators:exec --project demo-test --only auth,firestore,storage 'vitest run --coverage'",
"test": "firebase emulators:exec --project demo-test --only auth,firestore,storage 'vitest --mode emulator'",
"test:once": "firebase emulators:exec --project demo-test --only auth,firestore,storage 'vitest --run --mode emulator'",
"test:ui": "firebase emulators:exec --project demo-test --only auth,firestore,storage 'vitest --ui --mode emulator'",
"coverage": "firebase emulators:exec --project demo-test --only auth,firestore,storage 'vitest run --coverage --mode emulator'",
"emulators": "firebase emulators:start --project demo-test --only auth,firestore,storage"
},
"dependencies": {
Expand Down
25 changes: 25 additions & 0 deletions src/components/DevSignIn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {signInWithEmailAndPassword} from 'firebase/auth';
import {auth} from '../firebase';

export default function DevSignIn() {
return (
<div className="flex flex-col items-center">
<div className="text-red-600">
This button only exists on emulator mode builds.
</div>
<button
type="button"
className="btn"
onClick={() => {
void signInWithEmailAndPassword(
auth,
'test@doesnotexist.com',
'123456',
);
}}
>
Sign In with Test Account
</button>
</div>
);
}
13 changes: 10 additions & 3 deletions src/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Import the functions you need from the SDKs you need
import {initializeApp} from 'firebase/app';
import {getAuth} from 'firebase/auth';
import {connectAuthEmulator, getAuth} from 'firebase/auth';
import {getAnalytics} from 'firebase/analytics';
import {getFirestore} from 'firebase/firestore';
import {connectFirestoreEmulator, getFirestore} from 'firebase/firestore';

// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
Expand All @@ -21,10 +21,17 @@ const firebaseConfig = {
export const app = initializeApp(firebaseConfig);

// Only load analytics in production
export const analytics = import.meta.env.PROD ? getAnalytics(app) : undefined;
export const analytics =
import.meta.env.MODE === 'production' ? getAnalytics(app) : undefined;

export const auth = getAuth(app);
export const firestore = getFirestore(app);

// We don't export storage because it is only used in a few pages (upload and editor).
// That helps reduce the bundle size for the majority of the pages.

if (import.meta.env.MODE === 'emulator') {
console.log('setting up emulators');
connectAuthEmulator(auth, 'http://127.0.0.1:9099', {disableWarnings: true});
connectFirestoreEmulator(firestore, '127.0.0.1', 8080);
}
12 changes: 3 additions & 9 deletions src/pages/Audience.Test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import {connectStorageEmulator, getStorage} from 'firebase/storage';
import {connectAuthEmulator, signInAnonymously} from 'firebase/auth';
import {connectFirestoreEmulator, doc, setDoc} from 'firebase/firestore';
import {signInAnonymously} from 'firebase/auth';
import {doc, setDoc} from 'firebase/firestore';
import {RouterProvider, createMemoryRouter} from 'react-router-dom';
import {app, auth, firestore} from '../firebase';
import {auth, firestore} from '../firebase';
import {screen, userEvent, render, findByRole} from '../test/test-utils';
import Routes from '../Routes';

beforeAll(async () => {
const storage = getStorage(app);
connectAuthEmulator(auth, 'http://127.0.0.1:9099');
connectFirestoreEmulator(firestore, '127.0.0.1', 8080);
connectStorageEmulator(storage, '127.0.0.1', 9199);

// Use anonymous auth because email link is complicated to emulate
const cred = await signInAnonymously(auth);

Expand Down
12 changes: 3 additions & 9 deletions src/pages/Presentation.Test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import {connectStorageEmulator, getStorage} from 'firebase/storage';
import {connectAuthEmulator, signInAnonymously} from 'firebase/auth';
import {connectFirestoreEmulator, doc, setDoc} from 'firebase/firestore';
import {app, auth, firestore} from '../firebase';
import {signInAnonymously} from 'firebase/auth';
import {doc, setDoc} from 'firebase/firestore';
import {auth, firestore} from '../firebase';
import {screen, userEvent, renderRoute} from '../test/test-utils';

beforeAll(async () => {
const storage = getStorage(app);
connectAuthEmulator(auth, 'http://127.0.0.1:9099');
connectFirestoreEmulator(firestore, '127.0.0.1', 8080);
connectStorageEmulator(storage, '127.0.0.1', 9199);

// Use anonymous auth because email link is complicated to emulate
const cred = await signInAnonymously(auth);

Expand Down
5 changes: 4 additions & 1 deletion src/pages/SignIn.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useCallback, useEffect, useState} from 'react';
import {lazy, useCallback, useEffect, useState} from 'react';
import {
type User,
isSignInWithEmailLink,
Expand All @@ -11,6 +11,8 @@ import clsx from 'clsx';
import {auth} from '../firebase';
import Loading from '../components/Loading';

const DevSignIn = lazy(async () => import('../components/DevSignIn'));

export default function SignIn() {
useEffect(() => {
document.title = `Slidr - Sign In`;
Expand Down Expand Up @@ -157,6 +159,7 @@ export default function SignIn() {
Please click on the link sent in the email. You can close this page.
</div>
)}
{import.meta.env.MODE === 'emulator' && <DevSignIn />}
</div>
);
}
12 changes: 3 additions & 9 deletions src/pages/Speaker.Test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {connectStorageEmulator, getStorage} from 'firebase/storage';
import {connectAuthEmulator, signInAnonymously} from 'firebase/auth';
import {connectFirestoreEmulator, doc, setDoc} from 'firebase/firestore';
import {signInAnonymously} from 'firebase/auth';
import {doc, setDoc} from 'firebase/firestore';
import {RouterProvider, createMemoryRouter} from 'react-router-dom';
import {app, auth, firestore} from '../firebase';
import {auth, firestore} from '../firebase';
import {
screen,
userEvent,
Expand All @@ -13,11 +12,6 @@ import {
import Routes from '../Routes';

beforeAll(async () => {
const storage = getStorage(app);
connectAuthEmulator(auth, 'http://127.0.0.1:9099');
connectFirestoreEmulator(firestore, '127.0.0.1', 8080);
connectStorageEmulator(storage, '127.0.0.1', 9199);

// Use anonymous auth because email link is complicated to emulate
const cred = await signInAnonymously(auth);

Expand Down
5 changes: 5 additions & 0 deletions src/pages/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
uploadBytes,
getDownloadURL,
getStorage,
connectStorageEmulator,
} from 'firebase/storage';
import {nanoid} from 'nanoid';
import {auth, firestore, app} from '../firebase';
Expand All @@ -35,6 +36,10 @@ pdfjs.GlobalWorkerOptions.workerSrc = src.toString();

const storage = getStorage(app);

if (import.meta.env.MODE === 'emulator') {
connectStorageEmulator(storage, '127.0.0.1', 9199);
}

export default function Export() {
useEffect(() => {
document.title = `Slidr - Upload`;
Expand Down
1 change: 1 addition & 0 deletions test-data/auth_export/accounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"kind":"identitytoolkit#DownloadAccountResponse","users":[{"localId":"8xv74jU1Ac5srObCVP2QphcQ6emp","createdAt":"1689154854814","lastLoginAt":"1689160869623","displayName":"","photoUrl":"","passwordHash":"fakeHash:salt=fakeSaltQvugFwADIS9rqdVcbniJ:password=123456","salt":"fakeSaltQvugFwADIS9rqdVcbniJ","passwordUpdatedAt":1689160862685,"providerUserInfo":[{"providerId":"password","email":"test@doesnotexist.com","federatedId":"test@doesnotexist.com","rawId":"test@doesnotexist.com","displayName":"","photoUrl":""}],"validSince":"1689160862","email":"test@doesnotexist.com","emailVerified":false,"disabled":false,"lastRefreshAt":"2023-07-12T11:21:09.623Z"}]}
1 change: 1 addition & 0 deletions test-data/auth_export/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"signIn":{"allowDuplicateEmails":false}}
16 changes: 16 additions & 0 deletions test-data/firebase-export-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "12.4.3",
"firestore": {
"version": "1.18.1",
"path": "firestore_export",
"metadata_file": "firestore_export/firestore_export.overall_export_metadata"
},
"auth": {
"version": "12.4.3",
"path": "auth_export"
},
"storage": {
"version": "12.4.3",
"path": "storage_export"
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions test-data/storage_export/buckets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"buckets": [
{
"id": "demo-test.appspot.com"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "presentations/9jzgjgn3zXnbUY7eJj5h/002_2RqsUw7RZMRZMdFCw2BOi.webp",
"bucket": "demo-test.appspot.com",
"metageneration": 1,
"generation": 1689160590613,
"contentType": "image/webp",
"storageClass": "STANDARD",
"contentDisposition": "inline",
"cacheControl": "public, max-age=604800, immutable",
"downloadTokens": [
"72128112-dcb6-486c-9004-6c578129a247"
],
"etag": "Pcmbnb/ia7IVvKByJiFXteq7wNs",
"customMetadata": {},
"timeCreated": "2023-07-12T11:16:30.613Z",
"updated": "2023-07-12T11:16:30.613Z",
"size": 7382,
"md5Hash": "c74UGqmyp7MgaaRY4uVJuw==",
"crc32c": "1162502542"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "presentations/9jzgjgn3zXnbUY7eJj5h/000_9TK4Ccd6MhAkvKuVI0rYa.webp",
"bucket": "demo-test.appspot.com",
"metageneration": 1,
"generation": 1689160590397,
"contentType": "image/webp",
"storageClass": "STANDARD",
"contentDisposition": "inline",
"cacheControl": "public, max-age=604800, immutable",
"downloadTokens": [
"0c423914-a73f-4c1a-bc96-59532b548d93"
],
"etag": "7J1xM1VzsueiIjJCdhD8kuV211Q",
"customMetadata": {},
"timeCreated": "2023-07-12T11:16:30.397Z",
"updated": "2023-07-12T11:16:30.397Z",
"size": 12100,
"md5Hash": "bxARG6lhZ5e6TLkbPUgsWg==",
"crc32c": "1655021759"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "presentations/9jzgjgn3zXnbUY7eJj5h/z_kSDVjTQXem8cwS88jlP.pdf",
"bucket": "demo-test.appspot.com",
"metageneration": 1,
"generation": 1689160589927,
"contentType": "application/pdf",
"storageClass": "STANDARD",
"contentDisposition": "inline",
"cacheControl": "public;max-age=604800",
"downloadTokens": [
"c3344610-9fa3-49d9-acd1-b3538d046234"
],
"etag": "kurT2U7ED0PLH2nk6CZ6X4MnlMA",
"customMetadata": {},
"timeCreated": "2023-07-12T11:16:29.927Z",
"updated": "2023-07-12T11:16:29.927Z",
"size": 18597,
"md5Hash": "aUyWgatDVA4L2ZuFlrvGKw==",
"crc32c": "327831818"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "presentations/9jzgjgn3zXnbUY7eJj5h/001_DKQpEpn99enBJ8cBYeCui.webp",
"bucket": "demo-test.appspot.com",
"metageneration": 1,
"generation": 1689160590510,
"contentType": "image/webp",
"storageClass": "STANDARD",
"contentDisposition": "inline",
"cacheControl": "public, max-age=604800, immutable",
"downloadTokens": [
"da5ca447-630b-4787-a98e-d2ab4e2ed207"
],
"etag": "fTVOvHzTrf0uMtUGw1e3TuXNOAY",
"customMetadata": {},
"timeCreated": "2023-07-12T11:16:30.510Z",
"updated": "2023-07-12T11:16:30.510Z",
"size": 7248,
"md5Hash": "LuDCdu7PnOLkBgWGEfHToQ==",
"crc32c": "1166286266"
}
Binary file added test.pdf
Binary file not shown.

0 comments on commit e9ac542

Please sign in to comment.